ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::monomem::TPoolAllocator< TAllocator, TAlignment > Class Template Reference

Description:

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
class alib::monomem::TPoolAllocator< TAllocator, TAlignment >
Important Note
Please consult the Programmer's Manual of ALib module ALib Monomem for an introduction into the rather complex topic of monotonous and pool allocation and how to use this class.

This class implements the interface prototyped with Allocator. Whenever an object is freed with this allocator, the memory is stored in a stack for later re-use. This means, with allocations, first it is checked if a previously freed object of fitting size is available, and only if not, new memory is allocated. To implement this in a performant way (preferably with compile-time calculations), the following approach was taken:

  • The size of every allocation request is increased to the next higher power of 2 bound. For example, if a memory piece of 42 bytes is requested, a piece of size 64 bytes is returned.
  • For each allocation size, a simple stack (single directed linked list) is used to dispose and recycle memory.
  • The minimum allocation size is sizeof(void*), because for the recycling mechanism a pointer needs to be stored in the freed pool memory, when it is added to the internal "recycling stack".
  • This results in only 61 different allocation sizes (on a 64-bit machine).
  • A fixed alignment for all objects is used. This is also the maximum alignment allowed to pass to methods allocate and reallocate. Its value is defined by template parameter TAlignment, which defaults to what is given with the compiler-symbol ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT, which in turn defaults to alignof(uint64_t).
  • This approach results in only 61 different allocation sizes (on a 64-bit machine).

The consequences of this strategy are straight forward:

  • Using this allocator type requires disposing (free) every allocated piece of memory (which is in contrast to MonoAllocator).
  • This type is well suited for allocation scenarios, which release each object within a certain period of time, just to allocate new ones shortly afterward. A good use case example is given with module ALib ThreadModel: Here, request objects are pushed into priority queues, which are disposed after the request got processed. The objects are rather small and of different sizes. In complex server scenarios, it can become quickly millions of messages per second which are created and disposed.
  • A statistical average of 25% memory allocation overhead (waste) is taken into account with the use of this class.

Chapter 1.5 Recycling of the joint Programmer's Manual of modules ALib Containers and ALib Monomem provides an in-depth discussion of what is called "recycling" in this context. Several strategies are proposed with container types provided by module ALib Containers. From this perspective, this allocator could be seen as a "recycling proxy" in front of a MonoAllocator (or a different, chained allocator).

See also
Template Parameters
TAllocatorThe underlying allocator to use.
With type definition PoolAllocator,this is bound to type MonoAllocator.
With type definition PoolAllocatorHA,this is bound to type HeapAllocator.
For general information see chapter 8. Allocator Chaining of the Programmer's Manual.
TAlignmentThe fixed alignment to use. Corresponding method parameters are ignored.
Defaults to the compiler-symbol ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT, which in turn defaults to alignof(uint64_t).
It is statically asserted that this value is not higher than field MAX_ALIGNMENT of the given chained TAllocator and not smaller than alignof(void*).
See Chapter 11.1 Alignment of the Programmer's Manual of this module.

Definition at line 84 of file poolallocator.inl.

Inheritance diagram for alib::monomem::TPoolAllocator< TAllocator, TAlignment >:
[legend]
Collaboration diagram for alib::monomem::TPoolAllocator< TAllocator, TAlignment >:
[legend]

Public Type Index:

using ChainedAllocator = TAllocator
 
- Public Type Index: inherited from alib::lang::AllocatorMember< TAllocator >
using AllocatorType = TAllocator
 Exposes the allocator type.
 

Public Static Field Index:

static constexpr size_t MAX_ALIGNMENT = TAlignment
 
static constexpr size_t MIN_ALIGNMENT = TAlignment
 
- Public Static Field Index: inherited from alib::lang::DbgCriticalSections
static ALIB_DLL const char * ASSERTION_FORMAT
 

Public Static Method Index:

Specific Interface (Static Methods)
static constexpr int AllocationInformationBitCount ()
 
template<typename T>
static constexpr short GetAllocInformation ()
 
template<typename TIntegral>
requires std::integral<TIntegral>
static constexpr short GetAllocInformation (TIntegral size)
 
static constexpr size_t GetAllocationSize (short allocInfo)
 

Public Field Index:

const char * DbgName
 
