Debian Codegraph @

libphobos/libdruntime/core/internal/gc/impl/proto/gc.d

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