CrystalSpace

Public API Reference

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>

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

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

Definition at line 60 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 324 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 334 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 353 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 364 of file fixedsizeallocator.h.


Member Function Documentation

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 128 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 283 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.

Definition at line 385 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 157 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 228 of file fixedsizeallocator.h.

template<size_t Size, class Allocator = CS::Memory::AllocatorMalloc>
void csFixedSizeAllocator< Size, Allocator >::Empty (  )  [inline]
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 117 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 251 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 438 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 148 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 109 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 169 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.

Definition at line 455 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 269 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).

Definition at line 449 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 87 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 93 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 89 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 91 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 95 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 101 of file fixedsizeallocator.h.


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

Generated for Crystal Space 1.4.1 by doxygen 1.7.1