ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::lang::HeapAllocator Struct Reference

Description:

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.

See also
  • Struct AllocatorMember, which should be used as a base class to inherit from if an arbitrary allocator is to be stored. A specialization for this HeapAllocator is given, which does not contain a member and hence avoids increasing the footprint of derived types.
  • Types TMonoAllocator, TLocalAllocator, and TPoolAllocator introduced with module ALib Monomem.

Definition at line 524 of file allocation.inl.

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< HeapAllocatoroperator() () const noexcept
 
void * reallocate (void *mem, size_t oldSize, size_t newSize, size_t)
 

Type Definition Details:

◆ ChainedAllocator

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 545 of file allocation.inl.

Field Details:

◆ CLEAR

unsigned char alib::lang::HeapAllocator::CLEAR = 0xF1
staticconstexpr

A magic byte written when memory is freed.

See also
Field CLEAR.

Definition at line 533 of file allocation.inl.

◆ DbgName

const char* alib::lang::HeapAllocator::DbgName = "HeapAllocator"
staticconstexpr

With this allocator, the debug-name is constexpr "HeapAllocator".

Definition at line 612 of file allocation.inl.

◆ MAGIC

unsigned char alib::lang::HeapAllocator::MAGIC = 0xA1
staticconstexpr

A magic byte, used with the compiler-symbol ALIB_DEBUG_ALLOCATIONS to mark memory and detect out-of-bounds writes.

See also
Method Allocator::dbgCheckMemory.

Definition at line 529 of file allocation.inl.

◆ MAX_ALIGNMENT

size_t alib::lang::HeapAllocator::MAX_ALIGNMENT = alignof(std::max_align_t)
staticconstexpr

Evaluates to alignof(std::max_align_t).

See also
Field Allocator::MAX_ALIGNMENT.

Definition at line 541 of file allocation.inl.

◆ MIN_ALIGNMENT

size_t alib::lang::HeapAllocator::MIN_ALIGNMENT = alignof(std::max_align_t)
staticconstexpr

Evaluates to alignof(std::max_align_t).

See also
Field Allocator::MIN_ALIGNMENT.

Definition at line 537 of file allocation.inl.

Method Details:

◆ allocate()

void * alib::lang::HeapAllocator::allocate ( size_t size,
size_t alignment ) const
inline

Allocate memory using std::malloc.

Parameters
sizeThe size of memory the block to allocate in bytes. With this allocator this is not an input/output parameter.
alignmentThis type ignores the alignment parameter, because std::malloc always uses alignof(std::max_align_t).
Returns
Pointer to the allocated memory block.

Definition at line 553 of file allocation.inl.

Here is the call graph for this function:

◆ allowsMemSplit()

constexpr bool alib::lang::HeapAllocator::allowsMemSplit ( )
inlinestaticconstexpr

This is a constexpr static method that determines if a type allows splitting memory and later passing the pieces back to free.

Returns
false.

Definition at line 608 of file allocation.inl.

◆ dbgAcknowledgeIncreasedAllocSize()

template<typename TSize>
void alib::lang::HeapAllocator::dbgAcknowledgeIncreasedAllocSize ( void * ,
TSize  ) const
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.

Template Parameters
TSizeThe type of parameter allocSize. (Deduced by the compiler.)

Definition at line 596 of file allocation.inl.

◆ dbgCheckMemory()

template<typename TSize>
void alib::lang::HeapAllocator::dbgCheckMemory ( void * mem,
TSize size )
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.

See also
Chapter 11.4 Debugging of the Programmer's Manual.
Template Parameters
TSizeThe type of parameter size. (Deduced by the compiler.)
Parameters
memThe address of the allocated object.
sizeThe requested allocation size of the object.

Definition at line 624 of file allocation.inl.

Here is the call graph for this function:

◆ free()

void alib::lang::HeapAllocator::free ( void * mem,
size_t size ) const
inline

Frees memory by invoking std::free.

Parameters
memThe memory to dispose.
sizeThe size of the given mem.

Definition at line 584 of file allocation.inl.

Here is the call graph for this function:

◆ operator()()

AllocatorInterface< HeapAllocator > alib::lang::HeapAllocator::operator() ( ) const
inlinenoexcept

Returns a temporary object (which is usually optimized out together with a call to this operator) providing high-level convenience methods for allocation.

See also
Class lang::AllocatorInterface
Returns
A temporary high-level interface into the allocator.

Definition at line 602 of file allocation.inl.

◆ reallocate()

void * alib::lang::HeapAllocator::reallocate ( void * mem,
size_t oldSize,
size_t newSize,
size_t  )
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.

Parameters
memThe memory to reallocate.
oldSizeThe current size of mem.
newSizeThe now required size of mem in bytes. With this allocator this is not an input/output parameter.
Returns
Pointer to the re-allocated memory.

Definition at line 573 of file allocation.inl.

Here is the call graph for this function:

The documentation for this struct was generated from the following file: