Skip to content

Commit b925c16

Browse files
thewilsonatordenisa.ciobanu1208
authored and
denisa.ciobanu1208
committed
Make KRR allocate @safe
1 parent 08638dd commit b925c16

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

std/experimental/allocator/building_blocks/kernighan_ritchie.d

Lines changed: 17 additions & 12 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
@@ -139,6 +142,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
139142
return true;
140143
}
141144

145+
@safe
142146
Tuple!(void[], Node*) allocateHere(size_t bytes)
143147
{
144148
assert(bytes >= Node.sizeof);
@@ -152,7 +156,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
152156
if (leftover >= Node.sizeof)
153157
{
154158
// There's room for another node
155-
auto newNode = cast(Node*) ((cast(ubyte*) &this) + bytes);
159+
auto newNode = (() @trusted => cast(Node*) ((cast(ubyte*) &this) + bytes))();
156160
newNode.size = leftover;
157161
newNode.next = next == &this ? newNode : next;
158162
assert(next);
@@ -396,6 +400,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
396400
397401
Returns: A word-aligned buffer of `n` bytes, or `null`.
398402
*/
403+
@safe
399404
void[] allocate(size_t n)
400405
{
401406
if (!n || !root) return null;
@@ -413,7 +418,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
413418
immutable balance = root.size - actualBytes;
414419
if (balance >= Node.sizeof)
415420
{
416-
auto newRoot = cast(Node*) (result + actualBytes);
421+
auto newRoot = (() @trusted => cast(Node*) (result + actualBytes))();
417422
newRoot.next = root.next;
418423
newRoot.size = balance;
419424
root = newRoot;
@@ -423,7 +428,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
423428
root = null;
424429
switchToFreeList;
425430
}
426-
return result[0 .. n];
431+
return (() @trusted => result[0 .. n])();
427432
}
428433

429434
// Not enough memory, switch to freelist mode and fall through
@@ -754,7 +759,7 @@ version (StdUnittest)
754759
n => KRRegion!GCAllocator(max(n * 16, 1024 * 1024)))());
755760
}
756761

757-
@system unittest
762+
@safe unittest
758763
{
759764
import std.experimental.allocator.gc_allocator : GCAllocator;
760765

@@ -763,7 +768,7 @@ version (StdUnittest)
763768
void[][] array;
764769
foreach (i; 1 .. 4)
765770
{
766-
array ~= alloc.allocate(i);
771+
array ~= (() nothrow @safe => alloc.allocate(i))();
767772
assert(array[$ - 1].length == i);
768773
}
769774
() nothrow @nogc { alloc.deallocate(array[1]); }();
@@ -778,7 +783,7 @@ version (StdUnittest)
778783
import std.typecons : Ternary;
779784
auto alloc = KRRegion!()(
780785
cast(ubyte[])(GCAllocator.instance.allocate(1024 * 1024)));
781-
const store = alloc.allocate(KRRegion!().sizeof);
786+
const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion!().sizeof))();
782787
auto p = cast(KRRegion!()* ) store.ptr;
783788
import core.lifetime : emplace;
784789
import core.stdc.string : memcpy;
@@ -791,7 +796,7 @@ version (StdUnittest)
791796
foreach (i; 0 .. array.length)
792797
{
793798
auto length = 100 * i + 1;
794-
array[i] = p.allocate(length);
799+
array[i] = (() pure nothrow @safe @nogc => p.allocate(length))();
795800
assert(array[i].length == length, text(array[i].length));
796801
assert((() pure nothrow @safe @nogc => p.owns(array[i]))() == Ternary.yes);
797802
}
@@ -820,7 +825,7 @@ version (StdUnittest)
820825
assert(p.length == 1024 * 1024);
821826
}
822827

823-
@system unittest
828+
@safe unittest
824829
{
825830
import std.random : randomCover;
826831
import std.typecons : Ternary;
@@ -846,7 +851,7 @@ version (StdUnittest)
846851

847852
foreach (size; sizes)
848853
{
849-
bufs ~= a.allocate(size);
854+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))();
850855
}
851856

852857
foreach (b; bufs.randomCover)
@@ -861,7 +866,7 @@ version (StdUnittest)
861866
test(sizes32);
862867
}
863868

864-
@system unittest
869+
@safe unittest
865870
{
866871
import std.typecons : Ternary;
867872

@@ -886,11 +891,11 @@ version (StdUnittest)
886891

887892
foreach (size; sizes)
888893
{
889-
bufs ~= a.allocate(size);
894+
bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))();
890895
}
891896

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

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

0 commit comments

Comments
 (0)