-
Notifications
You must be signed in to change notification settings - Fork 0
/
gc_test.d.c
398 lines (348 loc) · 10.3 KB
/
gc_test.d.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
@author: Michael Rohs
@date: January 19, 2022
*/
// #define NO_DEBUG
// #define NO_ASSERT
// #define NO_REQUIRE
// #define NO_ENSURE
#include <time.h>
#include <errno.h>
#include <sys/resource.h>
#include "util.h"
#include "gc.h"
/*
Allocate a C string. A string is an object without pointers to managed memory.
Thus it does not need a GCType and uses gc_alloc.
*/
char* new_str(char* s)
require_not_null(s)
char* t = gc_alloc(strlen(s) + 1)
strcpy(t, s)
return t
/*
Example type that contains one pointer to managed memory (t). The other pointer
(s) is not relevant, because it is supposed to point to memory that is not
managed by the garbage collector.
*/
typedef struct A A
struct A
int i
char* s // not managed
char* t // managed
// A needs a GCType, bacause it contains one pointer to managed memory (t).
int make_a_type(void)
int type = gc_new_type(sizeof(A), 1)
gc_set_offset(type, 0, offsetof(A, t))
PLf("%d, %d, %d", offsetof(A, i), offsetof(A, s), offsetof(A, t))
return type
// Singleton type object for objects of type A.
int a_type = 0
// Creates and initializes a new instance of type A.
A* new_a(int i, char* s, char* t)
require_not_null(s)
require_not_null(t)
require("not 0", a_type != 0)
A* a = gc_alloc_object(a_type)
a->i = i
a->s = s
a->t = new_str(t) // create a managed copy
return a
// Prints an A object.
void print_a(A* a)
require_not_null(a)
printf("A(%d, %s, %s)\n", a->i, a->s, a->t)
// Example type that contains one pointer to managed memory (a).
typedef struct B B
struct B
int j
A* a // managed
// B needs a type, bacause it contains one pointer to managed memory (a).
int make_b_type(void)
int type = gc_new_type(sizeof(B), 1)
gc_set_offset(type, 0, offsetof(B, a))
return type
// Singleton type object for objects of type B.
int b_type = 0
// Creates and initializes a new instance of type B.
B* new_b(int j, A* a)
require_not_null(a)
require("not 0", b_type != 0)
B* b = gc_alloc_object(b_type)
b->j = j
b->a = a
return b
// Prints a B object.
void print_b(B* b)
require_not_null(b)
printf("B(%d, %d, %s, %s)\n", b->j, b->a->i, b->a->s, b->a->t)
typedef struct Node Node
struct Node
int i
Node* left // managed
Node* right // managed
int make_node_type(void)
int type = gc_new_type(sizeof(Node), 2)
gc_set_offset(type, 0, offsetof(Node, left))
gc_set_offset(type, 1, offsetof(Node, right))
return type
int node_type = 0
Node* node(int i, Node* left, Node* right)
Node* node = gc_alloc_object(node_type)
// collect() // stress test collection
node->i = i
//PLi(i)
node->left = left
node->right = right
return node
Node* leaf(int i)
return node(i, NULL, NULL)
void print_tree(Node* t)
if t != NULL do
printf("o = %p, i = %d\n", t, t->i)
print_tree(t->left)
print_tree(t->right)
int sum_tree(Node* t)
if t == NULL do return 0
return sum_tree(t->left) + t->i + sum_tree(t->right)
//void test0(void)
void __attribute__((noinline)) test0(void)
PLf("frame address = %p", __builtin_frame_address(0))
uint64_t* p = __builtin_frame_address(0)
PLf("value at frame address = %llx", *p)
if a_type == 0 do
a_type = make_a_type()
printf("a_type = %d\n", a_type)
if b_type == 0 do
b_type = make_b_type()
printf("b_type = %d\n", b_type)
A* a1 = new_a(5, "hello", "world")
print_a(a1)
// a1 = NULL
PLf("&a1 = %p", &a1)
// a1 = NULL
//gc_add_root(a1)
B* b = new_b(3, a1)
print_b(b)
A* a2 = new_a(7, "abc", "def")
print_a(a2)
/*
gc_add_root(b)
gc_add_root(a2)
gc_remove_root(b)
gc_remove_root(a2)
*/
// a1 = NULL;
// a2 = NULL; b = NULL
B* bs = gc_alloc_array(b_type, 3)
for int i = 0; i < 3; i++ do
B* bi = bs + i
bi->j = i
bi->a = a1
print_b(bi)
for int i = 0; i < 3; i++ do
test_equal_i(bs[i].j, i)
test_equal_i(bs[i].a->i, 5)
///bs = NULL; a1 = NULL
int tree_count(Node* t)
if t == NULL do return 0
return tree_count(t->left) + 1 + tree_count(t->right)
int tree_sum(void)
Node* t = node(1,
node(2,
leaf(3),
leaf(4)),
node(5,
leaf(6),
leaf(7)))
// gc_add_root(t)
int n = sum_tree(t)
// gc_remove_root(t)
test_equal_i(tree_count(t), 7)
gc_collect()
test_equal_i(tree_count(t), 7)
t->right->left = NULL
gc_collect()
test_equal_i(tree_count(t), 6)
return n
void __attribute__((noinline)) test1(void)
if node_type == 0 do
node_type = make_node_type()
printf("node_type = %d\n", node_type)
int n = tree_sum()
printf("n = %d\n", n)
test_equal_i(n, 1+2+3+4+5+6+7)
// gc_collect()
// print_allocations()
void __attribute__((noinline)) test2(void)
if node_type == 0 do
node_type = make_node_type()
printf("node_type = %d\n", node_type)
Node* t = NULL
for int i = 0; i < 10; i++ do
t = node(i, t, NULL)
print_tree(t)
// gc_add_root(t)
t->left->left->left = t // handle cycles
// print_allocations()
// mark_stack(); print_allocations()
// mark_roots(); print_allocations()
// sweep(); print_allocations()
t->left->left = NULL
print_tree(t)
t = NULL
// mark_stack(); print_allocations()
// mark_roots(); print_allocations()
// sweep(); print_allocations()
Node* fill_tree(int i)
if i <= 0 do
return NULL
else
return node(i, fill_tree(i - 1), fill_tree(i - 2))
void __attribute__((noinline)) test3(void)
if node_type == 0 do
node_type = make_node_type()
printf("node_type = %d\n", node_type)
Node* t = NULL
clock_t time = clock()
//
for int i = 0; i < 10000000; i++ do
t = node(i, t, NULL)
// t = NULL
if (i % 123) == 990 do
t = NULL
gc_collect()
//
//t = fill_tree(24)
time = clock() - time
printf("time: %g ms\n", time * 1000.0 / CLOCKS_PER_SEC)
gc_print_stats()
/*
int count = 0; int max_level = 0; double mean_level = 0
trie_size(allocations, 0, &count, &max_level, &mean_level)
printf("count = %d, max_level = %d, mean_level = %.3f, trie_nodes = %d, %.2f\n",
count, max_level, mean_level / count, allocated_nodes,
(double)count * sizeof(Node) / (allocated_nodes * 16 * 8))
*/
// print_tree(t)
t = NULL
// gc_add_root(t)
// print_allocations()
time = clock()
gc_collect()
time = clock() - time
printf("time: %g ms\n", time * 1000.0 / CLOCKS_PER_SEC)
// gc_collect();
// print_allocations()
typedef struct Node3 Node3
struct Node3
int x
Node* a// managed
Node* b // managed
Node3* c // managed
int make_node3_type(void)
int type = gc_new_type(sizeof(Node3), 3)
gc_set_offset(type, 0, offsetof(Node3, a))
gc_set_offset(type, 1, offsetof(Node3, b))
gc_set_offset(type, 2, offsetof(Node3, c))
return type
int node3_type = 0
Node3* node3(int x, Node* a, Node* b, Node3* c)
require("not 0", node3_type != 0)
Node3* node = gc_alloc_object(node3_type)
node->x = x
node->a = a
node->b = b
node->c = c
return node
int tree3_count(Node3* t)
if t == NULL do return 0
return 1 + tree_count(t->a) + tree_count(t->b) + tree3_count(t->c)
int freed_count = 0
int test_freed_count = 0
void __attribute__((noinline)) test4(void)
if node_type == 0 do
node_type = make_node_type()
printf("node_type = %d\n", node_type)
if node3_type == 0 do
node3_type = make_node3_type()
printf("node3_type = %d\n", node3_type)
Node3* t = node3(1,
node(2,
leaf(3),
leaf(4)),
node(5,
leaf(6),
leaf(7)),
node3(8,
leaf(9),
leaf(10),
node3(11, NULL, NULL, NULL)))
test_equal_i(tree3_count(t), 11)
gc_collect()
printf("freed %d objects\n", freed_count)
test_freed_count = freed_count
test_equal_i(tree3_count(t), 11)
t->c = NULL
gc_collect()
printf("freed %d objects\n", freed_count)
test_freed_count += freed_count
test_equal_i(tree3_count(t), 7)
t->b->left = NULL
gc_collect()
printf("freed %d objects\n", freed_count)
test_freed_count += freed_count
test_equal_i(tree3_count(t), 6)
int main(void)
PLf("frame address = %p", __builtin_frame_address(0))
gc_set_bottom_of_stack(__builtin_frame_address(0))
#if 0
// Limit address space (in bytes)
struct rlimit limit
int err = getrlimit(RLIMIT_AS, &limit)
printf("%d, %d, %llu, %llu\n", err, errno, limit.rlim_cur, limit.rlim_max)
assert("no error", err == 0)
limit.rlim_cur = 34950000000
limit.rlim_max = RLIM_INFINITY
err = setrlimit(RLIMIT_AS, &limit)
printf("%d, %d, %llu, %llu\n", err, errno, limit.rlim_cur, limit.rlim_max)
assert("no error", err == 0)
err = getrlimit(RLIMIT_AS, &limit)
printf("%d, %d, %llu, %llu\n", err, errno, limit.rlim_cur, limit.rlim_max)
assert("no error", err == 0)
#endif
#if 1
// Limit address space (in bytes)
struct rlimit limit
int err = getrlimit(RLIMIT_STACK, &limit) // 8388608
printf("%d, %d, %llu, %llu\n", err, errno, limit.rlim_cur, limit.rlim_max)
assert("no error", err == 0)
limit.rlim_cur = 10000
err = setrlimit(RLIMIT_STACK, &limit)
printf("%d, %d, %llu, %llu\n", err, errno, limit.rlim_cur, limit.rlim_max)
assert("no error", err == 0)
err = getrlimit(RLIMIT_STACK, &limit)
printf("%d, %d, %llu, %llu\n", err, errno, limit.rlim_cur, limit.rlim_max)
assert("no error", err == 0)
#endif
test0()
PLs("main")
gc_collect()
test_equal_i(gc_is_empty(), true)
test1()
gc_collect()
test_equal_i(gc_is_empty(), true)
test2()
gc_collect()
test_equal_i(gc_is_empty(), true)
test3()
gc_collect()
test_equal_i(gc_is_empty(), true)
gc_print_stats()
test4()
gc_collect()
printf("freed %d objects\n", test_freed_count)
test_freed_count += freed_count
// test_equal_i(test_freed_count, 11)
test_equal_i(gc_is_empty(), true)
return 0