| 1 |
@system unittest |
| 2 |
{ |
| 3 |
import std.experimental.allocator; |
| 4 |
|
| 5 |
// Install a new allocator that is faster for 128-byte allocations. |
| 6 |
import std.experimental.allocator.building_blocks.free_list : FreeList; |
| 7 |
import std.experimental.allocator.gc_allocator : GCAllocator; |
| 8 |
auto oldAllocator = theAllocator; |
| 9 |
scope(exit) theAllocator = oldAllocator; |
| 10 |
theAllocator = allocatorObject(FreeList!(GCAllocator, 128)()); |
| 11 |
// Use the now changed allocator to allocate an array |
| 12 |
const ubyte[] arr = theAllocator.makeArray!ubyte(128); |
| 13 |
assert(arr.ptr); |
| 14 |
//... |
| 15 |
} |
| 16 |
|
| 17 |
@system unittest |
| 18 |
{ |
| 19 |
import std.experimental.allocator; |
| 20 |
|
| 21 |
// Dynamically allocate one integer |
| 22 |
const int* p1 = theAllocator.make!int; |
| 23 |
// It's implicitly initialized with its .init value |
| 24 |
assert(*p1 == 0); |
| 25 |
// Dynamically allocate one double, initialize to 42.5 |
| 26 |
const double* p2 = theAllocator.make!double(42.5); |
| 27 |
assert(*p2 == 42.5); |
| 28 |
|
| 29 |
// Dynamically allocate a struct |
| 30 |
static struct Point |
| 31 |
{ |
| 32 |
int x, y, z; |
| 33 |
} |
| 34 |
// Use the generated constructor taking field values in order |
| 35 |
const Point* p = theAllocator.make!Point(1, 2); |
| 36 |
assert(p.x == 1 && p.y == 2 && p.z == 0); |
| 37 |
|
| 38 |
// Dynamically allocate a class object |
| 39 |
static class Customer |
| 40 |
{ |
| 41 |
uint id = uint.max; |
| 42 |
this() {} |
| 43 |
this(uint id) { this.id = id; } |
| 44 |
// ... |
| 45 |
} |
| 46 |
Customer cust = theAllocator.make!Customer; |
| 47 |
assert(cust.id == uint.max); // default initialized |
| 48 |
cust = theAllocator.make!Customer(42); |
| 49 |
assert(cust.id == 42); |
| 50 |
|
| 51 |
// explicit passing of outer pointer |
| 52 |
static class Outer |
| 53 |
{ |
| 54 |
int x = 3; |
| 55 |
class Inner |
| 56 |
{ |
| 57 |
auto getX() { return x; } |
| 58 |
} |
| 59 |
} |
| 60 |
auto outer = theAllocator.make!Outer(); |
| 61 |
auto inner = theAllocator.make!(Outer.Inner)(outer); |
| 62 |
assert(outer.x == inner.getX); |
| 63 |
} |
| 64 |
|
| 65 |
@system unittest |
| 66 |
{ |
| 67 |
import std.experimental.allocator; |
| 68 |
|
| 69 |
import std.algorithm.comparison : equal; |
| 70 |
static void test(T)() |
| 71 |
{ |
| 72 |
T[] a = theAllocator.makeArray!T(2); |
| 73 |
assert(a.equal([0, 0])); |
| 74 |
a = theAllocator.makeArray!T(3, 42); |
| 75 |
assert(a.equal([42, 42, 42])); |
| 76 |
import std.range : only; |
| 77 |
a = theAllocator.makeArray!T(only(42, 43, 44)); |
| 78 |
assert(a.equal([42, 43, 44])); |
| 79 |
} |
| 80 |
test!int(); |
| 81 |
test!(shared int)(); |
| 82 |
test!(const int)(); |
| 83 |
test!(immutable int)(); |
| 84 |
} |
| 85 |
|
| 86 |
@system unittest |
| 87 |
{ |
| 88 |
import std.experimental.allocator; |
| 89 |
|
| 90 |
auto arr = theAllocator.makeArray!int([1, 2, 3]); |
| 91 |
assert(theAllocator.expandArray(arr, 2)); |
| 92 |
assert(arr == [1, 2, 3, 0, 0]); |
| 93 |
import std.range : only; |
| 94 |
assert(theAllocator.expandArray(arr, only(4, 5))); |
| 95 |
assert(arr == [1, 2, 3, 0, 0, 4, 5]); |
| 96 |
} |
| 97 |
|
| 98 |
@system unittest |
| 99 |
{ |
| 100 |
import std.experimental.allocator; |
| 101 |
|
| 102 |
int[] a = theAllocator.makeArray!int(100, 42); |
| 103 |
assert(a.length == 100); |
| 104 |
assert(theAllocator.shrinkArray(a, 98)); |
| 105 |
assert(a.length == 2); |
| 106 |
assert(a == [42, 42]); |
| 107 |
} |
| 108 |
|
| 109 |
@system unittest |
| 110 |
{ |
| 111 |
import std.experimental.allocator; |
| 112 |
|
| 113 |
import std.experimental.allocator.mallocator : Mallocator; |
| 114 |
|
| 115 |
auto mArray = Mallocator.instance.makeMultidimensionalArray!int(2, 3, 6); |
| 116 |
|
| 117 |
// deallocate when exiting scope |
| 118 |
scope(exit) |
| 119 |
{ |
| 120 |
Mallocator.instance.disposeMultidimensionalArray(mArray); |
| 121 |
} |
| 122 |
|
| 123 |
assert(mArray.length == 2); |
| 124 |
foreach (lvl2Array; mArray) |
| 125 |
{ |
| 126 |
assert(lvl2Array.length == 3); |
| 127 |
foreach (lvl3Array; lvl2Array) |
| 128 |
assert(lvl3Array.length == 6); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
@system unittest |
| 133 |
{ |
| 134 |
import std.experimental.allocator; |
| 135 |
|
| 136 |
struct TestAllocator |
| 137 |
{ |
| 138 |
import std.experimental.allocator.common : platformAlignment; |
| 139 |
import std.experimental.allocator.mallocator : Mallocator; |
| 140 |
|
| 141 |
alias allocator = Mallocator.instance; |
| 142 |
|
| 143 |
private static struct ByteRange |
| 144 |
{ |
| 145 |
void* ptr; |
| 146 |
size_t length; |
| 147 |
} |
| 148 |
|
| 149 |
private ByteRange[] _allocations; |
| 150 |
|
| 151 |
enum uint alignment = platformAlignment; |
| 152 |
|
| 153 |
void[] allocate(size_t numBytes) |
| 154 |
{ |
| 155 |
auto ret = allocator.allocate(numBytes); |
| 156 |
_allocations ~= ByteRange(ret.ptr, ret.length); |
| 157 |
return ret; |
| 158 |
} |
| 159 |
|
| 160 |
bool deallocate(void[] bytes) |
| 161 |
{ |
| 162 |
import std.algorithm.mutation : remove; |
| 163 |
import std.algorithm.searching : canFind; |
| 164 |
|
| 165 |
bool pred(ByteRange other) |
| 166 |
{ return other.ptr == bytes.ptr && other.length == bytes.length; } |
| 167 |
|
| 168 |
assert(_allocations.canFind!pred); |
| 169 |
|
| 170 |
_allocations = _allocations.remove!pred; |
| 171 |
return allocator.deallocate(bytes); |
| 172 |
} |
| 173 |
|
| 174 |
~this() |
| 175 |
{ |
| 176 |
assert(!_allocations.length); |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
TestAllocator allocator; |
| 181 |
|
| 182 |
auto mArray = allocator.makeMultidimensionalArray!int(2, 3, 5, 6, 7, 2); |
| 183 |
|
| 184 |
allocator.disposeMultidimensionalArray(mArray); |
| 185 |
} |
| 186 |
|
| 187 |
@system unittest |
| 188 |
{ |
| 189 |
import std.experimental.allocator; |
| 190 |
|
| 191 |
import std.experimental.allocator.mallocator : Mallocator; |
| 192 |
|
| 193 |
RCIAllocator a = allocatorObject(Mallocator.instance); |
| 194 |
auto b = a.allocate(100); |
| 195 |
assert(b.length == 100); |
| 196 |
assert(a.deallocate(b)); |
| 197 |
|
| 198 |
// The in-situ region must be used by pointer |
| 199 |
import std.experimental.allocator.building_blocks.region : InSituRegion; |
| 200 |
auto r = InSituRegion!1024(); |
| 201 |
a = allocatorObject(&r); |
| 202 |
b = a.allocate(200); |
| 203 |
assert(b.length == 200); |
| 204 |
// In-situ regions can deallocate the last allocation |
| 205 |
assert(a.deallocate(b)); |
| 206 |
} |
| 207 |
|
| 208 |
@system unittest |
| 209 |
{ |
| 210 |
import std.experimental.allocator; |
| 211 |
|
| 212 |
import std.experimental.allocator.building_blocks.free_list : FreeList; |
| 213 |
import std.experimental.allocator.gc_allocator : GCAllocator; |
| 214 |
import std.experimental.allocator.mallocator : Mallocator; |
| 215 |
|
| 216 |
static assert(!is(ThreadLocal!Mallocator)); |
| 217 |
static assert(!is(ThreadLocal!GCAllocator)); |
| 218 |
alias Allocator = ThreadLocal!(FreeList!(GCAllocator, 0, 8)); |
| 219 |
auto b = Allocator.instance.allocate(5); |
| 220 |
static assert(__traits(hasMember, Allocator, "allocate")); |
| 221 |
} |
| 222 |
|
| 223 |
@system unittest |
| 224 |
{ |
| 225 |
import std.experimental.allocator; |
| 226 |
|
| 227 |
import std.experimental.allocator.building_blocks.allocator_list : AllocatorList; |
| 228 |
import std.experimental.allocator.building_blocks.bitmapped_block : BitmappedBlock; |
| 229 |
import std.experimental.allocator.building_blocks.segregator : Segregator; |
| 230 |
import std.experimental.allocator.building_blocks.bucketizer : Bucketizer; |
| 231 |
import std.experimental.allocator.building_blocks.free_list : FreeList; |
| 232 |
import std.experimental.allocator.gc_allocator : GCAllocator; |
| 233 |
|
| 234 |
/// Define an allocator bound to the built-in GC. |
| 235 |
auto alloc = allocatorObject(GCAllocator.instance); |
| 236 |
auto b = alloc.allocate(42); |
| 237 |
assert(b.length == 42); |
| 238 |
assert(alloc.deallocate(b)); |
| 239 |
|
| 240 |
import std.algorithm.comparison : max; |
| 241 |
// Define an elaborate allocator and bind it to the class API. |
| 242 |
alias FList = FreeList!(GCAllocator, 0, unbounded); |
| 243 |
alias A = ThreadLocal!( |
| 244 |
Segregator!( |
| 245 |
8, FreeList!(GCAllocator, 0, 8), |
| 246 |
128, Bucketizer!(FList, 1, 128, 16), |
| 247 |
256, Bucketizer!(FList, 129, 256, 32), |
| 248 |
512, Bucketizer!(FList, 257, 512, 64), |
| 249 |
1024, Bucketizer!(FList, 513, 1024, 128), |
| 250 |
2048, Bucketizer!(FList, 1025, 2048, 256), |
| 251 |
3584, Bucketizer!(FList, 2049, 3584, 512), |
| 252 |
4072 * 1024, AllocatorList!( |
| 253 |
(n) => BitmappedBlock!(4096)(cast(ubyte[]) GCAllocator.instance.allocate( |
| 254 |
max(n, 4072 * 1024)))), |
| 255 |
GCAllocator |
| 256 |
) |
| 257 |
); |
| 258 |
|
| 259 |
auto alloc2 = allocatorObject(A.instance); |
| 260 |
b = alloc2.allocate(101); |
| 261 |
assert(alloc2.deallocate(b)); |
| 262 |
} |
| 263 |
|