CrystalSpace

Public API Reference

Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

csBlockAllocator< T > Class Template Reference

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

#include <csutil/blockallocator.h>

List of all members.

Public Member Functions

 csBlockAllocator (size_t s)
 Construct a new block allocator which uses 'size' elements per block.
 ~csBlockAllocator ()
 Destruct all.
void Compact ()
 Compact the block allocator so that all blocks that are completely free are removed.
T * Alloc ()
 Allocate a new element.
void Free (T *el)
 Deallocate an element.
void Dump ()
 For debugging: dump contents.


Detailed Description

template<class T>
class csBlockAllocator< T >

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 8 bytes) and is extremely fast, both for Alloc and Free. Only restriction is it can only be used for the same type of object.

Note that the objects are properly constructed and destructed.

WARNING: This class does VERY little error checking!

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

Definition at line 57 of file blockallocator.h.


Constructor & Destructor Documentation

template<class T>
csBlockAllocator< T >::csBlockAllocator size_t  s  )  [inline]
 

Construct a new block allocator which uses 'size' elements per block.

Using a bigger 'size' means there is more memory 'wasted' but it makes performance faster.

Definition at line 153 of file blockallocator.h.

template<class T>
csBlockAllocator< T >::~csBlockAllocator  )  [inline]
 

Destruct all.

Note that objects have to be freed properly before the block allocator is called, otherwise the destructors will not be called! In debug mode this destructor will also check that all objects are freed correctly.

Definition at line 185 of file blockallocator.h.


Member Function Documentation

template<class T>
T* csBlockAllocator< T >::Alloc  )  [inline]
 

Allocate a new element.

Definition at line 222 of file blockallocator.h.

Referenced by csTree< T >::csTree().

template<class T>
void csBlockAllocator< T >::Compact  )  [inline]
 

Compact the block allocator so that all blocks that are completely free are removed.

The blocks that still contain elements are not touched.

Definition at line 203 of file blockallocator.h.

template<class T>
void csBlockAllocator< T >::Dump  )  [inline]
 

For debugging: dump contents.

Definition at line 382 of file blockallocator.h.

template<class T>
void csBlockAllocator< T >::Free T *  el  )  [inline]
 

Deallocate an element.

It is ok to give a 0 pointer here.

Definition at line 255 of file blockallocator.h.

Referenced by csTree< T >::Delete().


The documentation for this class was generated from the following file:
Generated for Crystal Space by doxygen 1.3.9.1