Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.experimental.allocator.building_blocks.affix_allocator

Sourcestd/experimental/allocator/building_blocks/affix_allocator.d

structAffixAllocator(Allocator, Prefix, Suffix = void);
Allocator that adds some extra data before (of typePrefix) and/or after(of typeSuffix) any allocation made with its parent allocator. This isuseful for uses where additional allocation-related information is needed, suchas mutexes, reference counts, or walls for debugging memory corruption errors.
IfPrefix is notvoid,Allocator must guarantee an alignment atleast as large asPrefix.alignof.
Suffixes are slower to get at because of alignment rounding, so prefixes shouldbe preferred. However, small prefixes blunt the alignment so if a largealignment with a small affix is needed, suffixes should be chosen.
The following methods are defined ifAllocator defines them, and forward to it:deallocateAll,empty,owns.
Examples:
import std.experimental.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);
enum uintalignment;
IfPrefix isvoid, the alignment is that of the parent. Otherwise, the alignment is the same as thePrefix's alignment.
Allocator_parent;
If the parent allocatorAllocator is stateful, an instance of it is stored as a member. Otherwise,AffixAllocator usesAllocator.instance. In either case, the name_parent is uniformly used for accessing the parent allocator.
pure nothrow @nogc @safe Allocatorparent();
If the parent allocatorAllocator is stateful, an instance of it is stored as a member. Otherwise,AffixAllocator usesAllocator.instance. In either case, the name_parent is uniformly used for accessing the parent allocator.
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 forgoodAllocSize, which may use the global default). Also, the methods will be shared if the parent allocator defines them as such.
static AffixAllocatorinstance;
Theinstance singleton is defined if and only if the parent allocator has no state and defines its ownit object.
ref autoprefix(T)(T[]b);

ref autosuffix(T)(T[]b);
Affix access functions offering references to the affixes of a blockb previously allocated with this allocator.b may not be null. They are defined if and only if the corresponding affix is notvoid.
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 justassociated with the data and known to the allocator. The table below documents the type ofpreffix(b) andaffix(b) depending on the type ofb.
Result ofprefix/suffix depending on argument (U is any unqualified type,Affix isPrefix orSuffix)
Argument TypeReturnComments
shared(U)[]ref shared AffixData is shared across threads and the affix follows suit.
immutable(U)[]ref shared AffixAlthough the data is immutable, the allocator "knows" the underlying memory is mutable, soimmutable is elided for the affix which is independent from the data itself. However, the result isshared becauseimmutable is implicitly shareable so multiple threads may access and manipulate the affix for the same data.
const(shared(U))[]ref shared AffixThe data is always shareable across threads. Even if the data isconst, the affix is modifiable by the same reasoning as forimmutable.
const(U)[]ref const AffixThe input may have originated fromU[] orimmutable(U)[], so it may be actually shared or not. Returning an unqualified affix may result in race conditions, whereas returning ashared affix may result in inadvertent sharing of mutable thread-local data across multiple threads. So the returned type is conservativelyref const.
U[]ref AffixUnqualified data has unqualified affixes.

Preconditionb !is null andb must have been allocated with this allocator.

Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 06:43:20 2026

[8]ページ先頭

©2009-2026 Movatter.jp