Skip to content

Commit 5e311c3

Browse files
authored
Add AllocatorState (#8596)
1 parent dc4c187 commit 5e311c3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

std/experimental/allocator/common.d

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,39 @@ unittest
6666
static assert(stateSize!C3 == 2 * size_t.sizeof + char.sizeof);
6767
}
6868

69+
/**
70+
State of an allocator `A`.
71+
72+
`AllocatorState!(A).sizeof` is zero for `A` being `NullAllocator`, `Mallocator`,
73+
`GCAllocator`, and `MMapAllocator` and typically non-zero for the other.
74+
*/
75+
mixin template AllocatorState(A)
76+
if (isAllocator!A)
77+
{
78+
static if (stateSize!A == 0)
79+
alias allocator = A.instance;
80+
else
81+
A allocator;
82+
}
83+
84+
///
85+
@safe @nogc nothrow pure
86+
unittest
87+
{
88+
import std.experimental.allocator.building_blocks.null_allocator : NullAllocator;
89+
import std.experimental.allocator.mallocator : Mallocator;
90+
import std.experimental.allocator.gc_allocator : GCAllocator;
91+
import std.experimental.allocator.mmap_allocator : MmapAllocator;
92+
struct S
93+
{
94+
mixin AllocatorState!NullAllocator n;
95+
mixin AllocatorState!GCAllocator g;
96+
mixin AllocatorState!Mallocator m;
97+
mixin AllocatorState!MmapAllocator p;
98+
}
99+
static assert(S.sizeof == 1);
100+
}
101+
69102
/**
70103
Returns `true` if the `Allocator` has the alignment known at compile time;
71104
otherwise it returns `false`.

0 commit comments

Comments
 (0)