Class PooledAllocator

Class Documentation

class PooledAllocator

Pooled storage allocator

The following routines allow for the efficient allocation of storage in small chunks from a specified pool. Rather than allowing each structure to be freed individually, an entire pool of storage is freed at once. This method has two advantages over just using malloc() and free(). First, it is far more efficient for allocating small objects, as there is no overhead for remembering all the information needed to free each object or consolidating fragmented memory. Second, the decision about how long to keep an object is made at the time of allocation, and there is no need to track down all the objects to free them.

Public Functions

inline PooledAllocator()

Default constructor. Initializes a new pool.

inline ~PooledAllocator()

Destructor. Frees all the memory allocated in this pool.

inline void free_all()

Frees all allocated memory chunks

inline void *malloc(const size_t req_size)

Returns a pointer to a piece of new memory of the given size in bytes allocated from the pool.

template<typename T>
inline T *allocate(const size_t count = 1)

Allocates (using this pool) a generic type T.

Params: count = number of instances to allocate. Returns: pointer (of type T*) to memory buffer

Public Members

Size usedMemory = 0
Size wastedMemory = 0