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;
117 constexpr Snapshot( detail:: Buffer* pBuffer,
char* pFill) noexcept
281template<
typename TAllocator>
317 #if ALIB_DEBUG_MEMORY
324 #if ALIB_DEBUG_CRITICAL_SECTIONS
359 static constexpr size_t MAX_ALIGNMENT = (std::numeric_limits<size_t>::max)();
381 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
382 requires std::default_initializable<TRequires>
385 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
386 requires std::default_initializable<TRequires>
414 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
415 requires std::default_initializable<TRequires>
418 size_t pInitialBufferSizeInKB,
419 unsigned pBufferGrowthInPercent= 200 );
421 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
422 requires std::default_initializable<TRequires>
425 size_t pInitialBufferSizeInKB,
426 unsigned pBufferGrowthInPercent= 200 )
427 :
buffer ( pInitialBuffer )
433 ALIB_ASSERT_ERROR( (
size_t(pInitialBuffer) & (
alignof(
void*)-1) )== 0,
"MONOMEM",
434 "The given initial buffer is not aligned to at least 'alignof(void*)'." )
436 #if ALIB_DEBUG_CRITICAL_SECTIONS
439 buffer->previous=
nullptr;
458 TAllocator& pAllocator,
460 size_t pInitialBufferSizeInKB,
461 unsigned pBufferGrowthInPercent= 200 );
464 TAllocator& pAllocator,
466 size_t pInitialBufferSizeInKB,
467 unsigned pBufferGrowthInPercent= 200 )
469 ,
buffer ( pInitialBuffer )
475 ALIB_ASSERT_ERROR( (
size_t(pInitialBuffer) & (
alignof(
void*)-1) )== 0,
"MONOMEM",
476 "The given initial buffer is not aligned to at least 'alignof(void*)'." )
478 #if ALIB_DEBUG_CRITICAL_SECTIONS
481 buffer->previous=
nullptr;
505 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
506 requires std::default_initializable<TRequires>
508 size_t pInitialBufferSizeInKB,
509 unsigned pBufferGrowthInPercent= 200 );
511 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
512 requires std::default_initializable<TRequires>
514 size_t pInitialBufferSizeInKB,
515 unsigned pBufferGrowthInPercent= 200 )
520 #if ALIB_DEBUG && !ALIB_DEBUG_ASSERTION_PRINTABLES
521 ALIB_ASSERT_ERROR( pInitialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
523 size_t initialBufferSize= pInitialBufferSizeInKB * 1024;
526 #if ALIB_DEBUG_CRITICAL_SECTIONS
529 buffer->previous=
nullptr;
550 size_t pInitialBufferSizeInKB,
unsigned pBufferGrowthInPercent= 200 );
554 size_t pInitialBufferSizeInKB,
unsigned pBufferGrowthInPercent= 200 )
560 ALIB_ASSERT_ERROR( pInitialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
561 size_t initialBufferSize= pInitialBufferSizeInKB * 1024;
563 detail::Buffer( initialBufferSize );
564 #if ALIB_DEBUG_CRITICAL_SECTIONS
567 buffer->previous=
nullptr;
616 TAllocator& pAllocator,
617 size_t initialBufferSizeInKB,
622 TAllocator& pAllocator,
623 size_t initialBufferSizeInKB,
645 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
646 requires std::default_initializable<TRequires>
648 size_t initialBufferSizeInKB,
651 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
652 requires std::default_initializable<TRequires>
654 size_t initialBufferSizeInKB,
704 inline void*
allocate(
size_t size,
size_t alignment ) {
705 #if ALIB_DEBUG_CRITICAL_SECTIONS
711 "This MonoAllocator was constructed passing 'nullptr' and is not usable.")
714 "The requested alignment has to be a power of 2. Requested is: ",
int(alignment) )
716 #if ALIB_DEBUG_MEMORY
718 DBG_ALIGNMENT_INIT(
buffer )
721 #if ALIB_DEBUG_MEMORY
724 "MonoAllocator: Allocation size exceeds 1/2 of the current buffer size.\n"
725 "The allocator's buffer size should be increased.\n"
726 "Requested size: ", size )
730 char* mem=
buffer->allocate(size, alignment);
732 #if ALIB_DEBUG_MEMORY
733 DBG_ALIGNMENT_MEASURE(
buffer)
735 #if ALIB_DEBUG_CRITICAL_SECTIONS
742 #if ALIB_DEBUG_CRITICAL_SECTIONS
761 inline void*
reallocate(
void* mem,
size_t oldSize,
size_t newSize,
size_t alignment ) {
765 "The requested alignment has to be a power of 2. Requested is: ", alignment )
768 if( oldSize >= newSize )
774 auto* newMem=
allocate( newSize, alignment );
776 std::memcpy( newMem, mem, oldSize );
778 #if ALIB_DEBUG_MEMORY
794 inline void free(
void* mem,
size_t size)
const {
804 template<
typename TSize>
822 #if !ALIB_DEBUG_ALLOCATIONS
872 void Reset(
size_t firstObjectSize,
size_t firstObjectAlignment )
875 buffer->allocate( firstObjectSize, firstObjectAlignment );
923 template<
typename TSize>
927 #if ALIB_DEBUG_MEMORY
943extern template ALIB_DLL class TMonoAllocator<lang::HeapAllocator>;
957#if !ALIB_SINGLE_THREADED
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
TMonoAllocator(const char *dbgName, std::nullptr_t) noexcept
TMonoAllocator(const char *dbgName, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
static constexpr size_t MAX_ALIGNMENT
ALIB_DLL ~TMonoAllocator()
Destructor. Disposes all memory allocated with ChainedAllocator.
void DbgLock(bool onOff) noexcept
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
static ALIB_DLL TMonoAllocator * Create(const char *dbgName, TAllocator &pAllocator, size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent=200)
TMonoAllocator(const char *dbgName, TAllocator &pAllocator, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
ALIB_DLL void destructWithExternalBuffer()
static constexpr size_t MIN_ALIGNMENT
unsigned bufferGrowthInPercent
void dbgAcknowledgeIncreasedAllocSize(void *, TSize) const
TMonoAllocator(const char *dbgName, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
lang::AllocatorInterface< TMonoAllocator > operator()()
void * allocate(size_t size, size_t alignment)
ALIB_DLL TMonoAllocator(const char *dbgName, TAllocator &pAllocator, size_t pInitialBufferSizeInKB, unsigned pBufferGrowthInPercent=200)
void * reallocate(void *mem, size_t oldSize, size_t newSize, size_t alignment)
size_t nextBuffersUsableSize
static TMonoAllocator * Create(const char *dbgName, size_t initialBufferSizeInKB, unsigned bufferGrowthInPercent=200)
TAllocator ChainedAllocator
ALIB_DLL char * nextBuffer(size_t size, size_t alignment)
TMonoAllocator(TMonoAllocator &&)=delete
Not movable.
bool IsInitialized() const noexcept
ALIB_DLL void GetStatistics(Statistics &result)
void Reset(size_t firstObjectSize, size_t firstObjectAlignment)
ALIB_DLL void Reset(Snapshot snapshot=Snapshot())
const detail::Buffer * DbgGetBuffer() const noexcept
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_ERROR(cond, domain,...)
#define ALIB_REL_DBG(releaseCode,...)
constexpr int BitCount(TIntegral value)
Details of namespace alib::monomem.
ALIB_DLL TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
ALIB_DLL RecursiveLock GLOBAL_ALLOCATOR_LOCK
lang::StdAllocator< T, MonoAllocator > StdMA
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
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.
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)