CXXR (C++ R)
Public Member Functions
CXXR::CellPool Class Reference

Class to manage a pool of memory cells of a fixed size. More...

#include <CellPool.hpp>

List of all members.

Public Member Functions

 CellPool ()
 Constructor.
 ~CellPool ()
void * allocate () throw (std::bad_alloc)
 Allocate a cell from the pool.
size_t cellSize () const
 Size of cells.
unsigned int cellsAllocated () const
 Number of cells allocated from this CellPool.
bool check () const
 Integrity check.
void deallocate (void *p)
 Deallocate a cell.
void defragment ()
 Reorganise list of free cells within the CellPool.
void initialize (size_t dbls_per_cell, size_t cells_per_superblock)
 Initialize the CellPool.
size_t superblockSize () const

Detailed Description

Class to manage a pool of memory cells of a fixed size.

This class, based closely on Item 10 of Scott Meyers' 'Effective C++ (2nd edition)' manages a collection of memory cells of a specified size, and is intended as a back-end to implementations of operator new and operator delete to enable the allocation and deallocation of small objects quickly.

Normally class CellPool operates a last-in-first-out allocation policy; that is to say, if there are cells that have been deallocated and not yet reallocated, the next one to be reallocated will be the one that was most recently deallocated. This makes for efficient utilisation of the processor caches. However, if the class is compiled with the preprocessor variable CELLFIFO defined, it instead operates a first-in-first-out policy. This results in a longer turnround time in reallocating cells, which improves the lethality of the FILL55 preprocessor variable in showing up premature deallocation of cells.


Constructor & Destructor Documentation

CXXR::CellPool::CellPool ( )
inline

Constructor.

Note that CellPool objects must be initialized by calling initialize() before being used.

CellPool::~CellPool ( )

Destructor

It is up to the user to check that any cells allocated from the pool have been freed before this destructor is invoked. (Although the destructor could check this for itself and issue an error message, this message would probably be a nuisance if it occurred during program shutdown.)


Member Function Documentation

void* CXXR::CellPool::allocate ( ) throw (std::bad_alloc)
inline

Allocate a cell from the pool.

Returns:
a pointer to the allocated cell.
Exceptions:
bad_allocif a cell cannot be allocated.
unsigned int CXXR::CellPool::cellsAllocated ( ) const
inline

Number of cells allocated from this CellPool.

Returns:
the number of cells currently allocated from this pool.
size_t CXXR::CellPool::cellSize ( ) const
inline

Size of cells.

Returns:
the size of each cell in bytes (well, strictly as a multiple of sizeof(char)).
bool CellPool::check ( ) const

Integrity check.

Aborts the program with an error message if the object is found to be internally inconsistent.

Returns:
true, if it returns at all. The return value is to facilitate use with assert .
void CXXR::CellPool::deallocate ( void *  p)
inline

Deallocate a cell.

Parameters:
pPointer to a block of memory previously allocated from this pool, or a null pointer (in which case method does nothing).
void CellPool::defragment ( )

Reorganise list of free cells within the CellPool.

This is done with a view to increasing the probability that successive allocations will lie within the same cache line or (less importantly nowadays) memory page.

void CellPool::initialize ( size_t  dbls_per_cell,
size_t  cells_per_superblock 
)

Initialize the CellPool.

This function must be called exactly once for each CellPool, before any allocation is made from it.

Parameters:
dbls_per_cell(must be >= 1). Size of cells, expressed as a multiple of sizeof(double). For example, if you require cells large enough to contain one double, put dbls_per_cell as 1. (NB: cells can contain anything, not just doubles; we work in doubles because these are likely to have the most stringent address alignment requirements.)
cells_per_superblock(must be >= 1). Memory for cells is obtained from the main heap in 'superblocks' sufficient to contain this many cells.
size_t CXXR::CellPool::superblockSize ( ) const
inline
Returns:
The size in bytes of the superblocks from which cells are allocated.

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