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)
48 return ((firstObject + alignment - 1) / alignment) * alignment;
55 act =
reinterpret_cast<char*
>(
this + 1 );
56 end =
reinterpret_cast<char*
>( this ) + size;
65 size_t Size() {
return size_t(
end -
reinterpret_cast<char*
>(
this)) ; }
73 act=
reinterpret_cast<char*
>(
this + 1 );
74 #if ALIB_DEBUG_ALLOCATIONS
87 char*
allocate(
size_t size,
size_t alignment )
90 "Requested alignment is not a power of 2:", alignment )
95 char* aligned =
reinterpret_cast<char*
>( (size_t(
act) + alignment - 1) & ~(alignment -1) );
96 if(
size_t(
end - aligned) < dbgSize )
98 act= aligned + dbgSize;
127 constexpr Snapshot( detail:: Buffer* pBuffer,
char* pFill) noexcept
145 {
return buffer !=
nullptr; }
291template<
typename TAllocator>
327 #if ALIB_DEBUG_MEMORY
334 #if ALIB_DEBUG_CRITICAL_SECTIONS
371 static constexpr size_t MAX_ALIGNMENT = (std::numeric_limits<size_t>::max)();
393 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
394 requires std::default_initializable<TRequires>
397 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
398 requires std::default_initializable<TRequires>
426 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
427 requires std::default_initializable<TRequires>
430 size_t pInitialBufferSizeInKB,
431 unsigned int pBufferGrowthInPercent= 200 );
433 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
434 requires std::default_initializable<TRequires>
437 size_t pInitialBufferSizeInKB,
438 unsigned int pBufferGrowthInPercent= 200 )
439 :
buffer ( pInitialBuffer )
446 ALIB_ASSERT_ERROR( (
size_t(pInitialBuffer) & (
alignof(
void*)-1) )== 0,
"MONOMEM",
447 "The given initial buffer is not aligned to at least 'alignof(void*)'." )
449 #if ALIB_DEBUG_CRITICAL_SECTIONS
452 buffer->previous=
nullptr;
471 TAllocator& pAllocator,
473 size_t pInitialBufferSizeInKB,
474 unsigned int pBufferGrowthInPercent= 200 );
477 TAllocator& pAllocator,
479 size_t pInitialBufferSizeInKB,
480 unsigned int pBufferGrowthInPercent= 200 )
482 ,
buffer ( pInitialBuffer )
489 ALIB_ASSERT_ERROR( (
size_t(pInitialBuffer) & (
alignof(
void*)-1) )== 0,
"MONOMEM",
490 "The given initial buffer is not aligned to at least 'alignof(void*)'." )
492 #if ALIB_DEBUG_CRITICAL_SECTIONS
495 buffer->previous=
nullptr;
519 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
520 requires std::default_initializable<TRequires>
522 size_t pInitialBufferSizeInKB,
523 unsigned int pBufferGrowthInPercent= 200 );
525 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
526 requires std::default_initializable<TRequires>
528 size_t pInitialBufferSizeInKB,
529 unsigned int pBufferGrowthInPercent= 200 )
535 #if ALIB_DEBUG && !ALIB_DEBUG_ASSERTION_PRINTABLES
536 ALIB_ASSERT_ERROR( pInitialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
538 size_t initialBufferSize= pInitialBufferSizeInKB * 1024;
541 #if ALIB_DEBUG_CRITICAL_SECTIONS
544 buffer->previous=
nullptr;
565 size_t pInitialBufferSizeInKB,
unsigned int pBufferGrowthInPercent= 200 );
569 size_t pInitialBufferSizeInKB,
unsigned int pBufferGrowthInPercent= 200 )
576 ALIB_ASSERT_ERROR( pInitialBufferSizeInKB,
"MONOMEM",
"Initial buffer of 0kb requested." )
577 size_t initialBufferSize= pInitialBufferSizeInKB * 1024;
579 detail::Buffer( initialBufferSize );
580 #if ALIB_DEBUG_CRITICAL_SECTIONS
583 buffer->previous=
nullptr;
632 TAllocator& pAllocator,
633 size_t initialBufferSizeInKB,
638 TAllocator& pAllocator,
639 size_t initialBufferSizeInKB,
661 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
662 requires std::default_initializable<TRequires>
664 size_t initialBufferSizeInKB,
667 template<
typename TRequires= lang::AllocatorMember<TAllocator>>
668 requires std::default_initializable<TRequires>
670 size_t initialBufferSizeInKB,
720 inline void*
allocate(
size_t size,
size_t alignment )
722 #if ALIB_DEBUG_CRITICAL_SECTIONS
728 "This MonoAllocator was constructed passing 'nullptr' and is not usable.")
731 "The requested alignment has to be a power of 2. Requested is: ",
int(alignment) )
733 #if ALIB_DEBUG_MEMORY
735 DBG_ALIGNMENT_INIT(
buffer )
738 #if ALIB_DEBUG_MEMORY
741 "MonoAllocator: Allocation size exceeds 1/2 of the current buffer size.\n"
742 "The allocator's buffer size should be increased.\n"
743 "Requested size: ", size )
747 char* mem=
buffer->allocate(size, alignment);
750 #if ALIB_DEBUG_MEMORY
751 DBG_ALIGNMENT_MEASURE(
buffer)
753 #if ALIB_DEBUG_CRITICAL_SECTIONS
760 #if ALIB_DEBUG_CRITICAL_SECTIONS
779 inline void*
reallocate(
void* mem,
size_t oldSize,
size_t newSize,
size_t alignment )
784 "The requested alignment has to be a power of 2. Requested is: ", alignment )
787 if( oldSize >= newSize )
793 auto* newMem=
allocate( newSize, alignment );
795 std::memcpy( newMem, mem, oldSize );
797 #if ALIB_DEBUG_MEMORY
813 inline void free(
void* mem,
size_t size)
const
824 template<
typename TSize>
843 #if !ALIB_DEBUG_ALLOCATIONS
902 void Reset(
size_t firstObjectSize,
size_t firstObjectAlignment )
905 buffer->allocate( firstObjectSize, firstObjectAlignment );
950 template<
typename TSize>
954 #if ALIB_DEBUG_MEMORY
970 extern template ALIB_DLL class TMonoAllocator<lang::HeapAllocator>;
984#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
static constexpr size_t MAX_ALIGNMENT
ALIB_DLL ~TMonoAllocator()
Destructor. Disposes all memory allocated with ChainedAllocator.
TMonoAllocator(const char *dbgName, TAllocator &pAllocator, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200)
void DbgLock(bool onOff) noexcept
TMonoAllocator(const char *dbgName, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200)
lang::Placeholder< lang::DbgCriticalSections > DbgCriticalSectionsPH
void free(void *mem, size_t size) const
constexpr bool allowsMemSplit() noexcept
lang::AllocatorMember< TAllocator > allocMember
The allocator type that TAllocator specifies.
void dbgCheckMemory(void *mem, TSize size)
detail::Buffer * DbgGetBuffer() noexcept
TMonoAllocator(const TMonoAllocator &)=delete
Not copyable.
TMonoAllocator(const char *dbgName, detail::Buffer *pInitialBuffer, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200)
detail::Buffer * recyclables
ALIB_DLL void destructWithExternalBuffer()
static constexpr size_t MIN_ALIGNMENT
void dbgAcknowledgeIncreasedAllocSize(void *, TSize) const
lang::AllocatorInterface< TMonoAllocator > operator()()
void * allocate(size_t size, size_t alignment)
static ALIB_DLL TMonoAllocator * Create(const char *dbgName, TAllocator &pAllocator, size_t initialBufferSizeInKB, unsigned int bufferGrowthInPercent=200)
static TMonoAllocator * Create(const char *dbgName, size_t initialBufferSizeInKB, unsigned int bufferGrowthInPercent=200)
ALIB_DLL TMonoAllocator(const char *dbgName, TAllocator &pAllocator, size_t pInitialBufferSizeInKB, unsigned int pBufferGrowthInPercent=200)
void * reallocate(void *mem, size_t oldSize, size_t newSize, size_t alignment)
size_t nextBuffersUsableSize
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
unsigned int bufferGrowthInPercent
#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::StdContainerAllocator< T, MonoAllocator > SCAMono
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.
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.
Buffer()=default
Defaulted default constructor.
static constexpr unsigned char CLEAR
static constexpr unsigned char MAGIC
char * allocate(size_t size, size_t alignment)