On this page
std.experimental.allocator.building_blocks.region
- struct Region(ParentAllocator = NullAllocator, uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);
-
A
Regionallocator allocates memory straight from one contiguous chunk. There is no deallocation, and once the region is full, allocation requests returnnull. Therefore,Regions are often used (a) in conjunction with more sophisticated allocators; or (b) for batch-style very fast allocations that deallocate everything at once.The region only stores three pointers, corresponding to the current position in the store and the limits. One allocation entails rounding up the allocation size for alignment purposes, bumping the current pointer, and comparing it against the limit.
IfParentAllocatoris different fromNullAllocator,Regiondeallocates the chunk of memory during destruction.
TheminAlignparameter establishes alignment. IfminAlign > 1, the sizes of all allocation requests are rounded up to a multiple ofminAlign. Applications aiming at maximum speed may want to chooseminAlign = 1and control alignment externally.- Examples:
-
import std.algorithm.comparison : max; import std.experimental.allocator.building_blocks.allocator_list : AllocatorList; import std.experimental.allocator.mallocator : Mallocator; import std.typecons : Ternary; // Create a scalable list of regions. Each gets at least 1MB at a time by // using malloc. auto batchAllocator = AllocatorList!( (size_t n) => Region!Mallocator(max(n, 1024 * 1024)) )(); writeln(batchAllocator.empty); // Ternary.yes auto b = batchAllocator.allocate(101); writeln(b.length); // 101 writeln(batchAllocator.empty); // Ternary.no // This will cause a second allocation b = batchAllocator.allocate(2 * 1024 * 1024); writeln(b.length); // 2 * 1024 * 1024 // Destructor will free the memory
- ParentAllocator parent;
-
The parent allocator. Depending on whether
ParentAllocatorholds state or not, this is a member variable or an alias forParentAllocator.instance. -
pure nothrow @nogc this(ubyte[] store);
this(size_t n);
this(ParentAllocator parent, size_t n); -
Constructs a region backed by a user-provided store. Assumes the memory was allocated with
ParentAllocator(if different fromNullAllocator).- Parameters:
-
ubyte[] storeUser-provided store backing up the region. If ParentAllocatoris different fromNullAllocator, memory is assumed to have been allocated withParentAllocator.size_t nBytes to allocate using ParentAllocator. This constructor is only defined IfParentAllocatoris different fromNullAllocator. Ifparent.allocate(n)returnsnull, the region will be initialized as empty (correctly initialized but unable to allocate).
- const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);
-
Rounds the given size to a multiple of the
alignment - alias alignment = minAlign;
-
Alignment offered.
- pure nothrow @nogc @trusted void[] allocate(size_t n);
-
Allocates
nbytes of memory. The shortest path involves an alignment adjustment (ifalignment > 1), an increment, and a comparison.- Parameters:
-
size_t nnumber of bytes to allocate
- Returns:
-
A properly-aligned buffer of size
nornullif request could not be satisfied.
- pure nothrow @nogc @trusted void[] alignedAllocate(size_t n, uint a);
-
Allocates
nbytes of memory aligned at alignmenta.- Parameters:
-
size_t nnumber of bytes to allocate uint aalignment for the allocated block
- Returns:
-
Either a suitable block of
nbytes aligned ata, ornull.
- pure nothrow @nogc @trusted void[] allocateAll();
-
Allocates and returns all memory available to this region.
- pure nothrow @nogc @safe bool expand(ref void[] b, size_t delta);
-
Expands an allocated block in place. Expansion will succeed only if the block is the last allocated. Defined only if
growDownwardsisNo.growDownwards. - pure nothrow @nogc bool deallocate(void[] b);
-
Deallocates
b. This works only ifbwas obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing.- Parameters:
-
void[] bBlock previously obtained by a call to allocateagainst this allocator (nullis allowed).
- pure nothrow @nogc bool deallocateAll();
-
Deallocates all memory allocated by this region, which can be subsequently reused for new allocations.
- const pure nothrow @nogc @trusted Ternary owns(const void[] b);
-
Queries whether
bhas been allocated with this region.- Parameters:
-
void[] bArbitrary block of memory ( nullis allowed;owns(null)returnsfalse).
- Returns:
trueifbhas been allocated with this region,falseotherwise.
- const pure nothrow @nogc @safe Ternary empty();
-
Returns
Ternary.yesif no memory has been allocated in this region,Ternary.nootherwise. (Never returnsTernary.unknown.) - const pure nothrow @nogc @safe size_t available();
-
Nonstandard property that returns bytes available for allocation.
- struct InSituRegion(size_t size, size_t minAlign = platformAlignment);
-
InSituRegionis a convenient region that carries its storage within itself (in the form of a statically-sized array).The first template argument is the size of the region and the second is the needed alignment. Depending on the alignment requested and platform details, the actual available storage may be smaller than the compile-time parameter. To make sure that at least
nbytes are available in the region, useInSituRegion!(n + a - 1, a).
Given that the most frequent use ofInSituRegionis as a stack allocator, it allocates starting at the end on systems where stack grows downwards, such that hot memory is used first.- Examples:
-
// 128KB region, allocated to x86's cache line InSituRegion!(128 * 1024, 16) r1; auto a1 = r1.allocate(101); writeln(a1.length); // 101 // 128KB region, with fallback to the garbage collector. import std.experimental.allocator.building_blocks.fallback_allocator : FallbackAllocator; import std.experimental.allocator.building_blocks.free_list : FreeList; import std.experimental.allocator.building_blocks.bitmapped_block : BitmappedBlock; import std.experimental.allocator.gc_allocator : GCAllocator; FallbackAllocator!(InSituRegion!(128 * 1024), GCAllocator) r2; const a2 = r2.allocate(102); writeln(a2.length); // 102 // Reap with GC fallback. InSituRegion!(128 * 1024, 8) tmp3; FallbackAllocator!(BitmappedBlock!(64, 8), GCAllocator) r3; r3.primary = BitmappedBlock!(64, 8)(cast(ubyte[]) (tmp3.allocateAll())); const a3 = r3.allocate(103); writeln(a3.length); // 103 // Reap/GC with a freelist for small objects up to 16 bytes. InSituRegion!(128 * 1024, 64) tmp4; FreeList!(FallbackAllocator!(BitmappedBlock!(64, 64), GCAllocator), 0, 16) r4; r4.parent.primary = BitmappedBlock!(64, 64)(cast(ubyte[]) (tmp4.allocateAll())); const a4 = r4.allocate(104); writeln(a4.length); // 104
- alias alignment = minAlign;
-
An alias for
minAlign, which must be a valid alignment (nonzero power of 2). The start of the region and all allocation requests will be rounded up to a multiple of the alignment.InSituRegion!(4096) a1; assert(a1.alignment == platformAlignment); InSituRegion!(4096, 64) a2; assert(a2.alignment == 64); - void[] allocate(size_t n);
-
Allocates
bytesand returns them, ornullif the region cannot accommodate the request. For efficiency reasons, ifbytes == 0the function returns an empty non-null slice. - void[] alignedAllocate(size_t n, uint a);
-
As above, but the memory allocated is aligned at
abytes. - bool deallocate(void[] b);
-
Deallocates
b. This works only ifbwas obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing. This semantics is tricky and thereforedeallocateis defined only ifRegionis instantiated withYes.defineDeallocateas the third template argument.- Parameters:
-
void[] bBlock previously obtained by a call to allocateagainst this allocator (nullis allowed).
- pure nothrow @nogc @safe Ternary owns(const void[] b);
-
Returns
Ternary.yesifbis the result of a previous allocation,Ternary.nootherwise. - bool expand(ref void[] b, size_t delta);
-
Expands an allocated block in place. Expansion will succeed only if the block is the last allocated.
- bool deallocateAll();
-
Deallocates all memory allocated with this allocator.
- void[] allocateAll();
-
Allocates all memory available with this allocator.
- size_t available();
-
Nonstandard function that returns the bytes available for allocation.
- struct SbrkRegion(uint minAlign = platformAlignment);
-
Allocator backed by
sbrkfor Posix systems. Due to the fact thatsbrkis not thread-safe by design,SbrkRegionuses a mutex internally. This implies that uncontrolled calls tobrkandsbrkmay affect the workings ofSbrkRegionadversely.- static shared SbrkRegion instance;
-
Instance shared by all callers.
- enum uint alignment;
-
Standard allocator primitives.
-
shared const pure nothrow @nogc @safe size_t goodAllocSize(size_t n);
shared nothrow @nogc @trusted void[] allocate(size_t bytes);
shared nothrow @nogc @trusted void[] alignedAllocate(size_t bytes, uint a); -
Rounds the given size to a multiple of thew
alignment -
shared nothrow @nogc @trusted bool expand(ref void[] b, size_t delta);
shared pure nothrow @nogc @trusted Ternary owns(const void[] b); -
The
expandmethod may only succeed if the argument is the last block allocated. In that case,expandattempts to push the break pointer to the right. - shared nothrow @nogc bool deallocate(void[] b);
-
The
deallocatemethod only works (and returnstrue) on systems that support reducing the break address (i.e. accept calls tosbrkwith negative offsets). OSX does not accept such. In addition the argument must be the last block allocated. - shared nothrow @nogc bool deallocateAll();
-
The
deallocateAllmethod only works (and returnstrue) on systems that support reducing the break address (i.e. accept calls tosbrkwith negative offsets). OSX does not accept such. - shared pure nothrow @nogc @safe Ternary empty();
-
Standard allocator API.
-
The threadsafe version of the
Regionallocator. Allocations and deallocations are lock-free based usingcore.atomic.cas.-
The parent allocator. Depending on whether
ParentAllocatorholds state or not, this is a member variable or an alias forParentAllocator.instance. -
Constructs a region backed by a user-provided store. Assumes the memory was allocated with
ParentAllocator(if different fromNullAllocator).- Parameters:
-
ubyte[] storeUser-provided store backing up the region. If ParentAllocatoris different fromNullAllocator, memory is assumed to have been allocated withParentAllocator.size_t nBytes to allocate using ParentAllocator. This constructor is only defined IfParentAllocatoris different fromNullAllocator. Ifparent.allocate(n)returnsnull, the region will be initialized as empty (correctly initialized but unable to allocate).
-
Rounds the given size to a multiple of the
alignment -
Alignment offered.
-
Allocates
nbytes of memory. The allocation is served by atomically incrementing a pointer which keeps track of the current used space.- Parameters:
-
size_t nnumber of bytes to allocate
- Returns:
-
A properly-aligned buffer of size
n, ornullif request could not be satisfied.
-
Deallocates
b. This works only ifbwas obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing.- Parameters:
-
void[] bBlock previously obtained by a call to allocateagainst this allocator (nullis allowed).
-
Deallocates all memory allocated by this region, which can be subsequently reused for new allocations.
-
Allocates
nbytes of memory aligned at alignmenta.- Parameters:
-
size_t nnumber of bytes to allocate uint aalignment for the allocated block
- Returns:
-
Either a suitable block of
nbytes aligned ata, ornull.
-
Queries whether
bhas been allocated with this region.- Parameters:
-
void[] bArbitrary block of memory ( nullis allowed;owns(null)returnsfalse).
- Returns:
trueifbhas been allocated with this region,falseotherwise.
-
Returns
Ternary.yesif no memory has been allocated in this region,Ternary.nootherwise. (Never returnsTernary.unknown.)
-
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_experimental_allocator_building_blocks_region.html