View source code
Display the source code in std/experimental/allocator/typed.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.typed.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.
struct TypedAllocator(PrimaryAllocator, Policies...)
;
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 combination | Description |
|---|---|
| `AllocFlag.threadLocal | AllocFlag.hasNoIndirections | AllocFlag.fixedSize` | This 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.hasNoIndirections` | As 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.threadLocal` | As above, but may embed indirections. Examples of types fitting this
description are int*[], Object[], Tuple!(int, string)[]. |
| `AllocFlag.immutableShared | AllocFlag.hasNoIndirections | AllocFlag.fixedSize` | The type being allocated is immutable and has no pointers. The thread that
allocated it must also deallocate it. Example: immutable(int). |
| `AllocFlag.immutableShared | AllocFlag.hasNoIndirections` | As above, but the type may be appended to in the future. Example: string. |
| `AllocFlag.immutableShared` | As above, but the type may embed references. Example: immutable(Object)[]. |
| `AllocFlag.hasNoIndirections | AllocFlag.fixedSize` | The type being allocated may be shared across threads, embeds no indirections, and has fixed size. |
| `AllocFlag.hasNoIndirections` | The type being allocated may be shared across threads, may embed indirections, and has variable size. |
| `AllocFlag.fixedSize` | The type being allocated may be shared across threads, may embed indirections, and has fixed size. |
| `0` | The most conservative/general allocation: memory may be shared, deallocated in a different thread, may or may not be resized, and may embed references. |
Methods
| Name | Description |
|---|---|
allocatorFor
()
|
Given flags as a combination of AllocFlag values, or a type T, returns
the allocator that's a closest fit in capabilities.
|
dispose
(p)
|
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
(array, delta)
|
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
(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
(length)
|
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
(arr, delta)
|
Shrinks an array by delta elements using allocatorFor!(T[]).
|
type2flags
()
|
Given a type T, returns its allocation-related flags as a combination of
AllocFlag values.
|
Parameters
| Name | Description |
|---|---|
| PrimaryAllocator | The default allocator. |
| Policies | Zero or more pairs consisting of an AllocFlag and an allocator
type. |
Example
import std .experimental .allocator .gc_allocator : GCAllocator;
import std .experimental .allocator .mallocator : Mallocator;
import std .experimental .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) == shared const(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));
Authors
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.