ReGameDLL_CS/regamedll/public/MemPool.cpp

96 lines
1.8 KiB
C++
Raw Normal View History

2015-12-09 01:39:54 +03:00
#include "precompiled.h"
2015-06-30 12:46:07 +03:00
CMemoryPool::CMemoryPool(int blockSize, int numElements)
{
2015-12-09 01:39:54 +03:00
_blocksPerBlob = numElements;
_blockSize = blockSize;
_numBlobs = 0;
_numElements = 0;
AddNewBlob();
_peakAlloc = 0;
2015-06-30 12:46:07 +03:00
_blocksAllocated = 0;
2015-12-09 01:39:54 +03:00
}
CMemoryPool::~CMemoryPool()
2015-12-09 01:39:54 +03:00
{
for (int i = 0; i < _numBlobs; ++i)
2015-12-09 01:39:54 +03:00
free(_memBlob[i]);
}
void CMemoryPool::AddNewBlob()
2015-12-09 01:39:54 +03:00
{
int sizeMultiplier = pow(2.0, _numBlobs);
int nElements = _blocksPerBlob * sizeMultiplier;
int blobSize = _blockSize * nElements;
_memBlob[_numBlobs] = malloc(blobSize);
#ifdef _WIN32
if (!_memBlob[_numBlobs])
DebugBreak();
#endif // _WIN32
_headOfFreeList = _memBlob[_numBlobs];
#ifdef _WIN32
if (!_headOfFreeList)
DebugBreak();
#endif // _WIN32
void **newBlob = (void **)_headOfFreeList;
for (int j = 0; j < nElements - 1; ++j)
2015-12-09 01:39:54 +03:00
{
newBlob[0] = (char *)newBlob + _blockSize;
newBlob = (void **)newBlob[0];
}
newBlob[0] = NULL;
_numElements += nElements;
++_numBlobs;
2015-12-09 01:39:54 +03:00
#ifdef _WIN32
if (_numBlobs >= MAX_BLOBS - 1)
DebugBreak();
#endif // _WIN32
}
void *CMemoryPool::Alloc(unsigned int amount)
{
void *returnBlock;
if (amount > (unsigned int)_blockSize)
return NULL;
++_blocksAllocated;
2015-12-09 01:39:54 +03:00
_peakAlloc = Q_max(_peakAlloc, _blocksAllocated);
if (_blocksAllocated >= _numElements)
AddNewBlob();
#ifdef _WIN32
if (!_headOfFreeList)
DebugBreak();
#endif // _WIN32
returnBlock = _headOfFreeList;
_headOfFreeList = *((void **)_headOfFreeList);
return returnBlock;
}
void CMemoryPool::Free(void *memblock)
{
if (!memblock)
return;
#ifdef _DEBUG
Q_memset(memblock, 0xDD, _blockSize);
#endif // _DEBUG
--_blocksAllocated;
2015-12-09 01:39:54 +03:00
*((void **)memblock) = _headOfFreeList;
_headOfFreeList = memblock;
}