Inheritance diagram for ObjPool:
The class manages the fixed-size memory blocks. These memory blocks are organized as a single-linked list (list of unused blocks or pool). The get() method returns the first memory block from the pool. The put() method puts the memory block back into the pool. If there are no unused memory blocks (i.e. the pool is empty), then you should call grow() method. The grow() method allocates memory chunk (several adjacent memory blocks) and puts these blocks into the list of unused blocks.
For chunk allocation/deallocation global new / delete operators are used.
The template parameter A is an alignment of memory blocks. It should be integer power of two: 1, 2, 4, 8, 16, ...
The class does not contain the memory block size. So you should provide the correct block size each time the grow() method is called.
As an example of using ObjPool class see implementation of FastObjT class.
typedef size_t size_type |
Size type.
typedef void* pointer |
Pointer type.
ObjPool | ( | ) | [inline] |
~ObjPool | ( | ) | [inline] |
Destruction.
The destructor releases all (used and unused) memory blocks.
Grow the pool.
One memory chunk is several adjacent memory blocks. This method allocates one memory chunk and puts these blocks into the list of unused blocks.
Allocated memory chunk contains at least one memory block.
The ObjPool class does not contain memory block size, so, you should provide the correct memory block size each time grow() method is called. You should specify the same memory block size obj_size each time grow() is called. Otherwise your program will have undefined behavior.
[in] | obj_size | The memory block size in bytes. |
[in] | chunk_size | Approximate memory chunk size in bytes. |
bool empty | ( | ) | const [inline] |
Is the pool empty?
This method checks the list of unused blocks. If the pool is empty, then you should call grow() method before using get().
For example:
ObjPool<4> m_pool; size_t m_block_size; // ... void* my_alloc() { if (m_pool.empty()) m_pool.grow(m_block_size); return m_pool.get(); }
pointer get | ( | ) | [inline] |
void put | ( | pointer | obj | ) | [inline] |
Align block size.
This method aligns the memory block size obj_size.
[in] | obj_size | The memory block size. |
Align block pointer.
This method aligns the memory block pointer ptr.
[in] | ptr | The memory block pointer. |