- Public Field Index: inherited from alib::lang::DbgCriticalSections
CallerInfo DCSAcq
 Source location of acquirement.
 
AssociatedLockDCSLock {nullptr}
 
const char * DCSName
 The name of this DCS. Used for debug-output.
 
std::atomic< int > DCSReaderCnt {0}
 Tracks enter/exit calls of readers.
 
CallerInfo DCSRel
 Source location of the last "reader" seen.
 
CallerInfo DCSSAcq
 Source location of acquirement.
 
CallerInfo DCSSRel
 Source location of the last "reader" seen.
 
std::atomic< int > DCSWriterCnt {0}
 Tracks enter/exit calls (including readers)
 
int DCSYieldOrSleepTimeInNS = -1
 

Public Method Index:

template<typename TRequires = allocMember>
requires std::default_initializable<TRequires>
 TPoolAllocator ()
 
ALIB_DLL TPoolAllocator (TAllocator &pAllocator)
 
ALIB_DLL ~TPoolAllocator ()
 Destructs this type.
 
lang::Allocator Implementation
void * allocate (size_t &size, size_t pAlignment)
 
void * reallocate (void *mem, size_t oldSize, size_t &newSize, size_t pAlignment)
 
void free (void *mem, size_t size)
 
template<typename TSize>
void dbgAcknowledgeIncreasedAllocSize (void *mem, TSize allocSize) const
 
constexpr bool allowsMemSplit () noexcept
 
lang::AllocatorInterface< TPoolAllocatoroperator() ()
 
Specific Interface
ALIB_DLL void * AllocateByAllocationInfo (int allocInfo)
 
void FreeByAllocationInfo (int allocInfo, void *mem)
 
void Reset ()
 
ALIB_DLL integer GetPoolSize (size_t size)
 
template<typename TSize>
void dbgCheckMemory (void *mem, TSize size)
 
int DbgCountedOpenAllocations (size_t size)
 
int DbgCountedOpenAllocations ()
 
int DbgSuppressNonFreedObjectsWarning ()
 
uinteger DbgStatAllocCounter (size_t size)
 
uinteger DbgStatAllocCounter ()
 
- Public Method Index: inherited from alib::lang::AllocatorMember< TAllocator >
 AllocatorMember ()=delete
 Deleted default constructor. (The allocator has to be given with construction)
 
 AllocatorMember (TAllocator &pAllocator) noexcept
 
AllocatorInterface< TAllocator > AI () const noexcept
 
TAllocator & GetAllocator () const noexcept
 
- Public Method Index: inherited from alib::lang::DbgCriticalSections
 DbgCriticalSections (const char *name)
 
 ~DbgCriticalSections ()
 Destructor. Checks that this instance is unused.
 
ALIB_DLL void Acquire (const CallerInfo &ci) const
 
ALIB_DLL void AcquireShared (const CallerInfo &ci) const
 
ALIB_DLL void doAssert (bool cond, const CallerInfo &ciAssert, const CallerInfo &ci, const char *headline) const
 
ALIB_DLL void Release (const CallerInfo &ci) const
 
ALIB_DLL void ReleaseShared (const CallerInfo &ci) const
 
void yieldOrSleep () const
 

Protected Type Index:

using allocMember = lang::AllocatorMember<TAllocator>
 A shortcut to a base type.
 

Protected Static Method Index:

template<size_t TRequestedObjectSize>
static constexpr short hookIndex ()
 
static constexpr short hookIndex (size_t requestedObjectSize)
 
static constexpr short minimumHookIndex ()
 
static constexpr short qtyHooks ()
 

Protected Field Index:

size_t dbgLastRequestedSize
 
int * dbgOpenAllocations = nullptr
 The current number of allocations that have not been freed.
 
uintegerdbgStatAllocCounter = nullptr
 The overall number of allocations for each size.
 
void ** hooks = nullptr
 
- Protected Field Index: inherited from alib::lang::AllocatorMember< TAllocator >
TAllocator & allocator
 A reference to the allocator.
 

Protected Method Index:

void deletePool ()
 Frees all recycled pool objects.
 

Private Static Fields Index:

static constexpr unsigned char CLEAR = 0xF3
 
static constexpr unsigned char MAGIC = 0xA3
 

Type Definition Details:

