## Public API Reference

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

- Alphabetical List
- Class List
- Class Hierarchy
- Class Members

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

## Public Types

typedef Allocator | AllocatorType
typedef csFixedSizeAllocator< Size, Allocator > | ThisType

## 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 (size_t nelem=32)
| Construct a new fixed size allocator.
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.
Functions for useability as a allocator template parameter
void * | Alloc (void *p, size_t newSize)
void * | Alloc (size_t n)
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.
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.

## Classes

struct | BlockKey
struct | BlocksWrapper
class | DefaultDisposer
| Default disposer mixin, just reporting leaks. More...
struct | FreeNode

## 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 55 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 308 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 322 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 csBlockAllocator< T, Allocator, ObjectDispose >, csBlockAllocator< csGLFontCache::GLGlyphCacheData >, csBlockAllocator< csFontCache::LRUEntry >, csBlockAllocator< csShaderVarBlockAlloc::BlockAllocatedSV, CS::Memory::AllocatorMalloc, ObjectDispose >, and csBlockAllocator< Node, CS::Memory::AllocatorAlign< 2 > >.

Definition at line 387 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Alloc().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocCommon().

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

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

Find and allocate a block.

Definition at line 275 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Alloc().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll(), csFixedSizeAllocator< sizeof(T), Allocator >::Free(), and csFixedSizeAllocator< sizeof(T), Allocator >::TryFree().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Empty(), and csFixedSizeAllocator< sizeof(T), Allocator >::~csFixedSizeAllocator().

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 csBlockAllocator< T, Allocator, ObjectDispose >, csBlockAllocator< csGLFontCache::GLGlyphCacheData >, csBlockAllocator< csFontCache::LRUEntry >, csBlockAllocator< csShaderVarBlockAlloc::BlockAllocatedSV, CS::Memory::AllocatorMalloc, ObjectDispose >, and csBlockAllocator< Node, CS::Memory::AllocatorAlign< 2 > >.

Definition at line 333 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 109 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Free(), csFixedSizeAllocator< sizeof(T), Allocator >::GetAllocationMap(), and csFixedSizeAllocator< sizeof(T), Allocator >::TryFree().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Free().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), and csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::FindBlock().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), and csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll().

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 413 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 407 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 261 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::TryFree().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocBlock(), csFixedSizeAllocator< sizeof(T), Allocator >::AllocCommon(), csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), csFixedSizeAllocator< sizeof(T), Allocator >::csFixedSizeAllocator(), csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll(), csFixedSizeAllocator< sizeof(T), Allocator >::FindBlock(), csFixedSizeAllocator< sizeof(T), Allocator >::FreeBlock(), and csFixedSizeAllocator< sizeof(T), Allocator >::GetAllocationMap().

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

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

Size in bytes per block.

Definition at line 85 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocBlock(), csFixedSizeAllocator< sizeof(T), Allocator >::csFixedSizeAllocator(), csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll(), and csFixedSizeAllocator< sizeof(T), Allocator >::FindBlock().

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

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

Number of elements per block.

Definition at line 81 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocBlock(), csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), csFixedSizeAllocator< sizeof(T), Allocator >::csFixedSizeAllocator(), csFixedSizeAllocator< sizeof(T), Allocator >::GetAllocationMap(), and csFixedSizeAllocator< sizeof(T), Allocator >::GetBlockElements().

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

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

Element size; >= sizeof(void*).

Definition at line 83 of file fixedsizeallocator.h.

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocBlock(), csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), csFixedSizeAllocator< sizeof(T), Allocator >::csFixedSizeAllocator(), csFixedSizeAllocator< sizeof(T), Allocator >::DestroyObject(), csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll(), and csFixedSizeAllocator< sizeof(T), Allocator >::GetAllocationMap().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocCommon(), csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll(), csFixedSizeAllocator< sizeof(T), Allocator >::Free(), csFixedSizeAllocator< sizeof(T), Allocator >::GetAllocationMap(), and csFixedSizeAllocator< sizeof(T), Allocator >::TryFree().

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

Referenced by csFixedSizeAllocator< sizeof(T), Allocator >::AllocCommon(), csFixedSizeAllocator< sizeof(T), Allocator >::Compact(), csFixedSizeAllocator< sizeof(T), Allocator >::DisposeAll(), csFixedSizeAllocator< sizeof(T), Allocator >::Free(), and csFixedSizeAllocator< sizeof(T), Allocator >::TryFree().

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

- csutil/fixedsizeallocator.h