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
31 import tango.stdc.stdlib;
42 private int _termCleanupLevel=1;
44 /// sets the cleanup level done by gc
45 /// (0: none, 1: fullCollect, 2: fullCollectNoStack (might crash daemonThreads))
46 /// result !=0 if the value was invalid
47 extern (C) int gc_setTermCleanupLevel(int cLevel){
48 if (cLevel<0 || cLevel>2) return cLevel;
49 _termCleanupLevel=cLevel;
53 /// returns the cleanup level done by gc
54 extern (C) int gc_getTermCleanupLevel(){
55 return _termCleanupLevel;
58 version (DigitalMars) version(OSX) {
59 extern(C) void _d_osx_image_init();
62 extern (C) void thread_init();
64 extern (C) void gc_init()
68 ClassInfo ci = GC.classinfo;
70 p = tango.stdc.stdlib.malloc(ci.init.length);
71 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
76 _gc = cast(GC*) tango.stdc.stdlib.calloc(1, GC.sizeof);
79 version (DigitalMars) version(OSX) {
82 // NOTE: The GC must initialize the thread library
83 // before its first collection.
87 extern (C) void gc_term()
89 if (_termCleanupLevel<1) {
91 } else if (_termCleanupLevel==2){
92 // a more complete cleanup
93 // NOTE: There may be daemons threads still running when this routine is
94 // called. If so, cleaning memory out from under then is a good
95 // way to make them crash horribly.
96 // Often this probably doesn't matter much since the app is
97 // supposed to be shutting down anyway, but for example tests might
98 // crash (and be considerd failed even if the test was ok).
99 // thus this is not the default and should be enabled by
100 // I'm disabling cleanup for now until I can think about it some
103 _gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
104 // static data area, roots, and ranges.
107 // default (safe) clenup
112 extern (C) void gc_enable()
117 extern (C) void gc_disable()
122 extern (C) void gc_collect()
128 extern (C) void gc_minimize()
133 extern (C) uint gc_getAttr( void* p )
135 return _gc.getAttr( p );
138 extern (C) uint gc_setAttr( void* p, uint a )
140 return _gc.setAttr( p, a );
143 extern (C) uint gc_clrAttr( void* p, uint a )
145 return _gc.clrAttr( p, a );
148 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
150 return _gc.malloc( sz, ba );
153 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
155 return _gc.calloc( sz, ba );
158 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
160 return _gc.realloc( p, sz, ba );
163 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
165 return _gc.extend( p, mx, sz );
168 extern (C) size_t gc_reserve( size_t sz )
170 return _gc.reserve( sz );
173 extern (C) void gc_free( void* p )
178 extern (C) void* gc_addrOf( void* p )
180 return _gc.addrOf( p );
183 extern (C) size_t gc_sizeOf( void* p )
185 return _gc.sizeOf( p );
188 extern (C) BlkInfo gc_query( void* p )
190 return _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()
197 GCStats stats = void;
198 _gc.getStats( stats );
202 extern (C) void gc_addRoot( void* p )
207 extern (C) void gc_addRange( void* p, size_t sz )
209 _gc.addRange( p, sz );
212 extern (C) void gc_removeRoot( void *p )
217 extern (C) void gc_removeRange( void *p )
219 _gc.removeRange( p );