2 * This module contains the garbage collector front-end.
4 * Copyright: Copyright (C) 2005-2006 Digital Mars, www.digitalmars.com.
7 * This software is provided 'as-is', without any express or implied
8 * warranty. In no event will the authors be held liable for any damages
9 * arising from the use of this software.
11 * Permission is granted to anyone to use this software for any purpose,
12 * including commercial applications, and to alter it and redistribute it
13 * freely, in both source and binary form, subject to the following
16 * o The origin of this software must not be misrepresented; you must not
17 * claim that you wrote the original software. If you use this software
18 * in a product, an acknowledgment in the product documentation would be
19 * appreciated but is not required.
20 * o Altered source versions must be plainly marked as such, and must not
21 * be misrepresented as being the original software.
22 * o This notice may not be removed or altered from any source
24 * Authors: Walter Bright, Sean Kelly
30 private import gcstats;
31 private import core.stdc.stdlib;
42 extern (C) void thread_init();
44 extern (C) void gc_init()
48 ClassInfo ci = GC.classinfo;
50 p = malloc(ci.init.length);
51 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
56 _gc = cast(GC*) calloc(1, GC.sizeof);
59 // NOTE: The GC must initialize the thread library
60 // before its first collection.
64 extern (C) void gc_term()
66 // NOTE: There may be daemons threads still running when this routine is
67 // called. If so, cleaning memory out from under then is a good
68 // way to make them crash horribly. This probably doesn't matter
69 // much since the app is supposed to be shutting down anyway, but
70 // I'm disabling cleanup for now until I can think about it some
73 // NOTE: Due to popular demand, this has been re-enabled. It still has
74 // the problems mentioned above though, so I guess we'll see.
75 _gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
76 // static data area, roots, and ranges.
80 extern (C) void gc_enable()
84 return proxy.gc_enable();
87 extern (C) void gc_disable()
91 return proxy.gc_disable();
94 extern (C) void gc_collect()
97 return _gc.fullCollect();
98 return proxy.gc_collect();
102 extern (C) void gc_minimize()
105 return _gc.minimize();
106 return proxy.gc_minimize();
109 extern (C) uint gc_getAttr( void* p )
112 return _gc.getAttr( p );
113 return proxy.gc_getAttr( p );
116 extern (C) uint gc_setAttr( void* p, uint a )
119 return _gc.setAttr( p, a );
120 return proxy.gc_setAttr( p, a );
123 extern (C) uint gc_clrAttr( void* p, uint a )
126 return _gc.clrAttr( p, a );
127 return proxy.gc_clrAttr( p, a );
130 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
133 return _gc.malloc( sz, ba );
134 return proxy.gc_malloc( sz, ba );
137 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
140 return _gc.calloc( sz, ba );
141 return proxy.gc_calloc( sz, ba );
144 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
147 return _gc.realloc( p, sz, ba );
148 return proxy.gc_realloc( p, sz, ba );
151 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
154 return _gc.extend( p, mx, sz );
155 return proxy.gc_extend( p, mx, sz );
158 extern (C) size_t gc_reserve( size_t sz )
161 return _gc.reserve( sz );
162 return proxy.gc_reserve( sz );
165 extern (C) void gc_free( void* p )
168 return _gc.free( p );
169 return proxy.gc_free( p );
172 extern (C) void* gc_addrOf( void* p )
175 return _gc.addrOf( p );
176 return proxy.gc_addrOf( p );
179 extern (C) size_t gc_sizeOf( void* p )
182 return _gc.sizeOf( p );
183 return proxy.gc_sizeOf( p );
186 extern (C) BlkInfo gc_query( void* p )
189 return _gc.query( p );
190 return proxy.gc_query( p );
193 // NOTE: This routine is experimental. The stats or function name may change
194 // before it is made officially available.
195 extern (C) GCStats gc_stats()
199 GCStats stats = void;
200 _gc.getStats( stats );
203 // TODO: Add proxy support for this once the layout of GCStats is
205 //return proxy.gc_stats();
209 extern (C) void gc_addRoot( void* p )
212 return _gc.addRoot( p );
213 return proxy.gc_addRoot( p );
216 extern (C) void gc_addRange( void* p, size_t sz )
219 return _gc.addRange( p, sz );
220 return proxy.gc_addRange( p, sz );
223 extern (C) void gc_removeRoot( void* p )
226 return _gc.removeRoot( p );
227 return proxy.gc_removeRoot( p );
230 extern (C) void gc_removeRange( void* p )
233 return _gc.removeRange( p );
234 return proxy.gc_removeRange( p );
239 extern (C) void function() gc_enable;
240 extern (C) void function() gc_disable;
241 extern (C) void function() gc_collect;
242 extern (C) void function() gc_minimize;
244 extern (C) uint function(void*) gc_getAttr;
245 extern (C) uint function(void*, uint) gc_setAttr;
246 extern (C) uint function(void*, uint) gc_clrAttr;
248 extern (C) void* function(size_t, uint) gc_malloc;
249 extern (C) void* function(size_t, uint) gc_calloc;
250 extern (C) void* function(void*, size_t, uint ba) gc_realloc;
251 extern (C) size_t function(void*, size_t, size_t) gc_extend;
252 extern (C) size_t function(size_t) gc_reserve;
253 extern (C) void function(void*) gc_free;
255 extern (C) void* function(void*) gc_addrOf;
256 extern (C) size_t function(void*) gc_sizeOf;
258 extern (C) BlkInfo function(void*) gc_query;
260 extern (C) void function(void*) gc_addRoot;
261 extern (C) void function(void*, size_t) gc_addRange;
263 extern (C) void function(void*) gc_removeRoot;
264 extern (C) void function(void*) gc_removeRange;
272 pthis.gc_enable = &gc_enable;
273 pthis.gc_disable = &gc_disable;
274 pthis.gc_collect = &gc_collect;
275 pthis.gc_minimize = &gc_minimize;
277 pthis.gc_getAttr = &gc_getAttr;
278 pthis.gc_setAttr = &gc_setAttr;
279 pthis.gc_clrAttr = &gc_clrAttr;
281 pthis.gc_malloc = &gc_malloc;
282 pthis.gc_calloc = &gc_calloc;
283 pthis.gc_realloc = &gc_realloc;
284 pthis.gc_extend = &gc_extend;
285 pthis.gc_reserve = &gc_reserve;
286 pthis.gc_free = &gc_free;
288 pthis.gc_addrOf = &gc_addrOf;
289 pthis.gc_sizeOf = &gc_sizeOf;
291 pthis.gc_query = &gc_query;
293 pthis.gc_addRoot = &gc_addRoot;
294 pthis.gc_addRange = &gc_addRange;
296 pthis.gc_removeRoot = &gc_removeRoot;
297 pthis.gc_removeRange = &gc_removeRange;
300 extern (C) Proxy* gc_getProxy()
305 export extern (C) void gc_setProxy( Proxy* p )
309 // TODO: Decide if this is an error condition.
312 // TODO: Add known ranges and roots to the proxy.
315 export extern (C) void gc_clrProxy()
317 // TODO: Remove known ranges and roots from the proxy.