Am trying to build a memory pool for C++ objects of a certain type. The memory pool simply allocates a big chunk of memory at startup and manages a table of allocations from that chunk later on.
BTW, the pool is used in a multi-threading environment and I applied a lock to the allocation table on alloc / delete operations.
Also, if that's relevant, am using VC++.
However, it seems that I'm getting overlaps and corrupted objects when the memory pool is put to action, as if allocated objects are overlapping.
Below is the source code. Is there any flaw in it?
Help is so highly appreciated.
virtual T * allocate(size_t nUnits) = 0;
virtual void free(T * ptr) = 0;
class FixedMemAllocator : public BaseAllocator
// allocate the massive buffer
_pBuffer = (T *)(new char[nInitCap * sizeof(T) ]);
// initialize allocation table
_nFreeSlots = 0;
_pAllocTable = new T*[nInitCap];
T * pBufferItr = _pBuffer;
size_t i = 0;
// fill alloc table
while (i < nInitCap)
_pAllocTable[_nFreeSlots++] = pBufferItr;
virtual T * allocate(size_t nUnits)
T * ptr = _pAllocTable[--_nFreeSlots];
virtual void free(T * ptr)
_pAllocTable[_nFreeSlots++] = ptr;
T * * _pAllocTable;
T * _pBuffer;
// table mutex is the sync object (basically it wraps a critical section)[/color]