TypedAllocator

TypedAllocator acts like a chassis on which several specialized allocators can be assembled. To let the system make a choice about a particular kind of allocation, use Default for the respective parameters.

There is a hierarchy of allocation kinds. When an allocator is implemented for a given combination of flags, it is used. Otherwise, the next down the list is chosen.

AllocFlag combinationDescription
AllocFlag.threadLocal | AllocFlag.hasNoIndirections | AllocFlag.fixedSizeThis is the most specific allocation policy: the memory being allocated is thread local, has no indirections at all, and will not be reallocated. Examples of types fitting this description: int, double, Tuple!(int, long), but not Tuple!(int, string), which contains an indirection.
AllocFlag.threadLocal | AllocFlag.hasNoIndirectionsAs above, but may be reallocated later. Examples of types fitting this description are int[], double[], Tuple!(int, long)[], but not Tuple!(int, string)[], which contains an indirection.
AllocFlag.threadLocalAs above, but may embed indirections. Examples of types fitting this description are int*[], Object[], Tuple!(int, string)[].
AllocFlag.immutableShared | AllocFlag.hasNoIndirections | AllocFlag.fixedSizeThe type being allocated is immutable and has no pointers. The thread that allocated it must also deallocate it. Example: immutable(int).
AllocFlag.immutableShared | AllocFlag.hasNoIndirectionsAs above, but the type may be appended to in the future. Example: string.
AllocFlag.immutableSharedAs above, but the type may embed references. Example: immutable(Object)[].
AllocFlag.hasNoIndirections | AllocFlag.fixedSizeThe type being allocated may be shared across threads, embeds no indirections, and has fixed size.
AllocFlag.hasNoIndirectionsThe type being allocated may be shared across threads, may embed indirections, and has variable size.
AllocFlag.fixedSizeThe type being allocated may be shared across threads, may embed indirections, and has fixed size.
0The most conservative/general allocation: memory may be shared, deallocated in a different thread, may or may not be resized, and may embed references.

Members

Aliases

primary
alias primary = PrimaryAllocator.instance
Undocumented in source.

Functions

allocatorFor
auto ref allocatorFor()

Given flags as a combination of AllocFlag values, or a type T, returns the allocator that's a closest fit in capabilities.

dispose
void dispose(T* p)
void dispose(T p)
void dispose(T[] array)

Destroys and then deallocates (using allocatorFor!T) the object pointed to by a pointer, the class object referred to by a class or interface reference, or an entire array. It is assumed the respective entities had been allocated with the same allocator.

expandArray
bool expandArray(T[] array, size_t delta)
bool expandArray(T[] array, size_t delta, T init)
bool expandArray(T[] array, R range)

Grows array by appending delta more elements. The needed memory is allocated using the same allocator that was used for the array type. The extra elements added are either default-initialized, filled with copies of init, or initialized with values fetched from range.

make
auto make(A args)

Dynamically allocates (using the appropriate allocator chosen with allocatorFor!T) and then creates in the memory allocated an object of type T, using args (if any) for its initialization. Initialization occurs in the memory allocated and is otherwise semantically the same as T(args). (Note that using make!(T[]) creates a pointer to an (empty) array of Ts, not an array. To allocate and initialize an array, use makeArray!T described below.)

makeArray
T[] makeArray(size_t length)
T[] makeArray(size_t length, T init)
T[] makeArray(R range)

Create an array of T with length elements. The array is either default-initialized, filled with copies of init, or initialized with values fetched from range.

shrinkArray
bool shrinkArray(T[] arr, size_t delta)

Shrinks an array by delta elements using allocatorFor!(T[]).

Static functions

type2flags
uint type2flags()

Given a type T, returns its allocation-related flags as a combination of AllocFlag values.

Parameters

PrimaryAllocator

The default allocator.

Policies

Zero or more pairs consisting of an AllocFlag and an allocator type.

Examples

import stdx.allocator.gc_allocator : GCAllocator;
import stdx.allocator.mallocator : Mallocator;
import stdx.allocator.mmap_allocator : MmapAllocator;
alias MyAllocator = TypedAllocator!(GCAllocator,
    AllocFlag.fixedSize | AllocFlag.threadLocal, Mallocator,
    AllocFlag.fixedSize | AllocFlag.threadLocal
            | AllocFlag.hasNoIndirections,
        MmapAllocator,
);
MyAllocator a;
auto b = a.allocatorFor!0();
static assert(is(typeof(b) == GCAllocator));
enum f1 = AllocFlag.fixedSize | AllocFlag.threadLocal;
auto c = a.allocatorFor!f1();
static assert(is(typeof(c) == Mallocator));
enum f2 = AllocFlag.fixedSize | AllocFlag.threadLocal;
static assert(is(typeof(a.allocatorFor!f2()) == Mallocator));
// Partial match
enum f3 = AllocFlag.threadLocal;
static assert(is(typeof(a.allocatorFor!f3()) == Mallocator));

int* p = a.make!int;
scope(exit) a.dispose(p);
int[] arr = a.makeArray!int(42);
scope(exit) a.dispose(arr);
assert(a.expandArray(arr, 3));
assert(a.shrinkArray(arr, 4));

Meta