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:
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".alignof(uint64_t)
.The consequences of this strategy are straight forward:
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).
TAllocator | The 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. |
TAlignment | The fixed alignment to use. Corresponding method parameters are ignored. Defaults to 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 93 of file poolallocator.hpp.
#include <poolallocator.hpp>
Public Type Index: | |
using | ChainedAllocator = TAllocator |
Public Type Index: inherited from 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 DbgCriticalSections | |
static ALIB_API 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 > | |
static constexpr short | GetAllocInformation (TIntegral size) |
static constexpr size_t | GetAllocationSize (short allocInfo) |
Public Field Index: | |
const char * | DbgName |
Public Field Index: inherited from DbgCriticalSections | |
CallerInfo | DCSAcq |
Source location of acquirement. | |
AssociatedLock * | DCSLock {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 TEnableIf = allocMember, ATMP_T_IF(int, std::is_default_constructible< TEnableIf >::value) = 0> | |
TPoolAllocator () | |
ALIB_API | TPoolAllocator (TAllocator &pAllocator) |
ALIB_API | ~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< TPoolAllocator > | operator() () |
Specific Interface | |
ALIB_API void * | AllocateByAllocationInfo (int allocInfo) |
void | FreeByAllocationInfo (int allocInfo, void *mem) |
void | Reset () |
ALIB_API 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 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 DbgCriticalSections | |
ALIB_FORCE_INLINE | DbgCriticalSections (const char *name) |
ALIB_FORCE_INLINE | ~DbgCriticalSections () |
Destructor. Checks that this instance is unused. | |
ALIB_API void | Acquire (const CallerInfo &ci) const |
ALIB_API void | AcquireShared (const CallerInfo &ci) const |
ALIB_API void | doAssert (bool cond, const CallerInfo &ciAssert, const CallerInfo &ci, const char *headline) const |
ALIB_API void | Release (const CallerInfo &ci) const |
ALIB_API void | ReleaseShared (const CallerInfo &ci) const |
ALIB_FORCE_INLINE 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. | |
uinteger * | dbgStatAllocCounter = nullptr |
The overall number of allocations for each size. | |
void ** | hooks = nullptr |
Protected Field Index: inherited from 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 |
|
protected |
A shortcut to a base type.
Definition at line 123 of file poolallocator.hpp.
using 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 208 of file poolallocator.hpp.
|
staticconstexprprivate |
A magic byte written when memory is freed.
Definition at line 119 of file poolallocator.hpp.
|
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 156 of file poolallocator.hpp.
const char* 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 138 of file poolallocator.hpp.
|
protected |
The current number of allocations that have not been freed.
Definition at line 149 of file poolallocator.hpp.
|
protected |
The overall number of allocations for each size.
Definition at line 144 of file poolallocator.hpp.
|
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 129 of file poolallocator.hpp.
|
staticconstexprprivate |
A magic byte, used with compiler-symbol ALIB_DEBUG_ALLOCATIONS to mark memory and detect out-of-bounds writes.
Definition at line 115 of file poolallocator.hpp.
|
staticconstexpr |
Evaluates to the value of template parameter TAlignment.
Definition at line 216 of file poolallocator.hpp.
|
staticconstexpr |
Evaluates to the value of template parameter TAlignment.
Definition at line 212 of file poolallocator.hpp.
ALIB_API TPoolAllocator | ( | TAllocator & | pAllocator | ) |
Constructs this type.
pAllocator | The allocator to use for allocating pool objects. |
|
inline |
Constructs this type without a given allocator. This constructor is applicable only if TAllocator is default-constructible (e.g., HeapAllocator).
Definition at line 228 of file poolallocator.hpp.
|
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.
[in,out] | size | The 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. |
pAlignment | The (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. |
Definition at line 273 of file poolallocator.hpp.
ALIB_API void * AllocateByAllocationInfo | ( | int | allocInfo | ) |
Allocates or recycles previously freed memory suitable to emplace an instance with the given allocation information.
allocInfo | The allocation information received with GetAllocInformation. |
|
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.
5
on 32-bit systems and 6
on 64 bit systems. GetAllocInformation. Definition at line 350 of file poolallocator.hpp.
|
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)
false
.
|
inline |
For an explanation, see Allocator::dbgAcknowledgeIncreasedAllocSize.
TSize | The type of parameter allocSize. (Deduced by the compiler.) |
mem | The address of the allocated object. |
allocSize | The true allocation size returned by the method allocate . |
Definition at line 321 of file poolallocator.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 477 of file poolallocator.hpp.
|
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
.
Definition at line 498 of file poolallocator.hpp.
|
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
.
Definition at line 487 of file poolallocator.hpp.
|
inline |
Returns the overall number of objects currently allocated (and not freed).
Only functional if the compiler symbol ALIB_DEBUG_MONOMEM is set. Otherwise, this is a static constexpr
function that returns 0
.
Definition at line 545 of file poolallocator.hpp.
|
inline |
Returns the overall number of allocated (and potentially freed) objects of the given size.
Only functional if the compiler symbol ALIB_DEBUG_MONOMEM is set. Otherwise, this is a static constexpr
function that returns 0
.
Definition at line 534 of file poolallocator.hpp.
|
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
.
Definition at line 513 of file poolallocator.hpp.
|
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.
mem | The memory to dispose. |
size | The size of the given mem. |
Definition at line 308 of file poolallocator.hpp.
|
inline |
Disposes an object (or piece of memory) which has been allocated using AllocateByAllocationInfo. If compiler symbol ALIB_DEBUG_ALLOCATIONS is set, the method will overwrite the freed memory with character 0xD3
.
allocInfo | The allocation information received with GetAllocInformation. |
mem | The object to dispose. |
Definition at line 427 of file poolallocator.hpp.
|
inlinestaticconstexpr |
Returns the allocation size for a value returned by GetAllocInformation.
allocInfo | The alloc information received with GetAllocInformation. |
Definition at line 405 of file poolallocator.hpp.
|
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.
constexpr
type. 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.
T | The object type that allocation information is to be retrieved for. |
Definition at line 379 of file poolallocator.hpp.
|
staticconstexpr |
Same as templated GetAllocInformation<T>() version, but accepts method parameter size instead of a template parameter. Thus, only potentially constexpr
.
TIntegral | The type that parameter size is provided with. If not integral, this method is not chosen by the compiler. |
size | The size of the object to be allocated or disposed in a probalble next step. |
|
inlinestaticconstexprprotected |
Templated version of hookIndex(size_t).
TRequestedObjectSize | The object size requested by the user, hence excluding the internal pointer. |
Definition at line 191 of file poolallocator.hpp.
|
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.
requestedObjectSize | The object size requested by the user. |
Definition at line 176 of file poolallocator.hpp.
|
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.
3
on 64-bit systems, 2
on 32-bit systems. Definition at line 164 of file poolallocator.hpp.
|
inline |
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 338 of file poolallocator.hpp.
|
inlinestaticconstexprprotected |
The number of hooks needed. Evaluates to 61
on a common 64-bit platform and 29
on a
61
on 64-bit systems, 29
on 32-bit systems. Definition at line 169 of file poolallocator.hpp.
void * 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.
mem | The memory to reallocate. | |
oldSize | The current size of mem. | |
[in,out] | newSize | The 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. |
pAlignment | The (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. |
|
inline |
Deletes all current pool objects with the ChainedAllocator. The state of this class equals the state after construction.
Definition at line 448 of file poolallocator.hpp.