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

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

lisp: remove old debug code.

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