Debian Codegraph @

libphobos/libdruntime/core/internal/gc/proxy.d

raw
1 /**
2 * Contains the external GC interface.
3 *
4 * Copyright: D Language Foundation 2005 - 2021.
5 * License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 * Authors: Walter Bright, Sean Kelly
7 */
8 module core.internal.gc.proxy;
9
10 import core.internal.gc.impl.proto.gc;
11 import core.gc.config;
12 import core.gc.gcinterface;
13 import core.gc.registry : createGCInstance;
14
15 static import core.memory;
16
17 private
18 {
19 static import core.memory;
20 alias BlkInfo = core.memory.GC.BlkInfo;
21
22 import core.internal.spinlock;
23 static SpinLock instanceLock;
24
25 __gshared bool isInstanceInit = false;
26 __gshared GC _instance = new ProtoGC();
27 __gshared GC proxiedGC; // used to iterate roots of Windows DLLs
28
29 pragma (inline, true) @trusted @nogc nothrow
30 GC instance() { return _instance; }
31 }
32
33 extern (C)
34 {
35 import core.attribute : weak;
36
37 // do not import GC modules, they might add a dependency to this whole module
38 void _d_register_conservative_gc();
39 void _d_register_manual_gc();
40
41 // if you don't want to include the default GCs, replace during link by another implementation
42 void* register_default_gcs() @weak
43 {
44 pragma(inline, false);
45 // do not call, they register implicitly through pragma(crt_constructor)
46 // avoid being optimized away
47 auto reg1 = &_d_register_conservative_gc;
48 auto reg2 = &_d_register_manual_gc;
49 return reg1 < reg2 ? reg1 : reg2;
50 }
51
52 void gc_init()
53 {
54 instanceLock.lock();
55 if (!isInstanceInit)
56 {
57 register_default_gcs();
58 config.initialize();
59 auto protoInstance = instance;
60 auto newInstance = createGCInstance(config.gc);
61 if (newInstance is null)
62 {
63 import core.stdc.stdio : fprintf, stderr;
64 import core.stdc.stdlib : exit;
65 import core.atomic : atomicLoad;
66
67 fprintf(atomicLoad(stderr), "No GC was initialized, please recheck the name of the selected GC ('%.*s').\n", cast(int)config.gc.length, config.gc.ptr);
68 instanceLock.unlock();
69 exit(1);
70
71 // Shouldn't get here.
72 assert(0);
73 }
74 _instance = newInstance;
75 // Transfer all ranges and roots to the real GC.
76 (cast(ProtoGC) protoInstance).transferRangesAndRoots();
77 isInstanceInit = true;
78 }
79 instanceLock.unlock();
80 }
81
82 void gc_init_nothrow() nothrow
83 {
84 scope(failure)
85 {
86 import core.internal.abort;
87 abort("Cannot initialize the garbage collector.\n");
88 assert(0);
89 }
90 gc_init();
91 }
92
93 void gc_term()
94 {
95 if (isInstanceInit)
96 {
97 switch (config.cleanup)
98 {
99 default:
100 import core.stdc.stdio : fprintf, stderr;
101 import core.atomic : atomicLoad;
102
103 fprintf(atomicLoad(stderr), "Unknown GC cleanup method, please recheck ('%.*s').\n",
104 cast(int)config.cleanup.length, config.cleanup.ptr);
105 break;
106 case "none":
107 break;
108 case "collect":
109 instance.collect();
110 break;
111 case "finalize":
112 instance.runFinalizers((cast(ubyte*)null)[0 .. size_t.max]);
113 break;
114 }
115 destroy(instance);
116 }
117 }
118
119 void gc_enable()
120 {
121 instance.enable();
122 }
123
124 void gc_disable()
125 {
126 instance.disable();
127 }
128
129 void gc_collect() nothrow
130 {
131 instance.collect();
132 }
133
134 void gc_minimize() nothrow
135 {
136 instance.minimize();
137 }
138
139 uint gc_getAttr( void* p ) nothrow
140 {
141 return instance.getAttr(p);
142 }
143
144 uint gc_setAttr( void* p, uint a ) nothrow
145 {
146 return instance.setAttr(p, a);
147 }
148
149 uint gc_clrAttr( void* p, uint a ) nothrow
150 {
151 return instance.clrAttr(p, a);
152 }
153
154 void* gc_malloc( size_t sz, uint ba = 0, const scope TypeInfo ti = null ) nothrow
155 {
156 return instance.malloc(sz, ba, ti);
157 }
158
159 BlkInfo gc_qalloc( size_t sz, uint ba = 0, const scope TypeInfo ti = null ) nothrow
160 {
161 return instance.qalloc( sz, ba, ti );
162 }
163
164 void* gc_calloc( size_t sz, uint ba = 0, const scope TypeInfo ti = null ) nothrow
165 {
166 return instance.calloc( sz, ba, ti );
167 }
168
169 void* gc_realloc( void* p, size_t sz, uint ba = 0, const scope TypeInfo ti = null ) nothrow
170 {
171 return instance.realloc( p, sz, ba, ti );
172 }
173
174 size_t gc_extend( void* p, size_t mx, size_t sz, const scope TypeInfo ti = null ) nothrow
175 {
176 return instance.extend( p, mx, sz,ti );
177 }
178
179 size_t gc_reserve( size_t sz ) nothrow
180 {
181 return instance.reserve( sz );
182 }
183
184 void gc_free( void* p ) nothrow @nogc
185 {
186 return instance.free( p );
187 }
188
189 void* gc_addrOf( void* p ) nothrow @nogc
190 {
191 return instance.addrOf( p );
192 }
193
194 size_t gc_sizeOf( void* p ) nothrow @nogc
195 {
196 return instance.sizeOf( p );
197 }
198
199 BlkInfo gc_query( void* p ) nothrow
200 {
201 return instance.query( p );
202 }
203
204 core.memory.GC.Stats gc_stats() @safe nothrow @nogc
205 {
206 return instance.stats();
207 }
208
209 core.memory.GC.ProfileStats gc_profileStats() @safe nothrow @nogc
210 {
211 return instance.profileStats();
212 }
213
214 void gc_addRoot( void* p ) nothrow @nogc
215 {
216 return instance.addRoot( p );
217 }
218
219 void gc_addRange( void* p, size_t sz, const TypeInfo ti = null ) nothrow @nogc
220 {
221 return instance.addRange( p, sz, ti );
222 }
223
224 void gc_removeRoot( void* p ) nothrow
225 {
226 return instance.removeRoot( p );
227 }
228
229 void gc_removeRange( void* p ) nothrow
230 {
231 return instance.removeRange( p );
232 }
233
234 void gc_runFinalizers(const scope void[] segment ) nothrow
235 {
236 return instance.runFinalizers( segment );
237 }
238
239 bool gc_inFinalizer() nothrow @nogc @safe
240 {
241 return instance.inFinalizer();
242 }
243
244 ulong gc_allocatedInCurrentThread() nothrow
245 {
246 return instance.allocatedInCurrentThread();
247 }
248
249 void[] gc_getArrayUsed(void *ptr, bool atomic) nothrow
250 {
251 return instance.getArrayUsed( ptr, atomic );
252 }
253
254 bool gc_expandArrayUsed(void[] slice, size_t newUsed, bool atomic) nothrow
255 {
256 return instance.expandArrayUsed( slice, newUsed, atomic );
257 }
258
259 size_t gc_reserveArrayCapacity(void[] slice, size_t request, bool atomic) nothrow
260 {
261 return instance.reserveArrayCapacity( slice, request, atomic );
262 }
263
264 bool gc_shrinkArrayUsed(void[] slice, size_t existingUsed, bool atomic) nothrow
265 {
266 return instance.shrinkArrayUsed( slice, existingUsed, atomic );
267 }
268
269 GC gc_getProxy() nothrow
270 {
271 return instance;
272 }
273
274 export
275 {
276 void gc_setProxy( GC proxy )
277 {
278 foreach (root; instance.rootIter)
279 {
280 proxy.addRoot(root);
281 }
282
283 foreach (range; instance.rangeIter)
284 {
285 proxy.addRange(range.pbot, range.ptop - range.pbot, range.ti);
286 }
287
288 proxiedGC = instance; // remember initial GC to later remove roots
289 _instance = proxy;
290 }
291
292 void gc_clrProxy()
293 {
294 foreach (root; proxiedGC.rootIter)
295 {
296 instance.removeRoot(root);
297 }
298
299 foreach (range; proxiedGC.rangeIter)
300 {
301 instance.removeRange(range);
302 }
303
304 _instance = proxiedGC;
305 proxiedGC = null;
306 }
307 }
308 }