33Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/kernighan_ritchie.d)
44*/
55module std.experimental.allocator.building_blocks.kernighan_ritchie ;
6- import std.experimental.allocator.building_blocks.null_allocator :
7- NullAllocator;
6+ import std.experimental.allocator.building_blocks.null_allocator ;
87
98// debug = KRRegion;
109debug (KRRegion) import std.stdio ;
@@ -362,7 +361,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
362361 // / Ditto
363362 static if (! is (ParentAllocator == NullAllocator)
364363 && hasMember! (ParentAllocator, " deallocate" ))
365- ~this ()
364+ @trusted ~this ()
366365 {
367366 parent.deallocate(payload);
368367 }
@@ -652,7 +651,7 @@ fronting the GC allocator.
652651 import std.experimental.allocator.gc_allocator : GCAllocator;
653652 import std.typecons : Ternary;
654653 // KRRegion fronting a general-purpose allocator
655- align (KRRegion ! ().alignment) ubyte [1024 * 128 ] buf;
654+ ubyte [1024 * 128 ] buf;
656655 auto alloc = fallbackAllocator(KRRegion! ()(buf), GCAllocator.instance);
657656 auto b = alloc.allocate(100 );
658657 assert (b.length == 100 );
@@ -674,6 +673,7 @@ it actually returns memory to the operating system when possible.
674673 import std.algorithm.comparison : max;
675674 import std.experimental.allocator.building_blocks.allocator_list
676675 : AllocatorList;
676+ import std.experimental.allocator.gc_allocator : GCAllocator;
677677 import std.experimental.allocator.mmap_allocator : MmapAllocator;
678678 AllocatorList! (n => KRRegion! MmapAllocator(max(n * 16 , 1024 * 1024 ))) alloc;
679679}
@@ -683,6 +683,7 @@ it actually returns memory to the operating system when possible.
683683 import std.algorithm.comparison : max;
684684 import std.experimental.allocator.building_blocks.allocator_list
685685 : AllocatorList;
686+ import std.experimental.allocator.gc_allocator : GCAllocator;
686687 import std.experimental.allocator.mallocator : Mallocator;
687688 import std.typecons : Ternary;
688689 /*
@@ -715,6 +716,7 @@ it actually returns memory to the operating system when possible.
715716 import std.algorithm.comparison : max;
716717 import std.experimental.allocator.building_blocks.allocator_list
717718 : AllocatorList;
719+ import std.experimental.allocator.gc_allocator : GCAllocator;
718720 import std.experimental.allocator.mmap_allocator : MmapAllocator;
719721 import std.typecons : Ternary;
720722 /*
@@ -747,7 +749,6 @@ it actually returns memory to the operating system when possible.
747749 }
748750}
749751
750- version (StdUnittest)
751752@system unittest
752753{
753754 import std.algorithm.comparison : max;
@@ -759,12 +760,13 @@ version (StdUnittest)
759760 n => KRRegion! GCAllocator(max(n * 16 , 1024 * 1024 )))());
760761}
761762
762- @safe unittest
763+ @trusted unittest
763764{
764765 import std.experimental.allocator.gc_allocator : GCAllocator;
765-
766766 auto alloc = KRRegion! GCAllocator(1024 * 1024 );
767767
768+
769+
768770 void [][] array;
769771 foreach (i; 1 .. 4 )
770772 {
@@ -785,9 +787,9 @@ version (StdUnittest)
785787 cast (ubyte [])(GCAllocator.instance.allocate(1024 * 1024 )));
786788 const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion! ().sizeof))();
787789 auto p = cast (KRRegion! ()* ) store.ptr;
788- import core.lifetime : emplace;
789790 import core.stdc.string : memcpy;
790- import std.conv : text;
791+ import std.algorithm.mutation : move;
792+ import std.conv : text, emplace;
791793
792794 memcpy(p, &alloc, alloc.sizeof);
793795 emplace(&alloc);
@@ -825,14 +827,16 @@ version (StdUnittest)
825827 assert (p.length == 1024 * 1024 );
826828}
827829
830+
828831@safe unittest
829832{
830- import std.random : randomCover;
833+ import std.experimental.allocator.building_blocks ;
834+ import std.random ;
831835 import std.typecons : Ternary;
832836
833837 // Both sequences must work on either system
834838
835- // A sequence of allocs which generates the error described in https://issues.dlang.org/show_bug.cgi?id= 16564
839+ // A sequence of allocs which generates the error described in issue 16564
836840 // that is a gap at the end of buf from the perspective of the allocator
837841
838842 // for 64 bit systems (leftover balance = 8 bytes < 16)
@@ -842,10 +846,10 @@ version (StdUnittest)
842846 int [] sizes32 = [81412 , 107068 , 49892 , 23768 ];
843847
844848
845- void test (int [] sizes)
849+ @system void test(int [] sizes)
846850 {
847851 align (size_t .sizeof) ubyte [256 * 1024 ] buf;
848- auto a = KRRegion ! () (buf);
852+ auto a = (() @trusted => createAllocator (buf))( );
849853
850854 void [][] bufs;
851855
@@ -862,12 +866,22 @@ version (StdUnittest)
862866 assert ((() pure nothrow @safe @nogc => a.empty)() == Ternary.yes);
863867 }
864868
865- test(sizes64);
866- test(sizes32);
869+ () @trusted {
870+ test(sizes64);
871+ test(sizes32);
872+ }();
867873}
868874
875+ @safe KRRegion! NullAllocator createAllocator(ubyte [] buf)
876+ {
877+ return KRRegion! NullAllocator(buf);
878+ }
879+
880+
869881@safe unittest
870882{
883+ import std.experimental.allocator.building_blocks ;
884+ import std.random ;
871885 import std.typecons : Ternary;
872886
873887 // For 64 bits, we allocate in multiples of 8, but the minimum alloc size is 16.
@@ -921,7 +935,7 @@ version (StdUnittest)
921935@system unittest
922936{ import std.typecons : Ternary;
923937
924- align (KRRegion ! ().alignment) ubyte [1024 ] b;
938+ ubyte [1024 ] b;
925939 auto alloc = KRRegion! ()(b);
926940
927941 auto k = alloc.allocate(128 );
@@ -941,4 +955,4 @@ version (StdUnittest)
941955 assert (alloc.empty == Ternary.no);
942956 assert (alloc.deallocate(k));
943957 assert (alloc.empty == Ternary.yes);
944- }
958+ }
0 commit comments