/
g.c
225 lines (194 loc) · 4.82 KB
/
g.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
#include <stdlib.h>
#include <stdio.h>
///// The primitive
enum LispType { LISP_INTEGER, LISP_CHARACTER, LISP_PRIMITIVE, LISP_CONS, LISP_FREE };
struct Memory; // need it for the *primitive pointer type
struct Cell {
enum LispType type;
int mark;
union {
int integer;
char character;
struct Cell* cell;
struct Cell* (*primitive) (struct Memory* mem, struct Cell* args);
} car;
struct Cell* cdr;
};
///// Memory
struct Memory {
int size;
struct Cell* all;
struct Cell* free;
struct Cell* nil;
struct Cell* root;
};
struct Memory* new_memory(int cell_count) {
// allocate stuff
struct Memory* mem = malloc(sizeof(struct Memory));
struct Cell* nil = malloc(sizeof(struct Cell));
struct Cell* all_memory = malloc(sizeof(struct Cell) * cell_count);
// Initialize cons pointers
struct Cell* cur;
int i;
cur = &all_memory[0];
cur->type = LISP_FREE;
cur->car.cell = nil;
cur->cdr = nil; // The "last" one links to NIL
for (i = 1; i < cell_count; ++i) {
cur = &all_memory[i];
cur->type = LISP_FREE;
cur->car.cell = nil;
cur->cdr = &all_memory[i-1];
};
mem->size = cell_count;
mem->free = cur;
mem->nil = nil;
mem->root = nil;
mem->all = all_memory;
return mem;
}
void deallocate(struct Memory* mem, struct Cell* cell) {
cell->type = LISP_FREE;
cell->car.cell = mem->nil;
cell->cdr = mem->free;
mem->free = cell;
}
void mark(struct Memory* mem, struct Cell* cell) {
if ((cell != mem->nil) && (cell->mark == 0)) {
cell->mark = 1;
if (cell->type == LISP_CONS) {
mark(mem, cell->car.cell);
mark(mem, cell->cdr);
}
}
}
void sweep(struct Memory* mem) {
int i;
struct Cell* cur;
for (i = 0; i < mem->size; ++i) {
cur = &mem->all[i];
if (cur->mark == 1) {
cur->mark = 0;
} else if (cur->type != LISP_FREE) {
deallocate(mem, cur);
}
}
}
void run_collection(struct Memory* mem) {
mark(mem, mem->root);
sweep(mem);
}
struct Cell* _allocate(struct Memory* mem) {
struct Cell* new;
new = mem->free;
mem->free = mem->free->cdr;
new->cdr = mem->nil;
return new;
}
struct Cell* allocate(struct Memory* mem) {
if (mem->nil == mem->free) {
run_collection(mem);
if (mem->nil == mem->free) {
printf("OUT OF MEMORY\n");
exit(1);
} else {
return _allocate(mem);
}
} else {
return _allocate(mem);
}
}
///// Allocating primitives
struct Cell* new_integer(struct Memory* mem, int i) {
struct Cell* new = allocate(mem);
new->type = LISP_INTEGER;
new->car.integer = i;
return new;
}
struct Cell* new_character(struct Memory* mem, char c) {
struct Cell* new = allocate(mem);
new->type = LISP_CHARACTER;
new->car.character = c;
return new;
}
struct Cell* new_primitive(struct Memory* mem, struct Cell* (*prim) (struct Memory*, struct Cell*)) {
struct Cell* new = allocate(mem);
new->type = LISP_PRIMITIVE;
new->car.primitive = prim;
return new;
}
struct Cell* new_cons(struct Memory* mem, struct Cell* a, struct Cell* b) {
struct Cell* new = allocate(mem);
new->type = LISP_CONS;
new->car.cell = a;
new->cdr = b;
}
///// Testing/demo functions
void print_cell_type(struct Cell* cell) {
switch (cell->type) {
case LISP_FREE: printf("."); break;
case LISP_INTEGER: printf("i"); break;
case LISP_CONS: printf("|"); break;
case LISP_CHARACTER: printf("c"); break;
default: printf("o");
}
}
void print_free_list(struct Memory* mem) {
struct Cell* cur = mem->free;
while (cur != mem->nil) {
print_cell_type(cur);
cur = cur->cdr;
}
}
void print_memory(struct Memory* mem) {
int i;
struct Cell* cur;
printf(" ");
for (i = 0; i < mem->size; ++i) {
cur = &mem->all[i];
if (cur->mark) printf("+");
else printf(" ");
}
printf("\nMem: ");
for (i = 0; i < mem->size; ++i) {
cur = &mem->all[i];
print_cell_type(cur);
}
printf("\nFree: ");
print_free_list(mem);
printf("\n\n");
}
void fill_memory(struct Memory* mem) {
while (mem->nil != mem->free) {
new_integer(mem, 34);
}
}
///// Main thing
int main() {
struct Cell* c;
struct Cell* lst;
struct Memory* mem = new_memory(100);
print_memory(mem);
c = new_cons(mem, new_character(mem, 'c'), new_integer(mem, 34));
print_memory(mem);
lst = new_cons(mem, new_integer(mem, 1), new_cons(mem, new_integer(mem, 2), new_cons(mem, new_integer(mem, 3), mem->nil)));
mem->root = lst;
print_memory(mem);
fill_memory(mem);
print_memory(mem);
deallocate(mem, c);
print_memory(mem);
mark(mem, mem->root);
print_memory(mem);
sweep(mem);
print_memory(mem);
lst = new_cons(mem, new_character(mem, 'a'), new_cons(mem, new_character(mem, 'b'), new_cons(mem, new_character(mem, 'c'), mem->nil)));
mem->root = lst;
print_memory(mem);
mark(mem, mem->root);
print_memory(mem);
sweep(mem);
print_memory(mem);
fill_memory(mem);
return 0;
}