-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmunit.c
371 lines (294 loc) · 5.99 KB
/
munit.c
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <munit/munit.h>
#ifndef UGC_IMPLEMENTATION
#define UGC_IMPLEMENTATION
#endif
#include "ugc.h"
typedef struct gc_obj_s gc_obj_t;
typedef struct fixture_s fixture_t;
struct gc_obj_s
{
ugc_header_t header;
gc_obj_t* ref;
bool live;
};
struct fixture_s
{
ugc_t* gc;
gc_obj_t* root;
};
static void
scan_gc_obj(ugc_t* gc, ugc_header_t* obj)
{
if(obj != NULL) // Scan obj
{
munit_logf(MUNIT_LOG_INFO, "Scan %p", (void*)obj);
gc_obj_t* ref = ((gc_obj_t*)obj)->ref;
if(ref) { ugc_visit(gc, &ref->header); }
}
else // Scan root
{
munit_log(MUNIT_LOG_INFO, "Scan root");
fixture_t* fixture = gc->userdata;
if(fixture->root) { ugc_visit(gc, &fixture->root->header); }
}
}
static void
free_gc_obj(ugc_t* gc, ugc_header_t* obj_)
{
(void)gc;
munit_assert_not_null(obj_);
munit_logf(MUNIT_LOG_INFO, "Free %p", (void*)obj_);
gc_obj_t* obj = (gc_obj_t*)obj_;
munit_assert_true(obj->live);
obj->live = false;
}
static void*
setup(const MunitParameter params[], void* data)
{
(void)params;
(void)data;
fixture_t* fixture = malloc(sizeof(fixture_t));
ugc_t* gc = malloc(sizeof(ugc_t));
ugc_init(gc, scan_gc_obj, free_gc_obj);
gc->userdata = fixture;
*fixture = (fixture_t){ .gc = gc };
return fixture;
}
static void
teardown(void* fixture_)
{
fixture_t* fixture = fixture_;
free(fixture->gc);
free(fixture);
}
static void
alloc(ugc_t* gc, gc_obj_t* obj)
{
munit_logf(MUNIT_LOG_INFO, "Alloc %p", (void*)obj);
// Not a real allocation
obj->live = true;
obj->ref = NULL;
ugc_register(gc, &obj->header);
}
static void
set_ref(ugc_t* gc, gc_obj_t* src, gc_obj_t* dst)
{
munit_logf(MUNIT_LOG_INFO, "Store %p <- %p", (void*)src, (void*)dst);
src->ref = dst;
if(dst)
{
ugc_write_barrier(
gc,
UGC_BARRIER_BACKWARD,
&src->header,
&dst->header
);
}
}
static MunitResult
basic(const MunitParameter params[], void* fixture_)
{
(void)params;
fixture_t* fixture = fixture_;
ugc_t* gc = fixture->gc;
gc_obj_t a, b, c;
alloc(gc, &a);
alloc(gc, &b);
alloc(gc, &c);
munit_assert_true(a.live);
munit_assert_true(b.live);
munit_assert_true(c.live);
set_ref(gc, &a, &b);
set_ref(gc, &b, &c);
ugc_collect(gc);
munit_assert_true(!a.live);
munit_assert_true(!b.live);
munit_assert_true(!c.live);
return MUNIT_OK;
}
static MunitResult
root(const MunitParameter params[], void* fixture_)
{
(void)params;
fixture_t* fixture = fixture_;
ugc_t* gc = fixture->gc;
gc_obj_t a, b, c;
alloc(gc, &a);
alloc(gc, &b);
alloc(gc, &c);
set_ref(gc, &a, &c);
fixture->root = &a;
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(!b.live);
munit_assert_true(c.live);
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(!b.live);
munit_assert_true(c.live);
return MUNIT_OK;
}
static MunitResult
write_barrier(const MunitParameter params[], void* fixture_)
{
(void)params;
fixture_t* fixture = fixture_;
ugc_t* gc = fixture->gc;
gc_obj_t a, b, c, d;
alloc(gc, &a);
alloc(gc, &b);
alloc(gc, &c);
set_ref(gc, &a, &b);
set_ref(gc, &b, &c);
fixture->root = &a;
while(ugc_color(&c.header) != !gc->white)
{
ugc_step(gc);
}
alloc(gc, &d);
set_ref(gc, &b, &d);
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(b.live);
munit_assert_true(c.live);
munit_assert_true(d.live);
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(b.live);
munit_assert_true(!c.live);
munit_assert_true(d.live);
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(b.live);
munit_assert_true(!c.live);
munit_assert_true(d.live);
return MUNIT_OK;
}
static MunitResult
root_change(const MunitParameter params[], void* fixture_)
{
(void)params;
fixture_t* fixture = fixture_;
ugc_t* gc = fixture->gc;
gc_obj_t a, b, c;
alloc(gc, &a);
alloc(gc, &b);
alloc(gc, &c);
fixture->root = &a;
set_ref(gc, &a, &b);
set_ref(gc, &b, &c);
while(ugc_color(&c.header) != !gc->white)
{
ugc_step(gc);
}
fixture->root = &b;
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(b.live);
munit_assert_true(c.live);
ugc_collect(gc);
munit_assert_true(!a.live);
munit_assert_true(b.live);
munit_assert_true(c.live);
ugc_collect(gc);
munit_assert_true(!a.live);
munit_assert_true(b.live);
munit_assert_true(c.live);
return MUNIT_OK;
}
static MunitResult
interupt_sweep(const MunitParameter params[], void* fixture_)
{
(void)params;
fixture_t* fixture = fixture_;
ugc_t* gc = fixture->gc;
gc_obj_t a, b, c;
alloc(gc, &a);
alloc(gc, &b);
fixture->root = &a;
while(gc->state != UGC_SWEEP)
{
ugc_step(gc);
}
alloc(gc, &c);
set_ref(gc, &a, &c);
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(!b.live);
munit_assert_true(c.live);
ugc_collect(gc);
munit_assert_true(a.live);
munit_assert_true(!b.live);
munit_assert_true(c.live);
return MUNIT_OK;
}
static MunitResult
release_all(const MunitParameter params[], void* fixture_)
{
(void)params;
fixture_t* fixture = fixture_;
ugc_t* gc = fixture->gc;
gc_obj_t a, b, c;
alloc(gc, &a);
alloc(gc, &b);
alloc(gc, &c);
a.ref = &b;
fixture->root = &a;
for(int i = 0; i < 3; ++i) { ugc_step(gc); }
ugc_release_all(gc);
munit_assert_true(!a.live);
munit_assert_true(!b.live);
munit_assert_true(!c.live);
return MUNIT_OK;
}
static MunitTest tests[] = {
{
.name = "/basic",
.test = basic,
.setup = setup,
.tear_down = teardown
},
{
.name = "/root",
.test = root,
.setup = setup,
.tear_down = teardown
},
{
.name = "/write_barrier",
.test = write_barrier,
.setup = setup,
.tear_down = teardown
},
{
.name = "/root_change",
.test = root_change,
.setup = setup,
.tear_down = teardown
},
{
.name = "/interupt_sweep",
.test = interupt_sweep,
.setup = setup,
.tear_down = teardown
},
{
.name = "/release_all",
.test = release_all,
.setup = setup,
.tear_down = teardown
},
{ .test = NULL }
};
static MunitSuite suite = {
.prefix = "",
.tests = tests
};
int
main(int argc, char* argv[])
{
return munit_suite_main(&suite, NULL, argc, argv);
}