This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
/
gc.d
238 lines (194 loc) · 4.88 KB
/
gc.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
module gc.impl.proto.gc;
import gc.config;
import gc.gcinterface;
import rt.util.container.array;
import cstdlib = core.stdc.stdlib : calloc, free, malloc, realloc;
static import core.memory;
extern (C) void onOutOfMemoryError(void* pretend_sideffect = null) @trusted pure nothrow @nogc; /* dmd @@@BUG11461@@@ */
private
{
extern (C) void gc_init_nothrow() nothrow @nogc;
extern (C) void gc_term();
extern (C) void gc_enable() nothrow;
extern (C) void gc_disable() nothrow;
extern (C) void* gc_malloc( size_t sz, uint ba = 0, const TypeInfo = null ) pure nothrow;
extern (C) void* gc_calloc( size_t sz, uint ba = 0, const TypeInfo = null ) pure nothrow;
extern (C) BlkInfo gc_qalloc( size_t sz, uint ba = 0, const TypeInfo = null ) pure nothrow;
extern (C) void* gc_realloc( void* p, size_t sz, uint ba = 0, const TypeInfo = null ) pure nothrow;
extern (C) size_t gc_reserve( size_t sz ) nothrow;
extern (C) void gc_addRange( void* p, size_t sz, const TypeInfo ti = null ) nothrow @nogc;
extern (C) void gc_addRoot( void* p ) nothrow @nogc;
}
class ProtoGC : GC
{
Array!Root roots;
Array!Range ranges;
// Call this function when initializing the real GC
// upon ProtoGC term. This function should be called
// after the real GC is in place.
void term()
{
// Transfer all ranges
foreach (ref r; ranges)
{
// Range(p, p + sz, cast() ti)
gc_addRange(r.pbot, r.ptop - r.pbot, r.ti);
}
// Transfer all roots
foreach (ref r; roots)
{
gc_addRoot(r.proot);
}
}
this()
{
}
void Dtor()
{
}
void enable()
{
.gc_init_nothrow();
.gc_enable();
}
void disable()
{
.gc_init_nothrow();
.gc_disable();
}
void collect() nothrow
{
}
void collectNoStack() nothrow
{
}
void minimize() nothrow
{
}
uint getAttr(void* p) nothrow
{
return 0;
}
uint setAttr(void* p, uint mask) nothrow
{
return 0;
}
uint clrAttr(void* p, uint mask) nothrow
{
return 0;
}
void* malloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
.gc_init_nothrow();
return .gc_malloc(size, bits, ti);
}
BlkInfo qalloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
.gc_init_nothrow();
return .gc_qalloc(size, bits, ti);
}
void* calloc(size_t size, uint bits, const TypeInfo ti) nothrow
{
.gc_init_nothrow();
return .gc_calloc(size, bits, ti);
}
void* realloc(void* p, size_t size, uint bits, const TypeInfo ti) nothrow
{
.gc_init_nothrow();
return .gc_realloc(p, size, bits, ti);
}
size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow
{
return 0;
}
size_t reserve(size_t size) nothrow
{
.gc_init_nothrow();
return .gc_reserve(size);
}
void free(void* p) nothrow @nogc
{
if (p) assert(false, "Invalid memory deallocation");
}
void* addrOf(void* p) nothrow @nogc
{
return null;
}
size_t sizeOf(void* p) nothrow @nogc
{
return 0;
}
BlkInfo query(void* p) nothrow
{
return BlkInfo.init;
}
core.memory.GC.Stats stats() nothrow
{
return typeof(return).init;
}
void addRoot(void* p) nothrow @nogc
{
roots.insertBack(Root(p));
}
void removeRoot(void* p) nothrow @nogc
{
foreach (ref r; roots)
{
if (r is p)
{
r = roots.back;
roots.popBack();
return;
}
}
}
@property RootIterator rootIter() return @nogc
{
return &rootsApply;
}
private int rootsApply(scope int delegate(ref Root) nothrow dg)
{
foreach (ref r; roots)
{
if (auto result = dg(r))
return result;
}
return 0;
}
void addRange(void* p, size_t sz, const TypeInfo ti = null) nothrow @nogc
{
ranges.insertBack(Range(p, p + sz, cast() ti));
}
void removeRange(void* p) nothrow @nogc
{
foreach (ref r; ranges)
{
if (r.pbot is p)
{
r = ranges.back;
ranges.popBack();
return;
}
}
}
@property RangeIterator rangeIter() return @nogc
{
return &rangesApply;
}
private int rangesApply(scope int delegate(ref Range) nothrow dg)
{
foreach (ref r; ranges)
{
if (auto result = dg(r))
return result;
}
return 0;
}
void runFinalizers(in void[] segment) nothrow
{
}
bool inFinalizer() nothrow
{
return false;
}
}