2 * This module contains a minimal garbage collector implementation according to
3 * published requirements. This library is mostly intended to serve as an
4 * example, but it is usable in applications which do not rely on a garbage
5 * collector to clean up memory (ie. when dynamic array resizing is not used,
6 * and all memory allocated with 'new' is freed deterministically with
9 * Please note that block attribute data must be tracked, or at a minimum, the
10 * FINALIZE bit must be tracked for any allocated memory block because calling
11 * rt_finalize on a non-object block can result in an access violation. In the
12 * allocator below, this tracking is done via a leading uint bitmask. A real
13 * allocator may do better to store this data separately, similar to the basic
16 * Copyright: Public Domain
17 * License: Public Domain
25 import core.stdc.stdlib;
29 FINALIZE = 0b0000_0001,
30 NO_SCAN = 0b0000_0010,
31 NO_MOVE = 0b0000_0100,
32 ALL_BITS = 0b1111_1111
42 extern (C) void thread_init();
43 extern (C) void onOutOfMemoryError();
47 extern (C) void function() gc_enable;
48 extern (C) void function() gc_disable;
49 extern (C) void function() gc_collect;
50 extern (C) void function() gc_minimize;
52 extern (C) uint function(void*) gc_getAttr;
53 extern (C) uint function(void*, uint) gc_setAttr;
54 extern (C) uint function(void*, uint) gc_clrAttr;
56 extern (C) void* function(size_t, uint) gc_malloc;
57 extern (C) void* function(size_t, uint) gc_calloc;
58 extern (C) void* function(void*, size_t, uint ba) gc_realloc;
59 extern (C) size_t function(void*, size_t, size_t) gc_extend;
60 extern (C) size_t function(size_t) gc_reserve;
61 extern (C) void function(void*) gc_free;
63 extern (C) void* function(void*) gc_addrOf;
64 extern (C) size_t function(void*) gc_sizeOf;
66 extern (C) BlkInfo function(void*) gc_query;
68 extern (C) void function(void*) gc_addRoot;
69 extern (C) void function(void*, size_t) gc_addRange;
71 extern (C) void function(void*) gc_removeRoot;
72 extern (C) void function(void*) gc_removeRange;
80 pthis.gc_enable = &gc_enable;
81 pthis.gc_disable = &gc_disable;
82 pthis.gc_collect = &gc_collect;
83 pthis.gc_minimize = &gc_minimize;
85 pthis.gc_getAttr = &gc_getAttr;
86 pthis.gc_setAttr = &gc_setAttr;
87 pthis.gc_clrAttr = &gc_clrAttr;
89 pthis.gc_malloc = &gc_malloc;
90 pthis.gc_calloc = &gc_calloc;
91 pthis.gc_realloc = &gc_realloc;
92 pthis.gc_extend = &gc_extend;
93 pthis.gc_reserve = &gc_reserve;
94 pthis.gc_free = &gc_free;
96 pthis.gc_addrOf = &gc_addrOf;
97 pthis.gc_sizeOf = &gc_sizeOf;
99 pthis.gc_query = &gc_query;
101 pthis.gc_addRoot = &gc_addRoot;
102 pthis.gc_addRange = &gc_addRange;
104 pthis.gc_removeRoot = &gc_removeRoot;
105 pthis.gc_removeRange = &gc_removeRange;
117 Range* ranges = null;
121 extern (C) void gc_init()
123 // NOTE: The GC must initialize the thread library before its first
124 // collection, and always before returning from gc_init().
129 extern (C) void gc_term()
135 extern (C) void gc_enable()
139 return proxy.gc_enable();
142 extern (C) void gc_disable()
146 return proxy.gc_disable();
149 extern (C) void gc_collect()
153 return proxy.gc_collect();
156 extern (C) void gc_minimize()
160 return proxy.gc_minimize();
163 extern (C) uint gc_getAttr( void* p )
167 return proxy.gc_getAttr( p );
170 extern (C) uint gc_setAttr( void* p, uint a )
174 return proxy.gc_setAttr( p, a );
177 extern (C) uint gc_clrAttr( void* p, uint a )
181 return proxy.gc_clrAttr( p, a );
184 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
188 void* p = malloc( sz );
190 if( sz && p is null )
191 onOutOfMemoryError();
194 return proxy.gc_malloc( sz, ba );
197 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
201 void* p = calloc( 1, sz );
203 if( sz && p is null )
204 onOutOfMemoryError();
207 return proxy.gc_calloc( sz, ba );
210 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
214 p = realloc( p, sz );
216 if( sz && p is null )
217 onOutOfMemoryError();
220 return proxy.gc_realloc( p, sz, ba );
223 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
227 return proxy.gc_extend( p, mx, sz );
230 extern (C) size_t gc_reserve( size_t sz )
234 return proxy.gc_reserve( sz );
237 extern (C) void gc_free( void* p )
241 return proxy.gc_free( p );
244 extern (C) void* gc_addrOf( void* p )
248 return proxy.gc_addrOf( p );
251 extern (C) size_t gc_sizeOf( void* p )
255 return proxy.gc_sizeOf( p );
258 extern (C) BlkInfo gc_query( void* p )
262 return proxy.gc_query( p );
265 extern (C) void gc_addRoot( void* p )
269 void** r = cast(void**) realloc( roots,
270 (nroots+1) * roots[0].sizeof );
272 onOutOfMemoryError();
277 return proxy.gc_addRoot( p );
280 extern (C) void gc_addRange( void* p, size_t sz )
284 Range* r = cast(Range*) realloc( ranges,
285 (nranges+1) * ranges[0].sizeof );
287 onOutOfMemoryError();
293 return proxy.gc_addRange( p, sz );
296 extern (C) void gc_removeRoot( void *p )
300 for( size_t i = 0; i < nroots; ++i )
304 roots[i] = roots[--nroots];
310 return proxy.gc_removeRoot( p );
313 extern (C) void gc_removeRange( void *p )
317 for( size_t i = 0; i < nranges; ++i )
319 if( ranges[i].pos is p )
321 ranges[i] = ranges[--nranges];
327 return proxy.gc_removeRange( p );
330 extern (C) Proxy* gc_getProxy()
335 export extern (C) void gc_setProxy( Proxy* p )
339 // TODO: Decide if this is an error condition.
342 foreach( r; roots[0 .. nroots] )
343 proxy.gc_addRoot( r );
344 foreach( r; ranges[0 .. nranges] )
345 proxy.gc_addRange( r.pos, r.len );
348 export extern (C) void gc_clrProxy()
350 foreach( r; ranges[0 .. nranges] )
351 proxy.gc_removeRange( r.pos );
352 foreach( r; roots[0 .. nroots] )
353 proxy.gc_removeRoot( r );