1 /** 2 C++-style automatic memory management smart pointers for D using `std.experimental.allocator`. 3 4 Unlike the C++ variants, the smart pointers themselves allocate the memory for the objects they contain. 5 That ensures the right allocator is used to dispose of the memory as well. 6 7 Allocators are template arguments instead of using `theAllocator` so 8 that these smart pointers can be used in `@nogc` code. However, they 9 will default to `typeof(theAllocator)` for simplicity. The examples 10 above will be explicit. 11 12 Another reason to have to pass in the type of allocator is to decide how it is to 13 be stored. Stateless allocators can be "stored" by value and imply zero-cost `Unique` pointers. 14 Singleton allocators such as Mallocator (that have an `instance` attribute/member function) 15 don't need to be passed in to the constructor. This is detected at compile-time as an example 16 of design by instrospection. 17 18 `RefCounted` leverages D's type system by doing atomic reference counting *iff* the type of the contained 19 object is `shared`. Otherwise it's non-atomic. 20 */ 21 module automem; 22 23 public import automem.unique; 24 public import automem.ref_counted; 25 public import automem.vector; 26 public import automem.array; 27 28 29 @safe unittest { 30 31 import std.algorithm: move; 32 33 static struct Point { 34 int x; 35 int y; 36 } 37 38 // set theAllocator as desired beforehand, e.g. 39 // theAllocator = allocatorObject(Mallocator.instance) 40 41 { 42 // must pass arguments to initialise the contained object 43 auto u1 = Unique!Point(2, 3); 44 assert(*u1 == Point(2, 3)); 45 assert(u1.y == 3); 46 47 // auto u2 = u1; // won't compile, can only move 48 typeof(u1) u2 = () @trusted { return u1.move; }(); 49 assert(cast(bool)u1 == false); // u1 is now empty 50 } 51 // memory freed for the Point structure created in the block 52 53 { 54 auto s1 = RefCounted!Point(4, 5); 55 assert(*s1 == Point(4, 5)); 56 assert(s1.x == 4); 57 { 58 auto s2 = s1; // can be copied 59 } // ref count goes to 1 here 60 61 } // ref count goes to 0 here, memory released 62 63 { 64 import std.algorithm: map, equal; 65 import std.range: iota; 66 67 // `vector` is also known as `array` 68 auto vec = vector(Point(1, 2), Point(3, 4), Point(5, 6)); 69 assert(equal(vec.range, [Point(1, 2), Point(3, 4), Point(5, 6)])); 70 71 // reallocations are @system since old pointers can dangle 72 () @trusted { 73 vec.length = 1; 74 assert(equal(vec.range, [Point(1, 2)])); 75 76 vec ~= Point(7, 8); 77 assert(equal(vec.range, [Point(1, 2), Point(7, 8)])); 78 79 vec ~= 2.iota.map!(i => Point(i + 10, i + 11)); 80 assert(equal(vec.range, [Point(1, 2), Point(7, 8), Point(10, 11), Point(11, 12)])); 81 }(); 82 } // memory for the array released here 83 } 84 85 86 // @nogc test - must explicitly use the allocator for compile-time guarantees 87 @safe @nogc unittest { 88 import std.experimental.allocator.mallocator: Mallocator; 89 90 static struct Point { 91 int x; 92 int y; 93 } 94 95 { 96 // must pass arguments to initialise the contained object 97 auto u1 = Unique!(Point, Mallocator)(2, 3); 98 assert(*u1 == Point(2, 3)); 99 assert(u1.y == 3); 100 } 101 // memory freed for the Point structure created in the block 102 103 // similarly for the other types 104 }