1 module nxt.pure_gc_allocator;
2 
3 // TODO Use std.experimental.allocator.building_blocks.gc_allocator.d instead of this module
4 
5 import std.experimental.allocator.common;
6 
7 /**
8  * D's built-in garbage-collected allocator purified.
9  *
10  * TODO remove and place with GCAllocator when https://github.com/dlang/phobos/pull/6432 has been merged.
11  */
12 struct PureGCAllocator
13 {
14     import core.memory : GC;
15     import std.typecons : Ternary;
16 
17     /**
18     The alignment is a static constant equal to `platformAlignment`, which
19     ensures proper alignment for any D data type.
20     */
21     enum uint alignment = platformAlignment;
22 
23     /**
24     Standard allocator methods per the semantics defined above. The $(D
25     deallocate) and `reallocate` methods are `@system` because they may
26     move memory around, leaving dangling pointers in user code.
27     */
28     pure nothrow @trusted void[] allocate(size_t bytes) shared const
29     {
30         if (!bytes) return null;
31         auto p = GC.malloc(bytes);
32         return p ? p[0 .. bytes] : null;
33     }
34 
35     /// Ditto
36     pure nothrow @trusted bool expand(ref void[] b, size_t delta) shared const
37     {
38         if (delta == 0) return true;
39         if (b is null) return false;
40         immutable curLength = GC.sizeOf(b.ptr);
41         assert(curLength != 0); // we have a valid GC pointer here
42         immutable desired = b.length + delta;
43         if (desired > curLength) // check to see if the current block can't hold the data
44         {
45             immutable sizeRequest = desired - curLength;
46             immutable newSize = GC.extend(b.ptr, sizeRequest, sizeRequest);
47             if (newSize == 0)
48             {
49                 // expansion unsuccessful
50                 return false;
51             }
52             assert(newSize >= desired);
53         }
54         b = b.ptr[0 .. desired];
55         return true;
56     }
57 
58     /// Ditto
59     pure nothrow @system bool reallocate(ref void[] b, size_t newSize) shared const
60     {
61         import core.exception : OutOfMemoryError;
62         try
63         {
64             auto p = cast(ubyte*) GC.realloc(b.ptr, newSize);
65             b = p[0 .. newSize];
66         }
67         catch (OutOfMemoryError)
68         {
69             // leave the block in place, tell caller
70             return false;
71         }
72         return true;
73     }
74 
75     /// Ditto
76     pure nothrow @trusted @nogc
77     Ternary resolveInternalPointer(const void* p, ref void[] result) shared const
78     {
79         auto r = GC.addrOf(cast(void*) p);
80         if (!r) return Ternary.no;
81         result = r[0 .. GC.sizeOf(r)];
82         return Ternary.yes;
83     }
84 
85     /// Ditto
86     pure nothrow @system @nogc
87     bool deallocate(void[] b) shared const
88     {
89         GC.free(b.ptr);
90         return true;
91     }
92 
93     /// Ditto
94     pure nothrow @safe @nogc
95     size_t goodAllocSize(size_t n) shared const
96     {
97         if (n == 0)
98             return 0;
99         if (n <= 16)
100             return 16;
101 
102         import core.bitop : bsr;
103 
104         auto largestBit = bsr(n-1) + 1;
105         if (largestBit <= 12) // 4096 or less
106             return size_t(1) << largestBit;
107 
108         // larger, we use a multiple of 4096.
109         return ((n + 4095) / 4096) * 4096;
110     }
111 
112     /**
113     Returns the global instance of this allocator type. The garbage collected
114     allocator is thread-safe, therefore all of its methods and `instance` itself
115     are `shared`.
116     */
117 
118     static shared const PureGCAllocator instance;
119 
120     // Leave it undocummented for now.
121     nothrow @trusted void collect() shared const
122     {
123         GC.collect();
124     }
125 }
126 
127 version(none):
128 
129 ///
130 pure @system unittest
131 {
132     auto buffer = PureGCAllocator.instance.allocate(1024 * 1024 * 4);
133     // deallocate upon scope's end (alternatively: leave it to collection)
134     scope(exit) PureGCAllocator.instance.deallocate(buffer);
135     //...
136 }
137 
138 pure @safe unittest
139 {
140     auto b = PureGCAllocator.instance.allocate(10_000);
141     assert(PureGCAllocator.instance.expand(b, 1));
142 }
143 
144 pure @system unittest
145 {
146     import core.memory : GC;
147     import std.typecons : Ternary;
148 
149     // test allocation sizes
150     assert((() nothrow @safe @nogc => PureGCAllocator.instance.goodAllocSize(1))() == 16);
151     for (size_t s = 16; s <= 8192; s *= 2)
152     {
153         assert((() nothrow @safe @nogc => PureGCAllocator.instance.goodAllocSize(s))() == s);
154         assert((() nothrow @safe @nogc => PureGCAllocator.instance.goodAllocSize(s - (s / 2) + 1))() == s);
155 
156         auto buffer = PureGCAllocator.instance.allocate(s);
157         scope(exit) () nothrow @nogc { PureGCAllocator.instance.deallocate(buffer); }();
158 
159         void[] p;
160         assert((() nothrow @safe => PureGCAllocator.instance.resolveInternalPointer(null, p))() == Ternary.no);
161         assert((() nothrow @safe => PureGCAllocator.instance.resolveInternalPointer(&buffer[0], p))() == Ternary.yes);
162         assert(p.ptr is buffer.ptr && p.length >= buffer.length);
163 
164         assert(GC.sizeOf(buffer.ptr) == s);
165 
166         auto buffer2 = PureGCAllocator.instance.allocate(s - (s / 2) + 1);
167         scope(exit) () nothrow @nogc { PureGCAllocator.instance.deallocate(buffer2); }();
168 
169         assert(GC.sizeOf(buffer2.ptr) == s);
170     }
171 
172     // anything above a page is simply rounded up to next page
173     assert((() nothrow @safe @nogc => PureGCAllocator.instance.goodAllocSize(4096 * 4 + 1))() == 4096 * 5);
174 }
175 
176 pure nothrow @safe unittest
177 {
178     import std.typecons : Ternary;
179 
180     void[] buffer = PureGCAllocator.instance.allocate(42);
181     void[] result;
182     Ternary found = PureGCAllocator.instance.resolveInternalPointer(&buffer[0], result);
183 
184     assert(found == Ternary.yes && &result[0] == &buffer[0] && result.length >= buffer.length);
185     assert(PureGCAllocator.instance.resolveInternalPointer(null, result) == Ternary.no);
186     void *badPtr = (() @trusted => cast(void*)(0xdeadbeef))();
187     assert(PureGCAllocator.instance.resolveInternalPointer(badPtr, result) == Ternary.no);
188 }