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
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*
*/
#include "sci/engine/gc.h"
#include "common/array.h"
namespace Sci {
//#define DEBUG_GC
//#define DEBUG_GC_VERBOSE
struct WorklistManager {
Common::Array<reg_t> _worklist;
reg_t_hash_map _map;
void push(reg_t reg) {
if (!reg.segment) // No numbers
return;
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] Adding "PREG"\n", PRINT_REG(reg));
#endif
if (_map.contains(reg))
return; // already dealt with it
_map.setVal(reg, true);
_worklist.push_back(reg);
}
};
static reg_t_hash_map *normalise_hashmap_ptrs(reg_t_hash_map &nonnormal_map, const Common::Array<SegInterface *> &interfaces) {
reg_t_hash_map *normal_map = new reg_t_hash_map();
for (reg_t_hash_map::iterator i = nonnormal_map.begin(); i != nonnormal_map.end(); ++i) {
reg_t reg = i->_key;
SegInterface *interfce = (reg.segment < interfaces.size()) ? interfaces[reg.segment] : NULL;
if (interfce) {
reg = interfce->findCanonicAddress(reg);
normal_map->setVal(reg, true);
}
}
return normal_map;
}
void add_outgoing_refs(void *refcon, reg_t addr) {
WorklistManager *wm = (WorklistManager *)refcon;
wm->push(addr);
}
reg_t_hash_map *find_all_used_references(EngineState *s) {
SegManager *sm = s->seg_manager;
Common::Array<SegInterface *> interfaces;
reg_t_hash_map *normal_map = NULL;
WorklistManager wm;
uint i;
interfaces.resize(sm->_heap.size());
for (i = 1; i < sm->_heap.size(); i++)
if (sm->_heap[i] == NULL)
interfaces[i] = NULL;
else
interfaces[i] = sm->getSegInterface(i);
// Initialise
// Init: Registers
wm.push(s->r_acc);
wm.push(s->r_prev);
// Init: Value Stack
// We do this one by hand since the stack doesn't know the current execution stack
{
ExecStack &xs = s->_executionStack[s->execution_stack_pos];
reg_t *pos;
for (pos = s->stack_base; pos < xs.sp; pos++)
wm.push(*pos);
}
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] -- Finished adding value stack");
#endif
// Init: Execution Stack
for (i = 0; (int)i <= s->execution_stack_pos; i++) {
ExecStack &es = s->_executionStack[i];
if (es.type != EXEC_STACK_TYPE_KERNEL) {
wm.push(es.objp);
wm.push(es.sendp);
if (es.type == EXEC_STACK_TYPE_VARSELECTOR)
wm.push(*(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]->getType() == MEM_OBJ_SCRIPT) {
Script *script = (Script *)(interfaces[i]->getMobj());
if (script->lockers) { // Explicitly loaded?
int obj_nr;
// Locals, if present
wm.push(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 *obj = script->objects + obj_nr;
wm.push(obj->pos);
}
}
}
#ifdef DEBUG_GC_VERBOSE
sciprintf("[GC] -- Finished explicitly loaded scripts, done with root set");
#endif
// Run Worklist Algorithm
while (!wm._worklist.empty()) {
reg_t reg = wm._worklist.back();
wm._worklist.pop_back();
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]->listAllOutgoingReferences(s, reg, &wm, add_outgoing_refs);
}
}
// Normalise
normal_map = normalise_hashmap_ptrs(wm._map, interfaces);
// Cleanup
for (i = 1; i < sm->_heap.size(); i++)
if (interfaces[i])
delete interfaces[i];
return normal_map;
}
struct deallocator_t {
SegInterface *interfce;
#ifdef DEBUG_GC
char *segnames[MEM_OBJ_MAX + 1];
int segcount[MEM_OBJ_MAX + 1];
#endif
reg_t_hash_map *use_map;
};
void free_unless_used(void *refcon, reg_t addr) {
deallocator_t *deallocator = (deallocator_t *)refcon;
reg_t_hash_map *use_map = deallocator->use_map;
if (!use_map->contains(addr)) {
// Not found -> we can free it
deallocator->interfce->freeAtAddress(addr);
#ifdef DEBUG_GC
sciprintf("[GC] Deallocating "PREG"\n", PRINT_REG(addr));
deallocator->segcount[deallocator->interfce->getType()]++;
#endif
}
}
void run_gc(EngineState *s) {
uint seg_nr;
deallocator_t deallocator;
SegManager *sm = s->seg_manager;
#ifdef DEBUG_GC
extern int c_segtable(EngineState *s);
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 = sm->getSegInterface(seg_nr);
#ifdef DEBUG_GC
deallocator.segnames[deallocator.interfce->getType()] = deallocator.interfce->type;
#endif
deallocator.interfce->listAllDeallocatable(&deallocator, free_unless_used);
delete deallocator.interfce;
}
}
delete 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
}
} // End of namespace Sci
|