Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_experimental_allocator_building_blocks_region.d

raw
1 @system nothrow unittest
2 {
3 import std.experimental.allocator.building_blocks.region;
4
5 import std.algorithm.comparison : max;
6 import std.experimental.allocator.building_blocks.allocator_list
7 : AllocatorList;
8 import std.experimental.allocator.mallocator : Mallocator;
9 import std.typecons : Ternary;
10 // Create a scalable list of regions. Each gets at least 1MB at a time by
11 // using malloc.
12 auto batchAllocator = AllocatorList!(
13 (size_t n) => Region!Mallocator(max(n, 1024 * 1024))
14 )();
15 assert(batchAllocator.empty == Ternary.yes);
16 auto b = batchAllocator.allocate(101);
17 assert(b.length == 101);
18 assert(batchAllocator.empty == Ternary.no);
19 // This will cause a second allocation
20 b = batchAllocator.allocate(2 * 1024 * 1024);
21 assert(b.length == 2 * 1024 * 1024);
22 // Destructor will free the memory
23 }
24
25 @system nothrow @nogc unittest
26 {
27 import std.experimental.allocator.building_blocks.region;
28
29 import std.typecons : Ternary;
30
31 ubyte[1024] store;
32 auto myRegion = BorrowedRegion!(1)(store[]);
33
34 assert(myRegion.empty == Ternary.yes);
35 assert(myRegion.available == store.length);
36
37 void[] b = myRegion.allocate(101);
38
39 assert(b.length == 101);
40 assert(myRegion.empty == Ternary.no);
41 assert(myRegion.owns(b) == Ternary.yes);
42 assert(myRegion.available == store.length - b.length);
43
44 void[] b2 = myRegion.allocate(256);
45
46 // Can only free the most recent allocation
47 assert(myRegion.deallocate(b) == false);
48 assert(myRegion.deallocate(b2) == true);
49
50 myRegion.deallocateAll();
51
52 assert(myRegion.empty == Ternary.yes);
53 }
54
55 @system unittest
56 {
57 import std.experimental.allocator.building_blocks.region;
58
59 // 128KB region, allocated to x86's cache line
60 InSituRegion!(128 * 1024, 16) r1;
61 auto a1 = r1.allocate(101);
62 assert(a1.length == 101);
63
64 // 128KB region, with fallback to the garbage collector.
65 import std.experimental.allocator.building_blocks.fallback_allocator
66 : FallbackAllocator;
67 import std.experimental.allocator.building_blocks.free_list
68 : FreeList;
69 import std.experimental.allocator.building_blocks.bitmapped_block
70 : BitmappedBlock;
71 import std.experimental.allocator.gc_allocator : GCAllocator;
72 FallbackAllocator!(InSituRegion!(128 * 1024), GCAllocator) r2;
73 const a2 = r2.allocate(102);
74 assert(a2.length == 102);
75
76 // Reap with GC fallback.
77 InSituRegion!(128 * 1024, 8) tmp3;
78 FallbackAllocator!(BitmappedBlock!(64, 8), GCAllocator) r3;
79 r3.primary = BitmappedBlock!(64, 8)(cast(ubyte[]) (tmp3.allocateAll()));
80 const a3 = r3.allocate(103);
81 assert(a3.length == 103);
82
83 // Reap/GC with a freelist for small objects up to 16 bytes.
84 InSituRegion!(128 * 1024, 64) tmp4;
85 FreeList!(FallbackAllocator!(BitmappedBlock!(64, 64), GCAllocator), 0, 16) r4;
86 r4.parent.primary = BitmappedBlock!(64, 64)(cast(ubyte[]) (tmp4.allocateAll()));
87 const a4 = r4.allocate(104);
88 assert(a4.length == 104);
89 }
90