@ -41,6 +41,7 @@ CFixedSizeAllocator::CFixedSizeAllocator(uint numBytesPerBlock, uint numBlockPer
nlassert ( _NumBytesPerBlock > = numBytesPerBlock ) ;
nlassert ( _NumBytesPerBlock > = numBytesPerBlock ) ;
_NumBlockPerChunk = std : : max ( numBlockPerChunk , ( uint ) 3 ) ;
_NumBlockPerChunk = std : : max ( numBlockPerChunk , ( uint ) 3 ) ;
_NumAlloc = 0 ;
_NumAlloc = 0 ;
_SpareMem = NULL ;
}
}
// *****************************************************************************************************************
// *****************************************************************************************************************
@ -49,10 +50,11 @@ CFixedSizeAllocator::~CFixedSizeAllocator()
if ( _NumAlloc ! = 0 )
if ( _NumAlloc ! = 0 )
{
{
# ifdef NL_DEBUG
# ifdef NL_DEBUG
nlwarning ( " %d blocks were not freed " , ( int ) _NumAlloc ) ;
nlwarning ( " %d blocks were not freed , leaking memory " , ( int ) _NumAlloc ) ;
# endif
# endif
return ;
}
}
else
{
if ( _NumChunks > 0 )
if ( _NumChunks > 0 )
{
{
nlassert ( _NumChunks = = 1 ) ;
nlassert ( _NumChunks = = 1 ) ;
@ -60,6 +62,11 @@ CFixedSizeAllocator::~CFixedSizeAllocator()
delete _FreeSpace - > Chunk ;
delete _FreeSpace - > Chunk ;
}
}
}
}
if ( _SpareMem )
{
aligned_free ( _SpareMem ) ;
}
}
// *****************************************************************************************************************
// *****************************************************************************************************************
void * CFixedSizeAllocator : : alloc ( )
void * CFixedSizeAllocator : : alloc ( )
@ -115,7 +122,10 @@ CFixedSizeAllocator::CChunk::~CChunk()
nlassert ( NumFreeObjs = = 0 ) ;
nlassert ( NumFreeObjs = = 0 ) ;
nlassert ( Allocator - > _NumChunks > 0 ) ;
nlassert ( Allocator - > _NumChunks > 0 ) ;
- - ( Allocator - > _NumChunks ) ;
- - ( Allocator - > _NumChunks ) ;
if ( Allocator - > _SpareMem )
aligned_free ( Mem ) ; //delete[] Mem;
aligned_free ( Mem ) ; //delete[] Mem;
else
Allocator - > _SpareMem = Mem ;
}
}
// *****************************************************************************************************************
// *****************************************************************************************************************
@ -125,7 +135,15 @@ void CFixedSizeAllocator::CChunk::init(CFixedSizeAllocator *alloc)
nlassert ( alloc ! = NULL ) ;
nlassert ( alloc ! = NULL ) ;
Allocator = alloc ;
Allocator = alloc ;
//
//
if ( Allocator - > _SpareMem )
{
Mem = Allocator - > _SpareMem ;
Allocator - > _SpareMem = NULL ;
}
else
{
Mem = ( uint8 * ) aligned_malloc ( getBlockSizeWithOverhead ( ) * alloc - > getNumBlockPerChunk ( ) , NL_DEFAULT_MEMORY_ALIGNMENT ) ; // new uint8[getBlockSizeWithOverhead() * alloc->getNumBlockPerChunk()];
Mem = ( uint8 * ) aligned_malloc ( getBlockSizeWithOverhead ( ) * alloc - > getNumBlockPerChunk ( ) , NL_DEFAULT_MEMORY_ALIGNMENT ) ; // new uint8[getBlockSizeWithOverhead() * alloc->getNumBlockPerChunk()];
}
//
//
getNode ( 0 ) . Chunk = this ;
getNode ( 0 ) . Chunk = this ;
getNode ( 0 ) . Next = & getNode ( 1 ) ;
getNode ( 0 ) . Next = & getNode ( 1 ) ;