From b925c1605f6985adecd28020ba7efb20b53a9eb3 Mon Sep 17 00:00:00 2001 From: Nicholas Lindsay Wilson Date: Sat, 17 Nov 2018 20:16:19 +0800 Subject: [PATCH] Make KRR allocate @safe --- .../building_blocks/kernighan_ritchie.d | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/std/experimental/allocator/building_blocks/kernighan_ritchie.d b/std/experimental/allocator/building_blocks/kernighan_ritchie.d index 167cf1bc6bc..328497f44d3 100644 --- a/std/experimental/allocator/building_blocks/kernighan_ritchie.d +++ b/std/experimental/allocator/building_blocks/kernighan_ritchie.d @@ -111,11 +111,13 @@ struct KRRegion(ParentAllocator = NullAllocator) this(this) @disable; + pure nothrow @trusted @nogc void[] payload() inout { return (cast(ubyte*) &this)[0 .. size]; } + pure nothrow @trusted @nogc bool adjacent(in Node* right) const { assert(right); @@ -123,6 +125,7 @@ struct KRRegion(ParentAllocator = NullAllocator) return p.ptr < right && right < p.ptr + p.length + Node.sizeof; } + pure nothrow @trusted @nogc bool coalesce(void* memoryEnd = null) { // Coalesce the last node before the memory end with any possible gap @@ -139,6 +142,7 @@ struct KRRegion(ParentAllocator = NullAllocator) return true; } + @safe Tuple!(void[], Node*) allocateHere(size_t bytes) { assert(bytes >= Node.sizeof); @@ -152,7 +156,7 @@ struct KRRegion(ParentAllocator = NullAllocator) if (leftover >= Node.sizeof) { // There's room for another node - auto newNode = cast(Node*) ((cast(ubyte*) &this) + bytes); + auto newNode = (() @trusted => cast(Node*) ((cast(ubyte*) &this) + bytes))(); newNode.size = leftover; newNode.next = next == &this ? newNode : next; assert(next); @@ -396,6 +400,7 @@ struct KRRegion(ParentAllocator = NullAllocator) Returns: A word-aligned buffer of `n` bytes, or `null`. */ + @safe void[] allocate(size_t n) { if (!n || !root) return null; @@ -413,7 +418,7 @@ struct KRRegion(ParentAllocator = NullAllocator) immutable balance = root.size - actualBytes; if (balance >= Node.sizeof) { - auto newRoot = cast(Node*) (result + actualBytes); + auto newRoot = (() @trusted => cast(Node*) (result + actualBytes))(); newRoot.next = root.next; newRoot.size = balance; root = newRoot; @@ -423,7 +428,7 @@ struct KRRegion(ParentAllocator = NullAllocator) root = null; switchToFreeList; } - return result[0 .. n]; + return (() @trusted => result[0 .. n])(); } // Not enough memory, switch to freelist mode and fall through @@ -754,7 +759,7 @@ version (StdUnittest) n => KRRegion!GCAllocator(max(n * 16, 1024 * 1024)))()); } -@system unittest +@safe unittest { import std.experimental.allocator.gc_allocator : GCAllocator; @@ -763,7 +768,7 @@ version (StdUnittest) void[][] array; foreach (i; 1 .. 4) { - array ~= alloc.allocate(i); + array ~= (() nothrow @safe => alloc.allocate(i))(); assert(array[$ - 1].length == i); } () nothrow @nogc { alloc.deallocate(array[1]); }(); @@ -778,7 +783,7 @@ version (StdUnittest) import std.typecons : Ternary; auto alloc = KRRegion!()( cast(ubyte[])(GCAllocator.instance.allocate(1024 * 1024))); - const store = alloc.allocate(KRRegion!().sizeof); + const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion!().sizeof))(); auto p = cast(KRRegion!()* ) store.ptr; import core.lifetime : emplace; import core.stdc.string : memcpy; @@ -791,7 +796,7 @@ version (StdUnittest) foreach (i; 0 .. array.length) { auto length = 100 * i + 1; - array[i] = p.allocate(length); + array[i] = (() pure nothrow @safe @nogc => p.allocate(length))(); assert(array[i].length == length, text(array[i].length)); assert((() pure nothrow @safe @nogc => p.owns(array[i]))() == Ternary.yes); } @@ -820,7 +825,7 @@ version (StdUnittest) assert(p.length == 1024 * 1024); } -@system unittest +@safe unittest { import std.random : randomCover; import std.typecons : Ternary; @@ -846,7 +851,7 @@ version (StdUnittest) foreach (size; sizes) { - bufs ~= a.allocate(size); + bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))(); } foreach (b; bufs.randomCover) @@ -861,7 +866,7 @@ version (StdUnittest) test(sizes32); } -@system unittest +@safe unittest { import std.typecons : Ternary; @@ -886,11 +891,11 @@ version (StdUnittest) foreach (size; sizes) { - bufs ~= a.allocate(size); + bufs ~= (() pure nothrow @safe @nogc => a.allocate(size))(); } () nothrow @nogc { a.deallocate(bufs[1]); }(); - bufs ~= a.allocate(sizes[1] - word); + bufs ~= (() pure nothrow @safe @nogc => a.allocate(sizes[1] - word))(); () nothrow @nogc { a.deallocate(bufs[0]); }(); foreach (i; 2 .. bufs.length)