8#if ALIB_DEBUG_ALLOCATIONS && !ALIB_STRINGS
9# error "Module ALib Strings needed in the ALib Build if Monomem is included and ALIB_DEBUG_ALLOCATIONS is set."
30 static constexpr unsigned char MAGIC= 0xA1;
34 static constexpr unsigned char CLEAR= 0xF1;
46 static constexpr size_t firstOffset(
size_t firstObject,
size_t alignment)
47 {
return ((firstObject + alignment - 1) / alignment) * alignment; }
53 act =
reinterpret_cast<char*
>(
this + 1 );
54 end =
reinterpret_cast<char*
>( this ) + size;
61 size_t Size() {
return size_t(
end -
reinterpret_cast<char*
>(
this)) ; }
66 act=
reinterpret_cast<char*
>(
this + 1 );
67 #if ALIB_DEBUG_ALLOCATIONS
78 char*
allocate(
size_t size,
size_t alignment ) {
80 "Requested alignment is not a power of 2:", alignment )
85 char* aligned =
reinterpret_cast<char*
>( (size_t(
act) + alignment - 1) & ~(alignment -1) );
86 if(
size_t(
end - aligned) < dbgSize )
88 act= aligned + dbgSize;
116 constexpr Snapshot( detail:: Buffer* pBuffer,
char* pFill) noexcept
280template<
typename TAllocator>
316 #if ALIB_DEBUG_MEMORY
323 #if ALIB_DEBUG_CRITICAL_SECTIONS
358 static constexpr size_t MAX_ALIGNMENT = (std::numeric_limits<size_t>::max)();
379 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
380 requires std::default_initializable<TRequires>
383 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
384 requires std::default_initializable<TRequires>
412 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
413 requires std::default_initializable<TRequires>
416 size_t pInitialBufferSizeInKB,
417 unsigned pBufferGrowthInPercent= 200 );
419 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
420 requires std::default_initializable<TRequires>
423 size_t pInitialBufferSizeInKB,
424 unsigned pBufferGrowthInPercent= 200 )
425 :
buffer ( pInitialBuffer )
431 ALIB_ASSERT_ERROR( (
size_t(pInitialBuffer) & (
alignof(
void*)-1) )== 0,
"MONOMEM",
432 "The given initial buffer is not aligned to at least 'alignof(void*)'." )
434 #if ALIB_DEBUG_CRITICAL_SECTIONS
437 buffer->previous=
nullptr;
456 TAllocator& pAllocator,
458 size_t pInitialBufferSizeInKB,
459 unsigned pBufferGrowthInPercent= 200 );
462 TAllocator& pAllocator,
464 size_t pInitialBufferSizeInKB,
465 unsigned pBufferGrowthInPercent= 200 )
467 ,
buffer ( pInitialBuffer )
473 ALIB_ASSERT_ERROR( (
size_t(pInitialBuffer) & (
alignof(
void*)-1) )== 0,
"MONOMEM",
474 "The given initial buffer is not aligned to at least 'alignof(void*)'." )
476 #if ALIB_DEBUG_CRITICAL_SECTIONS
479 buffer->previous=
nullptr;
503 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
504 requires std::default_initializable<TRequires>
506 size_t pInitialBufferSizeInKB,
507 unsigned pBufferGrowthInPercent= 200 );
509 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
510 requires std::default_initializable<TRequires>
512 size_t pInitialBufferSizeInKB,
513 unsigned pBufferGrowthInPercent= 200 )
518 #if ALIB_DEBUG && !ALIB_DEBUG_ASSERTION_PRINTABLES
519 ALIB_ASSERT_ERROR( pInitialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
521 size_t initialBufferSize= pInitialBufferSizeInKB * 1024;
524 #if ALIB_DEBUG_CRITICAL_SECTIONS
527 buffer->previous=
nullptr;
548 size_t pInitialBufferSizeInKB,
unsigned pBufferGrowthInPercent= 200 );
552 size_t pInitialBufferSizeInKB,
unsigned pBufferGrowthInPercent= 200 )
558 ALIB_ASSERT_ERROR( pInitialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
559 size_t initialBufferSize= pInitialBufferSizeInKB * 1024;
561 detail::Buffer( initialBufferSize );
562 #if ALIB_DEBUG_CRITICAL_SECTIONS
565 buffer->previous=
nullptr;
614 TAllocator& pAllocator,
615 size_t initialBufferSizeInKB,
620 TAllocator& pAllocator,
621 size_t initialBufferSizeInKB,
643 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
644 requires std::default_initializable<TRequires>
646 size_t initialBufferSizeInKB,
649 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
650 requires std::default_initializable<TRequires>
652 size_t initialBufferSizeInKB,
702 inline void*
allocate(
size_t size,
size_t alignment ) {
703 #if ALIB_DEBUG_CRITICAL_SECTIONS
709 "This MonoAllocator was constructed passing 'nullptr' and is not usable.")
712 "The requested alignment has to be a power of 2. Requested is: ",
int(alignment) )
714 #if ALIB_DEBUG_MEMORY
716 DBG_ALIGNMENT_INIT(
buffer )
719 #if ALIB_DEBUG_MEMORY
722 "MonoAllocator: Allocation size exceeds 1/2 of the current buffer size.\n"
723 "The allocator's buffer size should be increased.\n"
724 "Requested size: ", size )
728 char* mem=
buffer->allocate(size, alignment);
730 #if ALIB_DEBUG_MEMORY
731 DBG_ALIGNMENT_MEASURE(
buffer)
733 #if ALIB_DEBUG_CRITICAL_SECTIONS
740 #if ALIB_DEBUG_CRITICAL_SECTIONS
759 inline void*
reallocate(
void* mem,
size_t oldSize,
size_t newSize,
size_t alignment ) {
763 "The requested alignment has to be a power of 2. Requested is: ", alignment )
766 if( oldSize >= newSize )
772 auto* newMem=
allocate( newSize, alignment );
774 std::memcpy( newMem, mem, oldSize );
776 #if ALIB_DEBUG_MEMORY
792 inline void free(
void* mem,
size_t size)
const {
802 template<
typename TSize>
820 #if !ALIB_DEBUG_ALLOCATIONS
870 void Reset(
size_t firstObjectSize,
size_t firstObjectAlignment )
873 buffer->allocate( firstObjectSize, firstObjectAlignment );
921 template<
typename TSize>
925 #if ALIB_DEBUG_MEMORY
959extern template ALIB_DLL class TMonoAllocator<lang::HeapAllocator>;
973#if !ALIB_SINGLE_THREADED
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_REL_DBG(releaseCode,...)
constexpr Snapshot(detail::Buffer *pBuffer, char *pFill) noexcept
char * actFill
Pointer to the first free byte in the current buffer.
constexpr Snapshot() noexcept
detail::Buffer * buffer
The current buffer.
constexpr bool IsValid() noexcept
const DbgStatistics & DbgGetStatistics() const
static constexpr size_t MAX_ALIGNMENT
~TMonoAllocator()
Destructor. Disposes all memory allocated with ChainedAllocator.
void DbgLock(bool onOff) noexcept
TMonoAllocator(const char *dbgName, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
lang::Placeholder< lang::DbgCriticalSections > DbgCriticalSectionsPH
void free(void *mem, size_t size) const
constexpr bool allowsMemSplit() noexcept
lang::AllocatorMember< TAllocator > allocMember
The type of the base class that stores the chained allocator.
void dbgCheckMemory(void *mem, TSize size)
detail::Buffer * DbgGetBuffer() noexcept
TMonoAllocator(const TMonoAllocator &)=delete
Not copyable.
detail::Buffer * recyclables
TMonoAllocator(const char *dbgName, TAllocator &pAllocator, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
void destructWithExternalBuffer()
static constexpr size_t MIN_ALIGNMENT
static TMonoAllocator * Create(const char *dbgName, TAllocator &pAllocator, size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent=200)
unsigned bufferGrowthInPercent
TMonoAllocator(const char *dbgName, TAllocator &pAllocator, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
void dbgAcknowledgeIncreasedAllocSize(void *, TSize) const
lang::AllocatorInterface< TMonoAllocator > operator()()
void * allocate(size_t size, size_t alignment)
static TMonoAllocator * Create(const char *dbgName, size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent=200)
void * reallocate(void *mem, size_t oldSize, size_t newSize, size_t alignment)
size_t nextBuffersUsableSize
TAllocator ChainedAllocator
char * nextBuffer(size_t size, size_t alignment)
TMonoAllocator(TMonoAllocator &&)=delete
Not movable.
bool IsInitialized() const noexcept
void GetStatistics(Statistics &result)
void Reset(size_t firstObjectSize, size_t firstObjectAlignment)
void Reset(Snapshot snapshot=Snapshot())
TMonoAllocator(const char *dbgName, std::nullptr_t) noexcept
const detail::Buffer * DbgGetBuffer() const noexcept
TMonoAllocator(const char *dbgName, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
constexpr int BitCount(TIntegral value)
Details of namespace #"alib::monomem;2".
RecursiveLock GLOBAL_ALLOCATOR_LOCK
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
lang::StdAllocator< T, MonoAllocator > StdMA
threads::RecursiveLock RecursiveLock
Type alias in namespace alib.
static constexpr void clearMem(T *mem, size_t size, unsigned char magic)
static constexpr void annotate(T *mem, size_t size, unsigned char magic)
static constexpr size_t extSize(TIntegral size)
static void checkMem(void *mem, const TSize size, unsigned char magic, const char *name)
size_t QtyAllocationsInclResets
The number of allocations performed, cumulated over resets.
size_t QtyTrivialAllocationsInclResets
The number of allocations that did not create a new buffer, cumulated over resets.
size_t AlignmentWaste
The number of bytes lost for alignment.
size_t QtyBufferSizeExceeds
The number of allocations that have been larger than the buffer size.
size_t QtyAllocations
The number of allocations performed.
size_t AllocSizeInclResets
The number of allocated space, cumulated over resets.
size_t QtyTrivialAllocations
The number of allocations that did not create a new buffer .
size_t QtyResets
The number of resets performed.
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.
Resetter(TMonoAllocator &ma)
~Resetter()
Destructor. Resets the mono allocator given with construciton.
TMonoAllocator & MA
The mono allocator to reset on destruction.
Snapshot snapshot
The snapshot taken on construction.
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.
Buffer()=default
Defaulted default constructor.
static constexpr unsigned char CLEAR
static constexpr unsigned char MAGIC
char * allocate(size_t size, size_t alignment)