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

Last change on this file since 524 was 524, checked in by Sam Hocevar, 8 years ago

core: Get rid of mostly useless headers, move endianness handling to
common.h (and rewrite functions so that they do not need the SDL headers)
and move a few functions out of sdlport's video.cpp. These functions
were in the original video.cpp (which reappears) and shouldn't be part
of the SDL port.

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