Mercurial > projects > ldc
comparison druntime/src/gc/basic/gc.d @ 759:d3eb054172f9
Added copy of druntime from DMD 2.020 modified for LDC.
author | Tomas Lindquist Olsen <tomas.l.olsen@gmail.com> |
---|---|
date | Tue, 11 Nov 2008 01:52:37 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
758:f04dde6e882c | 759:d3eb054172f9 |
---|---|
1 /** | |
2 * This module contains the garbage collector front-end. | |
3 * | |
4 * Copyright: Copyright (C) 2005-2006 Digital Mars, www.digitalmars.com. | |
5 * All rights reserved. | |
6 * License: | |
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. | |
10 * | |
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 | |
14 * restrictions: | |
15 * | |
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 | |
23 * distribution. | |
24 * Authors: Walter Bright, Sean Kelly | |
25 */ | |
26 | |
27 module gc.gc; | |
28 | |
29 private import gcx; | |
30 private import gcstats; | |
31 private import stdc.stdlib; | |
32 | |
33 version=GCCLASS; | |
34 | |
35 version (GCCLASS) | |
36 alias GC gc_t; | |
37 else | |
38 alias GC* gc_t; | |
39 | |
40 gc_t _gc; | |
41 | |
42 extern (C) void thread_init(); | |
43 | |
44 extern (C) void gc_init() | |
45 { | |
46 version (GCCLASS) | |
47 { void* p; | |
48 ClassInfo ci = GC.classinfo; | |
49 | |
50 p = malloc(ci.init.length); | |
51 (cast(byte*)p)[0 .. ci.init.length] = ci.init[]; | |
52 _gc = cast(GC)p; | |
53 } | |
54 else | |
55 { | |
56 _gc = cast(GC*) calloc(1, GC.sizeof); | |
57 } | |
58 _gc.initialize(); | |
59 // NOTE: The GC must initialize the thread library | |
60 // before its first collection. | |
61 thread_init(); | |
62 } | |
63 | |
64 extern (C) void gc_term() | |
65 { | |
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 | |
71 // more. | |
72 // | |
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. | |
77 _gc.Dtor(); | |
78 } | |
79 | |
80 extern (C) void gc_enable() | |
81 { | |
82 _gc.enable(); | |
83 } | |
84 | |
85 extern (C) void gc_disable() | |
86 { | |
87 _gc.disable(); | |
88 } | |
89 | |
90 extern (C) void gc_collect() | |
91 { | |
92 _gc.fullCollect(); | |
93 } | |
94 | |
95 | |
96 extern (C) void gc_minimize() | |
97 { | |
98 _gc.minimize(); | |
99 } | |
100 | |
101 extern (C) uint gc_getAttr( void* p ) | |
102 { | |
103 return _gc.getAttr( p ); | |
104 } | |
105 | |
106 extern (C) uint gc_setAttr( void* p, uint a ) | |
107 { | |
108 return _gc.setAttr( p, a ); | |
109 } | |
110 | |
111 extern (C) uint gc_clrAttr( void* p, uint a ) | |
112 { | |
113 return _gc.clrAttr( p, a ); | |
114 } | |
115 | |
116 extern (C) void* gc_malloc( size_t sz, uint ba = 0 ) | |
117 { | |
118 return _gc.malloc( sz, ba ); | |
119 } | |
120 | |
121 extern (C) void* gc_calloc( size_t sz, uint ba = 0 ) | |
122 { | |
123 return _gc.calloc( sz, ba ); | |
124 } | |
125 | |
126 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 ) | |
127 { | |
128 return _gc.realloc( p, sz, ba ); | |
129 } | |
130 | |
131 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz ) | |
132 { | |
133 return _gc.extend( p, mx, sz ); | |
134 } | |
135 | |
136 extern (C) size_t gc_reserve( size_t sz ) | |
137 { | |
138 return _gc.reserve( sz ); | |
139 } | |
140 | |
141 extern (C) void gc_free( void* p ) | |
142 { | |
143 _gc.free( p ); | |
144 } | |
145 | |
146 extern (C) void* gc_addrOf( void* p ) | |
147 { | |
148 return _gc.addrOf( p ); | |
149 } | |
150 | |
151 extern (C) size_t gc_sizeOf( void* p ) | |
152 { | |
153 return _gc.sizeOf( p ); | |
154 } | |
155 | |
156 extern (C) BlkInfo gc_query( void* p ) | |
157 { | |
158 return _gc.query( p ); | |
159 } | |
160 | |
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() | |
164 { | |
165 GCStats stats = void; | |
166 _gc.getStats( stats ); | |
167 return stats; | |
168 } | |
169 | |
170 extern (C) void gc_addRoot( void* p ) | |
171 { | |
172 _gc.addRoot( p ); | |
173 } | |
174 | |
175 extern (C) void gc_addRange( void* p, size_t sz ) | |
176 { | |
177 _gc.addRange( p, sz ); | |
178 } | |
179 | |
180 extern (C) void gc_removeRoot( void *p ) | |
181 { | |
182 _gc.removeRoot( p ); | |
183 } | |
184 | |
185 extern (C) void gc_removeRange( void *p ) | |
186 { | |
187 _gc.removeRange( p ); | |
188 } | |
189 | |
190 extern (C) void* gc_getHandle() | |
191 { | |
192 return cast(void*)_gc; | |
193 } | |
194 | |
195 extern (C) void gc_setHandle(void* p) | |
196 { | |
197 void* oldp = gc_getHandle(); | |
198 gc_t g = cast(gc_t)p; | |
199 if (g.gcversion != gcx.GCVERSION) | |
200 throw new Error("incompatible gc versions"); | |
201 | |
202 // Add our static data to the new gc | |
203 GC.scanStaticData(g); | |
204 | |
205 _gc = g; | |
206 } | |
207 | |
208 extern (C) void gc_endHandle() | |
209 { | |
210 GC.unscanStaticData(_gc); | |
211 } |