CrystalSpace

Public API Reference

csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer > 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, SizeComputer >:

List of all members.

Public Member Functions

template<typename A1 >
T * Alloc (A1 &a1)
 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 , typename A2 >
T * Alloc (A1 &a1, A2 &a2)
 Allocate a new object.
T * Alloc ()
 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.

Detailed Description

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

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
CS::Memory::BlockAllocatorSafe for a thread-safe version

Definition at line 146 of file blockallocator.h.


Constructor & Destructor Documentation

template<class T, typename Allocator = CS::Memory::AllocatorMalloc, typename ObjectDispose = csBlockAllocatorDisposeDelete<T>, typename SizeComputer = csBlockAllocatorSizeObject<T>>
csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >::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 you 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 183 of file blockallocator.h.

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

Destroy all allocated objects and release memory.

Definition at line 193 of file blockallocator.h.


Member Function Documentation

template<class T, typename Allocator = CS::Memory::AllocatorMalloc, typename ObjectDispose = csBlockAllocatorDisposeDelete<T>, typename SizeComputer = csBlockAllocatorSizeObject<T>>
template<typename A1 >
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.

template<class T, typename Allocator = CS::Memory::AllocatorMalloc, typename ObjectDispose = csBlockAllocatorDisposeDelete<T>, typename SizeComputer = csBlockAllocatorSizeObject<T>>
template<typename A1 , typename A2 , typename A3 >
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.

template<class T, typename Allocator = CS::Memory::AllocatorMalloc, typename ObjectDispose = csBlockAllocatorDisposeDelete<T>, typename SizeComputer = csBlockAllocatorSizeObject<T>>
template<typename A1 , typename A2 >
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.

template<class T, typename Allocator = CS::Memory::AllocatorMalloc, typename ObjectDispose = csBlockAllocatorDisposeDelete<T>, typename SizeComputer = csBlockAllocatorSizeObject<T>>
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.

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

Destroy all objects allocated by the pool and release the memory.

Remarks:
All pointers returned by Alloc() are invalidated. It is safe to perform new allocations from the pool after invoking DeleteAll().

Reimplemented in CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, and CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >.

Definition at line 215 of file blockallocator.h.

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

Destroy all objects allocated by the pool without releasing the memory.

Remarks:
All pointers returned by Alloc() are invalidated. It is safe to perform new allocations from the pool after invoking Empty().

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.

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

Deallocate an object.

It is safe to provide a null pointer.

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

template<class T, typename Allocator = CS::Memory::AllocatorMalloc, typename ObjectDispose = csBlockAllocatorDisposeDelete<T>, typename SizeComputer = csBlockAllocatorSizeObject<T>>
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.


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

Generated for Crystal Space 2.0 by doxygen 1.6.1