source: abuse/trunk/src/lisp/lisp_gc.cpp @ 494

Last change on this file since 494 was 494, checked in by Sam Hocevar, 10 years ago

style: remove trailing spaces, fix copyright statements.

File size: 7.6 KB
Line 
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 or
8 *  Jonathan Clark.
9 */
10
11#include "config.h"
12
13#include <stdlib.h>
14#include <string.h>
15
16#include "lisp.h"
17#include "lisp_gc.h"
18#ifdef NO_LIBS
19#include "fakelib.h"
20#else
21#include "macs.h"
22#endif
23
24#include "stack.h"
25
26/*  Lisp garbage collection: uses copy/free algorithm
27    Places to check:
28      symbol
29        values
30    functions
31    names
32      stack
33*/
34
35// Stack where user programs can push data and have it GCed
36grow_stack<void> l_user_stack(150);
37// Stack of user pointers
38grow_stack<void *> PtrRef::stack(1500);
39
40size_t reg_ptr_total = 0;
41size_t reg_ptr_list_size = 0;
42void ***reg_ptr_list = NULL;
43
44static uint8_t *cstart, *cend, *collected_start, *collected_end;
45
46static void dump_memory(void *mem, int before, int after)
47{
48  uint8_t *p = (uint8_t *)mem;
49
50  fprintf(stderr, "dumping memory around %p:\n", p);
51  for (int i = -before; i < after; i++)
52  {
53    if (!(i & 15))
54      fprintf(stderr, "%p: ", p + i);
55    fprintf(stderr, "%c%02x%c", i ? ' ' : '[', p[i], i ? ' ' : ']');
56    if (!((i + 1) & 15))
57      fprintf(stderr, "\n");
58  }
59}
60
61void register_pointer(void **addr)
62{
63  if (reg_ptr_total >= reg_ptr_list_size)
64  {
65    reg_ptr_list_size += 0x100;
66    reg_ptr_list = (void ***)realloc(reg_ptr_list, sizeof(void **) * reg_ptr_list_size);
67  }
68  reg_ptr_list[reg_ptr_total++] = addr;
69}
70
71void unregister_pointer(void **addr)
72{
73  void ***reg_on = reg_ptr_list;
74  for (size_t i = 0; i < reg_ptr_total; i++, reg_on++)
75  {
76    if (*reg_on == addr)
77    {
78      reg_ptr_total--;
79      for (size_t j = i; j < reg_ptr_total; j++, reg_on++)
80        reg_on[0] = reg_on[1];
81      return ;
82    }
83  }
84  fprintf(stderr, "Unable to locate ptr to unregister");
85}
86
87static LObject *CollectObject(LObject *x);
88
89static LArray *CollectArray(LArray *x)
90{
91    size_t s = x->len;
92    LArray *a = LArray::Create(s, NULL);
93    LObject **src = x->GetData();
94    LObject **dst = a->GetData();
95    for (size_t i = 0; i < s; i++)
96        dst[i] = CollectObject(src[i]);
97
98    return a;
99}
100
101inline LList *CollectList(LList *x)
102{
103    LList *last = NULL, *first = NULL;
104
105    for (; x && item_type(x) == L_CONS_CELL; )
106    {
107        LList *p = LList::Create();
108        LObject *old_car = x->car;
109        LObject *old_cdr = x->cdr;
110        LObject *old_x = x;
111        x = (LList *)CDR(x);
112        ((LRedirect *)old_x)->type = L_COLLECTED_OBJECT;
113        ((LRedirect *)old_x)->ref = p;
114
115        p->car = CollectObject(old_car);
116        p->cdr = CollectObject(old_cdr);
117
118        if (last)
119            last->cdr = p;
120        else
121            first = p;
122        last = p;
123    }
124    if (x)
125        last->cdr = CollectObject(x);
126    return first; // we already set the collection pointers
127}
128
129static LObject *CollectObject(LObject *x)
130{
131  LObject *ret = x;
132
133  if (((uint8_t *)x) >= cstart && ((uint8_t *)x) < cend)
134  {
135    //dump_memory(x, 32, 48);
136    switch (item_type(x))
137    {
138      case L_BAD_CELL:
139        lbreak("error: GC corrupted cell\n");
140        break;
141      case L_NUMBER:
142        ret = LNumber::Create(((LNumber *)x)->num);
143        break;
144      case L_SYS_FUNCTION:
145        ret = new_lisp_sys_function(((LSysFunction *)x)->min_args,
146                                    ((LSysFunction *)x)->max_args,
147                                    ((LSysFunction *)x)->fun_number);
148        break;
149      case L_USER_FUNCTION:
150#ifndef NO_LIBS
151        ret = new_lisp_user_function(((LUserFunction *)x)->alist,
152                                     ((LUserFunction *)x)->blist);
153
154#else
155        {
156          LObject *arg = CollectObject(((LUserFunction *)x)->arg_list);
157          LObject *block = CollectObject(((LUserFunction *)x)->block_list);
158          ret = new_lisp_user_function(arg, block);
159        }
160#endif
161        break;
162      case L_STRING:
163        ret = LString::Create(lstring_value(x));
164        break;
165      case L_CHARACTER:
166        ret = LChar::Create(lcharacter_value(x));
167        break;
168      case L_C_FUNCTION:
169        ret = new_lisp_c_function(((LSysFunction *)x)->min_args,
170                                  ((LSysFunction *)x)->max_args,
171                                  ((LSysFunction *)x)->fun_number);
172        break;
173      case L_C_BOOL:
174        ret = new_lisp_c_bool(((LSysFunction *)x)->min_args,
175                              ((LSysFunction *)x)->max_args,
176                              ((LSysFunction *)x)->fun_number);
177        break;
178      case L_L_FUNCTION:
179        ret = new_user_lisp_function(((LSysFunction *)x)->min_args,
180                                     ((LSysFunction *)x)->max_args,
181                                     ((LSysFunction *)x)->fun_number);
182        break;
183      case L_POINTER:
184        ret = LPointer::Create(lpointer_value(x));
185        break;
186      case L_1D_ARRAY:
187        ret = CollectArray((LArray *)x);
188        break;
189      case L_FIXED_POINT:
190        ret = LFixedPoint::Create(lfixed_point_value(x));
191        break;
192      case L_CONS_CELL:
193        ret = CollectList((LList *)x);
194        break;
195      case L_OBJECT_VAR:
196        ret = LObjectVar::Create(((LObjectVar *)x)->index);
197        break;
198      case L_COLLECTED_OBJECT:
199        ret = ((LRedirect *)x)->ref;
200        break;
201      default:
202        dump_memory(x, 8, 196);
203        //*(char *)NULL = 0;
204        lbreak("shouldn't happen. collecting bad object 0x%x\n",
205               item_type(x));
206        break;
207    }
208    ((LRedirect *)x)->type = L_COLLECTED_OBJECT;
209    ((LRedirect *)x)->ref = ret;
210  }
211  else if ((uint8_t *)x < collected_start || (uint8_t *)x >= collected_end)
212  {
213    if (item_type(x) == L_CONS_CELL) // still need to remap cons_cells outside of space
214    {
215      for (; x && item_type(x) == L_CONS_CELL; x = CDR(x))
216        ((LList *)x)->car = CollectObject(((LList *)x)->car);
217      if (x)
218        ((LList *)x)->cdr = CollectObject(((LList *)x)->cdr);
219    }
220  }
221
222  return ret;
223}
224
225static void collect_symbols(LSymbol *root)
226{
227    if (!root)
228        return;
229
230    root->value = CollectObject(root->value);
231    root->function = CollectObject(root->function);
232    root->name = (LString *)CollectObject(root->name);
233    collect_symbols(root->left);
234    collect_symbols(root->right);
235}
236
237static void collect_stacks()
238{
239  long t = l_user_stack.son;
240
241  void **d = l_user_stack.sdata;
242  for (int i = 0; i < t; i++, d++)
243    *d = CollectObject((LObject *)*d);
244
245  t = PtrRef::stack.son;
246  void ***d2 = PtrRef::stack.sdata;
247  for (int i = 0; i < t; i++, d2++)
248  {
249    void **ptr = *d2;
250    *ptr = CollectObject((LObject *)*ptr);
251  }
252
253  d2 = reg_ptr_list;
254  for (size_t i = 0; i < reg_ptr_total; i++, d2++)
255  {
256    void **ptr = *d2;
257    *ptr = CollectObject((LObject *)*ptr);
258  }
259}
260
261void collect_space(int which_space) // should be tmp or permanent
262{
263  return; /* XXX */
264
265  int old_space = current_space;
266  cstart = space[which_space];
267  cend = free_space[which_space];
268
269  space_size[GC_SPACE] = space_size[which_space];
270  uint8_t *new_space = (uint8_t *)malloc(space_size[GC_SPACE]);
271  current_space = GC_SPACE;
272  free_space[GC_SPACE] = space[GC_SPACE] = new_space;
273
274  collected_start = new_space;
275  collected_end = new_space + space_size[GC_SPACE];
276
277//dump_memory((char *)lsym_root->name, 128, 196);
278//dump_memory((char *)0xb6782025, 32, 48);
279  collect_symbols(LSymbol::root);
280  collect_stacks();
281
282  // for debuging clear it out
283  memset(space[which_space], 0, space_size[which_space]);
284  free(space[which_space]);
285
286  space[which_space] = new_space;
287  free_space[which_space] = new_space
288                          + (free_space[GC_SPACE] - space[GC_SPACE]);
289  current_space = old_space;
290}
291
Note: See TracBrowser for help on using the repository browser.