From 360408871313b682e83c97d98c90659e6bf7d5a6 Mon Sep 17 00:00:00 2001 From: kaetemi Date: Wed, 11 Nov 2020 13:42:20 +0800 Subject: [PATCH] Keep a spare memory block in fixed size allocator to avoid large allocation overhead on the chunk boundary --- nel/include/nel/misc/fixed_size_allocator.h | 1 + nel/src/misc/fixed_size_allocator.cpp | 38 +++++++++++++++------ 2 files changed, 29 insertions(+), 10 deletions(-) diff --git a/nel/include/nel/misc/fixed_size_allocator.h b/nel/include/nel/misc/fixed_size_allocator.h index e00809344..1f49dfa68 100644 --- a/nel/include/nel/misc/fixed_size_allocator.h +++ b/nel/include/nel/misc/fixed_size_allocator.h @@ -107,6 +107,7 @@ private: uint _NumBlockPerChunk; // uint _NumAlloc; + uint8 *_SpareMem; }; } // NLMISC diff --git a/nel/src/misc/fixed_size_allocator.cpp b/nel/src/misc/fixed_size_allocator.cpp index 7a082b8d8..6784aff66 100644 --- a/nel/src/misc/fixed_size_allocator.cpp +++ b/nel/src/misc/fixed_size_allocator.cpp @@ -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);