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
21 private import stdc.stdlib;
27 FINALIZE = 0b0000_0001,
28 NO_SCAN = 0b0000_0010,
29 NO_MOVE = 0b0000_0100,
30 ALL_BITS = 0b1111_1111
40 extern (C) void thread_init();
41 extern (C) void onOutOfMemoryError();
44 extern (C) void gc_init()
46 // NOTE: The GC must initialize the thread library before its first
47 // collection, and always before returning from gc_init().
51 extern (C) void gc_term()
56 extern (C) void gc_enable()
61 extern (C) void gc_disable()
66 extern (C) void gc_collect()
71 extern (C) void gc_minimize()
76 extern (C) uint gc_getAttr( void* p )
81 extern (C) uint gc_setAttr( void* p, uint a )
86 extern (C) uint gc_clrAttr( void* p, uint a )
91 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
93 void* p = malloc( sz );
100 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
102 void* p = calloc( 1, sz );
104 if( sz && p is null )
105 onOutOfMemoryError();
109 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
111 p = realloc( p, sz );
113 if( sz && p is null )
114 onOutOfMemoryError();
118 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
123 extern (C) size_t gc_reserve( size_t sz )
128 extern (C) void gc_free( void* p )
133 extern (C) void* gc_addrOf( void* p )
138 extern (C) size_t gc_sizeOf( void* p )
143 extern (C) BlkInfo gc_query( void* p )
148 extern (C) void gc_addRoot( void* p )
153 extern (C) void gc_addRange( void* p, size_t sz )
158 extern (C) void gc_removeRoot( void *p )
163 extern (C) void gc_removeRange( void *p )