NLMISC::CObjectArenaAllocator Class Reference

#include <object_arena_allocator.h>


Detailed Description

An allocator that can allocate/release in O(1) for a finite number of possible blocks size (usually small).. For a given block size, a fixed size allocator is used. One possible use is with a family of class for which new and delete have been redefined at the top of the hierarchy (which the NL_USES_DEFAULT_ARENA_OBJECT_ALLOCATOR does)

: thread safety

Author:
Nicolas Vizerie

Nevrax France

Date:
2004

Definition at line 46 of file object_arena_allocator.h.

Public Member Functions

void * alloc (uint size)
 CObjectArenaAllocator (uint maxAllocSize, uint granularity=4)
void free (void *)
uint getNumAllocatedBlocks () const
 ~CObjectArenaAllocator ()

Static Public Member Functions

CObjectArenaAllocatorgetDefaultAllocator ()

Private Attributes

uint _Granularity
uint _MaxAllocSize
std::vector< CFixedSizeAllocator * > _ObjectSizeToAllocator

Static Private Attributes

CObjectArenaAllocator_DefaultAllocator = NULL


Constructor & Destructor Documentation

NLMISC::CObjectArenaAllocator::CObjectArenaAllocator uint  maxAllocSize,
uint  granularity = 4
 

ctor

Parameters:
maxAllocSize maximum intended size of allocation.

Definition at line 37 of file object_arena_allocator.cpp.

References _Granularity, _MaxAllocSize, _ObjectSizeToAllocator, nlassert, and uint.

Referenced by getDefaultAllocator().

00038 {
00039         nlassert(granularity > 0);
00040         nlassert(maxAllocSize > 0);
00041         _MaxAllocSize = granularity * ((maxAllocSize + (granularity - 1)) / granularity);
00042         _ObjectSizeToAllocator.resize(_MaxAllocSize / granularity, NULL);
00043         _Granularity = granularity;
00044         #ifdef NL_DEBUG
00045                 _AllocID = 0;
00046                 _WantBreakOnAlloc = false;
00047                 _BreakAllocID = 0;
00048         #endif
00049 }

NLMISC::CObjectArenaAllocator::~CObjectArenaAllocator  ) 
 

Definition at line 52 of file object_arena_allocator.cpp.

References _ObjectSizeToAllocator, and uint.

00053 {
00054         for(uint k = 0; k < _ObjectSizeToAllocator.size(); ++k)
00055         {
00056                 delete _ObjectSizeToAllocator[k];
00057         }
00058 }


Member Function Documentation

void * NLMISC::CObjectArenaAllocator::alloc uint  size  ) 
 

Allocate a block with the given size. 0 is an invalid size an will cause an assert. If the size is > to the max size given at init, the allocation will succeed, but will use the standard allocator.

Definition at line 61 of file object_arena_allocator.cpp.

References _Granularity, _MaxAllocSize, _ObjectSizeToAllocator, nlassert, size, uint, and uint8.

00062 {
00063         #ifdef NL_DEBUG
00064                 if (_WantBreakOnAlloc)
00065                 {
00066                         if (_AllocID == _BreakAllocID)
00067                         {
00068                                 nlassert(0);
00069                         }
00070                 }
00071         #endif
00072         if (size > _MaxAllocSize)
00073         {
00074                 // use standard allocator
00075                 uint8 *block = new uint8[size + sizeof(uint)]; // an additionnal uint is needed to store size of block 
00076                 if (!block) return NULL;
00077                 #ifdef NL_DEBUG                                         
00078                         _MemBlockToAllocID[block] = _AllocID;                   
00079                 #endif
00080                 *(uint *) block = size;
00081                 return block + sizeof(uint);
00082         }
00083         uint entry = ((size + (_Granularity - 1)) / _Granularity) ;
00084         if (!_ObjectSizeToAllocator[entry])
00085         {
00086                 _ObjectSizeToAllocator[entry] = new CFixedSizeAllocator(entry * _Granularity + sizeof(uint), _MaxAllocSize / size); // an additionnal uint is needed to store size of block
00087         }
00088         void *block = _ObjectSizeToAllocator[entry]->alloc();   
00089         #ifdef NL_DEBUG
00090                 if (block)
00091                 {               
00092                         _MemBlockToAllocID[block] = _AllocID;
00093                 }
00094                 ++_AllocID;
00095         #endif
00096         *(uint *) block = size;
00097         return (void *) ((uint8 *) block + sizeof(uint));
00098 }

