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;
}
// *****************************************************************************************************************
@ -49,10 +50,11 @@ CFixedSizeAllocator::~CFixedSizeAllocator()
if (_NumAlloc != 0)
{
#ifdef NL_DEBUG
nlwarning("%d blocks were not freed", (int) _NumAlloc);
nlwarning("%d blocks were not freed, leaking memory", (int) _NumAlloc);
#endif
return;
}
else
{
if (_NumChunks > 0)
{
nlassert(_NumChunks == 1);
@ -60,6 +62,11 @@ CFixedSizeAllocator::~CFixedSizeAllocator()
delete _FreeSpace->Chunk;
}
}
if (_SpareMem)
{
aligned_free(_SpareMem);
}
}
// *****************************************************************************************************************
void *CFixedSizeAllocator::alloc()
@ -115,7 +122,10 @@ CFixedSizeAllocator::CChunk::~CChunk()
nlassert(NumFreeObjs == 0);
nlassert(Allocator->_NumChunks > 0);
-- (Allocator->_NumChunks);
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;
//
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