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
|
#include "gc.h"
#include "exc.h"
#include "consts.h"
#include <vector>
#include <cstdio>
#if defined(GC_DEBUG) || defined (GC_INFO)
typedef unsigned long long ull;
#endif
static EvalObj *gcq[GC_QUEUE_SIZE];
static Container *cyc_list[GC_QUEUE_SIZE];
GCRecord *oe_null;
GarbageCollector::GarbageCollector() {
joined = new GCRecord(NULL, NULL);
joined->next = oe_null = new GCRecord(NULL, NULL);
joined_size = 0;
pending_list = NULL;
resolve_threshold = GC_CYC_THRESHOLD;
}
GarbageCollector::PendingEntry::PendingEntry(
EvalObj *_obj, PendingEntry *_next) : obj(_obj), next(_next) {}
void GarbageCollector::expose(EvalObj *ptr) {
if (ptr == NULL || ptr->gc_rec == NULL) return;
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx exposed. count = %lu \"%s\"\n",
(ull)ptr, ptr->gc_rec->gc_cnt - 1, ptr->ext_repr().c_str());
#endif
if (--ptr->gc_rec->gc_cnt == 0)
{
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx pending. \n", (ull)ptr);
#endif
pending_list = new PendingEntry(ptr, pending_list);
}
}
void GarbageCollector::force() {
EvalObj **l = gcq, **r = l;
for (PendingEntry *p = pending_list, *np; p; p = np)
{
np = p->next;
EvalObj *obj = p->obj;
if (obj->gc_rec && !obj->gc_rec->gc_cnt)
*r++ = obj;
delete p;
} // fetch the pending pointers in the list
// clear the list
pending_list = NULL;
#ifdef GC_INFO
fprintf(stderr, "%ld\n", joined_size);
size_t cnt = 0;
#endif
#ifdef GC_DEBUG
fprintf(stderr,
"================================\n"
"GC: Forcing the clear process...\n");
#endif
for (; l != r; l++)
{
#ifdef GC_DEBUG
fprintf(stderr, "GC: !!! destroying space 0x%llx: %s. \n",
(ull)*l, (*l)->ext_repr().c_str());
#endif
#ifdef GC_INFO
cnt++;
#endif
delete *l;
// maybe it's a complex structure,
// so that more pointers are reported
for (PendingEntry *p = pending_list, *np; p; p = np)
{
np = p->next;
*r++ = p->obj;
if (r == gcq + GC_QUEUE_SIZE)
throw NormalError(RUN_ERR_GC_OVERFLOW);
delete p;
}
pending_list = NULL;
}
#ifdef GC_INFO
fprintf(stderr, "GC: Forced clear, %lu objects are freed, "
"%lu remains\n"
"=============================\n", cnt, joined_size);
#endif
#ifdef GC_DEBUG
#endif
}
EvalObj *GarbageCollector::attach(EvalObj *ptr) {
if (!ptr) return NULL; // NULL pointer
/* bool flag = mapping.count(ptr);
if (flag) mapping[ptr]++;
else mapping[ptr] = 1;
*/
ptr->gc_rec->gc_cnt++;
#ifdef GC_DEBUG
fprintf(stderr, "GC: 0x%llx attached. count = %lu \"%s\"\n",
(ull)ptr, ptr->gc_rec->gc_cnt, ptr->ext_repr().c_str());
#endif
/* if (mapping.size() > GC_QUEUE_SIZE >> 1)
force();*/
return ptr; // passing through
}
void GarbageCollector::cycle_resolve() {
Container **clptr = cyc_list;
for (GCRecord *i = joined->next; i != oe_null; i = i->next)
{
EvalObj *obj = i->obj;
if (obj->is_container())
{
Container *p = static_cast<Container*>(obj);
(*clptr++ = p)->gc_refs = i->gc_cnt; // init the count
p->keep = false;
}
}
EvalObj **l = gcq, **r = l;
for (Container **p = cyc_list; p < clptr; p++)
(*p)->gc_decrement();
for (Container **p = cyc_list; p < clptr; p++)
if ((*p)->gc_refs) // must not be recycled
*r++ = *p;
for (; l != r; l++)
{
Container *p = static_cast<Container*>(*l);
p->keep = true;
p->gc_trigger(r);
}
for (Container **p = cyc_list; p < clptr; p++)
if (!(*p)->keep)
{
delete *p;
}
#ifdef GC_INFO
fprintf(stderr, "GC: cycle resolved.\n");
#endif
}
void GarbageCollector::collect() {
force();
if (joined_size >= resolve_threshold)
{
cycle_resolve();
force();
}
}
size_t GarbageCollector::get_remaining() {
return joined_size;
}
void GarbageCollector::set_resolve_threshold(size_t new_thres) {
resolve_threshold = new_thres;
}
GCRecord *GarbageCollector::join(EvalObj *ptr) {
GCRecord *p = new GCRecord(joined, joined->next);
p->prev->next = p;
p->next->prev = p;
p->gc_cnt = 0;
p->obj = ptr;
joined_size++;
return p;
}
void GarbageCollector::quit(EvalObj *ptr) {
GCRecord *p = ptr->gc_rec;
p->prev->next = p->next;
p->next->prev = p->prev;
ptr->gc_rec = NULL;
delete p;
joined_size--;
}
GCRecord::GCRecord(GCRecord *_prev, GCRecord *_next) :
prev(_prev), next(_next) {}
|