NLMISC::CContiguousBlockAllocator Class Reference

#include <contiguous_block_allocator.h>


Detailed Description

One of the simplest scheme of allocation around, but very useful in some situations. This allocator is just provided with a single block of memory at start (possibly of size 0). Each alloc get a new block in that big block, by simply advancing a pointer. When not enough space is available, the default stl allocator is used. When a block is deallocated, nothing happens, unless the block was allocated using the default stl allocator is used, in which case deallocate() is called.

The typical use is when an object makes a lot of allocations at init, but in a predictable way, and if it don't make alloc / realloc later. In this case the caller can measure the amount of memory needed to create the object, and can create this allocator with the good amount of memory. Subsequent allocations will then be very fast even for very differently sized blocks, with no fragmentation inside the allocated block and no memory overhead per allocated bloc.

Obviously, if the quantity of memory to be allocated can't be predicted (or if no max bytes can be forseen), then other allocators may be best suited.

: thread safety

Author:
Nicolas Vizerie

Nevrax France

Date:
2004

Definition at line 52 of file contiguous_block_allocator.h.

Public Member Functions

void * alloc (uint numBytes)
 CContiguousBlockAllocator ()
void free (void *block)
uint getNumAllocatedBytes () const
void init (uint numBytes=0)
void release ()
 ~CContiguousBlockAllocator ()

Private Attributes

uint8_BlockEnd
uint8_BlockStart
std::allocator< uint8_DefaultAlloc
uint8_NextAvailablePos
uint _NumAllocatedBytes


Constructor & Destructor Documentation

NLMISC::CContiguousBlockAllocator::CContiguousBlockAllocator  ) 
 

Definition at line 32 of file contiguous_block_allocator.cpp.

References _BlockEnd, _BlockStart, _NextAvailablePos, and _NumAllocatedBytes.

00033 {
00034         _BlockStart = NULL;
00035         _NextAvailablePos = NULL;
00036         _BlockEnd = 0;
00037         _NumAllocatedBytes = 0;
00038         #ifdef NL_DEBUG
00039                 _NumAlloc = 0;
00040                 _NumFree = 0;
00041         #endif
00042 }

NLMISC::CContiguousBlockAllocator::~CContiguousBlockAllocator  ) 
 

Definition at line 45 of file contiguous_block_allocator.cpp.

References init().

00046 {
00047         init(0);
00048 }


Member Function Documentation

void * NLMISC::CContiguousBlockAllocator::alloc uint  numBytes  ) 
 

Definition at line 72 of file contiguous_block_allocator.cpp.

References _BlockEnd, _BlockStart, _DefaultAlloc, _NextAvailablePos, _NumAllocatedBytes, uint, and uint8.

00073 {
00074         if (numBytes == 0) return NULL;
00075         _NumAllocatedBytes += numBytes;
00076         if (_BlockStart)
00077         {
00078                 if (_NextAvailablePos + numBytes <= _BlockEnd)
00079                 {
00080                         uint8 *block = _NextAvailablePos;
00081                         _NextAvailablePos += numBytes;
00082                         #ifdef NL_DEBUG
00083                                 ++ _NumAlloc;
00084                         #endif
00085                         return block;
00086                 }
00087         }
00088         // just uses standard new
00089         #ifdef NL_DEBUG
00090                 ++ _NumAlloc;
00091         #endif
00092         return _DefaultAlloc.allocate(numBytes);
00093 }

void NLMISC::CContiguousBlockAllocator::free void *  block  ) 
 

Definition at line 96 of file contiguous_block_allocator.cpp.

References _BlockEnd, _DefaultAlloc, and uint8.

00097 {
00098         if (!block) return;
00099         #ifdef NL_DEBUG
00100                 ++ _NumFree;
00101         #endif
00102         // no-op if block not inside the big block (sub-block are never deallocated until init(0) is encountered)
00103         if (block < _BlockStart || block >= _BlockEnd)
00104         {
00105                 // the block was allocated with std allocator
00106                 _DefaultAlloc.deallocate((uint8 *) block);
00107         }
00108 }

uint NLMISC::CContiguousBlockAllocator::getNumAllocatedBytes  )  const [inline]
 

Definition at line 70 of file contiguous_block_allocator.h.

References _NumAllocatedBytes, and uint.

Referenced by NL3D::CParticleSystemShape::instanciatePS().

00070 { return _NumAllocatedBytes; }

void NLMISC::CContiguousBlockAllocator::init uint  numBytes = 0  ) 
 

Definition at line 51 of file contiguous_block_allocator.cpp.

References _BlockEnd, _BlockStart, _DefaultAlloc, _NextAvailablePos, _NumAllocatedBytes, and uint.

Referenced by NL3D::CParticleSystemShape::flushTextures(), NL3D::CParticleSystemShape::instanciatePS(), release(), and ~CContiguousBlockAllocator().

00052 {
00053         if (_BlockStart) _DefaultAlloc.deallocate(_BlockStart);
00054         _BlockEnd =  NULL;
00055         _BlockStart = NULL;
00056         _NumAllocatedBytes = 0;
00057         _NextAvailablePos = NULL;
00058         if (numBytes != 0)
00059         {
00060                 _BlockStart = _DefaultAlloc.allocate(numBytes);
00061                 _NextAvailablePos = _BlockStart;
00062                 _BlockEnd = _BlockStart + numBytes;
00063                 _NumAllocatedBytes = 0;
00064         }
00065         #ifdef NL_DEBUG
00066                 _NumAlloc = 0;
00067                 _NumFree = 0;
00068         #endif
00069 }

void NLMISC::CContiguousBlockAllocator::release void   )  [inline]
 

Definition at line 63 of file contiguous_block_allocator.h.

References init().

00063 { init(0); }    


Field Documentation

uint8* NLMISC::CContiguousBlockAllocator::_BlockEnd [private]
 

Definition at line 79 of file contiguous_block_allocator.h.

Referenced by alloc(), CContiguousBlockAllocator(), free(), and init().

uint8* NLMISC::CContiguousBlockAllocator::_BlockStart [private]
 

Definition at line 78 of file contiguous_block_allocator.h.

Referenced by alloc(), CContiguousBlockAllocator(), and init().

std::allocator<uint8> NLMISC::CContiguousBlockAllocator::_DefaultAlloc [private]
 

Definition at line 82 of file contiguous_block_allocator.h.

Referenced by alloc(), free(), and init().

uint8* NLMISC::CContiguousBlockAllocator::_NextAvailablePos [private]
 

Definition at line 80 of file contiguous_block_allocator.h.

Referenced by alloc(), CContiguousBlockAllocator(), and init().

uint NLMISC::CContiguousBlockAllocator::_NumAllocatedBytes [private]
 

Definition at line 81 of file contiguous_block_allocator.h.

Referenced by alloc(), CContiguousBlockAllocator(), getNumAllocatedBytes(), and init().


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