-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmem.c
54 lines (47 loc) · 901 Bytes
/
mem.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
#include "mem.h"
static mem_slot *free_list;
static env *root;
void init_mem(env *global, unsigned long size)
{
unsigned long i;
mem_slot *prev = NULL;
root = global;
for (i = 0; i < size; ++i) {
object *new = malloc(sizeof(object));
mem_slot *next = malloc(sizeof(mem_slot));
if (new == NULL || next == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
next->o = new;
next->used = 0;
next->next = NULL;
next->mark = 0;
if (prev == NULL) {
free_list = next;
prev = free_list;
}
else {
prev->next = next;
prev = next;
}
}
prev->next = NULL;
}
object *alloc_obj()
{
mem_slot *cur = free_list;
while (cur != NULL) {
if (!cur->used) {
cur->used = 1;
return cur->o;
}
cur = cur->next;
}
/*gc() */
fprintf(stderr, "No more memory!\n");
exit(1);
}
void gc()
{
}