-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gbc.c
165 lines (140 loc) · 2.69 KB
/
gbc.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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <setjmp.h>
#include "npl.h"
//---------garbage collection-----------
void gbc(void)
{
int addr;
if (gbc_flag) {
printf("enter GBC free=%d\n", fc);
fflush(stdout);
}
gc++;
gbcmark();
gbcsweep();
fc = 0;
for (addr = 0; addr < HEAPSIZE; addr++)
if (IS_EMPTY(addr))
fc++;
if (gbc_flag) {
printf("exit GBC free=%d\n", fc);
fflush(stdout);
}
}
void markcell(int addr)
{
if (IS_ALPHA(addr)) {
if (variant[addr - CELLSIZE] != UNBIND) {
markcell(variant[addr - CELLSIZE]);
return;
} else
return;
}
if (IS_OUTCELL(addr))
return;
if (USED_CELL(addr))
return;
if (addr == 0)
return; //NIL
MARK_CELL(addr);
switch (GET_TAG(addr)) {
case EMP:
case INTN:
case FLTN:
case LONGN:
return;
case STREAM:
markcell(cdr(addr));
return;
case SINGLE:
markcell(car(addr));
markcell(cdr(addr));
markcell(GET_VAR(addr));
markcell(GET_RECORD(addr));
return;
case BIGX:
return;
case STRUCT:
markcell(car(addr));
markcell(cdr(addr));
markcell(GET_VAR(addr));
markcell(GET_RECORD(addr));
return;
}
}
void gbcmark(void)
{
int i, j;
//mark nil and basic symbol
MARK_CELL(NIL);
MARK_CELL(YES);
MARK_CELL(NO);
MARK_CELL(FEND);
MARK_CELL(UNDEF);
//mark variable-list
markcell(variables);
markcell(variables_save);
//mark listing-list
markcell(predicates);
//mark cells chained by symbol hash table
for (i = 0; i < HASHTBSIZE; i++)
markcell(cell_hash_table[i]);
//mard ley-list for key/1
markcell(key_list);
//mark hash table of recordh term
for (i = 0; i < HASHTBSIZE; i++) {
for (j = 0; j < record_pt; j++) {
markcell(record_hash_table[i][j]);
}
}
//mark stream
markcell(standard_input);
markcell(standard_output);
markcell(standard_error);
markcell(input_stream);
markcell(output_stream);
markcell(error_stream);
//mark stack
for (i = 0; i < sp; i++) {
if (alpha_variable_p(stack[i]))
markcell(variant[stack[i] - CELLSIZE]);
else
markcell(stack[i]);
}
}
void gbcsweep(void)
{
int addr;
addr = 0;
while (addr < HEAPSIZE) {
if (USED_CELL(addr))
NOMARK_CELL(addr);
else {
clrcell(addr);
SET_CDR(addr, hp);
hp = addr;
}
addr++;
}
}
void clrcell(int addr)
{
SET_TAG(addr, EMP);
free(heap[addr].name);
heap[addr].name = NULL;
SET_CAR(addr, 0);
SET_CDR(addr, 0);
SET_AUX(addr, 0);
SET_OPT(addr, 0);
SET_RECORD(addr, 0);
}
//when fc is less FREESIZE invoke gbc()
void checkgbc(void)
{
if (fc < FREESIZE) {
gbc();
}
}