15template<
typename TAllocator>
17 TAllocator& pAllocator,
18 size_t initialBufferSizeInKB,
19 unsigned int pBufferGrowthInPercent )
22 ALIB_ASSERT_ERROR( initialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
24 auto size= initialBufferSizeInKB * 1024;
25 auto* mem= pAllocator.allocate( size, alignof(
detail::Buffer) );
26 auto* firstBuffer= new (mem)
detail::Buffer( initialBufferSizeInKB * 1024 );
28 auto* result = reinterpret_cast<TMA*>( firstBuffer->allocate( sizeof(TMA), alignof(TMA) ) );
29 return new (result) TMA(
ALIB_DBG(pDbgName,) pAllocator, firstBuffer,
30 initialBufferSizeInKB, pBufferGrowthInPercent );
32#include "ALib.Lang.CIMethods.H"
35template<
typename TAllocator>
38 #if ALIB_DEBUG_CRITICAL_SECTIONS
54 auto* next= cnk->previous;
63 auto* next= cnk->previous;
74 "ALIB_DEBUG_MEMORY Warning: More than 15 buffers allocated. "
75 "Buffer size might be increased? #Buffers: ", cntBuffers )
81template<
typename TAllocator>
87 if( snapshot.
buffer ==
nullptr )
95 || snapshot.
actFill ==
reinterpret_cast<char*
>(1),
97 "A full MonoAllocator::Reset() was requested for a self-contained monotonic "
98 "allocator created with MonoAllocator::Create()." )
103 #if ALIB_DEBUG_MEMORY
105 #if ALIB_DEBUG_CRITICAL_SECTIONS
109 #if ALIB_DEBUG_CRITICAL_SECTIONS
124 while( it != snapshot.
buffer )
126 #if !ALIB_DEBUG_ALLOCATIONS
131 if ( it->
previous !=
nullptr || snapshot.
actFill !=
reinterpret_cast<char*
>(1) )
140 if ( next ==
nullptr )
142 #if ALIB_DEBUG_ALLOCATIONS
143 if ( snapshot.
actFill !=
reinterpret_cast<char*
>(1) )
161 #if ALIB_DEBUG_ALLOCATIONS
166template<
typename TAllocator>
172 while( (*it)->previous )
184template<
typename TAllocator>
199 #if ALIB_DEBUG_MEMORY
203 "MonoAllocator: Allocation size exceeds the next buffers' size.\n"
204 "The allocator's buffer size should be increased.\n"
205 "Requested size: ", size )
212 { DBG_ALIGNMENT_INIT( recyclable )
213 char* mem= recyclable->
allocate( size, alignment ); DBG_ALIGNMENT_MEASURE( recyclable )
216 *previousPointer= recyclable->
previous;
223 previousPointer= &recyclable->
previous;
232 buffer->previous = previousBuffer; DBG_ALIGNMENT_INIT(
buffer )
234 char* mem=
buffer->allocate( size, alignment ); DBG_ALIGNMENT_MEASURE(
buffer )
241template<
typename TAllocator>
254 while( it !=
nullptr )
266 while( it !=
nullptr )
char * actFill
Pointer to the first free byte in the current buffer.
detail::Buffer * buffer
The current buffer.
TMonoAllocator(const char *dbgName, std::nullptr_t) noexcept
ALIB_DLL ~TMonoAllocator()
Destructor. Disposes all memory allocated with ChainedAllocator.
detail::Buffer * buffer
The actual buffer. Contains a link to previously allocated buffers.
lang::Placeholder< lang::DbgCriticalSections > DbgCriticalSectionsPH
detail::Buffer * recyclables
The list of buffers that are to be recycled.
ALIB_DLL void destructWithExternalBuffer()
static ALIB_DLL TMonoAllocator * Create(const char *dbgName, TAllocator &pAllocator, size_t initialBufferSizeInKB, unsigned int bufferGrowthInPercent=200)
size_t nextBuffersUsableSize
ALIB_DLL char * nextBuffer(size_t size, size_t alignment)
ALIB_DLL void GetStatistics(Statistics &result)
ALIB_DLL void Reset(Snapshot snapshot=Snapshot())
unsigned int bufferGrowthInPercent
#define ALIB_ASSERT(cond, domain)
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_DCS_WITH(CS)
#define ALIB_ASSERT_ERROR(cond, domain,...)
Details of namespace alib::monomem.
lang::integer integer
Type alias in namespace alib.
TAllocator & GetAllocator() const noexcept
static constexpr size_t extSize(TIntegral size)
size_t CurrentBufferSize
The size of the current buffer.
size_t HeapSizeRecycled
The number of bytes allocated at the heap.
size_t NextBufferSize
The planned size of the next buffer (that is not an oversize-allocation).
size_t CurrentBufferFree
The free space in the current buffer.
unsigned int QtyBuffers
The number of created buffers.
unsigned int QtyRecyclables
The number of created buffers.
static constexpr size_t firstOffset(size_t firstObject, size_t alignment)
char * end
Pointer to the first byte behind the buffer.
char * act
Pointer to the next free space in the buffer.
Buffer * previous
the previously allocated buffer.
char * allocate(size_t size, size_t alignment)