◆ allocMember

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
using alib::monomem::TPoolAllocator< TAllocator, TAlignment >::allocMember = lang::AllocatorMember<TAllocator>
protected

A shortcut to a base type.

Definition at line 114 of file poolallocator.inl.

◆ ChainedAllocator

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
using alib::monomem::TPoolAllocator< TAllocator, TAlignment >::ChainedAllocator = TAllocator

The type of the allocator that this allocator uses underneath to allocate the buffers, given with template parameter TAllocator.
The instance can be accessed with inherited methods GetAllocator and AI.

Definition at line 199 of file poolallocator.inl.

Field Details:

◆ CLEAR

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
unsigned char alib::monomem::TPoolAllocator< TAllocator, TAlignment >::CLEAR = 0xF3
staticconstexprprivate

A magic byte written when memory is freed.

See also
Field CLEAR.

Definition at line 110 of file poolallocator.inl.

◆ dbgLastRequestedSize

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
size_t alib::monomem::TPoolAllocator< TAllocator, TAlignment >::dbgLastRequestedSize
protected

This member is used with ALIB_DEBUG_ALLOCATIONS to pass the requested size from interface methods to the implementation. While this approach is not thread-safe, it avoids adding some method parameter, which would exist only in debug versions. It was intentionally implemented like this, to cause testing errors in case that multithreaded access was not locked against racing conditions properly by the user.

Definition at line 147 of file poolallocator.inl.

◆ DbgName

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
const char* alib::monomem::TPoolAllocator< TAllocator, TAlignment >::DbgName

A name for this object. The constructors grab this name from the chained allocator, which most often is an already named MonoAllocator. Thus, no constructor argument is available (in contrast to class MonoAllocator). This name may be changed after construction by the using code as appropriate.

Definition at line 129 of file poolallocator.inl.

◆ dbgOpenAllocations

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
int* alib::monomem::TPoolAllocator< TAllocator, TAlignment >::dbgOpenAllocations = nullptr
protected

The current number of allocations that have not been freed.

Definition at line 140 of file poolallocator.inl.

◆ dbgStatAllocCounter

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
uinteger* alib::monomem::TPoolAllocator< TAllocator, TAlignment >::dbgStatAllocCounter = nullptr
protected

The overall number of allocations for each size.

Definition at line 135 of file poolallocator.inl.

◆ hooks

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
void** alib::monomem::TPoolAllocator< TAllocator, TAlignment >::hooks = nullptr
protected

Array of hooks. Each hook points to previously disposed memory of the same size. Its length is determined with constexpr qtyHooks and it is allocated with construction in allocator.

Definition at line 120 of file poolallocator.inl.

◆ MAGIC

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
unsigned char alib::monomem::TPoolAllocator< TAllocator, TAlignment >::MAGIC = 0xA3
staticconstexprprivate

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 106 of file poolallocator.inl.

◆ MAX_ALIGNMENT

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
size_t alib::monomem::TPoolAllocator< TAllocator, TAlignment >::MAX_ALIGNMENT = TAlignment
staticconstexpr

Evaluates to the value of template parameter TAlignment.

See also
Field Allocator::MAX_ALIGNMENT.

Definition at line 207 of file poolallocator.inl.

◆ MIN_ALIGNMENT

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
size_t alib::monomem::TPoolAllocator< TAllocator, TAlignment >::MIN_ALIGNMENT = TAlignment
staticconstexpr

Evaluates to the value of template parameter TAlignment.

See also
Field Allocator::MIN_ALIGNMENT.

Definition at line 203 of file poolallocator.inl.

Constructor(s) / Destructor Details:

