This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
proxy.d
379 lines (325 loc) · 9.5 KB
/
proxy.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* Contains the external GC interface.
*
* Copyright: Copyright Digital Mars 2005 - 2013.
* License: <a href="http://www.boost.org/LICENSE_1_0.txt">Boost License 1.0</a>.
* Authors: Walter Bright, Sean Kelly
*/
/* Copyright Digital Mars 2005 - 2013.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
module gc.proxy;
import gc.gc;
import gc.stats;
import core.stdc.stdlib;
private
{
version = GCCLASS;
version( GCCLASS )
alias GC gc_t;
else
alias GC* gc_t;
__gshared gc_t _gc;
static import core.memory;
alias BlkInfo = core.memory.GC.BlkInfo;
extern (C) void thread_init();
extern (C) void thread_term();
struct Proxy
{
extern (C)
{
void function() gc_enable;
void function() gc_disable;
nothrow:
void function() gc_collect;
void function() gc_minimize;
uint function(void*) gc_getAttr;
uint function(void*, uint) gc_setAttr;
uint function(void*, uint) gc_clrAttr;
void* function(size_t, uint) gc_malloc;
BlkInfo function(size_t, uint) gc_qalloc;
void* function(size_t, uint) gc_calloc;
void* function(void*, size_t, uint ba) gc_realloc;
size_t function(void*, size_t, size_t) gc_extend;
size_t function(size_t) gc_reserve;
void function(void*) gc_free;
void* function(void*) gc_addrOf;
size_t function(void*) gc_sizeOf;
BlkInfo function(void*) gc_query;
void function(void*) gc_addRoot;
void function(void*, size_t) gc_addRange;
void function(void*) gc_removeRoot;
void function(void*) gc_removeRange;
void function(in void[]) gc_runFinalizers;
}
}
__gshared Proxy pthis;
__gshared Proxy* proxy;
void initProxy()
{
pthis.gc_enable = &gc_enable;
pthis.gc_disable = &gc_disable;
pthis.gc_collect = &gc_collect;
pthis.gc_minimize = &gc_minimize;
pthis.gc_getAttr = &gc_getAttr;
pthis.gc_setAttr = &gc_setAttr;
pthis.gc_clrAttr = &gc_clrAttr;
pthis.gc_malloc = &gc_malloc;
pthis.gc_qalloc = &gc_qalloc;
pthis.gc_calloc = &gc_calloc;
pthis.gc_realloc = &gc_realloc;
pthis.gc_extend = &gc_extend;
pthis.gc_reserve = &gc_reserve;
pthis.gc_free = &gc_free;
pthis.gc_addrOf = &gc_addrOf;
pthis.gc_sizeOf = &gc_sizeOf;
pthis.gc_query = &gc_query;
pthis.gc_addRoot = &gc_addRoot;
pthis.gc_addRange = &gc_addRange;
pthis.gc_removeRoot = &gc_removeRoot;
pthis.gc_removeRange = &gc_removeRange;
pthis.gc_runFinalizers = &gc_runFinalizers;
}
}
extern (C)
{
void gc_init()
{
version (GCCLASS)
{ void* p;
ClassInfo ci = typeid(GC);
p = malloc(ci.init.length);
(cast(byte*)p)[0 .. ci.init.length] = ci.init[];
_gc = cast(GC)p;
}
else
{
_gc = cast(GC*) calloc(1, GC.sizeof);
}
_gc.initialize();
// NOTE: The GC must initialize the thread library
// before its first collection.
thread_init();
initProxy();
}
void gc_term()
{
// NOTE: There may be daemons threads still running when this routine is
// called. If so, cleaning memory out from under then is a good
// way to make them crash horribly. This probably doesn't matter
// much since the app is supposed to be shutting down anyway, but
// I'm disabling cleanup for now until I can think about it some
// more.
//
// NOTE: Due to popular demand, this has been re-enabled. It still has
// the problems mentioned above though, so I guess we'll see.
_gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
// static data area, roots, and ranges.
thread_term();
_gc.Dtor();
free(cast(void*)_gc);
_gc = null;
}
void gc_enable()
{
if( proxy is null )
return _gc.enable();
return proxy.gc_enable();
}
void gc_disable()
{
if( proxy is null )
return _gc.disable();
return proxy.gc_disable();
}
void gc_collect() nothrow
{
if( proxy is null )
{
_gc.fullCollect();
return;
}
return proxy.gc_collect();
}
void gc_minimize() nothrow
{
if( proxy is null )
return _gc.minimize();
return proxy.gc_minimize();
}
uint gc_getAttr( void* p ) nothrow
{
if( proxy is null )
return _gc.getAttr( p );
return proxy.gc_getAttr( p );
}
uint gc_setAttr( void* p, uint a ) nothrow
{
if( proxy is null )
return _gc.setAttr( p, a );
return proxy.gc_setAttr( p, a );
}
uint gc_clrAttr( void* p, uint a ) nothrow
{
if( proxy is null )
return _gc.clrAttr( p, a );
return proxy.gc_clrAttr( p, a );
}
void* gc_malloc( size_t sz, uint ba = 0 ) nothrow
{
if( proxy is null )
return _gc.malloc( sz, ba );
return proxy.gc_malloc( sz, ba );
}
BlkInfo gc_qalloc( size_t sz, uint ba = 0 ) nothrow
{
if( proxy is null )
{
BlkInfo retval;
retval.base = _gc.malloc( sz, ba, &retval.size );
retval.attr = ba;
return retval;
}
return proxy.gc_qalloc( sz, ba );
}
void* gc_calloc( size_t sz, uint ba = 0 ) nothrow
{
if( proxy is null )
return _gc.calloc( sz, ba );
return proxy.gc_calloc( sz, ba );
}
void* gc_realloc( void* p, size_t sz, uint ba = 0 ) nothrow
{
if( proxy is null )
return _gc.realloc( p, sz, ba );
return proxy.gc_realloc( p, sz, ba );
}
size_t gc_extend( void* p, size_t mx, size_t sz ) nothrow
{
if( proxy is null )
return _gc.extend( p, mx, sz );
return proxy.gc_extend( p, mx, sz );
}
size_t gc_reserve( size_t sz ) nothrow
{
if( proxy is null )
return _gc.reserve( sz );
return proxy.gc_reserve( sz );
}
void gc_free( void* p ) nothrow
{
if( proxy is null )
return _gc.free( p );
return proxy.gc_free( p );
}
void* gc_addrOf( void* p ) nothrow
{
if( proxy is null )
return _gc.addrOf( p );
return proxy.gc_addrOf( p );
}
size_t gc_sizeOf( void* p ) nothrow
{
if( proxy is null )
return _gc.sizeOf( p );
return proxy.gc_sizeOf( p );
}
BlkInfo gc_query( void* p ) nothrow
{
if( proxy is null )
return _gc.query( p );
return proxy.gc_query( p );
}
// NOTE: This routine is experimental. The stats or function name may change
// before it is made officially available.
GCStats gc_stats() nothrow
{
if( proxy is null )
{
GCStats stats = void;
_gc.getStats( stats );
return stats;
}
// TODO: Add proxy support for this once the layout of GCStats is
// finalized.
//return proxy.gc_stats();
return GCStats.init;
}
void gc_addRoot( void* p ) nothrow
{
if( proxy is null )
return _gc.addRoot( p );
return proxy.gc_addRoot( p );
}
void gc_addRange( void* p, size_t sz ) nothrow
{
if( proxy is null )
return _gc.addRange( p, sz );
return proxy.gc_addRange( p, sz );
}
void gc_removeRoot( void* p ) nothrow
{
if( proxy is null )
return _gc.removeRoot( p );
return proxy.gc_removeRoot( p );
}
void gc_removeRange( void* p ) nothrow
{
if( proxy is null )
return _gc.removeRange( p );
return proxy.gc_removeRange( p );
}
void gc_runFinalizers( in void[] segment ) nothrow
{
if( proxy is null )
return _gc.runFinalizers( segment );
return proxy.gc_runFinalizers( segment );
}
Proxy* gc_getProxy() nothrow
{
return &pthis;
}
export
{
void gc_setProxy( Proxy* p )
{
if( proxy !is null )
{
// TODO: Decide if this is an error condition.
}
proxy = p;
// @@@BUG12739@@@
_gc.rootIter()(
(ref Root r) {
proxy.gc_addRoot( r );
return 0;
}
);
_gc.rangeIter()(
(ref Range r) {
proxy.gc_addRange( r.pbot, r.ptop - r.pbot );
return 0;
}
);
}
void gc_clrProxy()
{
// @@@BUG12739@@@
_gc.rangeIter()(
(ref Range r) {
proxy.gc_removeRange( r.pbot );
return 0;
}
);
_gc.rootIter()(
(ref Root r) {
proxy.gc_removeRoot( r );
return 0;
}
);
proxy = null;
}
}
}