16#include "aeongames/MemoryPool.hpp"
20 mBlockSize{aBlockSize},
21 mNumOfBlocks{aNumOfBlocks},
22 mNumOfFreeBlocks{aNumOfBlocks},
23 mNumOfInitializedBlocks{0},
24 mMemory ( static_cast<
std::vector<uint8_t>::size_type> ( mBlockSize * mNumOfBlocks ) ),
25 mNext{mMemory.data() }
29 uint8_t * MemoryPool::AddrFromIndex (
size_t i )
const
31 return const_cast<uint8_t *
> ( mMemory.data() ) + ( i * mBlockSize );
34 size_t MemoryPool::IndexFromAddr (
const uint8_t* p )
const
36 return ( ( (
size_t ) ( p - mMemory.data() ) ) / mBlockSize );
41 if ( mNumOfInitializedBlocks < mNumOfBlocks )
43 size_t* p = (
size_t* ) AddrFromIndex ( mNumOfInitializedBlocks );
44 *p = mNumOfInitializedBlocks + 1;
45 mNumOfInitializedBlocks++;
48 if ( mNumOfFreeBlocks > 0 )
50 ret = (
void* ) mNext;
52 if ( mNumOfFreeBlocks != 0 )
54 mNext = AddrFromIndex ( * ( (
size_t* ) mNext ) );
66 if ( mNext !=
nullptr )
68 ( * (
size_t* ) p ) = IndexFromAddr ( mNext );
69 mNext = ( uint8_t* ) p;
73 * ( (
size_t* ) p ) = mNumOfBlocks;
74 mNext = ( uint8_t* ) p;
DLL void * Allocate()
Allocate a block from the pool.
DLL MemoryPool(size_t sizeOfEachBlock, size_t numOfBlocks)
Construct a memory pool.
DLL void DeAllocate(void *p)
Return a block to the pool.
<- This is here just for the literals