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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
/***************************************************************************
Copyright (C) 2005 Christoph Reichenbach <reichenb@colorado.edu>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public Licence as
published by the Free Software Foundaton; either version 2 of the
Licence, or (at your option) any later version.
It is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
merchantibility or fitness for a particular purpose. See the
GNU General Public Licence for more details.
You should have received a copy of the GNU General Public Licence
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
***************************************************************************/
#include "sci/engine/gc.h"
#define WORKLIST_CHUNK_SIZE 32
/*#define DEBUG_GC*/
/*#define DEBUG_GC_VERBOSE*/
typedef struct _worklist {
int used;
reg_t entries[WORKLIST_CHUNK_SIZE];
struct _worklist *next;
} worklist_t;
static worklist_t *
fresh_worklist(worklist_t *old)
{
worklist_t *retval = (worklist_t*)sci_malloc(sizeof(worklist_t));
retval->used = 0;
retval->next = old;
return retval;
}
static worklist_t *
new_worklist()
{
return fresh_worklist(NULL);
}
static void
worklist_push(worklist_t **wlp, reg_t_hash_map_ptr hashmap, reg_t reg)
{
worklist_t *wl = *wlp;
char added;
if (!reg.segment) /* No numbers */
return;
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] Adding "PREG"\n", PRINT_REG(reg));
#endif
reg_t_hash_map_check_value(hashmap, reg, 1, &added);
if (!added)
return; /* already dealt with it */
if (!wl || wl->used == WORKLIST_CHUNK_SIZE)
*wlp = wl = fresh_worklist(wl);
wl->entries[wl->used++] = reg;
}
static int
worklist_has_next(worklist_t *wl)
{
return (wl && wl->used);
}
static reg_t
worklist_pop(worklist_t **wlp)
{
worklist_t *wl = *wlp;
reg_t retval;
if (!wl || !wl->used) {
fprintf(stderr, "Attempt to pop from empty worklist");
exit(1);
}
retval = wl->entries[--wl->used];
if (!wl->used) {
*wlp = wl->next;
sci_free(wl);
}
return retval;
}
static void
free_worklist(worklist_t *wl)
{
if (wl) {
if (wl->next)
free_worklist(wl->next);
sci_free(wl);
}
}
typedef struct {
seg_interface_t **interfaces;
int interfaces_nr;
reg_t_hash_map_ptr normal_map;
} normaliser_t;
void
store_normalised(void *pre_normaliser, reg_t reg, int _)
{
seg_interface_t *interfce;
normaliser_t *normaliser = (normaliser_t *) pre_normaliser;
interfce = (reg.segment < normaliser->interfaces_nr)
? normaliser->interfaces[reg.segment]
: NULL;
if (interfce) {
reg = interfce->find_canonic_address(interfce, reg);
reg_t_hash_map_check_value(normaliser->normal_map, reg, 1, NULL);
}
}
static reg_t_hash_map_ptr
normalise_hashmap_ptrs(reg_t_hash_map_ptr nonnormal_map, seg_interface_t **interfaces, int interfaces_nr)
{
normaliser_t normaliser;
normaliser.normal_map = new_reg_t_hash_map();
normaliser.interfaces_nr = interfaces_nr;
normaliser.interfaces = interfaces;
apply_to_reg_t_hash_map(nonnormal_map, &normaliser, &store_normalised);
return normaliser.normal_map;
}
typedef struct {
reg_t_hash_map_ptr nonnormal_map;
worklist_t **worklist_ref;
} worklist_manager_t;
void
add_outgoing_refs(void *pre_wm, reg_t addr)
{
worklist_manager_t *wm = (worklist_manager_t *) pre_wm;
worklist_push(wm->worklist_ref, wm->nonnormal_map, addr);
}
reg_t_hash_map_ptr
find_all_used_references(state_t *s)
{
seg_manager_t *sm = &(s->seg_manager);
seg_interface_t **interfaces = (seg_interface_t**)sci_calloc(sizeof(seg_interface_t *), sm->heap_size);
reg_t_hash_map_ptr nonnormal_map = new_reg_t_hash_map();
reg_t_hash_map_ptr normal_map = NULL;
worklist_t *worklist = new_worklist();
worklist_manager_t worklist_manager;
int i;
worklist_manager.worklist_ref = &worklist;
worklist_manager.nonnormal_map = nonnormal_map;
for (i = 1; i < sm->heap_size; i++)
if (sm->heap[i] == NULL)
interfaces[i] = NULL;
else
interfaces[i] = get_seg_interface(sm, i);
/* Initialise */
/* Init: Registers */
worklist_push(&worklist, nonnormal_map, s->r_acc);
worklist_push(&worklist, nonnormal_map, s->r_prev);
/* Init: Value Stack */
/* We do this one by hand since the stack doesn't know the current execution stack */
{
exec_stack_t *xs = s->execution_stack + s->execution_stack_pos;
reg_t *pos;
for (pos = s->stack_base; pos < xs->sp; pos++)
worklist_push(&worklist, nonnormal_map, *pos);
}
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] -- Finished adding value stack");
#endif
/* Init: Execution Stack */
for (i = 0; i <= s->execution_stack_pos; i++) {
exec_stack_t *es = s->execution_stack + i;
if (es->type != EXEC_STACK_TYPE_KERNEL) {
worklist_push(&worklist, nonnormal_map, es->objp);
worklist_push(&worklist, nonnormal_map, es->sendp);
if (es->type == EXEC_STACK_TYPE_VARSELECTOR)
worklist_push(&worklist, nonnormal_map, *(es->addr.varp));
}
}
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] -- Finished adding execution stack");
#endif
/* Init: Explicitly loaded scripts */
for (i = 1; i < sm->heap_size; i++)
if (interfaces[i]
&& interfaces[i]->type_id == MEM_OBJ_SCRIPT) {
script_t *script = &(interfaces[i]->mobj->data.script);
if (script->lockers) { /* Explicitly loaded? */
int obj_nr;
/* Locals, if present */
worklist_push(&worklist, nonnormal_map, make_reg(script->locals_segment, 0));
/* All objects (may be classes, may be indirectly reachable) */
for (obj_nr = 0; obj_nr < script->objects_nr; obj_nr++) {
object_t *obj = script->objects + obj_nr;
worklist_push(&worklist,
nonnormal_map,
obj->pos);
}
}
}
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] -- Finished explicitly loaded scripts, done with root set");
#endif
/* Run Worklist Algorithm */
while (worklist_has_next(worklist)) {
reg_t reg = worklist_pop(&worklist);
if (reg.segment != s->stack_segment) { /* No need to repeat this one */
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] Checking "PREG"\n", PRINT_REG(reg));
#endif
if (reg.segment < sm->heap_size
&& interfaces[reg.segment])
interfaces[reg.segment]->list_all_outgoing_references(interfaces[reg.segment],
s,
reg,
&worklist_manager,
add_outgoing_refs);
}
}
/* Normalise */
normal_map = normalise_hashmap_ptrs(nonnormal_map, interfaces, sm->heap_size);
/* Cleanup */
for (i = 1; i < sm->heap_size; i++)
if (interfaces[i])
interfaces[i]->deallocate_self(interfaces[i]);
sci_free(interfaces);
free_reg_t_hash_map(nonnormal_map);
return normal_map;
}
typedef struct {
seg_interface_t *interfce;
#ifdef DEBUG_GC
char *segnames[MEM_OBJ_MAX + 1];
int segcount[MEM_OBJ_MAX + 1];
#endif
reg_t_hash_map_ptr use_map;
} deallocator_t;
void
free_unless_used (void *pre_use_map, reg_t addr)
{
deallocator_t *deallocator = (deallocator_t *) pre_use_map;
reg_t_hash_map_ptr use_map = deallocator->use_map;
if (0 > reg_t_hash_map_check_value(use_map, addr, 0, NULL)) {
/* Not found -> we can free it */
deallocator->interfce->free_at_address(deallocator->interfce, addr);
#ifdef DEBUG_GC
sciprintf("[GC] Deallocating "PREG"\n", PRINT_REG(addr));
deallocator->segcount[deallocator->interfce->type_id]++;
#endif
}
}
void
run_gc(state_t *s)
{
int seg_nr;
deallocator_t deallocator;
seg_manager_t *sm = &(s->seg_manager);
#ifdef DEBUG_GC
c_segtable(s);
sciprintf("[GC] Running...\n");
memset(&(deallocator.segcount), 0, sizeof(int) * (MEM_OBJ_MAX + 1));
#endif
deallocator.use_map = find_all_used_references(s);
for (seg_nr = 1; seg_nr < sm->heap_size; seg_nr++)
if (sm->heap[seg_nr] != NULL) {
deallocator.interfce = get_seg_interface(sm, seg_nr);
#ifdef DEBUG_GC
deallocator.segnames[deallocator.interfce->type_id] = deallocator.interfce->type;
#endif
deallocator.interfce->list_all_deallocatable(deallocator.interfce,
&deallocator,
free_unless_used);
deallocator.interfce->deallocate_self(deallocator.interfce);
}
free_reg_t_hash_map(deallocator.use_map);
#ifdef DEBUG_GC
{
int i;
sciprintf("[GC] Summary:\n");
for (i = 0; i <= MEM_OBJ_MAX; i++)
if (deallocator.segcount[i])
sciprintf("\t%d\t* %s\n",
deallocator.segcount[i],
deallocator.segnames[i]);
}
#endif
}
|