1 | /* |
---|
2 | * Abuse - dark 2D side-scrolling platform game |
---|
3 | * Copyright (c) 1995 Crack dot Com |
---|
4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | * |
---|
6 | * This software was released into the Public Domain. As with most public |
---|
7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
8 | * Jonathan Clark, or by Sam Hocevar. |
---|
9 | */ |
---|
10 | |
---|
11 | #if defined HAVE_CONFIG_H |
---|
12 | # include "config.h" |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <stdlib.h> |
---|
16 | #include <string.h> |
---|
17 | |
---|
18 | #include "lisp.h" |
---|
19 | #include "lisp_gc.h" |
---|
20 | |
---|
21 | #include "stack.h" |
---|
22 | |
---|
23 | /* Lisp garbage collection: uses copy/free algorithm |
---|
24 | Places to check: |
---|
25 | symbol |
---|
26 | values |
---|
27 | functions |
---|
28 | names |
---|
29 | stack |
---|
30 | */ |
---|
31 | |
---|
32 | // Stack where user programs can push data and have it GCed |
---|
33 | GrowStack<void> l_user_stack(150); |
---|
34 | |
---|
35 | // Stack of user pointers |
---|
36 | GrowStack<void *> PtrRef::stack(1500); |
---|
37 | |
---|
38 | static size_t reg_ptr_total = 0; |
---|
39 | static void ***reg_ptr_list = NULL; |
---|
40 | |
---|
41 | static uint8_t *cstart, *cend, *collected_start, *collected_end; |
---|
42 | |
---|
43 | LArray *LispGC::CollectArray(LArray *x) |
---|
44 | { |
---|
45 | size_t s = x->len; |
---|
46 | LArray *a = LArray::Create(s, NULL); |
---|
47 | LObject **src = x->GetData(); |
---|
48 | LObject **dst = a->GetData(); |
---|
49 | for (size_t i = 0; i < s; i++) |
---|
50 | dst[i] = CollectObject(src[i]); |
---|
51 | |
---|
52 | return a; |
---|
53 | } |
---|
54 | |
---|
55 | LList *LispGC::CollectList(LList *x) |
---|
56 | { |
---|
57 | LList *prev = NULL, *first = NULL; |
---|
58 | |
---|
59 | for (; x && item_type(x) == L_CONS_CELL; ) |
---|
60 | { |
---|
61 | LList *p = LList::Create(); |
---|
62 | LObject *old_car = x->car; |
---|
63 | LObject *old_x = x; |
---|
64 | x = (LList *)CDR(x); |
---|
65 | ((LRedirect *)old_x)->type = L_COLLECTED_OBJECT; |
---|
66 | ((LRedirect *)old_x)->ref = p; |
---|
67 | |
---|
68 | p->car = CollectObject(old_car); |
---|
69 | |
---|
70 | if (prev) |
---|
71 | prev->cdr = p; |
---|
72 | else |
---|
73 | first = p; |
---|
74 | prev = p; |
---|
75 | } |
---|
76 | if (x) |
---|
77 | prev->cdr = CollectObject(x); |
---|
78 | |
---|
79 | return first; // we already set the collection pointers |
---|
80 | } |
---|
81 | |
---|
82 | LObject *LispGC::CollectObject(LObject *x) |
---|
83 | { |
---|
84 | LObject *ret = x; |
---|
85 | |
---|
86 | if ((uint8_t *)x >= cstart && (uint8_t *)x < cend) |
---|
87 | { |
---|
88 | switch (item_type(x)) |
---|
89 | { |
---|
90 | case L_BAD_CELL: |
---|
91 | lbreak("error: collecting corrupted cell\n"); |
---|
92 | break; |
---|
93 | case L_NUMBER: |
---|
94 | ret = LNumber::Create(((LNumber *)x)->num); |
---|
95 | break; |
---|
96 | case L_SYS_FUNCTION: |
---|
97 | ret = new_lisp_sys_function(((LSysFunction *)x)->min_args, |
---|
98 | ((LSysFunction *)x)->max_args, |
---|
99 | ((LSysFunction *)x)->fun_number); |
---|
100 | break; |
---|
101 | case L_USER_FUNCTION: |
---|
102 | { |
---|
103 | LUserFunction *fun = (LUserFunction *)x; |
---|
104 | LList *arg = (LList *)CollectObject(fun->arg_list); |
---|
105 | LList *block = (LList *)CollectObject(fun->block_list); |
---|
106 | ret = new_lisp_user_function(arg, block); |
---|
107 | break; |
---|
108 | } |
---|
109 | case L_STRING: |
---|
110 | ret = LString::Create(lstring_value(x)); |
---|
111 | break; |
---|
112 | case L_CHARACTER: |
---|
113 | ret = LChar::Create(lcharacter_value(x)); |
---|
114 | break; |
---|
115 | case L_C_FUNCTION: |
---|
116 | ret = new_lisp_c_function(((LSysFunction *)x)->min_args, |
---|
117 | ((LSysFunction *)x)->max_args, |
---|
118 | ((LSysFunction *)x)->fun_number); |
---|
119 | break; |
---|
120 | case L_C_BOOL: |
---|
121 | ret = new_lisp_c_bool(((LSysFunction *)x)->min_args, |
---|
122 | ((LSysFunction *)x)->max_args, |
---|
123 | ((LSysFunction *)x)->fun_number); |
---|
124 | break; |
---|
125 | case L_L_FUNCTION: |
---|
126 | ret = new_user_lisp_function(((LSysFunction *)x)->min_args, |
---|
127 | ((LSysFunction *)x)->max_args, |
---|
128 | ((LSysFunction *)x)->fun_number); |
---|
129 | break; |
---|
130 | case L_POINTER: |
---|
131 | ret = LPointer::Create(lpointer_value(x)); |
---|
132 | break; |
---|
133 | case L_1D_ARRAY: |
---|
134 | ret = CollectArray((LArray *)x); |
---|
135 | break; |
---|
136 | case L_FIXED_POINT: |
---|
137 | ret = LFixedPoint::Create(lfixed_point_value(x)); |
---|
138 | break; |
---|
139 | case L_CONS_CELL: |
---|
140 | ret = CollectList((LList *)x); |
---|
141 | break; |
---|
142 | case L_OBJECT_VAR: |
---|
143 | ret = LObjectVar::Create(((LObjectVar *)x)->index); |
---|
144 | break; |
---|
145 | case L_COLLECTED_OBJECT: |
---|
146 | ret = ((LRedirect *)x)->ref; |
---|
147 | break; |
---|
148 | default: |
---|
149 | lbreak("error: collecting bad object 0x%x\n", item_type(x)); |
---|
150 | break; |
---|
151 | } |
---|
152 | ((LRedirect *)x)->type = L_COLLECTED_OBJECT; |
---|
153 | ((LRedirect *)x)->ref = ret; |
---|
154 | } |
---|
155 | else if ((uint8_t *)x < collected_start || (uint8_t *)x >= collected_end) |
---|
156 | { |
---|
157 | // Still need to remap cons_cells lying outside of space, for |
---|
158 | // instance on the stack. |
---|
159 | for (LObject *cell = NULL; x; cell = x, x = CDR(x)) |
---|
160 | { |
---|
161 | if (item_type(x) != L_CONS_CELL) |
---|
162 | { |
---|
163 | if (cell) |
---|
164 | CDR(cell) = CollectObject(CDR(cell)); |
---|
165 | break; |
---|
166 | } |
---|
167 | CAR(x) = CollectObject(CAR(x)); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | return ret; |
---|
172 | } |
---|
173 | |
---|
174 | void LispGC::CollectSymbols(LSymbol *root) |
---|
175 | { |
---|
176 | if (!root) |
---|
177 | return; |
---|
178 | |
---|
179 | root->value = CollectObject(root->value); |
---|
180 | root->function = CollectObject(root->function); |
---|
181 | root->name = (LString *)CollectObject(root->name); |
---|
182 | CollectSymbols(root->left); |
---|
183 | CollectSymbols(root->right); |
---|
184 | } |
---|
185 | |
---|
186 | void LispGC::CollectStacks() |
---|
187 | { |
---|
188 | void **d = l_user_stack.sdata; |
---|
189 | for (size_t i = 0; i < l_user_stack.m_size; i++, d++) |
---|
190 | *d = CollectObject((LObject *)*d); |
---|
191 | |
---|
192 | void ***d2 = PtrRef::stack.sdata; |
---|
193 | for (size_t i = 0; i < PtrRef::stack.m_size; i++, d2++) |
---|
194 | { |
---|
195 | void **ptr = *d2; |
---|
196 | *ptr = CollectObject((LObject *)*ptr); |
---|
197 | } |
---|
198 | |
---|
199 | void ***d3 = reg_ptr_list; |
---|
200 | for (size_t i = 0; i < reg_ptr_total; i++, d3++) |
---|
201 | { |
---|
202 | void **ptr = *d3; |
---|
203 | *ptr = CollectObject((LObject *)*ptr); |
---|
204 | } |
---|
205 | } |
---|
206 | |
---|
207 | void LispGC::CollectSpace(int which_space, int grow) |
---|
208 | { |
---|
209 | int old_space = current_space; |
---|
210 | cstart = space[which_space]; |
---|
211 | cend = free_space[which_space]; |
---|
212 | |
---|
213 | space_size[GC_SPACE] = space_size[which_space]; |
---|
214 | if (grow) |
---|
215 | { |
---|
216 | space_size[GC_SPACE] += space_size[which_space] >> 1; |
---|
217 | space_size[GC_SPACE] -= (space_size[GC_SPACE] & 7); |
---|
218 | } |
---|
219 | uint8_t *new_space = (uint8_t *)malloc(space_size[GC_SPACE]); |
---|
220 | current_space = GC_SPACE; |
---|
221 | free_space[GC_SPACE] = space[GC_SPACE] = new_space; |
---|
222 | |
---|
223 | collected_start = new_space; |
---|
224 | collected_end = new_space + space_size[GC_SPACE]; |
---|
225 | |
---|
226 | CollectSymbols(LSymbol::root); |
---|
227 | CollectStacks(); |
---|
228 | |
---|
229 | // for debuging clear it out |
---|
230 | memset(space[which_space], 0, space_size[which_space]); |
---|
231 | free(space[which_space]); |
---|
232 | |
---|
233 | space[which_space] = new_space; |
---|
234 | space_size[which_space] = space_size[GC_SPACE]; |
---|
235 | free_space[which_space] = new_space |
---|
236 | + (free_space[GC_SPACE] - space[GC_SPACE]); |
---|
237 | current_space = old_space; |
---|
238 | } |
---|
239 | |
---|