CrystalSpace

Public API Reference

csBlockAllocator< T, Allocator, ObjectDispose > Class Template Reference
[Memory Management]

This class implements a memory allocator which can efficiently allocate objects that all have the same size. More...

#include <csutil/blockallocator.h>

Inheritance diagram for csBlockAllocator< T, Allocator, ObjectDispose >:

List of all members.

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.
 csBlockAllocator (size_t nelem=32)
 Construct a new block allocator.
void Empty ()
 Destroy all objects allocated by the pool.
void Free (T *p)
 Deallocate an object.
bool TryFree (T *p)
 Try to delete an object.
 ~csBlockAllocator ()
 Destroy all allocated objects and release memory.

Detailed Description

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
class csBlockAllocator< T, Allocator, ObjectDispose >

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).

Remarks:
The objects are properly constructed and destructed.
Assumes that the class T with which the template is instantiated has a default (zero-argument) constructor. Alloc() uses this constructor to initialize each vended object.
See also:
csArray
csMemoryPool

Definition at line 126 of file blockallocator.h.


Constructor & Destructor Documentation

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
csBlockAllocator< T, Allocator, ObjectDispose >::csBlockAllocator ( size_t  nelem = 32  )  [inline]

Construct a new block allocator.

Parameters:
nelem Number of elements to store in each allocation unit.
Remarks:
Bigger values for 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).
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_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 159 of file blockallocator.h.

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
csBlockAllocator< T, Allocator, ObjectDispose >::~csBlockAllocator (  )  [inline]

Destroy all allocated objects and release memory.

Definition at line 166 of file blockallocator.h.


Member Function Documentation

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
T* csBlockAllocator< T, Allocator, ObjectDispose >::Alloc (  )  [inline]

Allocate a new object.

The default (no-argument) constructor of T is invoked.

Reimplemented from csFixedSizeAllocator< sizeof(T), Allocator >.

Definition at line 187 of file blockallocator.h.

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
template<typename A1 , typename A2 , typename A3 >
T* csBlockAllocator< T, Allocator, ObjectDispose >::Alloc ( A1 &  a1,
A2 &  a2,
A3 &  a3 
) [inline]

Allocate a new object.

The three-argument constructor of T is invoked.

Definition at line 207 of file blockallocator.h.

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
template<typename A1 , typename A2 >
T* csBlockAllocator< T, Allocator, ObjectDispose >::Alloc ( A1 &  a1,
A2 &  a2 
) [inline]

Allocate a new object.

The two-argument constructor of T is invoked.

Definition at line 197 of file blockallocator.h.

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
void csBlockAllocator< T, Allocator, ObjectDispose >::Empty (  )  [inline]

Destroy all objects allocated by the pool.

Remarks:
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 >.

Definition at line 177 of file blockallocator.h.

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
void csBlockAllocator< T, Allocator, ObjectDispose >::Free ( T *  p  )  [inline]

Deallocate an object.

It is safe to provide a null pointer.

Parameters:
p Pointer to deallocate.

Definition at line 216 of file blockallocator.h.

template<class T, class Allocator = CS::Memory::AllocatorMalloc, class ObjectDispose = csBlockAllocatorDisposeDelete<T>>
bool csBlockAllocator< T, Allocator, ObjectDispose >::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).

Definition at line 226 of file blockallocator.h.


The documentation for this class was generated from the following file:

Generated for Crystal Space 1.4.1 by doxygen 1.7.1