Debian Codegraph @

libphobos/testsuite/libphobos.phobos/std_experimental_allocator_building_blocks_bitmapped_block.d

raw
1 @system unittest
2 {
3 import std.experimental.allocator.building_blocks.bitmapped_block;
4
5 // Create a block allocator on top of a 10KB stack region.
6 import std.experimental.allocator.building_blocks.region : InSituRegion;
7 import std.traits : hasMember;
8 InSituRegion!(10_240, 64) r;
9 auto a = BitmappedBlock!(64, 64)(cast(ubyte[])(r.allocateAll()));
10 static assert(hasMember!(InSituRegion!(10_240, 64), "allocateAll"));
11 const b = a.allocate(100);
12 assert(b.length == 100);
13 }
14
15 @system unittest
16 {
17 import std.experimental.allocator.building_blocks.bitmapped_block;
18
19 import std.experimental.allocator.mallocator : Mallocator;
20 import std.typecons : Flag, Yes;
21
22 enum blockSize = 64;
23 enum numBlocks = 10;
24
25 // The 'BitmappedBlock' is implicitly instantiated with Yes.multiblock
26 auto a = BitmappedBlock!(blockSize, 8, Mallocator, Yes.multiblock)(numBlocks * blockSize);
27
28 // Instantiated with Yes.multiblock, can allocate more than one block at a time
29 void[] buf = a.allocate(2 * blockSize);
30 assert(buf.length == 2 * blockSize);
31 assert(a.deallocate(buf));
32
33 // Can also allocate less than one block
34 buf = a.allocate(blockSize / 2);
35 assert(buf.length == blockSize / 2);
36
37 // Expands inside the same block
38 assert(a.expand(buf, blockSize / 2));
39 assert(buf.length == blockSize);
40
41 // If Yes.multiblock, can expand past the size of a single block
42 assert(a.expand(buf, 3 * blockSize));
43 assert(buf.length == 4 * blockSize);
44 assert(a.deallocate(buf));
45 }
46
47 @system unittest
48 {
49 import std.experimental.allocator.building_blocks.bitmapped_block;
50
51 import std.experimental.allocator.mallocator : Mallocator;
52 import std.typecons : Flag, No;
53
54 enum blockSize = 64;
55 auto a = BitmappedBlock!(blockSize, 8, Mallocator, No.multiblock)(1024 * blockSize);
56
57 // Since instantiated with No.multiblock, can only allocate at most the block size
58 void[] buf = a.allocate(blockSize + 1);
59 assert(buf is null);
60
61 buf = a.allocate(blockSize);
62 assert(buf.length == blockSize);
63 assert(a.deallocate(buf));
64
65 // This is also fine, because it's less than the block size
66 buf = a.allocate(blockSize / 2);
67 assert(buf.length == blockSize / 2);
68
69 // Can expand the buffer until its length is at most 64
70 assert(a.expand(buf, blockSize / 2));
71 assert(buf.length == blockSize);
72
73 // Cannot expand anymore
74 assert(!a.expand(buf, 1));
75 assert(a.deallocate(buf));
76 }
77
78 @system unittest
79 {
80 import std.experimental.allocator.building_blocks.bitmapped_block;
81
82 import std.experimental.allocator.mallocator : Mallocator;
83 import std.experimental.allocator.common : platformAlignment;
84 import std.typecons : Flag, Yes, No;
85
86 // Create 'numThreads' threads, each allocating in parallel a chunk of memory
87 static void testAlloc(Allocator)(ref Allocator a, size_t allocSize)
88 {
89 import core.thread : ThreadGroup;
90 import std.algorithm.sorting : sort;
91 import core.internal.spinlock : SpinLock;
92
93 SpinLock lock = SpinLock(SpinLock.Contention.brief);
94 enum numThreads = 10;
95 void[][numThreads] buf;
96 size_t count = 0;
97
98 // Each threads allocates 'allocSize'
99 void fun()
100 {
101 void[] b = a.allocate(allocSize);
102 assert(b.length == allocSize);
103
104 lock.lock();
105 scope(exit) lock.unlock();
106
107 buf[count] = b;
108 count++;
109 }
110
111 auto tg = new ThreadGroup;
112 foreach (i; 0 .. numThreads)
113 {
114 tg.create(&fun);
115 }
116 tg.joinAll();
117
118 // Sorting the allocations made by each thread, we expect the buffers to be
119 // adjacent inside the SharedBitmappedBlock
120 sort!((a, b) => a.ptr < b.ptr)(buf[0 .. numThreads]);
121 foreach (i; 0 .. numThreads - 1)
122 {
123 assert(buf[i].ptr + a.goodAllocSize(buf[i].length) <= buf[i + 1].ptr);
124 }
125
126 // Deallocate everything
127 foreach (i; 0 .. numThreads)
128 {
129 assert(a.deallocate(buf[i]));
130 }
131 }
132
133 enum blockSize = 64;
134 auto alloc1 = SharedBitmappedBlock!(blockSize, platformAlignment, Mallocator, Yes.multiblock)(1024 * 1024);
135 auto alloc2 = SharedBitmappedBlock!(blockSize, platformAlignment, Mallocator, No.multiblock)(1024 * 1024);
136 testAlloc(alloc1, 2 * blockSize);
137 testAlloc(alloc2, blockSize);
138 }
139