15template<
typename TAllocator>
17 TAllocator& pAllocator,
18 size_t initialBufferSizeInKB,
19 unsigned pBufferGrowthInPercent) {
21 ALIB_ASSERT_ERROR( initialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
23 auto size= initialBufferSizeInKB * 1024;
24 auto* mem= pAllocator.allocate( size, alignof(
detail::Buffer) );
25 auto* firstBuffer= new (mem)
detail::Buffer( initialBufferSizeInKB * 1024 );
27 auto* result = reinterpret_cast<TMA*>( firstBuffer->allocate( sizeof(TMA), alignof(TMA) ) );
28 return new (result) TMA(
ALIB_DBG(pDbgName,) pAllocator, firstBuffer,
29 initialBufferSizeInKB, pBufferGrowthInPercent );
31#include "ALib.Lang.CIMethods.H"
34template<
typename TAllocator>
36 #if ALIB_DEBUG_CRITICAL_SECTIONS
51 auto* next= cnk->previous;
59 auto* next= cnk->previous;
70 "ALIB_DEBUG_MEMORY Warning: More than 15 buffers allocated. "
71 "Buffer size might be increased? #Buffers: ", cntBuffers )
77template<
typename TAllocator>
83 if( snapshot.
buffer ==
nullptr ) {
90 || snapshot.
actFill ==
reinterpret_cast<char*
>(1),
92 "A full MonoAllocator::Reset() was requested for a self-contained monotonic "
93 "allocator created with MonoAllocator::Create()." )
100 #if ALIB_DEBUG_CRITICAL_SECTIONS
104 #if ALIB_DEBUG_CRITICAL_SECTIONS
119 while( it != snapshot.
buffer ) {
120 #if !ALIB_DEBUG_ALLOCATIONS
125 if ( it->
previous !=
nullptr || snapshot.
actFill !=
reinterpret_cast<char*
>(1) )
134 if ( next ==
nullptr ) {
135 #if ALIB_DEBUG_ALLOCATIONS
136 if ( snapshot.
actFill !=
reinterpret_cast<char*
>(1) )
154 #if ALIB_DEBUG_ALLOCATIONS
159template<
typename TAllocator>
165 while( (*it)->previous )
177template<
typename TAllocator>
191 #if ALIB_DEBUG_MEMORY
195 "MonoAllocator: Allocation size exceeds the next buffers' size.\n"
196 "The allocator's buffer size should be increased.\n"
197 "Requested size: ", size )
204 { DBG_ALIGNMENT_INIT( recyclable )
205 char* mem= recyclable->
allocate( size, alignment ); DBG_ALIGNMENT_MEASURE( recyclable )
207 *previousPointer= recyclable->
previous;
214 previousPointer= &recyclable->
previous;
223 buffer->previous = previousBuffer; DBG_ALIGNMENT_INIT(
buffer )
225 char* mem=
buffer->allocate( size, alignment ); DBG_ALIGNMENT_MEASURE(
buffer )
232template<
typename TAllocator>
245 while( it !=
nullptr ) {
256 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.
static ALIB_DLL TMonoAllocator * Create(const char *dbgName, TAllocator &pAllocator, size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent=200)
ALIB_DLL void destructWithExternalBuffer()
unsigned bufferGrowthInPercent
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())
#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.
unsigned QtyBuffers
The number of created buffers.
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 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)