CXXR (C++ R) API
|
Class to manage a pool of memory cells of a fixed size. More...
#include <CellPool.hpp>
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 |
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.
|
inline |
Constructor.
Note that CellPool objects must be initialized by calling initialize() before being used.
CXXR::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.)
|
inline |
Allocate a cell from the pool.
bad_alloc | if a cell cannot be allocated. |
|
inline |
Number of cells allocated from this CellPool.
|
inline |
Size of cells.
bool CXXR::CellPool::check | ( | ) | const |
Integrity check.
Aborts the program with an error message if the object is found to be internally inconsistent.
assert
.
|
inline |
Deallocate a cell.
p | Pointer to a block of memory previously allocated from this pool, or a null pointer (in which case method does nothing). |
void CXXR::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 CXXR::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.
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. |
|
inline |