-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinobsy.cpp
153 lines (142 loc) · 4.24 KB
/
tinobsy.cpp
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
#include "tinobsy.hpp"
namespace tinobsy {
object_type::object_type(const char* const name, object* (*mark)(vm*, object*), void (*free)(object*), void (*print)(object* obj, object* stream))
: name(name),
mark(mark),
free(free),
print(print) {}
object* vm::alloc(const object_type* type) {
DBG("vm::alloc() a %s", type ? type->name : "wild pointer");
if (this->freespace == 0) {
DBG("out of freespace, trying to expand chunk list...");
chunk* newchunk = new chunk(this->chunks);
if (!newchunk) {
perror("calloc()");
abort();
}
this->n_chunks++;
this->chunks = newchunk;
// Zip free list
for (ssize_t i = TINOBSY_CHUNK_SIZE - 1; i >= 0; i--) {
object* here = &newchunk->d[i];
cdr(here) = this->freelist;
this->freelist = here;
this->freespace++;
}
DBG("successfully added 1 chunk (%u objects)", TINOBSY_CHUNK_SIZE);
}
// Pull object from freelist
object* newobj = this->freelist;
this->freelist = newobj->as_obj;
memset(newobj, 0, sizeof(object));
newobj->type = (object_type*)type;
this->freespace--;
return newobj;
}
void vm::markobject(object* o) {
MARK:
if (o == NULL) {
DBG("Marking NULL");
return;
}
object_type* t = o->type;
if (o->tst_flag(GC_MARKED)) {
DBG("already marked");
return;
}
o->set_flag(GC_MARKED);
if (t == NULL) {
DBG("Object exists, NULL type");
o = car(o);
goto MARK;
}
DBG("Marking a %s", t->name);
if (!t->mark) {
DBG("No MARK pointer, returning...");
return;
}
o = t->mark(this, o);
goto MARK;
}
size_t vm::gc() {
size_t start = this->freespace;
DBG("vm::gc() begin, %zu objects free / %zu objects total (%zu chunks) {", start, TINOBSY_CHUNK_SIZE * this->n_chunks, this->n_chunks);
this->mark_globals();
DBG("garbage collect sweeping objects");
size_t ci = 0;
this->freelist = NULL;
this->freespace = 0;
size_t freed_chunks = 0;
for (chunk** c = &this->chunks; c && *c; c = &(*c)->next) {
bool is_empty = true;
object* freelist_here = this->freelist;
for (size_t i = 0; i < TINOBSY_CHUNK_SIZE; i++) {
object* o = &(*c)->d[i];
if (!o->tst_flag(GC_MARKED)) this->release(o);
else {
o->clr_flag(GC_MARKED);
is_empty = false;
}
}
if (is_empty) {
DBG("Whole chunk is empty");
chunk* going = *c;
*c = (*c)->next;
delete going;
this->freelist = freelist_here; // Reset freelist to top of chunk
this->n_chunks--; // Decrement chunks
this->freespace -= TINOBSY_CHUNK_SIZE; // Un-count new objects
freed_chunks++;
}
DBG("}");
ci++;
}
size_t freed = this->freespace + TINOBSY_CHUNK_SIZE * freed_chunks - start;
DBG("vm::gc() after sweeping objects, %zu objects freed, %zu chunks freed", freed, freed_chunks);
return freed;
}
void vm::release(object* o) {
if (!o) return;
if (o->type && o->type->free) o->type->free(o);
memset(o, 0, sizeof(object));
cdr(o) = this->freelist;
this->freelist = o;
this->freespace++;
}
vm::~vm() {
DBG("vm::~vm() {");
while (this->chunks) {
chunk* c = this->chunks;
for (size_t i = 0; i < TINOBSY_CHUNK_SIZE; i++) {
this->release(&c->d[i]);
}
this->chunks = c->next;
delete c;
}
DBG("}");
}
template <class field_type>
object* vm::get_existing_object(object_type* sch, field_type val, bool (*cmp)(field_type, field_type)) {
chunk* c = this->chunks;
while (c) {
for (size_t i = 0; i < TINOBSY_CHUNK_SIZE; i++) {
object* obj = &c->d[i];
if (obj->type != sch) continue;
if (cmp(val, (field_type)(obj->as_ptr))) {
DBG("found equal object");
return obj;
}
}
c = c->next;
}
return NULL;
}
template <class type>
bool op_eq(type a, type b) {
return a == b;
}
object* markcons(vm* vm, object* self) {
vm->markobject(car(self));
return cdr(self);
}
}