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
27 module rt.gc.cdgc.iface;
29 import rt.gc.cdgc.gc: GC, BlkInfo;
30 import rt.gc.cdgc.stats: GCStats;
32 import cstdlib = tango.stdc.stdlib;
43 private int _termCleanupLevel=1;
45 /// sets the cleanup level done by gc
46 /// (0: none, 1: fullCollect, 2: fullCollectNoStack (might crash daemonThreads))
47 /// result !=0 if the value was invalid
48 extern (C) int gc_setTermCleanupLevel(int cLevel){
49 if (cLevel<0 || cLevel>2) return cLevel;
50 _termCleanupLevel=cLevel;
54 /// returns the cleanup level done by gc
55 extern (C) int gc_getTermCleanupLevel(){
56 return _termCleanupLevel;
59 version (DigitalMars) version(OSX) {
60 extern(C) void _d_osx_image_init();
63 extern (C) void thread_init();
65 extern (C) void gc_init()
69 ClassInfo ci = GC.classinfo;
70 void* p = cstdlib.malloc(ci.init.length);
71 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
76 _gc = cast(GC*) cstdlib.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.
106 // default (safe) clenup
111 extern (C) void gc_enable()
116 extern (C) void gc_disable()
121 extern (C) void gc_collect()
127 extern (C) void gc_minimize()
132 extern (C) uint gc_getAttr( void* p )
134 return _gc.getAttr( p );
137 extern (C) uint gc_setAttr( void* p, uint a )
139 return _gc.setAttr( p, a );
142 extern (C) uint gc_clrAttr( void* p, uint a )
144 return _gc.clrAttr( p, a );
147 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
149 return _gc.malloc( sz, ba );
152 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
154 return _gc.calloc( sz, ba );
157 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
159 return _gc.realloc( p, sz, ba );
162 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
164 return _gc.extend( p, mx, sz );
167 extern (C) size_t gc_reserve( size_t sz )
169 return _gc.reserve( sz );
172 extern (C) void gc_free( void* p )
177 extern (C) void* gc_addrOf( void* p )
179 return _gc.addrOf( p );
182 extern (C) size_t gc_sizeOf( void* p )
184 return _gc.sizeOf( p );
187 extern (C) BlkInfo gc_query( void* p )
189 return _gc.query( p );
192 // NOTE: This routine is experimental. The stats or function name may change
193 // before it is made officially available.
194 extern (C) GCStats gc_stats()
196 GCStats stats = void;
197 _gc.getStats( stats );
201 extern (C) void gc_addRoot( void* p )
206 extern (C) void gc_addRange( void* p, size_t sz )
208 _gc.addRange( p, sz );
211 extern (C) void gc_removeRoot( void *p )
216 extern (C) void gc_removeRange( void *p )
218 _gc.removeRange( p );
221 extern (C) void* gc_weakpointerCreate( Object r )
223 return _gc.weakpointerCreate(r);
226 extern (C) void gc_weakpointerDestroy( void* wp )
228 _gc.weakpointerDestroy(wp);
231 extern (C) Object gc_weakpointerGet( void* wp )
233 return _gc.weakpointerGet(wp);
237 // vim: set et sw=4 sts=4 :