void NLMISC::CObjectArenaAllocator::free void *   ) 
 

Definition at line 101 of file object_arena_allocator.cpp.

References _Granularity, _MaxAllocSize, _ObjectSizeToAllocator, nlassert, size, uint, and uint8.

00102 {
00103         if (!block) return;
00104         uint8 *realBlock = (uint8 *) block - sizeof(uint); // a uin is used at start of block to give its size
00105         uint size = *(uint *) realBlock;
00106         if (size > _MaxAllocSize)
00107         {
00108                 #ifdef NL_DEBUG
00109                                 std::map<void *, uint>::iterator it = _MemBlockToAllocID.find(realBlock);
00110                                 nlassert(it != _MemBlockToAllocID.end());
00111                                 _MemBlockToAllocID.erase(it);
00112                 #endif
00113                 delete realBlock;
00114                 return;
00115         }
00116         uint entry = ((size + (_Granularity - 1)) / _Granularity);
00117         nlassert(entry < _ObjectSizeToAllocator.size());
00118         _ObjectSizeToAllocator[entry]->free(realBlock);
00119         #ifdef NL_DEBUG
00120                 std::map<void *, uint>::iterator it = _MemBlockToAllocID.find(realBlock);
00121                 nlassert(it != _MemBlockToAllocID.end());
00122                 /*
00123                 #ifdef NL_DEBUG
00124                                 if (_WantBreakOnAlloc)
00125                                 {
00126                                         if (it->second == _BreakAllocID)
00127                                         {
00128                                                 nlassert(0);
00129                                         }
00130                                 }
00131                 #endif
00132                 */
00133                 _MemBlockToAllocID.erase(it);
00134         #endif
00135 }

CObjectArenaAllocator & NLMISC::CObjectArenaAllocator::getDefaultAllocator  )  [static]
 

Definition at line 149 of file object_arena_allocator.cpp.

References _DefaultAllocator, and CObjectArenaAllocator().

00150 {
00151         if (!_DefaultAllocator)
00152         {
00153                 _DefaultAllocator = new CObjectArenaAllocator(32768);
00154         }
00155         return *_DefaultAllocator;
00156 }

uint NLMISC::CObjectArenaAllocator::getNumAllocatedBlocks  )  const
 

Definition at line 138 of file object_arena_allocator.cpp.

References _ObjectSizeToAllocator, and uint.

00139 {
00140         uint numObjs = 0;
00141         for(uint k = 0; k < _ObjectSizeToAllocator.size(); ++k)
00142         {
00143                 if (_ObjectSizeToAllocator[k]) numObjs += _ObjectSizeToAllocator[k]->getNumAllocatedBlocks();
00144         }
00145         return numObjs;
00146 }


Field Documentation

CObjectArenaAllocator * NLMISC::CObjectArenaAllocator::_DefaultAllocator = NULL [static, private]
 

Definition at line 33 of file object_arena_allocator.cpp.

Referenced by getDefaultAllocator().

uint NLMISC::CObjectArenaAllocator::_Granularity [private]
 

Definition at line 74 of file object_arena_allocator.h.

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

uint NLMISC::CObjectArenaAllocator::_MaxAllocSize [private]
 

Definition at line 73 of file object_arena_allocator.h.

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

std::vector<CFixedSizeAllocator *> NLMISC::CObjectArenaAllocator::_ObjectSizeToAllocator [private]
 

Definition at line 72 of file object_arena_allocator.h.

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


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