20template<
typename TAllocator,
size_t TAlignment>
22: allocMember(pAllocator)
24,lang::DbgCriticalSections(
"PoolAllocator")
26ALIB_DBG(,DbgName(pAllocator.DbgName))
28 #if ALIB_CPP_STANDARD < 20 && defined(_WIN32)
30 "The fixed alignment of the pool allocator is not a power of 2. "
31 "Adjust template parameter TAlignment." )
35 hooks= allocMember::AI().template AllocArray<void*>(qtyHooks() );
36 std::memset( hooks, 0,
sizeof(
void*[qtyHooks()] ) );
37 #if ALIB_DEBUG_ALLOCATIONS
38 dbgOpenAllocations= allocMember::AI().template AllocArray<int>(qtyHooks() );
39 std::memset( dbgOpenAllocations, 0,
sizeof(
int[qtyHooks()] ) );
41 #if ALIB_DEBUG_MONOMEM
42 dbgStatAllocCounter= allocMember::AI().template AllocArray<uinteger>(qtyHooks() );
43 std::memset( dbgStatAllocCounter, 0,
sizeof( uinteger[qtyHooks()] ) );
47template<
typename TAllocator,
size_t TAlignment>
48TPoolAllocator<TAllocator,TAlignment>::~TPoolAllocator ()
50 if ( hooks ==
nullptr )
54 allocMember::AI().template FreeArray<void*>(hooks, qtyHooks());
59 for (
short i = 0; i < qtyHooks(); ++i)
61 if ( dbgOpenAllocations[i] > 0)
63 "PoolAllocator '"<<DbgName<<
"' destructor: There are still "
64 << dbgOpenAllocations[i] <<
" objects of size "
65 << GetAllocationSize(i) <<
" not freed. "
66 "This indicates a potential memory leak." )
68 allocMember::AI().template FreeArray<int>(dbgOpenAllocations, qtyHooks() );
71 #if ALIB_DEBUG_MONOMEM
72 allocMember::AI().template DeleteArray<uinteger>(dbgStatAllocCounter, qtyHooks() );
78template<
typename TAllocator,
size_t TAlignment>
79void TPoolAllocator<TAllocator,TAlignment>::deletePool()
81 for (
int idx = 0; idx < qtyHooks(); ++idx)
83 size_t allocSize= lang::DbgAlloc::extSize(
size_t(1) << (idx + minimumHookIndex()) );
85 void* elem= hooks[idx];
88 void* next= *
reinterpret_cast<void**
>( elem );
89 allocMember::GetAllocator().free( elem, allocSize );
98template<
typename TAllocator,
size_t TAlignment>
99void* TPoolAllocator<TAllocator,TAlignment>::reallocate(
void* mem,
size_t oldSize,
size_t& newSize,
size_t pAlignment )
107 "The requested alignment is higher than what was specified with "
108 "template parameter TAlignment." )
111 oldSizeIdx= hookIndex(oldSize);
112 newSizeIdx= hookIndex(newSize);
113 if( newSizeIdx == oldSizeIdx )
116 newSize= GetAllocationSize(newSizeIdx);
119 auto newMem= AllocateByAllocationInfo( newSizeIdx );
120 std::memcpy( newMem, mem, oldSize );
121 #if ALIB_DEBUG_ALLOCATIONS
122 dbgLastRequestedSize= oldSize;
124 FreeByAllocationInfo( oldSizeIdx, mem );
128template<
typename TAllocator,
size_t TAlignment>
129void* TPoolAllocator<TAllocator,TAlignment>::AllocateByAllocationInfo(
int allocInfo)
131 #if ALIB_DEBUG_ALLOCATIONS
134 if( dbgLastRequestedSize == 0 )
135 dbgLastRequestedSize= size_t(allocInfo);
139 #if ALIB_DEBUG_ALLOCATIONS
140 ++dbgOpenAllocations[allocInfo];
142 #if ALIB_DEBUG_MONOMEM
143 ++dbgStatAllocCounter[allocInfo];
147 void** hook= &hooks[allocInfo];
154 *hook= *
reinterpret_cast<void**
>(mem);
156 #if ALIB_DEBUG_ALLOCATIONS
157 lang::DbgAlloc::annotate( mem, dbgLastRequestedSize, MAGIC );
158 dbgLastRequestedSize= 0;
164 size_t allocSize= lang::DbgAlloc::extSize(
size_t(1) << (allocInfo + minimumHookIndex()) );
165 auto* mem= allocMember::AI().Alloc( allocSize, TAlignment );
167 #if ALIB_DEBUG_ALLOCATIONS
168 lang::DbgAlloc::annotate( mem, dbgLastRequestedSize, MAGIC );
169 dbgLastRequestedSize= 0;
177template<
typename TAllocator,
size_t TAlignment>
178integer TPoolAllocator<TAllocator,TAlignment>::TPoolAllocator::GetPoolSize(
size_t size)
182 void* node= hooks[hookIndex(size)];
186 node= *
reinterpret_cast<void**
>(node);
#define ALIB_WARNING(...)
#define ALIB_WARNINGS_RESTORE
#define ALIB_ASSERT_ERROR(cond,...)
#define ALIB_WARNINGS_ALLOW_UNSAFE_BUFFER_USAGE
#define ALIB_DEBUG_ALLOCATIONS
#define ALIB_DEBUG_CRITICAL_SECTIONS
platform_specific integer
NLocalString< 256 > NString256
Type alias name for TLocalString<nchar,256>.