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
23 private 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();
46 extern (C) void gc_init()
48 // NOTE: The GC must initialize the thread library before its first
49 // collection, and always before returning from gc_init().
53 extern (C) void gc_term()
58 extern (C) void gc_enable()
63 extern (C) void gc_disable()
68 extern (C) void gc_collect()
73 extern (C) void gc_minimize()
78 extern (C) uint gc_getAttr( void* p )
83 extern (C) uint gc_setAttr( void* p, uint a )
88 extern (C) uint gc_clrAttr( void* p, uint a )
93 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
95 void* p = malloc( sz );
102 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
104 void* p = calloc( 1, sz );
106 if( sz && p is null )
107 onOutOfMemoryError();
111 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
113 p = realloc( p, sz );
115 if( sz && p is null )
116 onOutOfMemoryError();
120 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
125 extern (C) size_t gc_reserve( size_t sz )
130 extern (C) void gc_free( void* p )
135 extern (C) void* gc_addrOf( void* p )
140 extern (C) size_t gc_sizeOf( void* p )
145 extern (C) BlkInfo gc_query( void* p )
150 extern (C) void gc_addRoot( void* p )
155 extern (C) void gc_addRange( void* p, size_t sz )
160 extern (C) void gc_removeRoot( void *p )
165 extern (C) void gc_removeRange( void *p )
170 extern (C) void* gc_getHandle()
175 extern (C) void gc_setHandle(void* p)
179 extern (C) void gc_endHandle()