17template<
typename TAllocator,
size_t TAlignment>
19: allocMember(pAllocator)
21,lang::DbgCriticalSections(
"PoolAllocator")
23ALIB_DBG(,DbgName(pAllocator.DbgName))
27 "The fixed alignment {} of the pool allocator is not a power of 2. "
28 "Adjust template parameter TAlignment.", TAlignment )
32 hooks= allocMember::AI().template AllocArray<void*>(qtyHooks() );
33 std::memset( hooks, 0,
sizeof(
void*[qtyHooks()] ) );
34 #if ALIB_DEBUG_ALLOCATIONS
35 dbgOpenAllocations= allocMember::AI().template AllocArray<int>(qtyHooks() );
36 std::memset( dbgOpenAllocations, 0,
sizeof(
int[qtyHooks()] ) );
39 dbgStatAllocCounter= allocMember::AI().template AllocArray<uinteger>(qtyHooks() );
40 std::memset( dbgStatAllocCounter, 0,
sizeof( uinteger[qtyHooks()] ) );
44template<
typename TAllocator,
size_t TAlignment>
45TPoolAllocator<TAllocator,TAlignment>::~TPoolAllocator () {
46 if ( hooks ==
nullptr )
50 allocMember::AI().template FreeArray<void*>(hooks, qtyHooks());
54 for (
short i = 0; i < qtyHooks(); ++i) {
55 if ( dbgOpenAllocations[i] > 0)
57 "PoolAllocator '{}' destructor: There are still {} objects of size {}"
58 " not freed.\n This indicates a potential memory leak.",
59 DbgName, dbgOpenAllocations[i], GetAllocationSize(i) )
61 allocMember::AI().template FreeArray<int>(dbgOpenAllocations, qtyHooks() );
64 allocMember::AI().template DeleteArray<uinteger>(dbgStatAllocCounter, qtyHooks() );
70template<
typename TAllocator,
size_t TAlignment>
71void TPoolAllocator<TAllocator,TAlignment>::deletePool() {
72 for (
int idx = 0; idx < qtyHooks(); ++idx) {
73 size_t allocSize= lang::DbgAlloc::extSize(
size_t(1) << (idx + minimumHookIndex()) );
74 void* elem= hooks[idx];
76 void* next= *
reinterpret_cast<void**
>( elem );
77 allocMember::GetAllocator().free( elem, allocSize );
83template<
typename TAllocator,
size_t TAlignment>
84void* TPoolAllocator<TAllocator,TAlignment>::reallocate(
void* mem,
94 "The requested alignment is higher than what was specified with "
95 "template parameter TAlignment: {} >= {}", pAlignment, TAlignment )
98 oldSizeIdx= hookIndex(oldSize);
99 newSizeIdx= hookIndex(newSize);
100 if( newSizeIdx == oldSizeIdx )
103 newSize= GetAllocationSize(newSizeIdx);
106 auto newMem= AllocateByAllocationInfo( newSizeIdx );
107 std::memcpy( newMem, mem, oldSize );
108 #if ALIB_DEBUG_ALLOCATIONS
109 dbgLastRequestedSize= oldSize;
111 FreeByAllocationInfo( oldSizeIdx, mem );
115template<
typename TAllocator,
size_t TAlignment>
116void* TPoolAllocator<TAllocator,TAlignment>::AllocateByAllocationInfo(
int allocInfo) {
ALIB_DCS
117 #if ALIB_DEBUG_ALLOCATIONS
120 if( dbgLastRequestedSize == 0 )
121 dbgLastRequestedSize= size_t(allocInfo);
124 #if ALIB_DEBUG_ALLOCATIONS
125 ++dbgOpenAllocations[allocInfo];
127 #if ALIB_DEBUG_MEMORY
128 ++dbgStatAllocCounter[allocInfo];
131 void** hook= &hooks[allocInfo];
136 *hook= *
reinterpret_cast<void**
>(mem);
138 #if ALIB_DEBUG_ALLOCATIONS
139 lang::DbgAlloc::annotate( mem, dbgLastRequestedSize, MAGIC );
140 dbgLastRequestedSize= 0;
146 size_t allocSize= lang::DbgAlloc::extSize(
size_t(1) << (allocInfo + minimumHookIndex()) );
147 auto* mem= allocMember::AI().Alloc( allocSize, TAlignment );
149 #if ALIB_DEBUG_ALLOCATIONS
150 lang::DbgAlloc::annotate( mem, dbgLastRequestedSize, MAGIC );
151 dbgLastRequestedSize= 0;
159template<
typename TAllocator,
size_t TAlignment>
160integer TPoolAllocator<TAllocator,TAlignment>::TPoolAllocator::GetPoolSize(
size_t size) {
162 void* node= hooks[hookIndex(size)];
164 node= *
reinterpret_cast<void**
>(node);
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_DEBUG_ALLOCATIONS
#define ALIB_DEBUG_CRITICAL_SECTIONS
platform_specific integer