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
30 private import gcstats;
31 private import stdc.stdlib;
42 extern (C) void thread_init();
44 extern (C) void gc_init()
48 ClassInfo ci = GC.classinfo;
50 p = malloc(ci.init.length);
51 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
56 _gc = cast(GC*) calloc(1, GC.sizeof);
59 // NOTE: The GC must initialize the thread library
60 // before its first collection.
64 extern (C) void gc_term()
66 // NOTE: There may be daemons threads still running when this routine is
67 // called. If so, cleaning memory out from under then is a good
68 // way to make them crash horribly. This probably doesn't matter
69 // much since the app is supposed to be shutting down anyway, but
70 // I'm disabling cleanup for now until I can think about it some
73 // NOTE: Due to popular demand, this has been re-enabled. It still has
74 // the problems mentioned above though, so I guess we'll see.
75 _gc.fullCollectNoStack(); // not really a 'collect all' -- still scans
76 // static data area, roots, and ranges.
80 extern (C) void gc_enable()
85 extern (C) void gc_disable()
90 extern (C) void gc_collect()
96 extern (C) void gc_minimize()
101 extern (C) uint gc_getAttr( void* p )
103 return _gc.getAttr( p );
106 extern (C) uint gc_setAttr( void* p, uint a )
108 return _gc.setAttr( p, a );
111 extern (C) uint gc_clrAttr( void* p, uint a )
113 return _gc.clrAttr( p, a );
116 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
118 return _gc.malloc( sz, ba );
121 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
123 return _gc.calloc( sz, ba );
126 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
128 return _gc.realloc( p, sz, ba );
131 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
133 return _gc.extend( p, mx, sz );
136 extern (C) size_t gc_reserve( size_t sz )
138 return _gc.reserve( sz );
141 extern (C) void gc_free( void* p )
146 extern (C) void* gc_addrOf( void* p )
148 return _gc.addrOf( p );
151 extern (C) size_t gc_sizeOf( void* p )
153 return _gc.sizeOf( p );
156 extern (C) BlkInfo gc_query( void* p )
158 return _gc.query( p );
161 // NOTE: This routine is experimental. The stats or function name may change
162 // before it is made officially available.
163 extern (C) GCStats gc_stats()
165 GCStats stats = void;
166 _gc.getStats( stats );
170 extern (C) void gc_addRoot( void* p )
175 extern (C) void gc_addRange( void* p, size_t sz )
177 _gc.addRange( p, sz );
180 extern (C) void gc_removeRoot( void *p )
185 extern (C) void gc_removeRange( void *p )
187 _gc.removeRange( p );