std.experimental.allocator.building_blocks.region
Types 5
A Region allocator allocates memory straight from one contiguous chunk. There is no deallocation, and once the region is full, allocation requests return null. 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.
Region deallocates the chunk of memory during destruction.
The minAlign parameter establishes alignment. If minAlign > 1, the sizes of all allocation requests are rounded up to a multiple of minAlign. Applications aiming at maximum speed may want to choose minAlign = 1 and control alignment externally.
private BorrowedRegion!(minAlign, growDownwards) _implsize_t goodAllocSize(size_t n) const pure nothrow @safe @nogcRounds the given size to a multiple of the `alignment`void[] allocate(size_t n) pure nothrow @trusted @nogcAllocates `n` bytes of memory. The shortest path involves an alignment adjustment (if alignment > 1), an increment, and a comparison.void[] alignedAllocate(size_t n, uint a) pure nothrow @trusted @nogcAllocates `n` bytes of memory aligned at alignment `a`.void[] allocateAll() pure nothrow @trusted @nogcAllocates and returns all memory available to this region.bool deallocate(void[] b) pure nothrow @nogcDeallocates `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.bool deallocateAll() pure nothrow @nogcDeallocates all memory allocated by this region, which can be subsequently reused for new allocations.Ternary owns(const void[] b) const pure nothrow @trusted @nogcQueries whether `b` has been allocated with this region.Ternary empty() const pure nothrow @safe @nogcReturns `Ternary.yes` if no memory has been allocated in this region, `Ternary.no` otherwise. (Never returns `Ternary.unknown`.)size_t available() const @safe pure nothrow @nogcNonstandard property that returns bytes available for allocation.this(ubyte[] store)Constructs a region backed by a user-provided store. Assumes the memory was allocated with `ParentAllocator`.A BorrowedRegion allocates directly from a user-provided block of memory.
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.
private void * _currentsize_t goodAllocSize(size_t n) const pure nothrow @safe @nogcRounds the given size to a multiple of the `alignment`void[] allocate(size_t n) pure nothrow @trusted @nogcAllocates `n` bytes of memory. The shortest path involves an alignment adjustment (if alignment > 1), an increment, and a comparison.void[] alignedAllocate(size_t n, uint a) pure nothrow @trusted @nogcAllocates `n` bytes of memory aligned at alignment `a`.void[] allocateAll() pure nothrow @trusted @nogcAllocates and returns all memory available to this region.bool deallocate(void[] b) pure nothrow @nogcDeallocates `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.bool deallocateAll() pure nothrow @nogcDeallocates all memory allocated by this region, which can be subsequently reused for new allocations.Ternary owns(const void[] b) const pure nothrow @trusted @nogcQueries whether `b` has been allocated with this region.Ternary empty() const pure nothrow @safe @nogcReturns `Ternary.yes` if no memory has been allocated in this region, `Ternary.no` otherwise. (Never returns `Ternary.unknown`.)size_t available() const @safe pure nothrow @nogcNonstandard property that returns bytes available for allocation.this(ubyte[] store)Constructs a region backed by a user-provided store.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.
private BorrowedRegion!(minAlign, growDownwards) _implvoid lazyInit()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.void[] alignedAllocate(size_t n, uint a)As above, but the memory allocated is aligned at `a` bytes.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 `...Ternary owns(const void[] b) pure nothrow @safe @nogcReturns `Ternary.yes` if `b` is the result of a previous allocation, `Ternary.no` otherwise.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.The threadsafe version of the Region allocator. Allocations and deallocations are lock-free based using cas.
private SharedBorrowedRegion!(minAlign, growDownwards) _implsize_t goodAllocSize(size_t n) const pure nothrow @safe @nogcRounds the given size to a multiple of the `alignment`void[] allocate(size_t n) pure nothrow @trusted @nogcAllocates `n` bytes of memory. The allocation is served by atomically incrementing a pointer which keeps track of the current used space.bool deallocate(void[] b) pure nothrow @nogcDeallocates `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.bool deallocateAll() pure nothrow @nogcDeallocates all memory allocated by this region, which can be subsequently reused for new allocations.void[] alignedAllocate(size_t n, uint a) pure nothrow @trusted @nogcAllocates `n` bytes of memory aligned at alignment `a`. Params: n = number of bytes to allocate a = alignment for the allocated blockA SharedBorrowedRegion allocates directly from a user-provided block of memory.
Unlike a SharedRegion, a SharedBorrowedRegion 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 SharedBorrowedRegion behaves exactly like a SharedRegion.
private void * _currentsize_t goodAllocSize(size_t n) shared const pure nothrow @safe @nogcRounds the given size to a multiple of the `alignment`void[] allocate(size_t n) shared pure nothrow @trusted @nogcAllocates `n` bytes of memory. The allocation is served by atomically incrementing a pointer which keeps track of the current used space.void[] alignedAllocate(size_t n, uint a) shared pure nothrow @trusted @nogcAllocates `n` bytes of memory aligned at alignment `a`.bool deallocate(void[] b) shared pure nothrow @nogcDeallocates `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.bool deallocateAll() shared pure nothrow @nogcDeallocates all memory allocated by this region, which can be subsequently reused for new allocations.this(ubyte[] store)Constructs a region backed by a user-provided store.