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;
31 import libc = rt.gc.cdgc.libc;
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;
69 void* p = libc.malloc(ci.init.length);
70 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
75 _gc = cast(GC*) libc.calloc(1, GC.sizeof);
78 version (DigitalMars) version(OSX) {
81 // NOTE: The GC must initialize the thread library
82 // before its first collection.
86 extern (C) void gc_term()
88 if (_termCleanupLevel<1) {
90 } else if (_termCleanupLevel==2){
91 // a more complete cleanup
92 // NOTE: There may be daemons threads still running when this routine is
93 // called. If so, cleaning memory out from under then is a good
94 // way to make them crash horribly.
95 // Often this probably doesn't matter much since the app is
96 // supposed to be shutting down anyway, but for example tests might
97 // crash (and be considerd failed even if the test was ok).
98 // thus this is not the default and should be enabled by
99 // I'm disabling cleanup for now until I can think about it some
102 _gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
103 // 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);