The OpenD Programming Language

SharedAlignedBlockList

SharedAlignedBlockList is the threadsafe version of AlignedBlockList. The Allocator template parameter must refer a shared allocator. Also, ParentAllocator must be a shared allocator, supporting alignedAllocate.

Members

Functions

allocate
void[] allocate(size_t n)

Returns a chunk of memory of size n It finds the first node in the AlignedBlockNode list which has available memory, and moves it to the front of the list.

deallocate
bool deallocate(void[] b)

Deallocates the buffer b given as parameter. Deallocations take place in constant time, regardless of the number of nodes in the list. b.ptr is rounded down to the nearest multiple of the alignment to quickly find the corresponding AlignedBlockNode.

owns
Ternary owns(void[] b)

Returns Ternary.yes if the buffer belongs to the parent allocator and Ternary.no otherwise.

Parameters

Allocator

the shared allocator which is used to manage each node; it must have a constructor which receives ubyte[] and it must not have any parent allocators, except for the NullAllocator

ParentAllocator

each node draws memory from the parent allocator; it must be shared and support alignedAllocate

theAlignment

alignment of each block and at the same time length of each node

Examples

import std.experimental.allocator.building_blocks.region : SharedBorrowedRegion;
import std.experimental.allocator.building_blocks.ascending_page_allocator : SharedAscendingPageAllocator;
import std.experimental.allocator.building_blocks.null_allocator : NullAllocator;
import core.thread : ThreadGroup;

enum numThreads = 8;
enum size = 2048;
enum maxIter = 10;

/*
In this example we use 'SharedAlignedBlockList' together with
'SharedBorrowedRegion', in order to create a fast, thread-safe allocator.
*/
alias SuperAllocator = SharedAlignedBlockList!(
        SharedBorrowedRegion!(1),
        SharedAscendingPageAllocator,
        4096);

SuperAllocator a;
// The 'SuperAllocator' will draw memory from a 'SharedAscendingPageAllocator'
a.parent = SharedAscendingPageAllocator(4096 * 1024);

// Launch 'numThreads', each performing allocations
void fun()
{
    foreach (i; 0 .. maxIter)
    {
        void[] b = a.allocate(size);
        assert(b.length == size);
    }
}

auto tg = new ThreadGroup;
foreach (i; 0 .. numThreads)
{
    tg.create(&fun);
}
tg.joinAll();

Meta