132
|
1 /**
|
|
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').
|
|
7 *
|
|
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.
|
|
14 *
|
|
15 * Copyright: Public Domain
|
|
16 * License: Public Domain
|
|
17 * Authors: Sean Kelly
|
|
18 */
|
|
19
|
|
20 private import tango.stdc.stdlib;
|
|
21
|
|
22 private
|
|
23 {
|
|
24 enum BlkAttr : uint
|
|
25 {
|
|
26 FINALIZE = 0b0000_0001,
|
|
27 NO_SCAN = 0b0000_0010,
|
|
28 NO_MOVE = 0b0000_0100,
|
|
29 ALL_BITS = 0b1111_1111
|
|
30 }
|
|
31
|
|
32 struct BlkInfo
|
|
33 {
|
|
34 void* base;
|
|
35 size_t size;
|
|
36 uint attr;
|
|
37 }
|
|
38
|
|
39 extern (C) void thread_init();
|
|
40 extern (C) void onOutOfMemoryError();
|
|
41 }
|
|
42
|
|
43 extern (C) void gc_init()
|
|
44 {
|
|
45 // NOTE: The GC must initialize the thread library before its first
|
|
46 // collection, and always before returning from gc_init().
|
|
47 thread_init();
|
|
48 }
|
|
49
|
|
50 extern (C) void gc_term()
|
|
51 {
|
|
52
|
|
53 }
|
|
54
|
|
55 extern (C) void gc_enable()
|
|
56 {
|
|
57
|
|
58 }
|
|
59
|
|
60 extern (C) void gc_disable()
|
|
61 {
|
|
62
|
|
63 }
|
|
64
|
|
65 extern (C) void gc_collect()
|
|
66 {
|
|
67
|
|
68 }
|
|
69
|
|
70 extern (C) uint gc_getAttr( void* p )
|
|
71 {
|
|
72 return 0;
|
|
73 }
|
|
74
|
|
75 extern (C) uint gc_setAttr( void* p, uint a )
|
|
76 {
|
|
77 return 0;
|
|
78 }
|
|
79
|
|
80 extern (C) uint gc_clrAttr( void* p, uint a )
|
|
81 {
|
|
82 return 0;
|
|
83 }
|
|
84
|
|
85 extern (C) void* gc_malloc( size_t sz, uint ba = 0 )
|
|
86 {
|
|
87 void* p = malloc( sz );
|
|
88
|
|
89 if( sz && p is null )
|
|
90 onOutOfMemoryError();
|
|
91 return p;
|
|
92 }
|
|
93
|
|
94 extern (C) void* gc_calloc( size_t sz, uint ba = 0 )
|
|
95 {
|
|
96 void* p = calloc( 1, sz );
|
|
97
|
|
98 if( sz && p is null )
|
|
99 onOutOfMemoryError();
|
|
100 return p;
|
|
101 }
|
|
102
|
|
103 extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0 )
|
|
104 {
|
|
105 p = realloc( p, sz );
|
|
106
|
|
107 if( sz && p is null )
|
|
108 onOutOfMemoryError();
|
|
109 return p;
|
|
110 }
|
|
111
|
|
112 extern (C) size_t gc_extend( void* p, size_t mx, size_t sz )
|
|
113 {
|
|
114 return 0;
|
|
115 }
|
|
116
|
|
117 extern (C) void gc_free( void* p )
|
|
118 {
|
|
119 free( p );
|
|
120 }
|
|
121
|
|
122 extern (C) void* gc_addrOf( void* p )
|
|
123 {
|
|
124 return null;
|
|
125 }
|
|
126
|
|
127 extern (C) size_t gc_sizeOf( void* p )
|
|
128 {
|
|
129 return 0;
|
|
130 }
|
|
131
|
|
132 extern (C) BlkInfo gc_query( void* p )
|
|
133 {
|
|
134 return BlkInfo.init;
|
|
135 }
|
|
136
|
|
137 extern (C) void gc_addRoot( void* p )
|
|
138 {
|
|
139
|
|
140 }
|
|
141
|
|
142 extern (C) void gc_addRange( void* p, size_t sz )
|
|
143 {
|
|
144
|
|
145 }
|
|
146
|
|
147 extern (C) void gc_removeRoot( void *p )
|
|
148 {
|
|
149
|
|
150 }
|
|
151
|
|
152 extern (C) void gc_removeRange( void *p )
|
|
153 {
|
|
154
|
|
155 }
|