ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
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 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< HeapAllocatoroperator() () const noexcept
 
void * reallocate (void *mem, size_t oldSize, size_t newSize, size_t)
 

Type Definition Details:

◆ ChainedAllocator

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.

Field Details:

◆ CLEAR

unsigned char CLEAR = 0xF1
staticconstexpr

A magic byte written when memory is freed.

See also
Field CLEAR.

Definition at line 528 of file allocation.hpp.

◆ DbgName

const char* DbgName = "HeapAllocator"
staticconstexpr

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

Definition at line 608 of file allocation.hpp.

◆ MAGIC

unsigned char MAGIC = 0xA1
staticconstexpr

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

See also
Method Allocator::dbgCheckMemory.

Definition at line 524 of file allocation.hpp.

◆ MAX_ALIGNMENT

size_t MAX_ALIGNMENT = alignof(std::max_align_t)
staticconstexpr

Evaluates to alignof(std::max_align_t).

See also
Field Allocator::MAX_ALIGNMENT.

Definition at line 536 of file allocation.hpp.

◆ MIN_ALIGNMENT

size_t MIN_ALIGNMENT = alignof(std::max_align_t)
staticconstexpr

Evaluates to alignof(std::max_align_t).

See also
Field Allocator::MIN_ALIGNMENT.

Definition at line 532 of file allocation.hpp.

Method Details:

◆ allocate()

void * 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 548 of file allocation.hpp.

◆ allowsMemSplit()

static constexpr bool 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 604 of file allocation.hpp.

◆ dbgAcknowledgeIncreasedAllocSize()

template<typename TSize >
void 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 592 of file allocation.hpp.

◆ dbgCheckMemory()

template<typename TSize >
void 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 620 of file allocation.hpp.

◆ free()

void 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 580 of file allocation.hpp.

◆ operator()()

AllocatorInterface< 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 598 of file allocation.hpp.

◆ reallocate()

void * 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 569 of file allocation.hpp.


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