View source code
Display the source code in std/experimental/allocator/package.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.ThreadLocal
Stores an allocator object in thread-local storage (i.e. non-shared
D
global). ThreadLocal!A
is a subtype of A
so it appears to implement
A
's allocator primitives.
struct ThreadLocal(A)
;
A
must hold state, otherwise ThreadLocal!A
refuses instantiation. This
means e.g. ThreadLocal!Mallocator
does not work because Mallocator
's
state is not stored as members of Mallocator
, but instead is hidden in the
C library implementation.
Constructors
Name | Description |
---|---|
this
()
|
ThreadLocal disables all constructors. The intended usage is
ThreadLocal!A .
|
Fields
Name | Type | Description |
---|---|---|
instance
|
A | The allocator instance. |
Example
import std .experimental .allocator .building_blocks .free_list : FreeList;
import std .experimental .allocator .gc_allocator : GCAllocator;
import std .experimental .allocator .mallocator : Mallocator;
static assert(!is(ThreadLocal!Mallocator));
static assert(!is(ThreadLocal!GCAllocator));
alias Allocator = ThreadLocal!(FreeList!(GCAllocator, 0, 8));
auto b = Allocator .instance .allocate(5);
static assert(__traits(hasMember, Allocator, "allocate"));
Example
import std .experimental .allocator .building_blocks .allocator_list : AllocatorList;
import std .experimental .allocator .building_blocks .bitmapped_block : BitmappedBlock;
import std .experimental .allocator .building_blocks .segregator : Segregator;
import std .experimental .allocator .building_blocks .bucketizer : Bucketizer;
import std .experimental .allocator .building_blocks .free_list : FreeList;
import std .experimental .allocator .gc_allocator : GCAllocator;
/// Define an allocator bound to the built-in GC.
auto alloc = allocatorObject(GCAllocator .instance);
auto b = alloc .allocate(42);
writeln(b .length); // 42
assert(alloc .deallocate(b));
import std .algorithm .comparison : max;
// Define an elaborate allocator and bind it to the class API.
alias FList = FreeList!(GCAllocator, 0, unbounded);
alias A = ThreadLocal!(
Segregator!(
8, FreeList!(GCAllocator, 0, 8),
128, Bucketizer!(FList, 1, 128, 16),
256, Bucketizer!(FList, 129, 256, 32),
512, Bucketizer!(FList, 257, 512, 64),
1024, Bucketizer!(FList, 513, 1024, 128),
2048, Bucketizer!(FList, 1025, 2048, 256),
3584, Bucketizer!(FList, 2049, 3584, 512),
4072 * 1024, AllocatorList!(
(n) => BitmappedBlock!(4096)(cast(ubyte[]) GCAllocator .instance .allocate(
max(n, 4072 * 1024)))),
GCAllocator
)
);
auto alloc2 = allocatorObject(A .instance);
b = alloc2 .allocate(101);
assert(alloc2 .deallocate(b));
Authors
License
Copyright © 1999-2024 by the D Language Foundation | Page generated by ddox.