stdx.allocator.building_blocks.affix_allocator
-
Declaration
structAffixAllocator(Allocator, Prefix, Suffix = void);Allocator that adds some extra data before (of type ) and/or after (of type ) any allocation made with its parent allocator. This is useful for uses where additional allocation-related information is needed, such as mutexes, reference counts, or walls for debugging memory corruption errors.
Discussion
If is not , must guarantee an alignment at least as large as .
Suffixes are slower to get at because of alignment rounding, so prefixes should be preferred. However, small prefixes blunt the alignment so if a large alignment with a small affix is needed, suffixes should be chosen.
The following methods are defined if defines them, and forward to it: , , .Examples
import stdx.allocator.mallocator : Mallocator; // One word before and after each allocation. alias A = AffixAllocator!(Mallocator, size_t, size_t); auto b = A.instance.allocate(11); A.instance.prefix(b) = 0xCAFE_BABE; A.instance.suffix(b) = 0xDEAD_BEEF; assert(A.instance.prefix(b) == 0xCAFE_BABE && A.instance.suffix(b) == 0xDEAD_BEEF);
-
Declaration
enum uintalignment;If is , the
alignmentis that of the parent. Otherwise, thealignmentis the same as the 'salignment. -
Declaration
Allocator_parent;If the parent allocator is stateful, an instance of it is stored as a member. Otherwise, uses
Allocator.instance. In either case, the name is uniformly used for accessing the parent allocator. -
Declaration
Allocatorparent();If the
parentallocator is stateful, an instance of it is stored as a member. Otherwise, usesAllocator.instance. In either case, the name is uniformly used for accessing theparentallocator. -
Declaration
size_tgoodAllocSize(size_t);
void[]allocate(size_t);
Ternaryowns(void[]);
boolexpand(ref void[]b, size_tdelta);
boolreallocate(ref void[]b, size_ts);
booldeallocate(void[]b);
booldeallocateAll();
Ternaryempty();Standard allocator methods. Each is defined if and only if the parent allocator defines the homonym method (except for , which may use the global default). Also, the methods will be if the parent allocator defines them as such.
-
Declaration
static AffixAllocatorinstance;The
singleton is defined if and only if the parent allocator has no state and defines its owninstanceitobject. -
Declaration
ref autoprefix(T)(T[]b);
ref autosuffix(T)(T[]b);Affix access functions offering references to the affixes of a block
previously allocated with this allocator.bmay not bebnull. They are defined if and only if the corresponding affix is notvoid.Discussion
The qualifiers of the affix are not always the same as the qualifiers of the argument. This is because the affixes are not part of the data itself, but instead are just associated with the data and known to the allocator. The table below documents the type of
preffix(andb)affix(depending on the type ofb).b
Precondition:
andb!is nullmust have been allocated with this allocator.b