SimpleLib

Pool and Plex Classes

SimpleLib includes two classes for improving allocation of many objects of the same type.

CPool maintains a pool of objects, with a minimum and maximum number of elements in the pool. CPool can be used in situations where a large number of objects of the same type are being allocated and freed.

CPlex maintains a sub-segmented memory allocation scheme where a number of objects of the same type are allocated in a contiguous block of memory to avoid the overhead of the memory manager. CPlex is used by SimpleLib's CMap and CHashMap classes where it improves key insertion speed by about 100%.

template <class T>
class CPool

template <class T>
class CPlex

The usage of these classes identical (except for the constructors). In the following examples, CPool can be replaced with CPlex.

Before using CPool or CPlex ensure the class you want managed by the pool has a default constructor (either explicit or compiler supplied). To declare a pool or plex instance:

// A pool of CObject* pointers with at least 10 and at most 100 
// cached items in the pool
CPool<CObject> ObjectPool(10, 100);

// A plex of CObject* pointers with 64 items in each allocated block
// (the parameter can be omitted to have SimpleLib pick a reasonable
// default where it aims for about 256 byte blocks)
CPlex<CObject> ObjectPlex(64);

To allocate an object instance:

CObject* pObject=ObjectPool.Alloc();

To free an object instance

ObjectPool.Free(pObject);

That's about it! These classes have a few other methods though they're generally never used.