#include <object_arena_allocator.h>
: thread safety
Nevrax France
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 | |
CObjectArenaAllocator & | getDefaultAllocator () |
Private Attributes | |
uint | _Granularity |
uint | _MaxAllocSize |
std::vector< CFixedSizeAllocator * > | _ObjectSizeToAllocator |
Static Private Attributes | |
CObjectArenaAllocator * | _DefaultAllocator = NULL |
|
ctor
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
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 } |
|
Definition at line 33 of file object_arena_allocator.cpp. Referenced by getDefaultAllocator(). |
|
Definition at line 74 of file object_arena_allocator.h. Referenced by alloc(), CObjectArenaAllocator(), and free(). |
|
Definition at line 73 of file object_arena_allocator.h. Referenced by alloc(), CObjectArenaAllocator(), and free(). |
|
Definition at line 72 of file object_arena_allocator.h. Referenced by alloc(), CObjectArenaAllocator(), free(), getNumAllocatedBlocks(), and ~CObjectArenaAllocator(). |