Protium
Math and Design Features
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros Pages
Protium::Allocation::FixedAllocator Class Reference

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 ChunkHasBlock (void *p) const
 
ChunkHasBlock (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< ChunkChunks
 Type of container used to hold Chunks. More...
 

Private Member Functions

 FixedAllocator (const FixedAllocator &)
 Not implemented. More...
 
void DoDeallocate (void *p)
 
bool MakeNewChunk (void)
 
FixedAllocatoroperator= (const FixedAllocator &)
 Not implemented. More...
 
ChunkVicinityFind (void *p) const
 

Private Attributes

ChunkallocChunk_
 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...
 
ChunkdeallocChunk_
 Pointer to Chunk used for last or next deallocation. More...
 
ChunkemptyChunk_
 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...
 

Detailed Description

Pool of allocation chunks.

Definition at line 13 of file FixedAllocator.h.

Member Typedef Documentation

typedef Chunks::const_iterator Protium::Allocation::FixedAllocator::ChunkCIter
private

Iterator through const container of Chunks.

Definition at line 53 of file FixedAllocator.h.

typedef Chunks::iterator Protium::Allocation::FixedAllocator::ChunkIter
private

Iterator through container of Chunks.

Definition at line 51 of file FixedAllocator.h.

typedef std::vector< Chunk > Protium::Allocation::FixedAllocator::Chunks
private

Type of container used to hold Chunks.

Definition at line 49 of file FixedAllocator.h.

Constructor & Destructor Documentation

Protium::Allocation::FixedAllocator::FixedAllocator ( const FixedAllocator )
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.

Member Function Documentation

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.

std::size_t Protium::Allocation::FixedAllocator::BlockSize ( ) const
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.

void Protium::Allocation::FixedAllocator::DoDeallocate ( void *  p)
private

Deallocated the chunk located at p

Parameters
ppoints to the chunk
Warning
Requires the internal pointer to be at the chunk in question

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.

Chunk* Protium::Allocation::FixedAllocator::HasBlock ( void *  p)
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.

bool Protium::Allocation::FixedAllocator::MakeNewChunk ( void  )
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.

Returns
true for success, false for failure.

Definition at line 260 of file FixedAllocator.cxx.

FixedAllocator& Protium::Allocation::FixedAllocator::operator= ( const FixedAllocator )
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.

Returns
False if no unused spots, true if some found and released.

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.

Returns
True if empty chunk found and released, false if none empty.

Definition at line 197 of file FixedAllocator.cxx.

Protium::Allocation::Chunk * Protium::Allocation::FixedAllocator::VicinityFind ( void *  p) const
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.

Returns
Pointer to Chunk that owns p, or NULL if no owner found.

Definition at line 384 of file FixedAllocator.cxx.

Member Data Documentation

Chunk* Protium::Allocation::FixedAllocator::allocChunk_
private

Pointer to Chunk used for last or next allocation.

Definition at line 69 of file FixedAllocator.h.

std::size_t Protium::Allocation::FixedAllocator::blockSize_
private

Number of bytes in a single block within a Chunk.

Definition at line 62 of file FixedAllocator.h.

Chunks Protium::Allocation::FixedAllocator::chunks_
private

Container of Chunks.

Definition at line 67 of file FixedAllocator.h.

Chunk* Protium::Allocation::FixedAllocator::deallocChunk_
private

Pointer to Chunk used for last or next deallocation.

Definition at line 71 of file FixedAllocator.h.

Chunk* Protium::Allocation::FixedAllocator::emptyChunk_
private

Pointer to the only empty Chunk if there is one, else NULL.

Definition at line 73 of file FixedAllocator.h.

unsigned char Protium::Allocation::FixedAllocator::MaxObjectsPerChunk_ = 255
staticprivate

Most # of objects managed by a Chunk - never exceeds UCHAR_MAX.

Definition at line 59 of file FixedAllocator.h.

unsigned char Protium::Allocation::FixedAllocator::MinObjectsPerChunk_ = 8
staticprivate

Fewest # of objects managed by a Chunk.

Definition at line 56 of file FixedAllocator.h.

unsigned char Protium::Allocation::FixedAllocator::numBlocks_
private

Number of blocks managed by each Chunk.

Definition at line 64 of file FixedAllocator.h.