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

Implements a memory allocation algorithm that balances speed with fair memory use. More...

#include <jem/util/StdAllocator.h>

Public Member Functions

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

Related Functions

(Note that these are not member functions.)

void swap (StdAllocator &lhs, StdAllocator &rhs)
 Interchanges two standard allocators. More...
 

Detailed Description

Like the other allocator classes, The StdAllocator class can be used to allocate equally-sized chunks of memory. It is faster than the MallocAllocator class – that uses simply uses the standardC library function malloc() – and manages memory more fairly than the FastAllocator class – which does not free memory at all.

See also
FastAllocator, MallocAllocator.

Constructor & Destructor Documentation

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

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

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

Frees the memory that has been allocated by this StdAllocator.

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::StdAllocator::alloc ( )

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

Returns
A pointer to a newly allocated memory chunk.
Exceptions
OutOfMemoryException- if there is no more memory available.
void jem::util::StdAllocator::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.

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::StdAllocator::swap ( StdAllocator 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.

Example:

StdAllocator tinyAllocator ( 1 );
StdAllocator hugeAllocator ( 1000 );
void* a = tinyAllocator.alloc ();
void* b = hugeAllocator.alloc ();
tinyAllocator.swap ( hugeAllocator );
tinyAllocator.dealloc ( b );
hugeAllocator.dealloc ( a );
Parameters
rhs- a StdAllocator instance.

Friends And Related Function Documentation

void swap ( StdAllocator lhs,
StdAllocator rhs 
)
related

This function is equivalent with

lhs.swap( rhs )