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

#include <CellHeap.hpp>

List of all members.

Public Member Functions

 CellHeap (size_t dbls_per_cell, size_t cells_per_superblock)
 ~CellHeap ()
void * allocate () throw (std::bad_alloc)
 Allocate a cell from the heap.
size_t cellSize () const
unsigned int cellsAllocated () const
bool check () const
 Integrity check.
void deallocate (void *p)
 Deallocate a cell.
void * easyAllocate () throw ()
 Allocate a cell 'from stock'.
size_t superblockSize () const

Detailed Description

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

This class 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. It differs from CellPool in that it always allocates the cell with the minimum address from among the currently available cells. This is achieved using a skew heap data structure (Sleator and Tarjan 1986). This data structure works most efficiently if cells are as far as possible released in the reverse order of allocation.


Constructor & Destructor Documentation

CXXR::CellHeap::CellHeap ( size_t  dbls_per_cell,
size_t  cells_per_superblock 
)
inline
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.
CellHeap::~CellHeap ( )

Destructor

It is up to the user to check that any cells allocated from the heap 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::CellHeap::allocate ( ) throw (std::bad_alloc)
inline

Allocate a cell from the heap.

Returns:
a pointer to the allocated cell.
Exceptions:
bad_allocif a cell cannot be allocated.
unsigned int CXXR::CellHeap::cellsAllocated ( ) const
inline
Returns:
the number of cells currently allocated from this heap.
size_t CXXR::CellHeap::cellSize ( ) const
inline
Returns:
the size of each cell in bytes (well, strictly as a multiple of sizeof(char)).
bool CellHeap::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 CellHeap::deallocate ( void *  p)

Deallocate a cell.

Parameters:
pPointer to a block of memory previously allocated from this heap, or a null pointer (in which case method does nothing).
void* CXXR::CellHeap::easyAllocate ( ) throw ()
inline

Allocate a cell 'from stock'.

Allocate a cell from the heap, provided it can be allocated 'from stock'. Can be useful when called from other inlined functions in that it doesn't throw any exceptions.

Returns:
a pointer to the allocated cell, or 0 if the cell cannot be allocated from the current memory superblocks.
size_t CXXR::CellHeap::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: