-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGarbageCollect.h
More file actions
134 lines (123 loc) · 2.78 KB
/
GarbageCollect.h
File metadata and controls
134 lines (123 loc) · 2.78 KB
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
#if 1
inline void tp_grey(tp_vm *tp, tp_obj v) {
if (v.type < TP_STRING || (!v.gci.data) || *v.gci.data) { return; }
*v.gci.data = 1;
if (v.type == TP_STRING || v.type == TP_DATA) {
_tp_list_appendx(tp, tp->black, v);
return;
}
_tp_list_appendx(tp, tp->grey, v);
}
inline void tp_follow(tp_vm *tp, tp_obj v) {
int type = v.type;
if (type == TP_LIST) {
int n;
for (n = 0; n < v.list.val->len; n++) {
tp_grey(tp, v.list.val->items[n]);
}
}
if (type == TP_DICT) {
int i;
for (i = 0; i < v.dict.val->len; i++) {
int n = _tp_dict_next(tp, v.dict.val);
tp_grey(tp, v.dict.val->items[n].key);
tp_grey(tp, v.dict.val->items[n].val);
}
tp_grey(tp, v.dict.val->meta);
}
if (type == TP_FNC) {
tp_grey(tp, v.fnc.info->self);
tp_grey(tp, v.fnc.info->globals);
tp_grey(tp, v.fnc.info->code);
}
}
inline void tp_reset(tp_vm *tp) {
int n;
_tp_list *tmp;
for (n = 0; n < tp->black->len; n++) {
*tp->black->items[n].gci.data = 0;
}
tmp = tp->white;
tp->white = tp->black;
tp->black = tmp;
}
inline void tp_gc_init(tp_vm *tp) {
tp->white = _tp_list_new(tp);
tp->grey = _tp_list_new(tp);
tp->black = _tp_list_new(tp);
tp->steps = 0;
}
inline void tp_gc_deinit(tp_vm *tp) {
_tp_list_free(tp, tp->white);
_tp_list_free(tp, tp->grey);
_tp_list_free(tp, tp->black);
}
inline void tp_delete(tp_vm *tp, tp_obj v) {
int type = v.type;
if (type == TP_LIST) {
_tp_list_free(tp, v.list.val);
return;
}
else if (type == TP_DICT) {
_tp_dict_free(tp, v.dict.val);
return;
}
else if (type == TP_STRING) {
free(v.string.info);
return;
}
else if (type == TP_DATA) {
if (v.data.info->free) {
v.data.info->free(tp, v);
}
free(v.data.info);
return;
}
else if (type == TP_FNC) {
free(v.fnc.info);
return;
}
tp_raise(, tp_string("(tp_delete) TypeError: ?"));
}
inline void tp_collect(tp_vm *tp) {
int n;
for (n = 0; n < tp->white->len; n++) {
tp_obj r = tp->white->items[n];
if (*r.gci.data) { continue; }
tp_delete(tp, r);
}
tp->white->len = 0;
tp_reset(tp);
}
inline void _tp_gcinc(tp_vm *tp) {
tp_obj v;
if (!tp->grey->len) {
return;
}
v = _tp_list_pop(tp, tp->grey, tp->grey->len - 1, "_tp_gcinc");
tp_follow(tp, v);
_tp_list_appendx(tp, tp->black, v);
}
inline void tp_full(tp_vm *tp) {
while (tp->grey->len) {
_tp_gcinc(tp);
}
tp_collect(tp);
tp_follow(tp, tp->root);
}
inline void tp_gcinc(tp_vm *tp) {
tp->steps += 1;
if (tp->steps < TP_GCMAX || tp->grey->len > 0) {
_tp_gcinc(tp); _tp_gcinc(tp);
}
if (tp->steps < TP_GCMAX || tp->grey->len > 0) { return; }
tp->steps = 0;
tp_full(tp);
return;
}
inline tp_obj tp_track(tp_vm *tp, tp_obj v) {
tp_gcinc(tp);
tp_grey(tp, v);
return v;
}
#endif