Skip to content

Commit 3b4e48c

Browse files
edi33416wilzbach
authored andcommitted
Make KRR allocate @safe
1 parent 25edf91 commit 3b4e48c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

std/experimental/allocator/building_blocks/kernighan_ritchie.d

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,21 @@ struct KRRegion(ParentAllocator = NullAllocator)
111111

112112
this(this) @disable;
113113

114+
pure nothrow @trusted @nogc
114115
void[] payload() inout
115116
{
116117
return (cast(ubyte*) &this)[0 .. size];
117118
}
118119

120+
pure nothrow @trusted @nogc
119121
bool adjacent(in Node* right) const
120122
{
121123
assert(right);
122124
auto p = payload;
123125
return p.ptr < right && right < p.ptr + p.length + Node.sizeof;
124126
}
125127

128+
pure nothrow @trusted @nogc
126129
bool coalesce(void* memoryEnd = null)
127130
{
128131
// Coalesce the last node before the memory end with any possible gap
@@ -152,7 +155,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
152155
if (leftover >= Node.sizeof)
153156
{
154157
// There's room for another node
155-
auto newNode = cast(Node*) ((cast(ubyte*) &this) + bytes);
158+
auto newNode = (() @trusted => cast(Node*) ((cast(ubyte*) &this) + bytes))();
156159
newNode.size = leftover;
157160
newNode.next = next == &this ? newNode : next;
158161
assert(next);
@@ -404,7 +407,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
404407
immutable balance = root.size - actualBytes;
405408
if (balance >= Node.sizeof)
406409
{
407-
auto newRoot = cast(Node*) (result + actualBytes);
410+
auto newRoot = (() @trusted => cast(Node*) (result + actualBytes))();
408411
newRoot.next = root.next;
409412
newRoot.size = balance;
410413
root = newRoot;
@@ -414,7 +417,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
414417
root = null;
415418
switchToFreeList;
416419
}
417-
return result[0 .. n];
420+
return (() @trusted => result[0 .. n])();
418421
}
419422

420423
// Not enough memory, switch to freelist mode and fall through
@@ -757,7 +760,7 @@ it actually returns memory to the operating system when possible.
757760
void[][] array;
758761
foreach (i; 1 .. 4)
759762
{
760-
array ~= alloc.allocate(i);
763+
array ~= (() nothrow @safe => alloc.allocate(i))();
761764
assert(array[$ - 1].length == i);
762765
}
763766
() nothrow @nogc { alloc.deallocate(array[1]); }();
@@ -772,7 +775,7 @@ it actually returns memory to the operating system when possible.
772775
import std.typecons : Ternary;
773776
auto alloc = KRRegion!()(
774777
cast(ubyte[])(GCAllocator.instance.allocate(1024 * 1024)));
775-
const store = alloc.allocate(KRRegion!().sizeof);
778+
const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion!().sizeof))();
776779
auto p = cast(KRRegion!()* ) store.ptr;
777780
import core.stdc.string : memcpy;
778781
import std.algorithm.mutation : move;
@@ -785,7 +788,7 @@ it actually returns memory to the operating system when possible.
785788
foreach (i; 0 .. array.length)
786789
{
787790
auto length = 100 * i + 1;
788-
array[i] = p.allocate(length);
791+
array[i] = (() pure nothrow @safe @nogc => p.allocate(length))();
789792
assert(array[i].length == length, text(array[i].length));
790793
assert((() pure nothrow @safe @nogc => p.owns(array[i]))() == Ternary.yes);
791794
}
@@ -841,7 +844,7 @@ it actually returns memory to the operating system when possible.
841844

842845
foreach (size; sizes)
843846
{
844-
bufs ~= a.allocate(size);
847+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))();
845848
}
846849

847850
foreach (b; bufs.randomCover)
@@ -883,11 +886,11 @@ it actually returns memory to the operating system when possible.
883886

884887
foreach (size; sizes)
885888
{
886-
bufs ~= a.allocate(size);
889+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))();
887890
}
888891

889892
() nothrow @nogc { a.deallocate(bufs[1]); }();
890-
bufs ~= a.allocate(sizes[1] - word);
893+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(sizes[1] - word))();
891894

892895
() nothrow @nogc { a.deallocate(bufs[0]); }();
893896
foreach (i; 2 .. bufs.length)

0 commit comments

Comments
 (0)