Jive reference manual
List of all members | Public Member Functions | Related Functions
jem::util::FastAllocator Class Reference

Implements a memory allocation algorithm that favors speed above fair memory use. More...

#include <jem/util/FastAllocator.h>

Public Member Functions

 FastAllocator (size_t itemSize)
 Creates a new allocator instance. More...
 
 ~FastAllocator ()
 Frees all allocated memory. More...
 
void * alloc ()
 Allocates a new memory chunk. More...
 
void dealloc (void *p)
 Deallocates a memory chunk. More...
 
void swap (FastAllocator &rhs)
 Interchanges the internal state of two allocators. More...
 

Related Functions

(Note that these are not member functions.)

void swap (FastAllocator &lhs, FastAllocator &rhs)
 Interchanges two allocator instances. More...
 

Detailed Description

Like the other allocator classes, the FastAllocator class can be used to allocate equally-sized chunks of memory. It is the fastest allocator class because it does not free memory until its destructor is called. Use this allocator when you need performance and the total number of allocated memory chunks does not vary too much in time.

See also
StandardAllocator, MallocAllocator.

Constructor & Destructor Documentation

jem::util::FastAllocator::FastAllocator ( size_t  itemSize)
explicit

Creates a FastAllocator object that allocates memory chunks of itemSize bytes.

Parameters
itemSize- the size in bytes of the memory chunks to be allocated by this FastAllocator.
Precondition
itemSize > 0
jem::util::FastAllocator::~FastAllocator ( )

Frees the memory that has been allocated by this FastAllocator object.

Warning
The lifetime of this allocator should exceed the lifetime of all the memory chunks that have been allocated by this allocator.

Member Function Documentation

void* jem::util::FastAllocator::alloc ( )

Allocates a memory chunk of size equal to the argument passed to the constructor of this FastAllocator object.

Returns
A pointer to a newly allocated memory chunk.
Exceptions
OutOfMemoryException- if there is no more memory available.
void jem::util::FastAllocator::dealloc ( void *  p)

Deallocates the memory chunk pointed to by the pointer p. This chunk must have been allocated by a call to the alloc() member function.

The FastAllocator class does not actually free the memory chunk, but marks it as not being used, so that it can be used again when alloc() is called.

Parameters
p- a pointer to the memory chunk to be deallocated.
Precondition
The memory chunk pointed to by p has been allocated by the member function alloc().
void jem::util::FastAllocator::swap ( FastAllocator rhs)

Interchanges the internal state of this allocator with the rhs allocator. After calling this function, you must invoke the dealloc() function on the rhs allocator to deallocate all memory chunks that have been obtained by invoking the alloc() function on this allocator, and the other way around.

Parameters
rhs- a FastAllocator instance.
See also
StdAllocator::swap().

Friends And Related Function Documentation

void swap ( FastAllocator lhs,
FastAllocator rhs 
)
related

This function is equivalent with:

lhs.swap( rhs )