Implementation of (prototype-) class Allocator which performs standard heap allocation by using std::malloc
, std::realloc
, and std::free
This class may be passed as the template parameter TAllocator with ALib- or custom types that expect an allocator. For example, the type definition alib::AString uses this type and thus implements a common heap-allocated string buffer.
In contrast to other allocator types, this type is stateless and default-constructible, and temporary instances can be used and disposed of right away with no effort. Therefore, all types across ALib that are templated over an allocator type, provide two versions of each constructor, one that expects an allocator and one that does not expect an external instance. If this type HeapAllocator is used, the constructors that do not expect an allocator become effective.
Definition at line 519 of file allocation.hpp.
#include <allocation.hpp>
Public Type Index: | |
using | ChainedAllocator = void |
Public Static Field Index: | |
static constexpr unsigned char | CLEAR = 0xF1 |
static constexpr const char * | DbgName = "HeapAllocator" |
With this allocator, the debug-name is constexpr "HeapAllocator". | |
static constexpr unsigned char | MAGIC = 0xA1 |
static constexpr size_t | MAX_ALIGNMENT = alignof(std::max_align_t) |
static constexpr size_t | MIN_ALIGNMENT = alignof(std::max_align_t) |
Public Static Method Index: | |
static constexpr bool | allowsMemSplit () |
Public Method Index: | |
void * | allocate (size_t size, size_t alignment) const |
template<typename TSize > | |
void | dbgAcknowledgeIncreasedAllocSize (void *, TSize) const |
template<typename TSize > | |
void | dbgCheckMemory (void *mem, TSize size) |
void | free (void *mem, size_t size) const |
AllocatorInterface< HeapAllocator > | operator() () const noexcept |
void * | reallocate (void *mem, size_t oldSize, size_t newSize, size_t) |
using ChainedAllocator = void |
The type of the allocator that this allocator uses underneath. In this case, no chaining is available, hence this evaluates to void
.
Definition at line 540 of file allocation.hpp.
|
staticconstexpr |
A magic byte written when memory is freed.
Definition at line 528 of file allocation.hpp.
|
staticconstexpr |
With this allocator, the debug-name is constexpr
"HeapAllocator".
Definition at line 608 of file allocation.hpp.
|
staticconstexpr |
A magic byte, used with compiler-symbol ALIB_DEBUG_ALLOCATIONS to mark memory and detect out-of-bounds writes.
Definition at line 524 of file allocation.hpp.
|
staticconstexpr |
Evaluates to alignof(std::max_align_t)
.
Definition at line 536 of file allocation.hpp.
|
staticconstexpr |
Evaluates to alignof(std::max_align_t)
.
Definition at line 532 of file allocation.hpp.
|
inline |
Allocate memory using std::malloc
.
size | The size of memory the block to allocate in bytes. With this allocator this is not an input/output parameter. |
alignment | This type ignores the alignment parameter, because std::malloc always uses alignof(std::max_align_t) . |
Definition at line 548 of file allocation.hpp.
|
inlinestaticconstexpr |
This is a constexpr static method that determines if a type allows splitting memory and later passing the pieces back to free.
false
. Definition at line 604 of file allocation.hpp.
|
inline |
This is an empty implementation of the prototyped method. It is empty because this allocator never returns a higher allocation size than requested. Allocator::dbgAcknowledgeIncreasedAllocSize.
TSize | The type of parameter allocSize. (Deduced by the compiler.) |
Definition at line 592 of file allocation.hpp.
|
inline |
If the compiler symbol ALIB_DEBUG_ALLOCATIONS is not set, this method is empty and will be optimized out. Otherwise, this will raise an ALib assertion if the piece of allocated memory is corrupted or its allocation size is not rightfully given by the using code.
TSize | The type of parameter size. (Deduced by the compiler.) |
mem | The address of the allocated object. |
size | The requested allocation size of the object. |
Definition at line 620 of file allocation.hpp.
|
inline |
Frees memory by invoking std::free
.
mem | The memory to dispose. |
size | The size of the given mem. |
Definition at line 580 of file allocation.hpp.
|
inlinenoexcept |
Returns a temporary object (which is usually optimized out together with a call to this operator) providing high-level convenience methods for allocation.
Definition at line 598 of file allocation.hpp.
|
inline |
Shrinks or grows a piece of memory. If a new allocation was performed, the existing data is copied. Note that unlike specified by the prototype, parameter newSize is not provided as a reference. This allocator will not allocate bigger objects.
mem | The memory to reallocate. |
oldSize | The current size of mem. |
newSize | The now required size of mem in bytes. With this allocator this is not an input/output parameter. |
Definition at line 569 of file allocation.hpp.