3
3
Source: $(PHOBOSSRC std/experimental/allocator/building_blocks/kernighan_ritchie.d)
4
4
*/
5
5
module 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 ;
8
7
9
8
// debug = KRRegion;
10
9
debug (KRRegion) import std.stdio ;
@@ -362,7 +361,7 @@ struct KRRegion(ParentAllocator = NullAllocator)
362
361
// / Ditto
363
362
static if (! is (ParentAllocator == NullAllocator)
364
363
&& hasMember! (ParentAllocator, " deallocate" ))
365
- ~this ()
364
+ @trusted ~this ()
366
365
{
367
366
parent.deallocate(payload);
368
367
}
@@ -652,7 +651,7 @@ fronting the GC allocator.
652
651
import std.experimental.allocator.gc_allocator : GCAllocator;
653
652
import std.typecons : Ternary;
654
653
// KRRegion fronting a general-purpose allocator
655
- align (KRRegion ! ().alignment) ubyte [1024 * 128 ] buf;
654
+ ubyte [1024 * 128 ] buf;
656
655
auto alloc = fallbackAllocator(KRRegion! ()(buf), GCAllocator.instance);
657
656
auto b = alloc.allocate(100 );
658
657
assert (b.length == 100 );
@@ -674,6 +673,7 @@ it actually returns memory to the operating system when possible.
674
673
import std.algorithm.comparison : max;
675
674
import std.experimental.allocator.building_blocks.allocator_list
676
675
: AllocatorList;
676
+ import std.experimental.allocator.gc_allocator : GCAllocator;
677
677
import std.experimental.allocator.mmap_allocator : MmapAllocator;
678
678
AllocatorList! (n => KRRegion! MmapAllocator(max(n * 16 , 1024 * 1024 ))) alloc;
679
679
}
@@ -683,6 +683,7 @@ it actually returns memory to the operating system when possible.
683
683
import std.algorithm.comparison : max;
684
684
import std.experimental.allocator.building_blocks.allocator_list
685
685
: AllocatorList;
686
+ import std.experimental.allocator.gc_allocator : GCAllocator;
686
687
import std.experimental.allocator.mallocator : Mallocator;
687
688
import std.typecons : Ternary;
688
689
/*
@@ -715,6 +716,7 @@ it actually returns memory to the operating system when possible.
715
716
import std.algorithm.comparison : max;
716
717
import std.experimental.allocator.building_blocks.allocator_list
717
718
: AllocatorList;
719
+ import std.experimental.allocator.gc_allocator : GCAllocator;
718
720
import std.experimental.allocator.mmap_allocator : MmapAllocator;
719
721
import std.typecons : Ternary;
720
722
/*
@@ -747,7 +749,6 @@ it actually returns memory to the operating system when possible.
747
749
}
748
750
}
749
751
750
- version (StdUnittest)
751
752
@system unittest
752
753
{
753
754
import std.algorithm.comparison : max;
@@ -759,12 +760,13 @@ version (StdUnittest)
759
760
n => KRRegion! GCAllocator(max(n * 16 , 1024 * 1024 )))());
760
761
}
761
762
762
- @safe unittest
763
+ @trusted unittest
763
764
{
764
765
import std.experimental.allocator.gc_allocator : GCAllocator;
765
-
766
766
auto alloc = KRRegion! GCAllocator(1024 * 1024 );
767
767
768
+
769
+
768
770
void [][] array;
769
771
foreach (i; 1 .. 4 )
770
772
{
@@ -785,9 +787,9 @@ version (StdUnittest)
785
787
cast (ubyte [])(GCAllocator.instance.allocate(1024 * 1024 )));
786
788
const store = (() pure nothrow @safe @nogc => alloc.allocate(KRRegion! ().sizeof))();
787
789
auto p = cast (KRRegion! ()* ) store.ptr;
788
- import core.lifetime : emplace;
789
790
import core.stdc.string : memcpy;
790
- import std.conv : text;
791
+ import std.algorithm.mutation : move;
792
+ import std.conv : text, emplace;
791
793
792
794
memcpy(p, &alloc, alloc.sizeof);
793
795
emplace(&alloc);
@@ -825,14 +827,16 @@ version (StdUnittest)
825
827
assert (p.length == 1024 * 1024 );
826
828
}
827
829
830
+
828
831
@safe unittest
829
832
{
830
- import std.random : randomCover;
833
+ import std.experimental.allocator.building_blocks ;
834
+ import std.random ;
831
835
import std.typecons : Ternary;
832
836
833
837
// Both sequences must work on either system
834
838
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
836
840
// that is a gap at the end of buf from the perspective of the allocator
837
841
838
842
// for 64 bit systems (leftover balance = 8 bytes < 16)
@@ -842,10 +846,10 @@ version (StdUnittest)
842
846
int [] sizes32 = [81412 , 107068 , 49892 , 23768 ];
843
847
844
848
845
- void test (int [] sizes)
849
+ @system void test(int [] sizes)
846
850
{
847
851
align (size_t .sizeof) ubyte [256 * 1024 ] buf;
848
- auto a = KRRegion ! () (buf);
852
+ auto a = (() @trusted => createAllocator (buf))( );
849
853
850
854
void [][] bufs;
851
855
@@ -862,12 +866,22 @@ version (StdUnittest)
862
866
assert ((() pure nothrow @safe @nogc => a.empty)() == Ternary.yes);
863
867
}
864
868
865
- test(sizes64);
866
- test(sizes32);
869
+ () @trusted {
870
+ test(sizes64);
871
+ test(sizes32);
872
+ }();
867
873
}
868
874
875
+ @safe KRRegion! NullAllocator createAllocator(ubyte [] buf)
876
+ {
877
+ return KRRegion! NullAllocator(buf);
878
+ }
879
+
880
+
869
881
@safe unittest
870
882
{
883
+ import std.experimental.allocator.building_blocks ;
884
+ import std.random ;
871
885
import std.typecons : Ternary;
872
886
873
887
// For 64 bits, we allocate in multiples of 8, but the minimum alloc size is 16.
@@ -921,7 +935,7 @@ version (StdUnittest)
921
935
@system unittest
922
936
{ import std.typecons : Ternary;
923
937
924
- align (KRRegion ! ().alignment) ubyte [1024 ] b;
938
+ ubyte [1024 ] b;
925
939
auto alloc = KRRegion! ()(b);
926
940
927
941
auto k = alloc.allocate(128 );
@@ -941,4 +955,4 @@ version (StdUnittest)
941
955
assert (alloc.empty == Ternary.no);
942
956
assert (alloc.deallocate(k));
943
957
assert (alloc.empty == Ternary.yes);
944
- }
958
+ }
0 commit comments