Debian Codegraph @

libphobos/testsuite/libphobos.init_fini/custom_gc.d

raw
1 import core.gc.gcinterface;
2 import core.gc.registry;
3 import core.stdc.stdlib : calloc, malloc, realloc;
4
5 static import core.memory;
6
7 extern (C) __gshared string[] rt_options = ["gcopt=gc:malloc"];
8
9 extern (C) pragma(crt_constructor) void register_mygc()
10 {
11 registerGCFactory("malloc", &MallocGC.initialize);
12 }
13
14 extern (C) void register_default_gcs()
15 {
16 // remove default GCs
17 }
18
19 /** Simple GC that requires any pointers passed to it's API
20 to point to start of the allocation.
21 */
22 class MallocGC : GC
23 {
24 nothrow @nogc:
25 // To make sure all allocations are multiples of 8 bytes for alignment
26 private size_t alignUp(size_t size)
27 {
28 return (size + 7) & ~7LU;
29 }
30
31 static GC initialize()
32 {
33 import core.stdc.string : memcpy;
34
35 __gshared align(__traits(classInstanceAlignment, MallocGC))
36 ubyte[__traits(classInstanceSize, MallocGC)] buf;
37
38 auto init = typeid(MallocGC).initializer();
39 assert(init.length == buf.length);
40 auto instance = cast(MallocGC) memcpy(buf.ptr, init.ptr, init.length);
41 instance.__ctor();
42 return instance;
43 }
44
45 this()
46 {
47 }
48
49 void Dtor()
50 {
51 }
52
53 void enable()
54 {
55 }
56
57 void disable()
58 {
59 }
60
61 void collect() nothrow
62 {
63 }
64
65 void collectNoStack() nothrow
66 {
67 }
68
69 void minimize() nothrow
70 {
71 }
72
73 uint getAttr(void* p) nothrow
74 {
75 return 0;
76 }
77
78 uint setAttr(void* p, uint mask) nothrow
79 {
80 return mask;
81 }
82
83 uint clrAttr(void* p, uint mask) nothrow
84 {
85 return mask;
86 }
87
88 void* malloc(size_t size, uint bits, const TypeInfo ti) nothrow
89 {
90 size = alignUp(size);
91 return sentinelAdd(.malloc(size + sentinelSize), size);
92 }
93
94 BlkInfo qalloc(size_t size, uint bits, const scope TypeInfo ti) nothrow
95 {
96 size = alignUp(size);
97 return BlkInfo(malloc(size, bits, ti), size);
98 }
99
100 void* calloc(size_t size, uint bits, const TypeInfo ti) nothrow
101 {
102 size = alignUp(size);
103 return sentinelAdd(.calloc(1, size + sentinelSize), size);
104 }
105
106 void* realloc(void* p, size_t size, uint bits, const TypeInfo ti) nothrow
107 {
108 size = alignUp(size);
109 return sentinelAdd(.realloc(p - sentinelSize, size + sentinelSize), size);
110 }
111
112 size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow
113 {
114 return 0;
115 }
116
117 size_t reserve(size_t size) nothrow
118 {
119 return 0;
120 }
121
122 void free(void* p) nothrow
123 {
124 free(p - sentinelSize);
125 }
126
127 void* addrOf(void* p) nothrow
128 {
129 return p;
130 }
131
132 size_t sizeOf(void* p) nothrow
133 {
134 return query(p).size;
135 }
136
137 BlkInfo query(void* p) nothrow
138 {
139 return p ? BlkInfo(p, sentinelGet(p)) : BlkInfo.init;
140 }
141
142 core.memory.GC.Stats stats() nothrow
143 {
144 return core.memory.GC.Stats.init;
145 }
146
147 core.memory.GC.ProfileStats profileStats() nothrow
148 {
149 return typeof(return).init;
150 }
151
152 void addRoot(void* p) nothrow @nogc
153 {
154 }
155
156 void removeRoot(void* p) nothrow @nogc
157 {
158 }
159
160 @property RootIterator rootIter() @nogc
161 {
162 return null;
163 }
164
165 void addRange(void* p, size_t sz, const TypeInfo ti) nothrow @nogc
166 {
167 }
168
169 void removeRange(void* p) nothrow @nogc
170 {
171 }
172
173 @property RangeIterator rangeIter() @nogc
174 {
175 return null;
176 }
177
178 void runFinalizers(const scope void[] segment) nothrow
179 {
180 }
181
182 bool inFinalizer() nothrow
183 {
184 return false;
185 }
186
187 ulong allocatedInCurrentThread() nothrow
188 {
189 return stats().allocatedInCurrentThread;
190 }
191
192 void[] getArrayUsed(void *ptr, bool atomic = false) nothrow
193 {
194 return null;
195 }
196
197 bool expandArrayUsed(void[] slice, size_t newUsed, bool atomic = false) nothrow @safe
198 {
199 return false;
200 }
201
202 size_t reserveArrayCapacity(void[] slice, size_t request, bool atomic = false) nothrow @safe
203 {
204 return 0;
205 }
206
207 bool shrinkArrayUsed(void[] slice, size_t existingUsed, bool atomic = false) nothrow
208 {
209 return false;
210 }
211
212 private:
213 // doesn't care for alignment
214 static void* sentinelAdd(void* p, size_t value)
215 {
216 *cast(size_t*) p = value;
217 return p + sentinelSize;
218 }
219
220 static size_t sentinelGet(void* p)
221 {
222 return *cast(size_t*)(p - sentinelSize);
223 }
224
225 enum sentinelSize = size_t.sizeof;
226 }
227
228 void main()
229 {
230 // test array append cache
231 char[] s;
232 foreach (char c; char.min .. char.max + 1)
233 s ~= c;
234 }