csFixedSizeAllocator< Size, Allocator > Class Template Reference (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- Class List
- Class Index
- Class Hierarchy
- Class Members

Classes | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes
csFixedSizeAllocator< Size, Allocator > 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/fixedsizeallocator.h>`

Inheritance diagram for csFixedSizeAllocator< Size, Allocator >:

List of all members.

## Classes
class | DefaultDisposer
| Default disposer mixin, just reporting leaks. More...
## Public Member Functions
void * | Alloc ()
| Allocate a chunk of memory.
void | Compact ()
| Compact the allocator so that all blocks that are completely unused are removed.
| csFixedSizeAllocator (csFixedSizeAllocator const &other)
| Construct a new fixed size allocator, copying the amounts of elements to store in an allocation unit.
void | Empty ()
| Destroy all chunks allocated.
void | Free (void *p)
| Deallocate a chunk of memory.
size_t | GetAllocatedElems () const
| Return number of allocated elements (potentially slow).
size_t | GetBlockElements () const
| Query number of elements per block.
bool | TryFree (void *p)
| Try to delete a chunk of memory.
| ~csFixedSizeAllocator ()
| Destroy all allocated objects and release memory.
| csFixedSizeAllocator (size_t nelem=32)
| Construct a new fixed size allocator.
| csFixedSizeAllocator (size_t nelem, const Allocator &alloc)
| Construct a new fixed size allocator.
Functions for useability as a allocator template parameter
void * | Alloc (size_t n)
void * | Alloc (void *p, size_t newSize)
void | SetMemTrackerInfo (const char *)
## Protected Member Functions
uint8 * | AllocBlock ()
| Allocate a block and initialize its free-node chain.
void * | AllocCommon ()
| Find and allocate a block.
template<typename Disposer >
void | DestroyObject (Disposer &disposer, void *p) const
| Destroy an object.
template<typename Disposer >
void | DisposeAll (Disposer &disposer)
| Destroys all living objects and releases all memory allocated by the pool.
size_t | FindBlock (void const *m) const
| Find the memory block which contains the given memory.
template<typename Disposer >
void | Free (Disposer &disposer, void *p)
| Deallocate a chunk of memory.
template<typename Disposer >
void | FreeAll (Disposer &disposer)
| Free all objects without releasing the memory blocks themselves.
void | FreeBlock (uint8 *p)
| Dispose of a block.
csBitArray | GetAllocationMap () const
| Get a usage mask showing all used (1's) and free (0's) nodes in the entire allocator.
template<typename Disposer >
bool | TryFree (Disposer &disposer, void *p)
| Try to delete a chunk of memory.
## Static Protected Member Functions
static int | FuzzyCmp (uint8 *const &block, BlockKey const &k)
| Comparison function for FindBlock() which does a "fuzzy" search given an arbitrary address.
## Protected Attributes
BlocksWrapper | blocks
| List of allocated blocks; sorted by address.
size_t | blocksize
| Size in bytes per block.
size_t | elcount
| Number of elements per block.
size_t | elsize
| Element size; >= sizeof(void*).
FreeNode * | freenode
| Head of the chain of free nodes.
bool | insideDisposeAll
| Flag to ignore calls to Compact() and Free() if they're called recursively while disposing the entire allocation set.

## Detailed Description

### template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
class csFixedSizeAllocator< Size, Allocator >

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:
  Defining the macro CS_FIXEDSIZEALLOC_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.)

See also:
  csArray
  csMemoryPool
  CS::Memory::FixedSizeAllocatorSafe for a thread-safe version

Definition at line 61 of file fixedsizeallocator.h.

## Constructor & Destructor Documentation

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

csFixedSizeAllocator< Size, Allocator >::csFixedSizeAllocator | ( | size_t | nelem = `32` | ) | `[inline]`

Construct a new fixed size 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).

Definition at line 351 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

csFixedSizeAllocator< Size, Allocator >::csFixedSizeAllocator | ( | size_t | nelem,
|  | const Allocator & | alloc
| ) |  | `[inline]`

Construct a new fixed size 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).

Definition at line 361 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

csFixedSizeAllocator< Size, Allocator >::csFixedSizeAllocator | ( | csFixedSizeAllocator< Size, Allocator > const & | other | ) | `[inline]`

Construct a new fixed size allocator, copying the amounts of elements to store in an allocation unit.

Remarks:
  Copy-constructing an allocator is only valid if the allocator copied from is not empty. Attempting to copy a non-empty allocator will cause an assertion to fail at runtime!

Definition at line 380 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

csFixedSizeAllocator< Size, Allocator >::~csFixedSizeAllocator | ( |  | ) | `[inline]`

Destroy all allocated objects and release memory.

Definition at line 394 of file fixedsizeallocator.h.

## Member Function Documentation

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

void* csFixedSizeAllocator< Size, Allocator >::Alloc | ( |  | ) | `[inline]`

Allocate a chunk of memory.

Reimplemented in CS::Memory::FixedSizeAllocatorSafe< Size, Allocator >, CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >, csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >, csBlockAllocator< LRUEntry >, csBlockAllocator< SubRect >, csBlockAllocator< BlockAllocatedSV, CS::Memory::AllocatorMalloc, csBlockAllocatorDisposeLeaky< csShaderVariable > >, csBlockAllocator< BlockAllocatedSV, CS::Memory::AllocatorMalloc, ObjectDispose >, csBlockAllocator< GLGlyphCacheData >, csBlockAllocator< Element >, csBlockAllocator< ContextNode >, csBlockAllocator< Node, CS::Memory::AllocatorAlign< 32 >, csBlockAllocatorDisposeLeaky< Node >, csBlockAllocatorSizeObjectAlign< Node, 32 > >, csBlockAllocator< MeshNode >, and csBlockAllocator< CS::Math::Matrix4, AllocatorMalloc, csBlockAllocatorDisposeDelete< CS::Math::Matrix4 >, csBlockAllocatorSizeObject< CS::Math::Matrix4 > >.

Definition at line 468 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

uint8* csFixedSizeAllocator< Size, Allocator >::AllocBlock | ( |  | ) | `[inline, protected]`

Allocate a block and initialize its free-node chain.

Returns:
  The returned address is both the reference to the overall block, and the address of the first free node in the chain.

Definition at line 129 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

void* csFixedSizeAllocator< Size, Allocator >::AllocCommon | ( |  | ) | `[inline, protected]`

Find and allocate a block.

Definition at line 310 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

void csFixedSizeAllocator< Size, Allocator >::Compact | ( |  | ) | `[inline]`

Compact the allocator so that all blocks that are completely unused are removed.

The blocks that still contain elements are not touched.

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

Definition at line 415 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
template<typename Disposer >

void csFixedSizeAllocator< Size, Allocator >::DestroyObject | ( | Disposer & | disposer,
|  | void * | p
| ) |  | const`[inline, protected]`

Destroy an object.

Definition at line 158 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
template<typename Disposer >

void csFixedSizeAllocator< Size, Allocator >::DisposeAll | ( | Disposer & | disposer | ) | `[inline, protected]`

Destroys all living objects and releases all memory allocated by the pool.

Parameters:

  disposer | Object with a Dispose(void* p) method which is called prior to freeing the actual memory.

Definition at line 229 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

void csFixedSizeAllocator< Size, Allocator >::Empty | ( |  | ) | `[inline]`

Destroy all chunks allocated.

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

Reimplemented in CS::Memory::FixedSizeAllocatorSafe< Size, Allocator >, CS::Memory::BlockAllocatorSafe< T, Allocator, ObjectDispose, SizeComputer >, CS::Memory::BlockAllocatorSafe< CS::Math::Matrix4 >, csBlockAllocator< T, Allocator, ObjectDispose, SizeComputer >, csBlockAllocator< LRUEntry >, csBlockAllocator< SubRect >, csBlockAllocator< BlockAllocatedSV, CS::Memory::AllocatorMalloc, csBlockAllocatorDisposeLeaky< csShaderVariable > >, csBlockAllocator< BlockAllocatedSV, CS::Memory::AllocatorMalloc, ObjectDispose >, csBlockAllocator< GLGlyphCacheData >, csBlockAllocator< Element >, csBlockAllocator< ContextNode >, csBlockAllocator< Node, CS::Memory::AllocatorAlign< 32 >, csBlockAllocatorDisposeLeaky< Node >, csBlockAllocatorSizeObjectAlign< Node, 32 > >, csBlockAllocator< MeshNode >, and csBlockAllocator< CS::Math::Matrix4, AllocatorMalloc, csBlockAllocatorDisposeDelete< CS::Math::Matrix4 >, csBlockAllocatorSizeObject< CS::Math::Matrix4 > >.

Definition at line 405 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

size_t csFixedSizeAllocator< Size, Allocator >::FindBlock | ( | void const * | m | ) | const`[inline, protected]`

Find the memory block which contains the given memory.

Definition at line 118 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
template<typename Disposer >

void csFixedSizeAllocator< Size, Allocator >::Free | ( | Disposer & | disposer,
|  | void * | p
| ) |  | `[inline, protected]`

Deallocate a chunk of memory.

It is safe to provide a null pointer.

Parameters:

  disposer | Disposer object that is passed to DestroyObject().
  p | Pointer to deallocate.

Definition at line 252 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

void csFixedSizeAllocator< Size, Allocator >::Free | ( | void * | p | ) | `[inline]`

Deallocate a chunk of memory.

It is safe to provide a null pointer.

Parameters:

  p | Pointer to deallocate.

Definition at line 477 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
template<typename Disposer >

void csFixedSizeAllocator< Size, Allocator >::FreeAll | ( | Disposer & | disposer | ) | `[inline, protected]`

Free all objects without releasing the memory blocks themselves.

This works almost as DisposeAll but does not free the memory.

Parameters:

  disposer | Disposer object that is passed to DestroyObject().

Definition at line 288 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

void csFixedSizeAllocator< Size, Allocator >::FreeBlock | ( | uint8 * | p | ) | `[inline, protected]`

Dispose of a block.

Definition at line 149 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

static int csFixedSizeAllocator< Size, Allocator >::FuzzyCmp | ( | uint8 *const & | block,
|  | BlockKey const & | k
| ) |  | `[inline, static, protected]`

Comparison function for FindBlock() which does a "fuzzy" search given an arbitrary address.

It checks if the address falls somewhere within a block rather than checking if the address exactly matches the start of the block (which is the only information recorded in blocks[] array).

Definition at line 110 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

size_t csFixedSizeAllocator< Size, Allocator >::GetAllocatedElems | ( |  | ) | const`[inline]`

Return number of allocated elements (potentially slow).

Reimplemented in CS::Memory::FixedSizeAllocatorSafe< Size, Allocator >.

Definition at line 459 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

csBitArray csFixedSizeAllocator< Size, Allocator >::GetAllocationMap | ( |  | ) | const`[inline, protected]`

Get a usage mask showing all used (1's) and free (0's) nodes in the entire allocator.

Definition at line 170 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

size_t csFixedSizeAllocator< Size, Allocator >::GetBlockElements | ( |  | ) | const`[inline]`

Query number of elements per block.

Reimplemented in CS::Memory::FixedSizeAllocatorSafe< Size, Allocator >.

Definition at line 494 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
template<typename Disposer >

bool csFixedSizeAllocator< Size, Allocator >::TryFree | ( | Disposer & | disposer,
|  | void * | p
| ) |  | `[inline, protected]`

Try to delete a chunk of memory.

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 270 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

bool csFixedSizeAllocator< Size, Allocator >::TryFree | ( | void * | p | ) | `[inline]`

Try to delete a chunk of memory.

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::FixedSizeAllocatorSafe< Size, Allocator >.

Definition at line 488 of file fixedsizeallocator.h.

## Member Data Documentation

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

BlocksWrapper csFixedSizeAllocator< Size, Allocator >::blocks`[protected]`

List of allocated blocks; sorted by address.

Definition at line 88 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

size_t csFixedSizeAllocator< Size, Allocator >::blocksize`[protected]`

Size in bytes per block.

Definition at line 94 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

size_t csFixedSizeAllocator< Size, Allocator >::elcount`[protected]`

Number of elements per block.

Definition at line 90 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

size_t csFixedSizeAllocator< Size, Allocator >::elsize`[protected]`

Element size; >= sizeof(void*).

Definition at line 92 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

FreeNode* csFixedSizeAllocator< Size, Allocator >::freenode`[protected]`

Head of the chain of free nodes.

Definition at line 96 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>

bool csFixedSizeAllocator< Size, Allocator >::insideDisposeAll`[protected]`

Flag to ignore calls to Compact() and Free() if they're called recursively while disposing the entire allocation set.

Recursive calls to Alloc() will signal an assertion failure.

Definition at line 102 of file fixedsizeallocator.h.

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

- csutil/fixedsizeallocator.h