Skip to content

Commit 8c3d974

Browse files
thewilsonatorwilzbach
authored andcommitted
Make KRR allocate @safe
1 parent cbc4afc commit 8c3d974

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Diff for: std/experimental/allocator/building_blocks/kernighan_ritchie.d

+17-12
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,21 @@ struct KRRegion(ParentAllocator = NullAllocator)
110110

111111
this(this) @disable;
112112

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

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

127+
pure nothrow @trusted @nogc
125128
bool coalesce(void* memoryEnd = null)
126129
{
127130
// Coalesce the last node before the memory end with any possible gap
@@ -138,6 +141,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
138141
return true;
139142
}
140143

144+
@safe
141145
Tuple!(void[], Node*) allocateHere(size_t bytes)
142146
{
143147
assert(bytes >= Node.sizeof);
@@ -151,7 +155,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
151155
if (leftover >= Node.sizeof)
152156
{
153157
// There's room for another node
154-
auto newNode = cast(Node*) ((cast(ubyte*) &this) + bytes);
158+
auto newNode = (() @trusted => cast(Node*) ((cast(ubyte*) &this) + bytes))();
155159
newNode.size = leftover;
156160
newNode.next = next == &this ? newNode : next;
157161
assert(next);
@@ -395,6 +399,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
395399
396400
Returns: A word-aligned buffer of `n` bytes, or `null`.
397401
*/
402+
@safe
398403
void[] allocate(size_t n)
399404
{
400405
if (!n || !root) return null;
@@ -412,7 +417,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
412417
immutable balance = root.size - actualBytes;
413418
if (balance >= Node.sizeof)
414419
{
415-
auto newRoot = cast(Node*) (result + actualBytes);
420+
auto newRoot = (() @trusted => cast(Node*) (result + actualBytes))();
416421
newRoot.next = root.next;
417422
newRoot.size = balance;
418423
root = newRoot;
@@ -422,7 +427,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
422427
root = null;
423428
switchToFreeList;
424429
}
425-
return result[0 .. n];
430+
return (() @trusted => result[0 .. n])();
426431
}
427432

428433
// Not enough memory, switch to freelist mode and fall through
@@ -755,7 +760,7 @@ it actually returns memory to the operating system when possible.
755760
n => KRRegion!GCAllocator(max(n * 16, 1024 * 1024)))());
756761
}
757762

758-
@system unittest
763+
@safe unittest
759764
{
760765
import std.experimental.allocator.gc_allocator : GCAllocator;
761766

@@ -764,7 +769,7 @@ it actually returns memory to the operating system when possible.
764769
void[][] array;
765770
foreach (i; 1 .. 4)
766771
{
767-
array ~= alloc.allocate(i);
772+
array ~= (() nothrow @safe => alloc.allocate(i))();
768773
assert(array[$ - 1].length == i);
769774
}
770775
() nothrow @nogc { alloc.deallocate(array[1]); }();
@@ -779,7 +784,7 @@ it actually returns memory to the operating system when possible.
779784
import std.typecons : Ternary;
780785
auto alloc = KRRegion!()(
781786
cast(ubyte[])(GCAllocator.instance.allocate(1024 * 1024)));
782-
const store = alloc.allocate(KRRegion!().sizeof);
787+
const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion!().sizeof))();
783788
auto p = cast(KRRegion!()* ) store.ptr;
784789
import core.stdc.string : memcpy;
785790
import std.algorithm.mutation : move;
@@ -792,7 +797,7 @@ it actually returns memory to the operating system when possible.
792797
foreach (i; 0 .. array.length)
793798
{
794799
auto length = 100 * i + 1;
795-
array[i] = p.allocate(length);
800+
array[i] = (() pure nothrow @safe @nogc => p.allocate(length))();
796801
assert(array[i].length == length, text(array[i].length));
797802
assert((() pure nothrow @safe @nogc => p.owns(array[i]))() == Ternary.yes);
798803
}
@@ -821,7 +826,7 @@ it actually returns memory to the operating system when possible.
821826
assert(p.length == 1024 * 1024);
822827
}
823828

824-
@system unittest
829+
@safe unittest
825830
{
826831
import std.experimental.allocator.building_blocks;
827832
import std.random;
@@ -848,7 +853,7 @@ it actually returns memory to the operating system when possible.
848853

849854
foreach (size; sizes)
850855
{
851-
bufs ~= a.allocate(size);
856+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))();
852857
}
853858

854859
foreach (b; bufs.randomCover)
@@ -863,7 +868,7 @@ it actually returns memory to the operating system when possible.
863868
test(sizes32);
864869
}
865870

866-
@system unittest
871+
@safe unittest
867872
{
868873
import std.experimental.allocator.building_blocks;
869874
import std.random;
@@ -890,11 +895,11 @@ it actually returns memory to the operating system when possible.
890895

891896
foreach (size; sizes)
892897
{
893-
bufs ~= a.allocate(size);
898+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))();
894899
}
895900

896901
() nothrow @nogc { a.deallocate(bufs[1]); }();
897-
bufs ~= a.allocate(sizes[1] - word);
902+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(sizes[1] - word))();
898903

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

0 commit comments

Comments
 (0)