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
28 private import gcstats;
29 private import tango.stdc.stdlib;
40 private int _termCleanupLevel=1;
42 /// sets the cleanup level done by gc
43 /// (0: none, 1: fullCollect, 2: fullCollectNoStack (might crash daemonThreads))
44 /// result !=0 if the value was invalid
45 extern (C) int gc_setTermCleanupLevel(int cLevel){
46 if (cLevel<0 || cLevel>2) return cLevel;
47 _termCleanupLevel=cLevel;
51 /// returns the cleanup level done by gc
52 extern (C) int gc_getTermCleanupLevel(){
53 return _termCleanupLevel;
56 version (DigitalMars) version(OSX) {
57 extern(C) void _d_osx_image_init();
60 extern (C) void thread_init();
62 extern (C) void gc_init()
66 ClassInfo ci = GC.classinfo;
68 p = tango.stdc.stdlib.malloc(ci.init.length);
69 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
74 _gc = cast(GC*) tango.stdc.stdlib.calloc(1, GC.sizeof);
77 version (DigitalMars) version(OSX) {
80 // NOTE: The GC must initialize the thread library
81 // before its first collection.
85 extern (C) void gc_term()
87 if (_termCleanupLevel<1) {
89 } else if (_termCleanupLevel==2){
90 // a more complete cleanup
91 // NOTE: There may be daemons threads still running when this routine is
92 // called. If so, cleaning memory out from under then is a good
93 // way to make them crash horribly.
94 // Often this probably doesn't matter much since the app is
95 // supposed to be shutting down anyway, but for example tests might
96 // crash (and be considerd failed even if the test was ok).
97 // thus this is not the default and should be enabled by
98 // I'm disabling cleanup for now until I can think about it some
101 _gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
102 // static data area, roots, and ranges.
105 // default (safe) clenup
110 extern (C) void gc_enable()
115 extern (C) void gc_disable()
120 extern (C) void gc_collect()
126 extern (C) void gc_minimize()
131 extern (C) uint gc_getAttr( void* p )
133 return _gc.getAttr( p );
136 extern (C) uint gc_setAttr( void* p, uint a )
138 return _gc.setAttr( p, a );
141 extern (C) uint gc_clrAttr( void* p, uint a )
143 return _gc.clrAttr( p, a );
146 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
148 return _gc.malloc( sz, ba );
151 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
153 return _gc.calloc( sz, ba );
156 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
158 return _gc.realloc( p, sz, ba );
161 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
163 return _gc.extend( p, mx, sz );
166 extern (C) size_t gc_reserve( size_t sz )
168 return _gc.reserve( sz );
171 extern (C) void gc_free( void* p )
176 extern (C) void* gc_addrOf( void* p )
178 return _gc.addrOf( p );
181 extern (C) size_t gc_sizeOf( void* p )
183 return _gc.sizeOf( p );
186 extern (C) BlkInfo gc_query( void* p )
188 return _gc.query( p );
191 // NOTE: This routine is experimental. The stats or function name may change
192 // before it is made officially available.
193 extern (C) GCStats gc_stats()
195 GCStats stats = void;
196 _gc.getStats( stats );
200 extern (C) void gc_addRoot( void* p )
205 extern (C) void gc_addRange( void* p, size_t sz )
207 _gc.addRange( p, sz );
210 extern (C) void gc_removeRoot( void *p )
215 extern (C) void gc_removeRange( void *p )
217 _gc.removeRange( p );