![]() |
Protium
Math and Design Features
|
Pool of allocation chunks. More...
#include <FixedAllocator.h>
Public Member Functions | |
FixedAllocator () | |
Create a FixedAllocator which manages blocks of 'blockSize' size. More... | |
~FixedAllocator () | |
Destroy the FixedAllocator and release all its Chunks. More... | |
void * | Allocate (void) |
std::size_t | BlockSize () const |
Returns block size with which the FixedAllocator was initialized. More... | |
std::size_t | CountEmptyChunks (void) const |
bool | Deallocate (void *p, Chunk *hint) |
const Chunk * | HasBlock (void *p) const |
Chunk * | HasBlock (void *p) |
void | Initialize (std::size_t blockSize, std::size_t pageSize) |
Initializes a FixedAllocator by calculating # of blocks per Chunk. More... | |
bool | IsCorrupt (void) const |
bool | TrimChunkList (void) |
bool | TrimEmptyChunk (void) |
Private Types | |
typedef Chunks::const_iterator | ChunkCIter |
Iterator through const container of Chunks. More... | |
typedef Chunks::iterator | ChunkIter |
Iterator through container of Chunks. More... | |
typedef std::vector< Chunk > | Chunks |
Type of container used to hold Chunks. More... | |
Private Member Functions | |
FixedAllocator (const FixedAllocator &) | |
Not implemented. More... | |
void | DoDeallocate (void *p) |
bool | MakeNewChunk (void) |
FixedAllocator & | operator= (const FixedAllocator &) |
Not implemented. More... | |
Chunk * | VicinityFind (void *p) const |
Private Attributes | |
Chunk * | allocChunk_ |
Pointer to Chunk used for last or next allocation. More... | |
std::size_t | blockSize_ |
Number of bytes in a single block within a Chunk. More... | |
Chunks | chunks_ |
Container of Chunks. More... | |
Chunk * | deallocChunk_ |
Pointer to Chunk used for last or next deallocation. More... | |
Chunk * | emptyChunk_ |
Pointer to the only empty Chunk if there is one, else NULL. More... | |
unsigned char | numBlocks_ |
Number of blocks managed by each Chunk. More... | |
Static Private Attributes | |
static unsigned char | MaxObjectsPerChunk_ = 255 |
Most # of objects managed by a Chunk - never exceeds UCHAR_MAX. More... | |
static unsigned char | MinObjectsPerChunk_ = 8 |
Fewest # of objects managed by a Chunk. More... | |
Pool of allocation chunks.
Definition at line 13 of file FixedAllocator.h.
|
private |
Iterator through const container of Chunks.
Definition at line 53 of file FixedAllocator.h.
|
private |
Iterator through container of Chunks.
Definition at line 51 of file FixedAllocator.h.
|
private |
Type of container used to hold Chunks.
Definition at line 49 of file FixedAllocator.h.
|
private |
Not implemented.
Protium::Allocation::FixedAllocator::FixedAllocator | ( | ) |
Create a FixedAllocator which manages blocks of 'blockSize' size.
Definition at line 10 of file FixedAllocator.cxx.
Protium::Allocation::FixedAllocator::~FixedAllocator | ( | ) |
Destroy the FixedAllocator and release all its Chunks.
Definition at line 22 of file FixedAllocator.cxx.
void * Protium::Allocation::FixedAllocator::Allocate | ( | void | ) |
Returns pointer to allocated memory block of fixed size - or NULL if it failed to allocate.
Definition at line 292 of file FixedAllocator.cxx.
|
inline |
Returns block size with which the FixedAllocator was initialized.
Definition at line 98 of file FixedAllocator.h.
std::size_t Protium::Allocation::FixedAllocator::CountEmptyChunks | ( | void | ) | const |
Returns count of empty Chunks held by this allocator. Complexity is O(C) where C is the total number of Chunks - empty or used.
Definition at line 50 of file FixedAllocator.cxx.
bool Protium::Allocation::FixedAllocator::Deallocate | ( | void * | p, |
Chunk * | hint | ||
) |
Deallocate a memory block previously allocated with Allocate. If the block is not owned by this FixedAllocator, it returns false so that SmallObjAllocator can call the default deallocator. If the block was found, this returns true.
Definition at line 349 of file FixedAllocator.cxx.
|
private |
Deallocated the chunk located at p
p | points to the chunk |
Definition at line 426 of file FixedAllocator.cxx.
const Protium::Allocation::Chunk * Protium::Allocation::FixedAllocator::HasBlock | ( | void * | p | ) | const |
Returns true if the block at address p is within a Chunk owned by this FixedAllocator. Complexity is O(C) where C is the total number of Chunks - empty or used.
Definition at line 184 of file FixedAllocator.cxx.
|
inline |
Definition at line 131 of file FixedAllocator.h.
void Protium::Allocation::FixedAllocator::Initialize | ( | std::size_t | blockSize, |
std::size_t | pageSize | ||
) |
Initializes a FixedAllocator by calculating # of blocks per Chunk.
Definition at line 34 of file FixedAllocator.cxx.
bool Protium::Allocation::FixedAllocator::IsCorrupt | ( | void | ) | const |
Determines if FixedAllocator is corrupt. Checks data members to see if any have erroneous values, or violate class invariants. It also checks if any Chunk is corrupt. Complexity is O(C) where C is the number of Chunks. If any data is corrupt, this will return true in release mode, or assert in debug mode.
Definition at line 70 of file FixedAllocator.cxx.
|
private |
Creates an empty Chunk and adds it to the end of the ChunkList. All calls to the lower-level memory allocation functions occur inside this function, and so the only try-catch block is inside here.
Definition at line 260 of file FixedAllocator.cxx.
|
private |
Not implemented.
bool Protium::Allocation::FixedAllocator::TrimChunkList | ( | void | ) |
Releases unused spots from ChunkList. This takes constant time with respect to # of Chunks, but actual time depends on underlying memory allocator.
Definition at line 242 of file FixedAllocator.cxx.
bool Protium::Allocation::FixedAllocator::TrimEmptyChunk | ( | void | ) |
Releases the memory used by the empty Chunk. This will take constant time under any situation.
Definition at line 197 of file FixedAllocator.cxx.
|
private |
Finds the Chunk which owns the block at address p. It starts at deallocChunk_ and searches in both forwards and backwards directions from there until it finds the Chunk which owns p. This algorithm should find the Chunk quickly if it is deallocChunk_ or is close to it in the Chunks container. This goes both forwards and backwards since that works well for both same-order and opposite-order deallocations. (Same-order = objects are deallocated in the same order in which they were allocated. Opposite order = objects are deallocated in a last to first order. Complexity is O(C) where C is count of all Chunks. This never throws.
Definition at line 384 of file FixedAllocator.cxx.
|
private |
Pointer to Chunk used for last or next allocation.
Definition at line 69 of file FixedAllocator.h.
|
private |
Number of bytes in a single block within a Chunk.
Definition at line 62 of file FixedAllocator.h.
|
private |
Container of Chunks.
Definition at line 67 of file FixedAllocator.h.
|
private |
Pointer to Chunk used for last or next deallocation.
Definition at line 71 of file FixedAllocator.h.
|
private |
Pointer to the only empty Chunk if there is one, else NULL.
Definition at line 73 of file FixedAllocator.h.
|
staticprivate |
Most # of objects managed by a Chunk - never exceeds UCHAR_MAX.
Definition at line 59 of file FixedAllocator.h.
|
staticprivate |
Fewest # of objects managed by a Chunk.
Definition at line 56 of file FixedAllocator.h.
|
private |
Number of blocks managed by each Chunk.
Definition at line 64 of file FixedAllocator.h.