PoolAllocator Class Reference

#include <Pt/PoolAllocator.h>

Pool based allocator. More...

Inherits Allocator, and NonCopyable.

Public Member Functions

 PoolAllocator (std::size_t maxSize, std::size_t align=16, std::size_t maxBlock=8192)
 Contruct with maximum record size, alignment and block size.
 
 ~PoolAllocator ()
 Destructor.
 
void * allocate (std::size_t size)
 Allocates size bytes of memory.
 
void deallocate (void *p, std::size_t size)
 Deallocates memory of size bytes.
 

Detailed Description

The PoolAllocator uses pools to allocate memory. Each pool consists of blocks of equally sized records, which can be used for allocations up to the size of a record. The record sizes increase from pool to pool. When memory is allocated, a record is used from the pool, which handles the requested size. When memory is deallocated, the record is returned to the corresponding pool. This method of allocation is effective, because larger blocks of memory are allocated and then reused in the form of many smaller records. An advantage of this kind of allocator, compared to free list based allocators, is that it is able to release completely unused blocks.

// Contruct with max. record size, alignment and block size
Pt::PoolAllocator allocator(32, 8, 4096);
// will use a record from the pools
void* p1 = allocator.allocate( sizeof(float) );
// too large, will use operator new
void* p2 = allocator.allocate( 64 );

When a PoolAllocator is constructed, the maximum size for records has to be specified. The reason for this is that this type of allocator is ineffective for large allocations. Therefore, memory which is larger than this limit will be allocated using the new operator, instead of a record from a memory pool. Optionally, the alignment and the maximum block size can be set. The record sizes of the pools will be multiples of the alignment. So if the alignment is 8, the first pool will have records of size 8, the second pool records of size 16 and so forth, until the maximum size is reached. The maximum block size controls the number of records per block. A new block of records is added, when a pool is depleted and has to be extended to allow more allocations.