![]() |
Public API Reference |
![]() |
This class implements a memory allocator which can efficiently allocate objects that all have the same size. More...
#include <csutil/blockallocator.h>
Public Member Functions | |
T * | Alloc () |
Allocate a new object. | |
template<typename A1 , typename A2 > | |
T * | Alloc (A1 &a1, A2 &a2) |
Allocate a new object. | |
template<typename A1 , typename A2 , typename A3 > | |
T * | Alloc (A1 &a1, A2 &a2, A3 &a3) |
Allocate a new object. | |
template<typename A1 > | |
T * | Alloc (A1 &a1) |
Allocate a new object. | |
csBlockAllocator (size_t nelem=32) | |
Construct a new block allocator. | |
void | DeleteAll () |
Destroy all objects allocated by the pool and release the memory. | |
void | Empty () |
Destroy all objects allocated by the pool without releasing the memory. | |
void | Free (T *p) |
Deallocate an object. | |
bool | TryFree (T *p) |
Try to delete an object. | |
~csBlockAllocator () | |
Destroy all allocated objects and release memory. |
This class implements a memory allocator which can efficiently allocate objects that all have the same size.
It has no memory overhead per allocation (unless the objects are smaller than sizeof(void*) bytes) and is extremely fast, both for Alloc() and Free(). The only restriction is that any specific allocator can be used for just one type of object (the type for which the template is instantiated).
T
with which the template is instantiated has a default (zero-argument) constructor. Alloc() uses this constructor to initialize each vended object.Definition at line 146 of file blockallocator.h.
csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::csBlockAllocator | ( | size_t | nelem = 32 | ) | [inline] |
Construct a new block allocator.
nelem | Number of elements to store in each allocation unit. |
nelem
will improve allocation performance, but at the cost of having some potential waste if you do not add nelem
elements to each pool. For instance, if nelem
is 50 but you only add 3 elements to the pool, then the space for the remaining 47 elements, though allocated, will remain unused (until you add more elements).warn_unfreed
to false. On the other hand, if you use csBlockAllocator only as a fast allocator but intend to manage each object's life time manually, then you may want to set warn_unfreed
to true in order to receive diagnostics about objects which you have forgotten to release explicitly via manual invocation of Free(). Definition at line 183 of file blockallocator.h.
csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::~csBlockAllocator | ( | ) | [inline] |
Destroy all allocated objects and release memory.
Definition at line 193 of file blockallocator.h.
T* csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::Alloc | ( | ) | [inline] |
Allocate a new object.
The default (no-argument) constructor of T is invoked.
Reimplemented from csFixedSizeAllocator< SizeComputer::value, Allocator >.
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 225 of file blockallocator.h.
T* csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::Alloc | ( | A1 & | a1, |
A2 & | a2 | ||
) | [inline] |
Allocate a new object.
The two-argument constructor of T is invoked.
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 235 of file blockallocator.h.
T* csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::Alloc | ( | A1 & | a1, |
A2 & | a2, | ||
A3 & | a3 | ||
) | [inline] |
Allocate a new object.
The three-argument constructor of T is invoked.
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 245 of file blockallocator.h.
T* csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::Alloc | ( | A1 & | a1 | ) | [inline] |
Allocate a new object.
The one-argument constructor of T is invoked.
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 255 of file blockallocator.h.
void csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::DeleteAll | ( | ) | [inline] |
Destroy all objects allocated by the pool and release the memory.
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 215 of file blockallocator.h.
void csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::Empty | ( | ) | [inline] |
Destroy all objects allocated by the pool without releasing the memory.
Reimplemented from csFixedSizeAllocator< SizeComputer::value, Allocator >.
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 204 of file blockallocator.h.
void csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::Free | ( | T * | p | ) | [inline] |
Deallocate an object.
It is safe to provide a null pointer.
p | Pointer to deallocate. |
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 264 of file blockallocator.h.
bool csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::TryFree | ( | T * | p | ) | [inline] |
Try to delete an object.
Usage is the same as Free(), the difference being that false
is returned if the deallocation failed (the reason is most likely that the memory was not allocated by the allocator).
Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.
Definition at line 274 of file blockallocator.h.