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