22 template<
typename TIntegral>
23 static constexpr size_t extSize (TIntegral size)
25 #if !ALIB_DEBUG_ALLOCATIONS
28 return size_t(size) + 4 +
sizeof(size_t);
39 static constexpr void annotate(T* mem,
size_t size,
unsigned char magic)
41 #if ALIB_DEBUG_ALLOCATIONS
42 auto* cMem=
reinterpret_cast<unsigned char*
>(mem);
44 for( ; idx< size + 4; ++idx)
46 for(
size_t i= 0; i<
sizeof(size_t); ++i)
48 cMem[idx++]=
static_cast<unsigned char>( size & 0xFF );
52 (void) mem; (void) size; (void) magic;
63 static constexpr void clearMem(T* mem,
size_t size,
unsigned char magic)
65 #if ALIB_DEBUG_ALLOCATIONS
66 memset(
reinterpret_cast<char*
>(mem), magic,
extSize(size) );
68 (void) mem; (void) size; (void) magic;
82 template<
typename TSize>
84 void checkMem(
void* mem,
const TSize size,
unsigned char magic,
const char* name )
86 #if ALIB_DEBUG_ALLOCATIONS
87 auto* cMem=
reinterpret_cast<unsigned char*
>(mem);
89 while ( idx< size + 4)
90 if( cMem[idx++] != magic )
91 ALIB_ERROR(
"MONOMEM",
"Corrupt memory with allocator ", name )
98 storedSize|= cMem[--idx];
100 while( idx > size + 4);
102 if( storedSize != size )
103 ALIB_ERROR(
"MONOMEM",
"Given size does not match the allocated size "
104 "(or corrupt memory). Allocator: ", name )
106 (void) mem; (void) size; (void) magic; (void) name;
185 void*
reallocate(
void* mem,
size_t oldSize,
size_t& newSize,
size_t alignment );
191 void free(
void* mem,
size_t size);
243 template<typename TSize>
254 template<typename TSize>
309template<
typename TAllocator>
328 template<
typename TSize,
typename TAlignment>
329 void*
Alloc( TSize size, TAlignment alignment)
331 size_t s= size_t(size);
332 return allocator.allocate( s,
size_t(alignment) );
341 auto size=
sizeof(T);
342 return reinterpret_cast<T*
>(
allocator.allocate(size,
alignof(T) ) );
352 template<
typename T,
typename TLength>
355 auto size=
sizeof(T[1]) *
size_t(length);
356 return reinterpret_cast<T*
>(
allocator.allocate( size,
alignof(T[]) ) );
365 template<
typename T,
typename... TArgs>
366 T*
New( TArgs&& ... args )
367 {
return new (
Alloc<T>()) T( std::forward<TArgs>( args )... ); }
380 template<
typename T,
typename TSize,
typename... TArgs>
384 for( TSize i= 0 ; i < length ; ++i )
385 new (mem + i) T(std::forward<TArgs>( args )...);
422 template<
typename T,
typename TIntegral>
426 for( TIntegral i= 0 ; i < length ; ++i )
428 allocator.free(array,
sizeof(T[1]) *
size_t(length) );
440 {
allocator.free(
static_cast<void*
>(
const_cast<std::remove_const_t<T>*
>(mem)),
sizeof(T) ); }
451 template<
typename T,
typename TIntegral>
452 void Free(T* mem, TIntegral size)
453 {
allocator.free(
static_cast<void*
>(
const_cast<std::remove_const_t<T>*
>(mem)),
size_t(size) ); }
462 template<
typename T,
typename TIntegral>
466 allocator.free(
static_cast<void*
>(
const_cast<std::remove_const_t<T>*
>(array)),
467 sizeof(T[1]) *
size_t(length) );
481 requires(T obj,
size_t& size,
size_t alignment,
size_t oldSize,
void* mem) {
482 typename T::ChainedAllocator;
483 { T::MIN_ALIGNMENT } -> std::convertible_to<size_t>;
484 { T::MAX_ALIGNMENT } -> std::convertible_to<size_t>;
485 { obj.allocate (size, alignment) } -> std::same_as<void*>;
486 { obj.reallocate( mem, oldSize, size, alignment ) } -> std::same_as<void*>;
487 { obj.free (mem, size) } -> std::same_as<void>;
488 { obj.operator()() } -> std::same_as<AllocatorInterface<T>>;
489 { obj.allowsMemSplit() } -> std::convertible_to<bool>;
492 { obj.DbgName } -> std::convertible_to<const char*>;
493 { obj.dbgAcknowledgeIncreasedAllocSize(mem, size) } -> std::same_as<void>;
494 { obj.dbgCheckMemory(mem, size) } -> std::same_as<void>;
529 static constexpr unsigned char MAGIC= 0xA1;
533 static constexpr unsigned char CLEAR= 0xF1;
553 inline void*
allocate(
size_t size,
size_t alignment )
const
556 "The HeapAllocator is not designed to provide alignments greater "
557 "than alignof(std::max_align_t): {} > {}. ", alignment,
alignof(std::max_align_t) )
573 inline void*
reallocate(
void* mem,
size_t oldSize,
size_t newSize,
size_t )
584 inline void free(
void* mem,
size_t size)
const
595 template<
typename TSize>
612 static constexpr const char*
DbgName =
"HeapAllocator";
623 template<
typename TSize>
661template<
typename TAllocator>
#define ALIB_WARNINGS_RESTORE
#define ALIB_ERROR(domain,...)
#define ALIB_WARNINGS_IGNORE_DOCS
#define ALIB_ASSERT_ERROR(cond, domain,...)
lang::HeapAllocator HeapAllocator
Type alias in namespace alib.
T * AllocArray(TLength length)
void Free(T *mem, TIntegral size)
T * New(TArgs &&... args)
void FreeArray(T *array, TIntegral length)
AllocatorInterface(TAllocator &pAllocator)
TAllocator & allocator
The allocator type to use.
void DeleteArray(T *array, TIntegral length)
T * NewArray(TSize length, TArgs &&... args)
void * Alloc(TSize size, TAlignment alignment)
static constexpr HeapAllocator HEAP_ALLOCATOR_INSTANCE
HeapAllocator & GetAllocator() const noexcept
AllocatorInterface< HeapAllocator > AI() const noexcept
AllocatorMember()=default
Default Constructor.
AllocatorMember(const HeapAllocator &heapAllocator) noexcept
TAllocator & allocator
A reference to the allocator.
AllocatorInterface< TAllocator > AI() const noexcept
TAllocator & GetAllocator() const noexcept
AllocatorMember(TAllocator &pAllocator) noexcept
AllocatorMember()=delete
Deleted default constructor. (The allocator has to be given with construction)
TAllocator AllocatorType
Exposes the allocator type.
TAllocator ChainedAllocator
void dbgAcknowledgeIncreasedAllocSize(void *mem, TSize allocSize) const
void * allocate(size_t &size, size_t alignment)
static constexpr size_t MAX_ALIGNMENT
void free(void *mem, size_t size)
static constexpr size_t MIN_ALIGNMENT
void * reallocate(void *mem, size_t oldSize, size_t &newSize, size_t alignment)
static constexpr bool allowsMemSplit() noexcept
AllocatorInterface< Allocator > operator()()
void dbgCheckMemory(void *mem, TSize size) const
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)
static constexpr unsigned char MAGIC
AllocatorInterface< HeapAllocator > operator()() const noexcept
static constexpr bool allowsMemSplit()
static constexpr size_t MAX_ALIGNMENT
void dbgAcknowledgeIncreasedAllocSize(void *, TSize) const
void * allocate(size_t size, size_t alignment) const
void * reallocate(void *mem, size_t oldSize, size_t newSize, size_t)
void free(void *mem, size_t size) const
void dbgCheckMemory(void *mem, TSize size)
static constexpr unsigned char CLEAR
static constexpr size_t MIN_ALIGNMENT
static constexpr const char * DbgName
With this allocator, the debug-name is constexpr "HeapAllocator".