InSituRegion

InSituRegion is 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 n bytes are available in the region, use InSituRegion!(n + a - 1, a).

Given that the most frequent use of InSituRegion is as a stack allocator, it allocates starting at the end on systems where stack grows downwards, such that hot memory is used first.

Postblit

this(this)
this(this)
Undocumented in source.

Members

Aliases

alignment
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.

Functions

alignedAllocate
void[] alignedAllocate(size_t n, uint a)

As above, but the memory allocated is aligned at a bytes.

allocate
void[] allocate(size_t n)

Allocates bytes and returns them, or null if the region cannot accommodate the request. For efficiency reasons, if bytes == 0 the function returns an empty non-null slice.

allocateAll
void[] allocateAll()

Allocates all memory available with this allocator.

available
size_t available()

Nonstandard function that returns the bytes available for allocation.

deallocate
bool deallocate(void[] b)

Deallocates b. This works only if b was obtained as the last call to allocate; otherwise (i.e. another allocation has occurred since) it does nothing. This semantics is tricky and therefore deallocate is defined only if Region is instantiated with Yes.defineDeallocate as the third template argument.

deallocateAll
bool deallocateAll()

Deallocates all memory allocated with this allocator.

expand
bool expand(void[] b, size_t delta)

Expands an allocated block in place. Expansion will succeed only if the block is the last allocated.

owns
Ternary owns(void[] b)

Returns Ternary.yes if b is the result of a previous allocation, Ternary.no otherwise.

Manifest constants

growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.
growDownwards
enum growDownwards;
Undocumented in source.

Unions

__anonymous
union __anonymous
Undocumented in source.

Examples

// 128KB region, allocated to x86's cache line
InSituRegion!(128 * 1024, 16) r1;
auto a1 = r1.allocate(101);
assert(a1.length == 101);

// 128KB region, with fallback to the garbage collector.
import stdx.allocator.building_blocks.fallback_allocator
    : FallbackAllocator;
import stdx.allocator.building_blocks.free_list
    : FreeList;
import stdx.allocator.building_blocks.bitmapped_block
    : BitmappedBlock;
import stdx.allocator.gc_allocator : GCAllocator;
FallbackAllocator!(InSituRegion!(128 * 1024), GCAllocator) r2;
const a2 = r2.allocate(102);
assert(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);
assert(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);
assert(a4.length == 104);

Meta