View source code
Display the source code in std/experimental/allocator/building_blocks/region.d from which this page was generated on github.
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 using local clone.

Struct std.experimental.allocator.building_blocks.region.BorrowedRegion

A BorrowedRegion allocates directly from a user-provided block of memory.

struct BorrowedRegion(uint minAlign = platformAlignment, Flag!("growDownwards") growDownwards = No.growDownwards) ;

Unlike a Region, a BorrowedRegion does not own the memory it allocates from and will not deallocate that memory upon destruction. Instead, it is the user's responsibility to ensure that the memory is properly disposed of.

In all other respects, a BorrowedRegion behaves exactly like a Region.


this (store) Constructs a region backed by a user-provided store.


alignedAllocate (n, a) Allocates n bytes of memory aligned at alignment a.
allocate (n) Allocates n bytes of memory. The shortest path involves an alignment adjustment (if alignment > 1), an increment, and a comparison.
allocateAll () Allocates and returns all memory available to this region.
available () Nonstandard property that returns bytes available for allocation.
deallocate (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.
deallocateAll () Deallocates all memory allocated by this region, which can be subsequently reused for new allocations.
empty () Returns Ternary.yes if no memory has been allocated in this region, otherwise. (Never returns Ternary.unknown.)
expand (b, delta) Expands an allocated block in place. Expansion will succeed only if the block is the last allocated. Defined only if growDownwards is No.growDownwards.
goodAllocSize (n) Rounds the given size to a multiple of the alignment
owns (b) Queries whether b has been allocated with this region.


alignment Alignment offered.


import std.typecons : Ternary;

ubyte[1024] store;
auto myRegion = BorrowedRegion!(1)(store[]);

writeln(myRegion.empty); // Ternary.yes
writeln(myRegion.available); // store.length

void[] b = myRegion.allocate(101);

writeln(b.length); // 101
writeln(myRegion.empty); //
writeln(myRegion.owns(b)); // Ternary.yes
writeln(myRegion.available); // store.length - b.length

void[] b2 = myRegion.allocate(256);

// Can only free the most recent allocation
writeln(myRegion.deallocate(b)); // false
writeln(myRegion.deallocate(b2)); // true


writeln(myRegion.empty); // Ternary.yes