-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheap.cpp
More file actions
219 lines (176 loc) · 3.92 KB
/
heap.cpp
File metadata and controls
219 lines (176 loc) · 3.92 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
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
//
// Heap management and garbage collection
//
#include "doctest.h"
#include "consVM.h"
const int N_CELLS = 1000 * 1000;;
static int sweep_heap();
static Free heap[N_CELLS];
static Free* free_list;
void init_heap()
{
free_list = &heap[0];
Free* p;
for (int i = 0; i < N_CELLS-1; ++i)
{
p = &heap[i];
p->type = Tag::FREE_TAG;
p->flags = 0;
p->next = &heap[i+1];
}
p = &heap[N_CELLS-1];
p->type = Tag::FREE_TAG;
p->flags = 0;
p->next = NULL;
}
static Free* alloc_heap()
{
if (free_list == NULL) {
GCStatus status = gc("heap exhausted");
std::cout <<
"gc: Heap size " << status.heap_size <<
", marked " << status.n_marked <<
", recovered " << status.n_recovered << std::endl;
}
Free* p = free_list;
free_list = free_list->next;
return p;
}
Cons* alloc_cons()
{
Cons* p = (Cons*) alloc_heap();
p->type = Tag::CONS_TAG;
p->flags = 0;
return p;
}
Atom* alloc_atom()
{
Atom* p = (Atom*) alloc_heap();
p->type = Tag::ATOM_TAG;
p->flags = 0;
return p;
}
// -------------------------------------
GCStatus gc(const char* context)
{
std::cout << std::endl << "gc: Starting";
if (context != NULL) {
std::cout << " - " << context;
}
std::cout << std::endl;
int nMarked = mark(global_env);
nMarked += mark_stack();
int nBytesRecovered = sweep_strings();
sweep_atoms();
int nRecovered = sweep_heap();
compactify_strings();
if (free_list == NULL)
{
throw LispError("gc: Heap exhausted", true);
}
std::cout << "gc: Done" << std::endl;
// These can be removed when the code matures
audit_atoms();
audit_cons();
audit_strings();
return GCStatus(N_CELLS, nMarked, nRecovered);
}
int mark(Cell* p)
{
// Sanity check
if (p == NULL) {
throw LispError("gc: null pointer to mark", true);
}
if (is_marked(p)) {
return 0;
}
int nMarked = 0;
set_mark(p);
++nMarked;
switch (p->type) {
case Tag::CONS_TAG:
{
Cons* q = (Cons*) p;
nMarked += mark(q->car);
nMarked += mark(q->cdr);
}
break;
case Tag::ATOM_TAG:
{
Atom* a = (Atom*) p;
set_mark(a->string);
}
break;
default:
throw LispError("mark: unhandled cell type", true);
}
return nMarked;
}
static int sweep_heap()
{
int nRecovered = 0;
free_list = NULL;
for (int i = 0; i < N_CELLS; ++i)
{
Free* p = &heap[i];
if (not_marked(p)) {
p->type = Tag::FREE_TAG;
p->next = free_list;
free_list = p;
++nRecovered;
} else {
clear_mark(p);
}
}
return nRecovered;
}
// -------------------------------------
void audit_heap()
{
for (int i = 0; i < N_CELLS; ++i)
{
Cell* p = &heap[i];
switch (p->type)
{
case Tag::FREE_TAG:
case Tag::ATOM_TAG:
case Tag::CONS_TAG:
break;
default:
throw LispError("audit_heap: Bad type", true);
}
}
}
// -------------------------------------
TEST_CASE("gc() is ok for globals") {
gc("gc() is ok for globals");
REQUIRE(nil->type == Tag::ATOM_TAG);
REQUIRE(a_t->type == Tag::ATOM_TAG);
}
TEST_CASE("gc() frees dead cons") {
cons(nil, nil);
Cell* dead = pop();
REQUIRE(dead->type == Tag::CONS_TAG);
gc("gc() frees dead cons");
REQUIRE(dead->type == Tag::FREE_TAG);
}
TEST_CASE("gc() leaves mark bits clear") {
Atom* gc_test_0 = atom("gc_test_0");
push(gc_test_0);
gc("gc() leaves mark bits clear");
REQUIRE(not_marked(nil));
REQUIRE(not_marked(a_t));
REQUIRE(not_marked(gc_test_0));
drop(1);
}
TEST_CASE("Strings can withstand garbage collection") {
Atom* gc_test_1 = atom("gc_test_1");
push(gc_test_1);
String* garbage = intern_string("garbage"); // Will be garbage-collected
Atom* gc_test_2 = atom("gc_test_2");
push(gc_test_2);
gc("Strings can withstand garbage collection"); // Should collect 'garbage' string
REQUIRE(std::strcmp(gc_test_1->string->body, "gc_test_1") == 0);
REQUIRE(std::strcmp(gc_test_2->string->body, "gc_test_2") == 0);
drop(2);
}