csBlockAllocator< T, Allocator, ObjectDispose > Class Template ReferenceThis class implements a memory allocator which can efficiently allocate objects that all have the same size.
Inheritance diagram for csBlockAllocator< T, Allocator, ObjectDispose >:
T, Allocator >
Public Member Functions
|T *||Alloc ()|
|Allocate a new object. |
|csBlockAllocator (size_t nelem=32)|
|Construct a new block allocator. |
|Destroy all objects allocated by the pool. |
|void||Free (T *p)|
|Deallocate an object. |
|bool||TryFree (T *p)|
|Try to delete an object. |
|Destroy all allocated objects and release memory. |
sizeof(T), Allocator >
template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
This class implements a memory allocator which can efficiently allocate objects that all have the same size.
class csBlockAllocator< T, Allocator, ObjectDispose >
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).
- The objects are properly constructed and destructed.
Assumes that the class
Twith which the template is instantiated has a default (zero-argument) constructor. Alloc() uses this constructor to initialize each vended object.
Defining the macro CS_BLOCKALLOC_DEBUG will cause freed objects to be overwritten with '0xfb' bytes. This can be useful to track use of already freed objects, as they can be more easily recognized (as some members will be likely bogus.)
Constructor & Destructor Documentation
|csBlockAllocator< T, Allocator, ObjectDispose >::csBlockAllocator||(||size_t|| nelem =
Construct a new block allocator.
nelem Number of elements to store in each allocation unit.
- Bigger values for
nelemwill improve allocation performance, but at the cost of having some potential waste if you do not add
nelemelements to each pool. For instance, if
nelemis 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).
If use use csBlockAllocator as a convenient and lightweight garbage collection facility (for which it is well-suited), and expect it to dispose of allocated objects when the pool itself is destroyed, then set
warn_unfreedto 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_unfreedto true in order to receive diagnostics about objects which you have forgotten to release explicitly via manual invocation of Free().
|csBlockAllocator< T, Allocator, ObjectDispose >::~csBlockAllocator||(||)||
Member Function Documentation
|T* csBlockAllocator< T, Allocator, ObjectDispose >::Alloc||(||)||
|void csBlockAllocator< T, Allocator, ObjectDispose >::Empty||(||)||
Destroy all objects allocated by the pool.
- All pointers returned by Alloc() are invalidated. It is safe to perform new allocations from the pool after invoking Empty().
Reimplemented from csFixedSizeAllocator< sizeof(T), Allocator >.
Referenced by csRedBlackTree< csRedBlackTreePayload< K, T > >::DeleteAll().
|void csBlockAllocator< T, Allocator, ObjectDispose >::Free||(||T *||p||)||
Deallocate an object.
It is safe to provide a null pointer.
p Pointer to deallocate.
|bool csBlockAllocator< T, Allocator, ObjectDispose >::TryFree||(||T *||p||)||
The documentation for this class was generated from the following file:
Generated for Crystal Space 1.0.2 by doxygen 1.4.7