diff --git a/std/experimental/allocator/building_blocks/kernighan_ritchie.d b/std/experimental/allocator/building_blocks/kernighan_ritchie.d index 328497f44d3..77c8e31e8f7 100644 --- a/std/experimental/allocator/building_blocks/kernighan_ritchie.d +++ b/std/experimental/allocator/building_blocks/kernighan_ritchie.d @@ -3,8 +3,7 @@ Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/kernighan_ritchie.d) */ module std.experimental.allocator.building_blocks.kernighan_ritchie; -import std.experimental.allocator.building_blocks.null_allocator : - NullAllocator; +import std.experimental.allocator.building_blocks.null_allocator; //debug = KRRegion; debug(KRRegion) import std.stdio; @@ -362,7 +361,7 @@ struct KRRegion(ParentAllocator = NullAllocator) /// Ditto static if (!is(ParentAllocator == NullAllocator) && hasMember!(ParentAllocator, "deallocate")) - ~this() + @trusted ~this() { parent.deallocate(payload); } @@ -652,7 +651,7 @@ fronting the GC allocator. import std.experimental.allocator.gc_allocator : GCAllocator; import std.typecons : Ternary; // KRRegion fronting a general-purpose allocator - align(KRRegion!().alignment) ubyte[1024 * 128] buf; + ubyte[1024 * 128] buf; auto alloc = fallbackAllocator(KRRegion!()(buf), GCAllocator.instance); auto b = alloc.allocate(100); assert(b.length == 100); @@ -674,6 +673,7 @@ it actually returns memory to the operating system when possible. import std.algorithm.comparison : max; import std.experimental.allocator.building_blocks.allocator_list : AllocatorList; + import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.mmap_allocator : MmapAllocator; AllocatorList!(n => KRRegion!MmapAllocator(max(n * 16, 1024 * 1024))) alloc; } @@ -683,6 +683,7 @@ it actually returns memory to the operating system when possible. import std.algorithm.comparison : max; import std.experimental.allocator.building_blocks.allocator_list : AllocatorList; + import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.mallocator : Mallocator; import std.typecons : Ternary; /* @@ -715,6 +716,7 @@ it actually returns memory to the operating system when possible. import std.algorithm.comparison : max; import std.experimental.allocator.building_blocks.allocator_list : AllocatorList; + import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.mmap_allocator : MmapAllocator; import std.typecons : Ternary; /* @@ -747,7 +749,6 @@ it actually returns memory to the operating system when possible. } } -version (StdUnittest) @system unittest { import std.algorithm.comparison : max; @@ -759,12 +760,13 @@ version (StdUnittest) n => KRRegion!GCAllocator(max(n * 16, 1024 * 1024)))()); } -@safe unittest +@trusted unittest { import std.experimental.allocator.gc_allocator : GCAllocator; - auto alloc = KRRegion!GCAllocator(1024 * 1024); + + void[][] array; foreach (i; 1 .. 4) { @@ -785,9 +787,9 @@ version (StdUnittest) cast(ubyte[])(GCAllocator.instance.allocate(1024 * 1024))); 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; - import std.conv : text; + import std.algorithm.mutation : move; + import std.conv : text, emplace; memcpy(p, &alloc, alloc.sizeof); emplace(&alloc); @@ -825,14 +827,16 @@ version (StdUnittest) assert(p.length == 1024 * 1024); } + @safe unittest { - import std.random : randomCover; + import std.experimental.allocator.building_blocks; + import std.random; import std.typecons : Ternary; // Both sequences must work on either system - // A sequence of allocs which generates the error described in https://issues.dlang.org/show_bug.cgi?id=16564 + // A sequence of allocs which generates the error described in issue 16564 // that is a gap at the end of buf from the perspective of the allocator // for 64 bit systems (leftover balance = 8 bytes < 16) @@ -842,10 +846,10 @@ version (StdUnittest) int[] sizes32 = [81412, 107068, 49892, 23768]; - void test(int[] sizes) + @system void test(int[] sizes) { align(size_t.sizeof) ubyte[256 * 1024] buf; - auto a = KRRegion!()(buf); + auto a = (() @trusted => createAllocator(buf))(); void[][] bufs; @@ -862,12 +866,22 @@ version (StdUnittest) assert((() pure nothrow @safe @nogc => a.empty)() == Ternary.yes); } - test(sizes64); - test(sizes32); + () @trusted { + test(sizes64); + test(sizes32); + }(); } +@safe KRRegion!NullAllocator createAllocator(ubyte[] buf) +{ + return KRRegion!NullAllocator(buf); +} + + @safe unittest { + import std.experimental.allocator.building_blocks; + import std.random; import std.typecons : Ternary; // For 64 bits, we allocate in multiples of 8, but the minimum alloc size is 16. @@ -921,7 +935,7 @@ version (StdUnittest) @system unittest { import std.typecons : Ternary; - align(KRRegion!().alignment) ubyte[1024] b; + ubyte[1024] b; auto alloc = KRRegion!()(b); auto k = alloc.allocate(128); @@ -941,4 +955,4 @@ version (StdUnittest) assert(alloc.empty == Ternary.no); assert(alloc.deallocate(k)); assert(alloc.empty == Ternary.yes); -} +} \ No newline at end of file