Skip to content

Commit

Permalink
Add AllocatorState (#8596)
Browse files Browse the repository at this point in the history
  • Loading branch information
nordlow authored Oct 27, 2024
1 parent dc4c187 commit 5e311c3
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions std/experimental/allocator/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,39 @@ unittest
static assert(stateSize!C3 == 2 * size_t.sizeof + char.sizeof);
}

/**
State of an allocator `A`.
`AllocatorState!(A).sizeof` is zero for `A` being `NullAllocator`, `Mallocator`,
`GCAllocator`, and `MMapAllocator` and typically non-zero for the other.
*/
mixin template AllocatorState(A)
if (isAllocator!A)
{
static if (stateSize!A == 0)
alias allocator = A.instance;
else
A allocator;
}

///
@safe @nogc nothrow pure
unittest
{
import std.experimental.allocator.building_blocks.null_allocator : NullAllocator;
import std.experimental.allocator.mallocator : Mallocator;
import std.experimental.allocator.gc_allocator : GCAllocator;
import std.experimental.allocator.mmap_allocator : MmapAllocator;
struct S
{
mixin AllocatorState!NullAllocator n;
mixin AllocatorState!GCAllocator g;
mixin AllocatorState!Mallocator m;
mixin AllocatorState!MmapAllocator p;
}
static assert(S.sizeof == 1);
}

/**
Returns `true` if the `Allocator` has the alignment known at compile time;
otherwise it returns `false`.
Expand Down

0 comments on commit 5e311c3

Please sign in to comment.