A simple pool for fixed-size memory blocks.
The main purpose of this class is to speed-up memory allocations, as well as to reduce memory fragmentation in situations where the same blocks are allocated all over again, such as in server applications.
All allocated blocks are retained for future use. A limit on the number of blocks can be specified. Blocks can be preallocated.
int preAlloc = 0,
int maxAlloc = 0
Creates a MemoryPool for blocks with the given blockSize. The number of blocks given in preAlloc are preallocated.
int allocated() const;
Returns the number of allocated blocks.
int available() const;
Returns the number of available blocks in the pool.
std::size_t blockSize() const;
Returns the block size.
void * get();
Returns a memory block. If there are no more blocks in the pool, a new block will be allocated.
If maxAlloc blocks are already allocated, an OutOfMemoryException is thrown.
void * ptr
Releases a memory block and returns it to the pool.