2 * This module contains a minimal garbage collector implementation according to
3 * Tango requirements. This library is mostly intended to serve as an example,
4 * but it is usable in applications which do not rely on a garbage collector
5 * to clean up memory (ie. when dynamic array resizing is not used, and all
6 * memory allocated with 'new' is freed deterministically with 'delete').
8 * Please note that block attribute data must be tracked, or at a minimum, the
9 * FINALIZE bit must be tracked for any allocated memory block because calling
10 * rt_finalize on a non-object block can result in an access violation. In the
11 * allocator below, this tracking is done via a leading uint bitmask. A real
12 * allocator may do better to store this data separately, similar to the basic
13 * GC normally used by Tango.
15 * Copyright: Public Domain
17 * Authors: Leandro Lucarella
24 import gc: NaiveGC, BlkInfo;
26 import tango.stdc.stdlib;
27 debug (gc_naive_iface) import tango.stdc.stdio;
31 class GCLock {} // TODO
35 extern (C) void gc_init()
37 debug (gc_naive_iface) printf("gc_init()\n");
41 extern (C) void gc_term()
43 debug (gc_naive_iface) printf("gc_term()\n");
47 extern (C) void gc_enable()
49 debug (gc_naive_iface) printf("gc_enable()\n");
53 extern (C) void gc_disable()
55 debug (gc_naive_iface) printf("gc_disable()\n");
59 extern (C) void gc_collect()
61 debug (gc_naive_iface) printf("gc_collect()\n");
65 extern (C) void gc_minimize()
67 debug (gc_naive_iface) printf("gc_minimize()\n");
71 extern (C) uint gc_getAttr(void* ptr)
73 debug (gc_naive_iface) printf("gc_getAttr(%p)\n", ptr);
74 return gc.getAttr(ptr);
77 // return the old value
78 extern (C) uint gc_setAttr(void* ptr, uint attr)
80 debug (gc_naive_iface) printf("gc_setAttr(%p, %u)\n", ptr, attr);
81 return gc.setAttr(ptr, attr);
84 extern (C) uint gc_clrAttr(void* ptr, uint attr)
86 debug (gc_naive_iface) printf("gc_clrAttr(%p, %u)\n", ptr, attr);
87 return gc.clrAttr(ptr, attr);
90 extern (C) void* gc_malloc(size_t size, uint attr=0)
92 debug (gc_naive_iface) printf("gc_malloc(%u, %u)\n", size, attr);
93 auto p = gc.malloc(size, attr);
94 debug (gc_naive_iface) printf("gc_malloc() -> %p\n", p);
98 extern (C) void* gc_calloc(size_t size, uint attr=0)
100 debug (gc_naive_iface) printf("gc_calloc(%u, %u)\n", size, attr);
101 return gc.calloc(size, attr);
104 extern (C) void* gc_realloc(void* ptr, size_t size, uint attr=0)
106 debug (gc_naive_iface) printf("gc_realloc(%p, %u, %u)\n", ptr, size, attr);
107 return gc.realloc(ptr, size, attr);
110 extern (C) size_t gc_extend(void* ptr, size_t min_size, size_t max_size)
112 debug (gc_naive_iface)
113 printf("gc_extend(%p, %u, %u)\n", ptr, min_size, max_size);
114 return gc.extend(ptr, min_size, max_size);
117 extern (C) size_t gc_reserve(size_t size)
119 debug (gc_naive_iface) printf("gc_reserve(%u)\n", size);
120 return gc.reserve(size);
123 extern (C) void gc_free(void* ptr)
125 debug (gc_naive_iface) printf("gc_free(%p)\n", ptr);
129 extern (C) void* gc_addrOf(void* ptr)
131 debug (gc_naive_iface) printf("gc_addrOf(%p)\n", ptr);
132 return gc.addrOf(ptr);
135 // TODO: acepta un address que no sea el base?
136 // es valido aceptar un ptr que no pertenezca al heap?
137 extern (C) size_t gc_sizeOf(void* ptr)
139 debug (gc_naive_iface) printf("gc_sizeOf(%p)\n", ptr);
140 return gc.sizeOf(ptr);
143 // TODO: acepta un address que no sea el base?
144 // es valido aceptar un ptr que no pertenezca al heap?
145 extern (C) BlkInfo gc_query(void* ptr)
147 debug (gc_naive_iface) printf("gc_query(%p)\n", ptr);
148 return gc.query(ptr);
151 extern (C) void gc_addRoot(void* ptr)
153 debug (gc_naive_iface) printf("gc_addRoot(%p)\n", ptr);
157 extern (C) void gc_addRange(void* ptr, size_t size)
159 debug (gc_naive_iface) printf("gc_addRange(%p, %u)\n", ptr, size);
160 gc.addRange(ptr, size);
163 extern (C) void gc_removeRoot(void* ptr)
165 debug (gc_naive_iface) printf("gc_removeRoot(%p)\n", ptr);
169 extern (C) void gc_removeRange(void* ptr)
171 debug (gc_naive_iface) printf("gc_removeRange(%p)\n", ptr);
175 // vim: set et sw=4 sts=4 :