Keep a spare memory block in fixed size allocator to avoid large allocation overhead on the chunk boundary

ryzomclassic-develop
kaetemi 4 years ago
parent cd4b746bfa
commit 3604088713

@ -107,6 +107,7 @@ private:
uint _NumBlockPerChunk;
//
uint _NumAlloc;
uint8 *_SpareMem;
};
} // NLMISC

@ -41,6 +41,7 @@ CFixedSizeAllocator::CFixedSizeAllocator(uint numBytesPerBlock, uint numBlockPer
nlassert(_NumBytesPerBlock >= numBytesPerBlock);
_NumBlockPerChunk = std::max(numBlockPerChunk, (uint) 3);
_NumAlloc = 0;
_SpareMem = NULL;
}
// *****************************************************************************************************************
@ -48,16 +49,22 @@ CFixedSizeAllocator::~CFixedSizeAllocator()
{
if (_NumAlloc != 0)
{
#ifdef NL_DEBUG
nlwarning("%d blocks were not freed", (int) _NumAlloc);
#endif
return;
#ifdef NL_DEBUG
nlwarning("%d blocks were not freed, leaking memory", (int) _NumAlloc);
#endif
}
else
{
if (_NumChunks > 0)
{
nlassert(_NumChunks == 1);
// delete the left chunk. This should force all the left nodes to be removed from the empty list
delete _FreeSpace->Chunk;
}
}
if (_NumChunks > 0)
if (_SpareMem)
{
nlassert(_NumChunks == 1);
// delete the left chunk. This should force all the left nodes to be removed from the empty list
delete _FreeSpace->Chunk;
aligned_free(_SpareMem);
}
}
@ -115,7 +122,10 @@ CFixedSizeAllocator::CChunk::~CChunk()
nlassert(NumFreeObjs == 0);
nlassert(Allocator->_NumChunks > 0);
-- (Allocator->_NumChunks);
aligned_free(Mem); //delete[] Mem;
if (Allocator->_SpareMem)
aligned_free(Mem); //delete[] Mem;
else
Allocator->_SpareMem = Mem;
}
// *****************************************************************************************************************
@ -125,7 +135,15 @@ void CFixedSizeAllocator::CChunk::init(CFixedSizeAllocator *alloc)
nlassert(alloc != NULL);
Allocator = alloc;
//
Mem = (uint8 *)aligned_malloc(getBlockSizeWithOverhead() * alloc->getNumBlockPerChunk(), NL_DEFAULT_MEMORY_ALIGNMENT); // new uint8[getBlockSizeWithOverhead() * alloc->getNumBlockPerChunk()];
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()];
}
//
getNode(0).Chunk = this;
getNode(0).Next = &getNode(1);

Loading…
Cancel
Save