NLMISC::CFixedSizeAllocator Class Reference

#include <fixed_size_allocator.h>


Detailed Description

An allocator that can allocate and deallocate blocks of fixed size in O(1) Blocks are managed by chunks. Any number of blocks can be allocated, but once a block is allocated, a whole chunk need to be. With 32 bits pointers, there may be a 4 - 12 bytes overhead per object NB: Unlike CBlockManager, when a chunk contains no allocated blocks, it will be freed. If only one chunk remains, it isn't deleted, however. Another motivation for that class is that it can be used for a memory arena style allocator, because size isn't a fixed parameter of template. A fixed size allocator implemented as a template for type T can be layered on this implementation.

NB : number of blocks per chunks must be at least 3

: thread safety

Author:
Nicolas Vizerie

Nevrax France

Date:
2004

Definition at line 49 of file fixed_size_allocator.h.

Public Member Functions

void * alloc ()
 alloc a block

 CFixedSizeAllocator (uint numBytesPerBlock, uint numBlockPerChunk)
void free (void *block)
 destroy and dealloc a block

uint getNumAllocatedBlocks () const
uint getNumBlockPerChunk () const
uint getNumBytesPerBlock () const
 ~CFixedSizeAllocator ()

Private Attributes

CNode_FreeSpace
uint _NumAlloc
uint _NumBlockPerChunk
uint _NumBytesPerBlock
uint _NumChunks

Friends

class CChunk
class CNode


Constructor & Destructor Documentation

NLMISC::CFixedSizeAllocator::CFixedSizeAllocator uint  numBytesPerBlock,
uint  numBlockPerChunk
 

Definition at line 33 of file fixed_size_allocator.cpp.

References _FreeSpace, _NumAlloc, _NumBlockPerChunk, _NumBytesPerBlock, _NumChunks, min, nlassert, and uint.

00034 {
00035         _FreeSpace = NULL;
00036         _NumChunks = 0;
00037         nlassert(numBytesPerBlock > 1);
00038         _NumBytesPerBlock = numBytesPerBlock;
00039         _NumBlockPerChunk = std::min(numBlockPerChunk, (uint) 3);
00040         _NumAlloc = 0;
00041 }

NLMISC::CFixedSizeAllocator::~CFixedSizeAllocator  ) 
 

Definition at line 44 of file fixed_size_allocator.cpp.

References _FreeSpace, _NumAlloc, _NumChunks, NLMISC::CFixedSizeAllocator::CNode::Chunk, nlassert, and nlwarning.

00045 {       
00046         if (_NumAlloc != 0)
00047         {
00048                 #ifdef NL_DEBUG
00049                         nlwarning("%d blocks were not freed", (int) _NumAlloc);
00050                 #endif
00051                 return;
00052         }
00053         if (_NumChunks > 0)
00054         {               
00055                 nlassert(_NumChunks == 1);
00056                 // delete the left chunk. This should force all the left nodes to be removed from the empty list
00057                 delete _FreeSpace->Chunk;
00058         }       
00059 }


Member Function Documentation

void * NLMISC::CFixedSizeAllocator::alloc  ) 
 

alloc a block

Definition at line 62 of file fixed_size_allocator.cpp.

References _FreeSpace, _NumAlloc, NLMISC::CFixedSizeAllocator::CChunk::init(), and NLMISC::CFixedSizeAllocator::CNode::unlink().

00063 {
00064         if (!_FreeSpace)
00065         {                               
00066                 CChunk *chunk = new CChunk; // link a new chunk to that object
00067                 chunk->init(this);
00068         }
00069         ++ _NumAlloc;
00070         return _FreeSpace->unlink();            
00071 }

void NLMISC::CFixedSizeAllocator::free void *  block  ) 
 

destroy and dealloc a block

get the node from the object

Definition at line 74 of file fixed_size_allocator.cpp.

References _NumAlloc, NLMISC::CFixedSizeAllocator::CChunk::Allocator, NLMISC::CFixedSizeAllocator::CNode::Chunk, NLMISC::CFixedSizeAllocator::CNode::link(), nlassert, and uint8.

00075 {
00076         if (!block) return;     
00078         CNode *node = (CNode *) ((uint8 *) block - offsetof(CNode, Next));
00079         //      
00080         nlassert(node->Chunk);
00081         nlassert(node->Chunk->Allocator == this);
00082         //      
00083         --_NumAlloc;
00084         node->link();
00085 }

uint NLMISC::CFixedSizeAllocator::getNumAllocatedBlocks  )  const [inline]
 

Definition at line 62 of file fixed_size_allocator.h.

References _NumAlloc, and uint.

00062 { return _NumAlloc; }

uint NLMISC::CFixedSizeAllocator::getNumBlockPerChunk  )  const [inline]
 

Definition at line 60 of file fixed_size_allocator.h.

References _NumBlockPerChunk, and uint.

Referenced by NLMISC::CFixedSizeAllocator::CChunk::add(), NLMISC::CFixedSizeAllocator::CChunk::getNode(), and NLMISC::CFixedSizeAllocator::CChunk::~CChunk().

00060 { return _NumBlockPerChunk; }

uint NLMISC::CFixedSizeAllocator::getNumBytesPerBlock  )  const [inline]
 

Definition at line 59 of file fixed_size_allocator.h.

References _NumBytesPerBlock, and uint.

Referenced by NLMISC::CFixedSizeAllocator::CChunk::getBlockSizeWithOverhead().

00059 { return _NumBytesPerBlock; }


Friends And Related Function Documentation

friend class CChunk [friend]
 

Definition at line 104 of file fixed_size_allocator.h.

friend class CNode [friend]
 

Definition at line 105 of file fixed_size_allocator.h.


Field Documentation

CNode* NLMISC::CFixedSizeAllocator::_FreeSpace [private]
 

Definition at line 107 of file fixed_size_allocator.h.

Referenced by alloc(), CFixedSizeAllocator(), NLMISC::CFixedSizeAllocator::CNode::link(), and ~CFixedSizeAllocator().

uint NLMISC::CFixedSizeAllocator::_NumAlloc [private]
 

Definition at line 114 of file fixed_size_allocator.h.

Referenced by alloc(), CFixedSizeAllocator(), free(), getNumAllocatedBlocks(), and ~CFixedSizeAllocator().

uint NLMISC::CFixedSizeAllocator::_NumBlockPerChunk [private]
 

Definition at line 112 of file fixed_size_allocator.h.

Referenced by CFixedSizeAllocator(), and getNumBlockPerChunk().

uint NLMISC::CFixedSizeAllocator::_NumBytesPerBlock [private]
 

Definition at line 111 of file fixed_size_allocator.h.

Referenced by CFixedSizeAllocator(), and getNumBytesPerBlock().

uint NLMISC::CFixedSizeAllocator::_NumChunks [private]
 

Definition at line 108 of file fixed_size_allocator.h.

Referenced by NLMISC::CFixedSizeAllocator::CChunk::add(), CFixedSizeAllocator(), NLMISC::CFixedSizeAllocator::CChunk::~CChunk(), and ~CFixedSizeAllocator().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:13:13 2004 for NeL by doxygen 1.3.6