MemoryPool Class Reference

#include <Pt/PoolAllocator.h>

Memory pool for objects of the same size. More...

Inherits NonCopyable.

Public Member Functions

 MemoryPool (std::size_t elemSize, std::size_t maxPageSize=8192)
 Construct with element size and maximum page size.
 ~MemoryPool ()
void * allocate ()
 Allocates memory.
void deallocate (void *ptr)
 deallocates memory.

Detailed Description

If memory of uniform sizes has to be allocated, a MemoryPool can be used directly, rather than indirectly as part of the PoolAllocator. This can be faster, because the PoolAllocator has to look up the pool for the requested size of memory each time it allocates and deallocates. To construct a MemoryPool, the size of the records, i.e. the size of memory it can allocate, has to be specified.

Pt::MemoryPool pool( sizeof(float), 4096 );
void* p = pool.allocate();
float* f = new (p) float(3.1415);

Optionally, the maximum size of the blocks in the pool can be controlled. In the example shown above, the pool can only allocate memory of the size required for a float. Each time the pool itself requires more memory, it will allocate a new block of 4096 bytes.