◆ TPoolAllocator() [1/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
ALIB_DLL alib::monomem::TPoolAllocator< TAllocator, TAlignment >::TPoolAllocator ( TAllocator & pAllocator)

Constructs this type.

Parameters
pAllocatorThe allocator to use for allocating pool objects.

◆ TPoolAllocator() [2/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
template<typename TRequires = allocMember>
requires std::default_initializable<TRequires>
alib::monomem::TPoolAllocator< TAllocator, TAlignment >::TPoolAllocator ( )
inline

Constructs this type without a given allocator. This constructor is applicable only if TAllocator is default-constructible (e.g., HeapAllocator).

Template Parameters
TRequiresDefaulted template parameter. Must not be specified.

Definition at line 220 of file poolallocator.inl.

Method Details:

◆ allocate()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
void * alib::monomem::TPoolAllocator< TAllocator, TAlignment >::allocate ( size_t & size,
size_t pAlignment )
inline

Allocates or re-uses a previously freed piece of memory of equal size. Any given size is rounded up to the next higher power of 2 before comparison and returned via in/output parameter size.

Parameters
[in,out]sizeThe size of memory the block to allocate in bytes. This will be rounded up to the next higher power of 2 when the function returns. Any additional size might be used by the caller.
pAlignmentThe (minimum) alignment of the memory block to allocate in bytes. This is ignored with this allocator. Instead, it uses what is specified by template parameter TAlignment. See Chapter 11.1 Alignment of the Programmer's Manual of this module.
Returns
Pointer to the allocated memory.

Definition at line 265 of file poolallocator.inl.

◆ AllocateByAllocationInfo()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
ALIB_DLL void * alib::monomem::TPoolAllocator< TAllocator, TAlignment >::AllocateByAllocationInfo ( int allocInfo)

Allocates or recycles previously freed memory suitable to emplace an instance with the given allocation information.

Note
This method is to be used in combination with template method GetAllocInformation in situations, where the type information of the allocation is not available or has to be transferred to a non-templated library function or similar.
Parameters
allocInfoThe allocation information received with GetAllocInformation.
Returns
A pointer to the allocated memory.

◆ AllocationInformationBitCount()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
constexpr int alib::monomem::TPoolAllocator< TAllocator, TAlignment >::AllocationInformationBitCount ( )
inlinestaticconstexpr

Returns the number of relevant bits of the allocation information value returned by overloaded methods GetAllocInformation. In other words, the value returned by these methods is between 0 and the power of 2 of the value returned by this method.

Returns
5 on 32-bit systems and 6 on 64 bit systems. GetAllocInformation.

Definition at line 342 of file poolallocator.inl.

◆ allowsMemSplit()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
bool alib::monomem::TPoolAllocator< TAllocator, TAlignment >::allowsMemSplit ( )
constexprnoexcept

See the description of this method with prototype Allocator::allowsMemSplit.
(Note: This method is static. For technical reasons this cannot be reflected in this documentation)

Returns
false.

◆ dbgAcknowledgeIncreasedAllocSize()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
template<typename TSize>
void alib::monomem::TPoolAllocator< TAllocator, TAlignment >::dbgAcknowledgeIncreasedAllocSize ( void * mem,
TSize allocSize ) const
inline

For an explanation, see Allocator::dbgAcknowledgeIncreasedAllocSize.

Template Parameters
TSizeThe type of parameter allocSize. (Deduced by the compiler.)
Parameters
memThe address of the allocated object.
allocSizeThe true allocation size returned by the method allocate .

Definition at line 313 of file poolallocator.inl.

◆ dbgCheckMemory()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
template<typename TSize>
void alib::monomem::TPoolAllocator< TAllocator, TAlignment >::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 460 of file poolallocator.inl.

◆ DbgCountedOpenAllocations() [1/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
int alib::monomem::TPoolAllocator< TAllocator, TAlignment >::DbgCountedOpenAllocations ( )
inline

Returns the number of objects currently allocated (and not freed).
Only functional if the compiler-symbol ALIB_DEBUG_ALLOCATIONS is set. Otherwise, this is a static constexpr function that returns 0.

Returns
The number of non-freed allocations.

Definition at line 479 of file poolallocator.inl.

◆ DbgCountedOpenAllocations() [2/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
int alib::monomem::TPoolAllocator< TAllocator, TAlignment >::DbgCountedOpenAllocations ( size_t size)
inline

Returns the number of objects of the given size currently allocated (and not freed).
Only functional if the compiler-symbol ALIB_DEBUG_ALLOCATIONS is set. Otherwise, this is a static constexpr function that returns 0.

See also
Overloaded method DbgCountedOpenAllocations() which returns the overall number.
Parameters
sizeThe size of the object requested, as given with allocate and free.
Returns
The number of non-freed allocations.

Definition at line 470 of file poolallocator.inl.

◆ DbgStatAllocCounter() [1/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
uinteger alib::monomem::TPoolAllocator< TAllocator, TAlignment >::DbgStatAllocCounter ( )
inline

Returns the overall number of objects currently allocated (and not freed).
Only functional if the compiler-symbol ALIB_DEBUG_MEMORY is set. Otherwise, this is a static constexpr function that returns 0.

Returns
The number of non-freed allocations.

Definition at line 522 of file poolallocator.inl.

◆ DbgStatAllocCounter() [2/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
uinteger alib::monomem::TPoolAllocator< TAllocator, TAlignment >::DbgStatAllocCounter ( size_t size)
inline

Returns the overall number of allocated (and potentially freed) objects of the given size.
Only functional if the compiler-symbol ALIB_DEBUG_MEMORY is set. Otherwise, this is a static constexpr function that returns 0.

See also
Overloaded method DbgStatAllocCounter() which returns the overall number.
Parameters
sizeThe size of the object requested, as given with allocate and free.
Returns
The number of non-freed allocations.

Definition at line 513 of file poolallocator.inl.

◆ DbgSuppressNonFreedObjectsWarning()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
int alib::monomem::TPoolAllocator< TAllocator, TAlignment >::DbgSuppressNonFreedObjectsWarning ( )
inline

Actively suppresses a warning on destruction in case DbgCountedOpenAllocations does not return 0.
Only functional if the compiler-symbol ALIB_DEBUG_ALLOCATIONS is set. Otherwise, this is a static constexpr function that returns 0.

Returns
The number of non-freed allocations.

Definition at line 492 of file poolallocator.inl.

◆ free()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
void alib::monomem::TPoolAllocator< TAllocator, TAlignment >::free ( void * mem,
size_t size )
inline

Disposes the given memory. The fragment is stored for later reuse with method allocate of memory sizes of the same power of 2 range.

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

Definition at line 300 of file poolallocator.inl.

◆ FreeByAllocationInfo()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
void alib::monomem::TPoolAllocator< TAllocator, TAlignment >::FreeByAllocationInfo ( int allocInfo,
void * mem )
inline

Disposes an object (or piece of memory) which has been allocated using AllocateByAllocationInfo. If the compiler-symbol ALIB_DEBUG_ALLOCATIONS is set, the method will overwrite the freed memory with character 0xD3.

Parameters
allocInfoThe allocation information received with GetAllocInformation.
memThe object to dispose.

Definition at line 412 of file poolallocator.inl.

◆ GetAllocationSize()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
constexpr size_t alib::monomem::TPoolAllocator< TAllocator, TAlignment >::GetAllocationSize ( short allocInfo)
inlinestaticconstexpr

Returns the allocation size for a value returned by GetAllocInformation.

Parameters
allocInfoThe alloc information received with GetAllocInformation.
Returns
The next higher power of 2 in respect to the size passed as parameter to GetAllocInformation.

Definition at line 389 of file poolallocator.inl.

◆ GetAllocInformation() [1/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
template<typename T>
constexpr short alib::monomem::TPoolAllocator< TAllocator, TAlignment >::GetAllocInformation ( )
inlinestaticconstexpr

This static constexpr method returns information that this allocator needs for allocation and freeing of objects. In the case that the original allocation type is not known on deletion (e.g., if virtual destructors are used), then this piece of information has to be collected with this function (probably on construction) and provided to FreeByAllocationInfo via custom mechanics.

Note
Two options are quite obvious to implement:
  1. The information is stored within the object and passed on destruction.
  2. The information is retrieved from a virtual function, which is implemented using this constexpr type.
While the second version has a small impact on code size and execution speed (because the virtual function has to be called with deletion), the first version increases the footprint of the object. So both approaches have their advantages and disadvantages.

A third option is explained in a step-by-step sample in chapter 6.3 Using Meta-Info Singletons to Reclaim Allocation Information of the Programmer's Manual of this module.

Technically, the returned value is the exponent base 2 of the next higher power of 2 value, minus the minimum supported exponent. This directly gives an index into field hooks.

Template Parameters
TThe object type that allocation information is to be retrieved for.
Returns
Allocation information.

Definition at line 370 of file poolallocator.inl.

◆ GetAllocInformation() [2/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
template<typename TIntegral>
requires std::integral<TIntegral>
constexpr short alib::monomem::TPoolAllocator< TAllocator, TAlignment >::GetAllocInformation ( TIntegral size)
inlinestaticconstexpr

Same as the templated GetAllocInformation<T>() version, but accepts method parameter size instead of a template parameter. Thus, this version is only potentially constexpr.

Template Parameters
TIntegralThe type that parameter size is provided with. If not integral, this method is not chosen by the compiler.
Parameters
sizeThe size of the object to be allocated or disposed in a probalble next step.
Returns
Allocation information. See GetAllocInformation<T>().

Definition at line 382 of file poolallocator.inl.

◆ GetPoolSize()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
ALIB_DLL integer alib::monomem::TPoolAllocator< TAllocator, TAlignment >::GetPoolSize ( size_t size)

Returns the number of available pool objects, hence those that had been allocated, freed, and not been reused with a next allocation again.

Parameters
sizeThe size of the object requested, as given with allocate and free.
Returns
The number of pool objects of the given size.

◆ hookIndex() [1/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
template<size_t TRequestedObjectSize>
constexpr short alib::monomem::TPoolAllocator< TAllocator, TAlignment >::hookIndex ( )
inlinestaticconstexprprotected

Templated version of hookIndex(size_t).

Template Parameters
TRequestedObjectSizeThe object size requested by the user, hence excluding the internal pointer.
Returns
The index of the recycler hook in hooks.

Definition at line 182 of file poolallocator.inl.

◆ hookIndex() [2/2]

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
constexpr short alib::monomem::TPoolAllocator< TAllocator, TAlignment >::hookIndex ( size_t requestedObjectSize)
inlinestaticconstexprprotected

Calculates the index of the hook in field hooks. This corresponds to the number of the most significant bit in the next higher power of 2 value minus minimumHookIndex.

Parameters
requestedObjectSizeThe object size requested by the user.
Returns
The index of the recycler hook in hooks.

Definition at line 167 of file poolallocator.inl.

◆ minimumHookIndex()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
constexpr short alib::monomem::TPoolAllocator< TAllocator, TAlignment >::minimumHookIndex ( )
inlinestaticconstexprprotected

To be able to store allocated memory in the recycling stacks, any allocated memory is at least of size sizeof(void*). Hence sizes 1 and 2 (with 64-bit systems also 4) will never be allocated and no hook is available for these sizes.

Returns
3 on 64-bit systems, 2 on 32-bit systems.

Definition at line 155 of file poolallocator.inl.

◆ operator()()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
lang::AllocatorInterface< TPoolAllocator > alib::monomem::TPoolAllocator< TAllocator, TAlignment >::operator() ( )
inline

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 330 of file poolallocator.inl.

◆ qtyHooks()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
constexpr short alib::monomem::TPoolAllocator< TAllocator, TAlignment >::qtyHooks ( )
inlinestaticconstexprprotected

The number of hooks needed. Evaluates to 61 on a common 64-bit platform and 29 on a

Returns
61 on 64-bit systems, 29 on 32-bit systems.

Definition at line 160 of file poolallocator.inl.

◆ reallocate()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
void * alib::monomem::TPoolAllocator< TAllocator, TAlignment >::reallocate ( void * mem,
size_t oldSize,
size_t & newSize,
size_t pAlignment )

Shrinks or grows a piece of memory. If a new allocation was performed the existing data is copied. Any given oldSize and newSize is rounded up to the next higher power of 2 before comparison. Note, that parameter newSize is provided as a reference.

Parameters
memThe memory to reallocate.
oldSizeThe current size of mem.
[in,out]newSizeThe size of memory the block to allocate in bytes. This will be rounded up to the next higher power of 2 when the function returns. Any additional size might be used by the caller.
pAlignmentThe (minimum) alignment of the memory to allocate in bytes. This is ignored with this allocator. Instead, it uses what is specified by template parameter TAlignment. See Chapter 11.1 Alignment of the Programmer's Manual of this module.
Returns
Pointer to the re-allocated memory.

◆ Reset()

template<typename TAllocator, size_t TAlignment = ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT>
void alib::monomem::TPoolAllocator< TAllocator, TAlignment >::Reset ( )
inline

Deletes all current pool objects with the ChainedAllocator. The state of this class equals the state after construction.

Definition at line 431 of file poolallocator.inl.


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