#include <contiguous_block_allocator.h>
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
Nevrax France
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 |
|
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 } |
|
Definition at line 45 of file contiguous_block_allocator.cpp. References init().
00046 { 00047 init(0); 00048 } |
|
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 } |
|
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 } |
|
Definition at line 70 of file contiguous_block_allocator.h. References _NumAllocatedBytes, and uint. Referenced by NL3D::CParticleSystemShape::instanciatePS().
00070 { return _NumAllocatedBytes; } |
|
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 } |
|
Definition at line 63 of file contiguous_block_allocator.h. References init().
00063 { init(0); } |
|
Definition at line 79 of file contiguous_block_allocator.h. Referenced by alloc(), CContiguousBlockAllocator(), free(), and init(). |
|
Definition at line 78 of file contiguous_block_allocator.h. Referenced by alloc(), CContiguousBlockAllocator(), and init(). |
|
Definition at line 82 of file contiguous_block_allocator.h. |
|
Definition at line 80 of file contiguous_block_allocator.h. Referenced by alloc(), CContiguousBlockAllocator(), and init(). |
|
Definition at line 81 of file contiguous_block_allocator.h. Referenced by alloc(), CContiguousBlockAllocator(), getNumAllocatedBytes(), and init(). |