Skip to content

Commit 9e81b5a

Browse files
authored
Merge pull request #6406 from edi33416/null_allocator_allocate
Make null_allocator allocate @safe
2 parents 618018e + 3e91474 commit 9e81b5a

File tree

1 file changed

+17
-21
lines changed

1 file changed

+17
-21
lines changed

std/experimental/allocator/building_blocks/null_allocator.d

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ composite allocators.
1212
struct NullAllocator
1313
{
1414
import std.typecons : Ternary;
15+
16+
nothrow @nogc pure @safe:
1517
/**
1618
`NullAllocator` advertises a relatively large _alignment equal to 64 KB.
1719
This is because `NullAllocator` never actually needs to honor this
@@ -33,65 +35,59 @@ struct NullAllocator
3335
Precondition: $(D b is null). This is because there is no other possible
3436
legitimate input.
3537
*/
36-
pure nothrow @safe @nogc
3738
bool expand(ref void[] b, size_t s) shared
3839
{ assert(b is null); return s == 0; }
3940
/// Ditto
40-
pure nothrow @nogc
4141
bool reallocate(ref void[] b, size_t) shared
4242
{ assert(b is null); return false; }
4343
/// Ditto
44-
pure nothrow @nogc
4544
bool alignedReallocate(ref void[] b, size_t, uint) shared
4645
{ assert(b is null); return false; }
4746
/// Returns `Ternary.no`.
48-
pure nothrow @safe @nogc
4947
Ternary owns(const void[]) shared const { return Ternary.no; }
5048
/**
5149
Returns `Ternary.no`.
5250
*/
53-
pure nothrow @safe @nogc
5451
Ternary resolveInternalPointer(const void*, ref void[]) shared const
5552
{ return Ternary.no; }
5653
/**
5754
No-op.
5855
Precondition: $(D b is null)
5956
*/
60-
pure nothrow @nogc
6157
bool deallocate(void[] b) shared { assert(b is null); return true; }
6258
/**
6359
No-op.
6460
*/
65-
pure nothrow @safe @nogc
6661
bool deallocateAll() shared { return true; }
6762
/**
6863
Returns `Ternary.yes`.
6964
*/
70-
pure nothrow @safe @nogc
7165
Ternary empty() shared const { return Ternary.yes; }
7266
/**
7367
Returns the `shared` global instance of the `NullAllocator`.
7468
*/
7569
static shared NullAllocator instance;
7670
}
7771

78-
@system unittest
72+
nothrow @nogc pure @safe unittest
7973
{
80-
assert(NullAllocator.instance.alignedAllocate(100, 0) is null);
81-
assert(NullAllocator.instance.allocateAll() is null);
82-
auto b = NullAllocator.instance.allocate(100);
74+
alias a = NullAllocator.instance;
75+
76+
assert(a.alignedAllocate(100, 0) is null);
77+
assert(a.allocateAll() is null);
78+
auto b = a.allocate(100);
8379
assert(b is null);
84-
assert((() nothrow @safe @nogc => NullAllocator.instance.expand(b, 0))());
85-
assert((() nothrow @safe @nogc => !NullAllocator.instance.expand(b, 42))());
86-
assert((() nothrow @nogc => !NullAllocator.instance.reallocate(b, 42))());
87-
assert((() nothrow @nogc => !NullAllocator.instance.alignedReallocate(b, 42, 0))());
88-
assert((() nothrow @nogc => NullAllocator.instance.deallocate(b))());
89-
assert((() nothrow @nogc => NullAllocator.instance.deallocateAll())());
80+
assert(a.expand(b, 0));
81+
assert(!a.expand(b, 42));
82+
assert(!a.reallocate(b, 42));
83+
assert(!a.alignedReallocate(b, 42, 0));
84+
assert(a.deallocate(b));
85+
assert(a.deallocateAll());
9086

9187
import std.typecons : Ternary;
92-
assert((() nothrow @safe @nogc => NullAllocator.instance.empty)() == Ternary.yes);
93-
assert((() nothrow @safe @nogc => NullAllocator.instance.owns(null))() == Ternary.no);
88+
assert(a.empty == Ternary.yes);
89+
assert(a.owns(null) == Ternary.no);
9490

9591
void[] p;
96-
assert((() nothrow @safe @nogc => NullAllocator.instance.resolveInternalPointer(null, p))() == Ternary.no);
92+
assert(a.resolveInternalPointer(null, p) == Ternary.no);
9793
}

0 commit comments

Comments
 (0)