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 ()
47 if ( hooks ==
nullptr )
51 allocMember::AI().template FreeArray<void*>(hooks, qtyHooks());
55 for (
short i = 0; i < qtyHooks(); ++i)
57 if ( dbgOpenAllocations[i] > 0)
59 "PoolAllocator '{}' destructor: There are still {} objects of size {}"
60 " not freed.\n This indicates a potential memory leak.",
61 DbgName, dbgOpenAllocations[i], GetAllocationSize(i) )
63 allocMember::AI().template FreeArray<int>(dbgOpenAllocations, qtyHooks() );
66 allocMember::AI().template DeleteArray<uinteger>(dbgStatAllocCounter, qtyHooks() );
72template<
typename TAllocator,
size_t TAlignment>
73void TPoolAllocator<TAllocator,TAlignment>::deletePool()
75 for (
int idx = 0; idx < qtyHooks(); ++idx)
77 size_t allocSize= lang::DbgAlloc::extSize(
size_t(1) << (idx + minimumHookIndex()) );
78 void* elem= hooks[idx];
81 void* next= *
reinterpret_cast<void**
>( elem );
82 allocMember::GetAllocator().free( elem, allocSize );
90template<
typename TAllocator,
size_t TAlignment>
91void* TPoolAllocator<TAllocator,TAlignment>::reallocate(
void* mem,
size_t oldSize,
size_t& newSize,
size_t pAlignment )
99 "The requested alignment is higher than what was specified with "
100 "template parameter TAlignment: {} >= {}", pAlignment, TAlignment )
103 oldSizeIdx= hookIndex(oldSize);
104 newSizeIdx= hookIndex(newSize);
105 if( newSizeIdx == oldSizeIdx )
108 newSize= GetAllocationSize(newSizeIdx);
111 auto newMem= AllocateByAllocationInfo( newSizeIdx );
112 std::memcpy( newMem, mem, oldSize );
113 #if ALIB_DEBUG_ALLOCATIONS
114 dbgLastRequestedSize= oldSize;
116 FreeByAllocationInfo( oldSizeIdx, mem );
120template<
typename TAllocator,
size_t TAlignment>
121void* TPoolAllocator<TAllocator,TAlignment>::AllocateByAllocationInfo(
int allocInfo)
123 #if ALIB_DEBUG_ALLOCATIONS
126 if( dbgLastRequestedSize == 0 )
127 dbgLastRequestedSize= size_t(allocInfo);
130 #if ALIB_DEBUG_ALLOCATIONS
131 ++dbgOpenAllocations[allocInfo];
133 #if ALIB_DEBUG_MEMORY
134 ++dbgStatAllocCounter[allocInfo];
137 void** hook= &hooks[allocInfo];
143 *hook= *
reinterpret_cast<void**
>(mem);
145 #if ALIB_DEBUG_ALLOCATIONS
146 lang::DbgAlloc::annotate( mem, dbgLastRequestedSize, MAGIC );
147 dbgLastRequestedSize= 0;
153 size_t allocSize= lang::DbgAlloc::extSize(
size_t(1) << (allocInfo + minimumHookIndex()) );
154 auto* mem= allocMember::AI().Alloc( allocSize, TAlignment );
156 #if ALIB_DEBUG_ALLOCATIONS
157 lang::DbgAlloc::annotate( mem, dbgLastRequestedSize, MAGIC );
158 dbgLastRequestedSize= 0;
166template<
typename TAllocator,
size_t TAlignment>
167integer TPoolAllocator<TAllocator,TAlignment>::TPoolAllocator::GetPoolSize(
size_t size)
170 void* node= hooks[hookIndex(size)];
173 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