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 stdc.stdlib;
40 extern (C) void thread_init();
42 extern (C) void gc_init()
46 ClassInfo ci = GC.classinfo;
48 p = malloc(ci.init.length);
49 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
54 _gc = cast(GC*) calloc(1, GC.sizeof);
57 // NOTE: The GC must initialize the thread library
58 // before its first collection.
62 extern (C) void gc_term()
64 // NOTE: There may be daemons threads still running when this routine is
65 // called. If so, cleaning memory out from under then is a good
66 // way to make them crash horribly. This probably doesn't matter
67 // much since the app is supposed to be shutting down anyway, but
68 // I'm disabling cleanup for now until I can think about it some
71 // NOTE: Due to popular demand, this has been re-enabled. It still has
72 // the problems mentioned above though, so I guess we'll see.
73 _gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
74 // static data area, roots, and ranges.
78 extern (C) void gc_enable()
83 extern (C) void gc_disable()
88 extern (C) void gc_collect()
94 extern (C) void gc_minimize()
99 extern (C) uint gc_getAttr( void* p )
101 return _gc.getAttr( p );
104 extern (C) uint gc_setAttr( void* p, uint a )
106 return _gc.setAttr( p, a );
109 extern (C) uint gc_clrAttr( void* p, uint a )
111 return _gc.clrAttr( p, a );
114 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
116 return _gc.malloc( sz, ba );
119 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
121 return _gc.calloc( sz, ba );
124 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
126 return _gc.realloc( p, sz, ba );
129 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
131 return _gc.extend( p, mx, sz );
134 extern (C) size_t gc_reserve( size_t sz )
136 return _gc.reserve( sz );
139 extern (C) void gc_free( void* p )
144 extern (C) void* gc_addrOf( void* p )
146 return _gc.addrOf( p );
149 extern (C) size_t gc_sizeOf( void* p )
151 return _gc.sizeOf( p );
154 extern (C) BlkInfo gc_query( void* p )
156 return _gc.query( p );
159 // NOTE: This routine is experimental. The stats or function name may change
160 // before it is made officially available.
161 extern (C) GCStats gc_stats()
163 GCStats stats = void;
164 _gc.getStats( stats );
168 extern (C) void gc_addRoot( void* p )
173 extern (C) void gc_addRange( void* p, size_t sz )
175 _gc.addRange( p, sz );
178 extern (C) void gc_removeRoot( void *p )
183 extern (C) void gc_removeRange( void *p )
185 _gc.removeRange( p );