Element pool

Files CCore/inc/ElementPool.h CCore/src/ElementPool.cpp

Element pool is a domain, where objects of different types can be created and stored. There is a restriction: the object type must have a trivial destructor. All objects in the pool dies together when the element pool is destroyed.

Element pool is required when you want to create a bunch of objects dynamically and then to finish their life-time simultaneously. Element pool can speed up this process, because its allocation function is faster and you don't have to track objects to destroy them. Destruction process is also cheap: it's just a few memory blocks release.

CCore provides two classes for the object pooling: MemPool and ElementPool .

ElementPool


class ElementPool : NoCopy
 {
   MemPool pool;

  public:
   
   // constructors

   ElementPool() noexcept {}
   
   template <class ... TT>
   explicit ElementPool(TT ... tt) : pool(tt...) {}
 
   ~ElementPool() {}
   
   // methods
 
   template <class T,class ... SS>
   T * create(SS && ... ss);
   
   StrLen dup(StrLen str);
   
   template <class ... TT>
   StrLen cat(TT ... tt);
   
   void shrink_extra();

   // createArray
   
   template <class T>
   PtrLen<T> createArray(ulen len);
  
   template <class T,class Creator>
   PtrLen<T> createArray(ulen len,Creator creator);
  
   template <class T>
   PtrLen<T> createArray_raw(ulen len);
  
   template <class T,class ... SS>
   PtrLen<T> createArray_fill(ulen len,SS && ... ss);
  
   template <class T>
   PtrLen<T> createArray_copy(ulen len,const T src[]);

   template <class T>
   PtrLen<T> createArray_copy(PtrLen<const T> src);
  
   template <class T,class S>
   PtrLen<T> createArray_cast(ulen len,const S src[]);

   template <class T,class S>
   PtrLen<T> createArray_cast(PtrLen<const S> src);
  
   template <class T>
   PtrLen<T> createArray_swap(ulen len,T objs[]);
   
   template <class T>
   PtrLen<T> createArray_swap(PtrLen<T> objs);

   // swap/move objects
   
   void objSwap(ElementPool &obj);
   
   explicit ElementPool(ToMoveCtor<ElementPool> obj);
 };

ElementPool constructor accepts the same arguments, as the MemPool (see below).

The method create<T>() creates an object of the type T in the pool and returns a pointer to it. On failure an exception is thrown. Arguments are forwarded to the T constructor.

The method dup() duplicates the given string in the pool and returns the duplicate.

cat() casts the given arguments to the type StrLen and makes a string in the pool by the concatenation of the received strings.

shrink_extra() releases the reserved memory at the pool tail.

There is a bunch of methods to create an array of objects in the pool.

The basic method createArray() initializes array by default. It takes an array length as the argument and returns the range of created objects.

Other variations of the createArray() have the same semantic as the different Array constructors.

createArray_raw() creates an uninitialized array, if the type T is a POD type. You must give values to array elements after.

createArray_fill() creates an array, where each element is constructed using the same constructor with provided arguments.

createArray_copy() creates an array by per element copying the given object range.

createArray_cast() creates an array by per element casting the given object range to the type T.

createArray_swap() uses swap-create, i.e. creates an object with the default constructor and then do swap.

createArray(ulen len,Creator creator) uses the given object Creator to create array elements.

ElementPool destructor kills all created objects.

ElementPool is swappable/movable (but not std movable).

MemPool


class MemPool : NoCopy
 {
  public:
   
   // constructors
   
   static const ulen DefaultBlockLen = 64_KByte ;

   MemPool() noexcept : MemPool(DefaultBlockLen) {}
   
   explicit MemPool(ulen block_len,ulen mem_cap=MaxULen);
   
   ~MemPool();

   // methods
   
   Place<void> alloc(ulen len);
   
   void shrink_extra();

   // swap/move objects
   
   void objSwap(MemPool &obj);
   
   explicit MemPool(ToMoveCtor<MemPool> obj);
 };

You probably don't need to use this class directly. MemPool is a pool of memory blocks. You may create a new memory block by the method alloc(). len is the required block length. On success, alloc() returns the Place<void> at the begining of the block. Blocks are always aligned. On failure an exception is thrown.

MemPool destructor releases all allocated memory blocks.

MemPool constructor accepts two arguments: block_len is an internal large blocks length, 64 KBytes by default, mem_cap is a total memory allocation cap, no cap by default.

shrink_extra() releases the reserved memory at the pool tail.

MemPool is swappable/movable (but not std movable).