Region(ParentAllocator, uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);Region allocator allocates memory straight from one contiguous chunk.There is no deallocation, and once the region is full, allocation requestsreturnnull. Therefore,Regions are often used (a) in conjunction withmore sophisticated allocators; or (b) for batch-style very fast allocationsthat deallocate everything at once.Region deallocates the chunk of memory during destruction.TheminAlign parameter establishes alignment. IfminAlign > 1, thesizes of all allocation requests are rounded up to a multiple ofminAlign.Applications aiming at maximum speed may want to chooseminAlign = 1 andcontrol alignment externally.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.yesauto b = batchAllocator.allocate(101);writeln(b.length);// 101writeln(batchAllocator.empty);// Ternary.no// This will cause a second allocationb = batchAllocator.allocate(2 * 1024 * 1024);writeln(b.length);// 2 * 1024 * 1024// Destructor will free the memory
parent;store);n);parent, size_tn);ubyte[]store | User-provided store backing up the region. Assumed to have been allocated withParentAllocator. |
size_tn | Bytes to allocate usingParentAllocator. Ifparent.allocate(n) returnsnull, the region will be initialized as empty (correctly initialized but unable to allocate). |
goodAllocSize(size_tn) const;alignment = minAlign;allocate(size_tn);n bytes of memory. The shortest path involves an alignment adjustment (ifalignment > 1), an increment, and a comparison.size_tn | number of bytes to allocate |
n ornull if request could not be satisfied.alignedAllocate(size_tn, uinta);n bytes of memory aligned at alignmenta.size_tn | number of bytes to allocate |
uinta | alignment for the allocated block |
n bytes aligned ata, ornull.allocateAll();expand(ref void[]b, size_tdelta);deallocate(void[]b);b. This works only ifb was obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing.void[]b | Block previously obtained by a call toallocate against this allocator (null is allowed). |
deallocateAll();owns(const void[]b) const;b has been allocated with this region.void[]b | Arbitrary block of memory (null is allowed;owns(null) returnsfalse). |
b has been allocated with this region,false otherwise.empty() const;available() const;BorrowedRegion(uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);BorrowedRegion allocates directly from a user-provided block of memory.BorrowedRegion does not own the memory it allocates fromand will not deallocate that memory upon destruction. Instead, it is the user'sresponsibility to ensure that the memory is properly disposed of.In all other respects, aBorrowedRegion behaves exactly like aRegion.import std.typecons : Ternary;ubyte[1024] store;auto myRegion =BorrowedRegion!(1)(store[]);writeln(myRegion.empty);// Ternary.yeswriteln(myRegion.available);// store.lengthvoid[] b = myRegion.allocate(101);writeln(b.length);// 101writeln(myRegion.empty);// Ternary.nowriteln(myRegion.owns(b));// Ternary.yeswriteln(myRegion.available);// store.length - b.lengthvoid[] b2 = myRegion.allocate(256);// Can only free the most recent allocationwriteln(myRegion.deallocate(b));// falsewriteln(myRegion.deallocate(b2));// truemyRegion.deallocateAll();writeln(myRegion.empty);// Ternary.yes
store);ubyte[]store | User-provided store backing up the region. |
goodAllocSize(size_tn) const;alignment = minAlign;allocate(size_tn);n bytes of memory. The shortest path involves an alignment adjustment (ifalignment > 1), an increment, and a comparison.size_tn | number of bytes to allocate |
n ornull if request could not be satisfied.alignedAllocate(size_tn, uinta);n bytes of memory aligned at alignmenta.size_tn | number of bytes to allocate |
uinta | alignment for the allocated block |
n bytes aligned ata, ornull.allocateAll();expand(ref void[]b, size_tdelta);deallocate(void[]b);b. This works only ifb was obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing.void[]b | Block previously obtained by a call toallocate against this allocator (null is allowed). |
deallocateAll();owns(const void[]b) const;b has been allocated with this region.void[]b | Arbitrary block of memory (null is allowed;owns(null) returnsfalse). |
b has been allocated with this region,false otherwise.empty() const;available() const;InSituRegion(size_t size, size_t minAlign = platformAlignment);InSituRegion is a convenient region that carries its storage within itself(in the form of a statically-sized array).InSituRegion is as a stack allocator, itallocates starting at the end on systems where stack grows downwards, such thathot memory is used first.// 128KB region, allocated to x86's cache lineInSituRegion!(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
alignment = minAlign;InSituRegion!(4096) a1;assert(a1.alignment == platformAlignment);InSituRegion!(4096, 64) a2;assert(a2.alignment == 64);
allocate(size_tn);alignedAllocate(size_tn, uinta);a bytes.deallocate(void[]b);b. This works only ifb was obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing. This semantics is tricky and thereforedeallocate is defined only ifRegion is instantiated withYes.defineDeallocate as the third template argument.void[]b | Block previously obtained by a call toallocate against this allocator (null is allowed). |
owns(const void[]b);b is the result of a previous allocation,Ternary.no otherwise.expand(ref void[]b, size_tdelta);deallocateAll();allocateAll();available();SbrkRegion(uint minAlign = platformAlignment);SbrkRegion uses a mutex internally. This impliesthat uncontrolled calls tobrk andsbrk may affect the workings ofSbrkRegion adversely.instance;alignment;goodAllocSize(size_tn) shared const;allocate(size_tbytes) shared;alignedAllocate(size_tbytes, uinta) shared;expand(ref void[]b, size_tdelta) shared;owns(const void[]b) shared;expand method may only succeed if the argument is the last block allocated. In that case,expand attempts to push the break pointer to the right.deallocate(void[]b) shared;deallocate method only works (and returnstrue) on systems that support reducing the break address (i.e. accept calls tosbrk with negative offsets). OSX does not accept such. In addition the argument must be the last block allocated.deallocateAll() shared;deallocateAll method only works (and returnstrue) on systems that support reducing the break address (i.e. accept calls tosbrk with negative offsets). OSX does not accept such.empty() shared;SharedRegion(ParentAllocator, uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);parent;store);n);ubyte[]store | User-provided store backing up the region. Assumed to have been allocated withParentAllocator. |
size_tn | Bytes to allocate usingParentAllocator. Ifparent.allocate(n) returnsnull, the region will be initialized as empty (correctly initialized but unable to allocate). |
goodAllocSize(size_tn) const;alignment = minAlign;allocate(size_tn);n bytes of memory. The allocation is served by atomically incrementing a pointer which keeps track of the current used space.size_tn | number of bytes to allocate |
n, ornull if request could not be satisfied.deallocate(void[]b);b. This works only ifb was obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing.void[]b | Block previously obtained by a call toallocate against this allocator (null is allowed). |
deallocateAll();alignedAllocate(size_tn, uinta);n bytes of memory aligned at alignmenta.size_tn | number of bytes to allocate |
uinta | alignment for the allocated block |
n bytes aligned ata, ornull.owns(const void[]b) const;b has been allocated with this region.void[]b | Arbitrary block of memory (null is allowed;owns(null) returnsfalse). |
b has been allocated with this region,false otherwise.empty() const;SharedBorrowedRegion(uint minAlign = platformAlignment, Flag!"growDownwards" growDownwards = No.growDownwards);SharedBorrowedRegion allocates directly from a user-provided block of memory.SharedBorrowedRegion does not own the memory itallocates from and will not deallocate that memory upon destruction. Instead,it is the user's responsibility to ensure that the memory is properly disposedof.In all other respects, aSharedBorrowedRegion behaves exactly like aSharedRegion.store) shared;ubyte[]store | User-provided store backing up the region. Must not be aliased. |
goodAllocSize(size_tn) shared const;alignment = minAlign;allocate(size_tn) shared;n bytes of memory. The allocation is served by atomically incrementing a pointer which keeps track of the current used space.size_tn | number of bytes to allocate |
n, ornull if request could not be satisfied.alignedAllocate(size_tn, uinta) shared;n bytes of memory aligned at alignmenta.size_tn | number of bytes to allocate |
uinta | alignment for the allocated block |
n bytes aligned ata, ornull.deallocate(void[]b) shared;b. This works only ifb was obtained as the last call toallocate; otherwise (i.e. another allocation has occurred since) it does nothing.void[]b | Block previously obtained by a call toallocate against this allocator (null is allowed). |
deallocateAll() shared;owns(const void[]b) shared const;b has been allocated with this region.void[]b | Arbitrary block of memory (null is allowed;owns(null) returnsfalse). |
b has been allocated with this region,false otherwise.empty() shared const;