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
|
/* 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.
*
*/
#include "sci/engine/gc.h"
#include "common/array.h"
#include "sci/graphics/ports.h"
namespace Sci {
//#define GC_DEBUG_CODE
#ifdef GC_DEBUG_CODE
const char *segmentTypeNames[] = {
"invalid", // 0
"script", // 1
"clones", // 2
"locals", // 3
"stack", // 4
"obsolete", // 5: obsolete system strings
"lists", // 6
"nodes", // 7
"hunk", // 8
"dynmem", // 9
"obsolete", // 10: obsolete string fragments
"array", // 11: SCI32 arrays
"string" // 12: SCI32 strings
};
#endif
void WorklistManager::push(reg_t reg) {
if (!reg.segment) // No numbers
return;
debugC(kDebugLevelGC, "[GC] Adding %04x:%04x", PRINT_REG(reg));
if (_map.contains(reg))
return; // already dealt with it
_map.setVal(reg, true);
_worklist.push_back(reg);
}
void WorklistManager::pushArray(const Common::Array<reg_t> &tmp) {
for (Common::Array<reg_t>::const_iterator it = tmp.begin(); it != tmp.end(); ++it)
push(*it);
}
static AddrSet *normalizeAddresses(SegManager *segMan, const AddrSet &nonnormal_map) {
AddrSet *normal_map = new AddrSet();
for (AddrSet::const_iterator i = nonnormal_map.begin(); i != nonnormal_map.end(); ++i) {
reg_t reg = i->_key;
SegmentObj *mobj = segMan->getSegmentObj(reg.segment);
if (mobj) {
reg = mobj->findCanonicAddress(segMan, reg);
normal_map->setVal(reg, true);
}
}
return normal_map;
}
static void processWorkList(SegManager *segMan, WorklistManager &wm, const Common::Array<SegmentObj *> &heap) {
SegmentId stackSegment = segMan->findSegmentByType(SEG_TYPE_STACK);
while (!wm._worklist.empty()) {
reg_t reg = wm._worklist.back();
wm._worklist.pop_back();
if (reg.segment != stackSegment) { // No need to repeat this one
debugC(kDebugLevelGC, "[GC] Checking %04x:%04x", PRINT_REG(reg));
if (reg.segment < heap.size() && heap[reg.segment]) {
// Valid heap object? Find its outgoing references!
wm.pushArray(heap[reg.segment]->listAllOutgoingReferences(reg));
}
}
}
}
AddrSet *findAllActiveReferences(EngineState *s) {
assert(!s->_executionStack.empty());
WorklistManager wm;
// Initialize registers
wm.push(s->r_acc);
wm.push(s->r_prev);
// Initialize value stack
// We do this one by hand since the stack doesn't know the current execution stack
Common::List<ExecStack>::const_iterator iter = s->_executionStack.reverse_begin();
// Skip fake kernel stack frame if it's on top
if ((*iter).type == EXEC_STACK_TYPE_KERNEL)
--iter;
assert((iter != s->_executionStack.end()) && ((*iter).type != EXEC_STACK_TYPE_KERNEL));
const StackPtr sp = iter->sp;
for (reg_t *pos = s->stack_base; pos < sp; pos++)
wm.push(*pos);
debugC(kDebugLevelGC, "[GC] -- Finished adding value stack");
// Init: Execution Stack
for (iter = s->_executionStack.begin();
iter != s->_executionStack.end(); ++iter) {
const ExecStack &es = *iter;
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.getVarPointer(s->_segMan)));
}
}
debugC(kDebugLevelGC, "[GC] -- Finished adding execution stack");
const Common::Array<SegmentObj *> &heap = s->_segMan->getSegments();
uint heapSize = heap.size();
// Init: Explicitly loaded scripts
for (uint i = 1; i < heapSize; i++) {
if (heap[i] && heap[i]->getType() == SEG_TYPE_SCRIPT) {
Script *script = (Script *)heap[i];
if (script->getLockers()) { // Explicitly loaded?
wm.pushArray(script->listObjectReferences());
}
}
}
debugC(kDebugLevelGC, "[GC] -- Finished explicitly loaded scripts, done with root set");
processWorkList(s->_segMan, wm, heap);
if (g_sci->_gfxPorts)
g_sci->_gfxPorts->processEngineHunkList(wm);
return normalizeAddresses(s->_segMan, wm._map);
}
void run_gc(EngineState *s) {
SegManager *segMan = s->_segMan;
// Some debug stuff
debugC(kDebugLevelGC, "[GC] Running...");
#ifdef GC_DEBUG_CODE
const char *segnames[SEG_TYPE_MAX + 1];
int segcount[SEG_TYPE_MAX + 1];
memset(segnames, 0, sizeof(segnames));
memset(segcount, 0, sizeof(segcount));
#endif
// Compute the set of all segments references currently in use.
AddrSet *activeRefs = findAllActiveReferences(s);
// Iterate over all segments, and check for each whether it
// contains stuff that can be collected.
const Common::Array<SegmentObj *> &heap = segMan->getSegments();
for (uint seg = 1; seg < heap.size(); seg++) {
SegmentObj *mobj = heap[seg];
if (mobj != NULL) {
#ifdef GC_DEBUG_CODE
const SegmentType type = mobj->getType();
segnames[type] = segmentTypeNames[type];
#endif
// Get a list of all deallocatable objects in this segment,
// then free any which are not referenced from somewhere.
const Common::Array<reg_t> tmp = mobj->listAllDeallocatable(seg);
for (Common::Array<reg_t>::const_iterator it = tmp.begin(); it != tmp.end(); ++it) {
const reg_t addr = *it;
if (!activeRefs->contains(addr)) {
// Not found -> we can free it
mobj->freeAtAddress(segMan, addr);
debugC(kDebugLevelGC, "[GC] Deallocating %04x:%04x", PRINT_REG(addr));
#ifdef GC_DEBUG_CODE
segcount[type]++;
#endif
}
}
}
}
delete activeRefs;
#ifdef GC_DEBUG_CODE
// Output debug summary of garbage collection
debugC(kDebugLevelGC, "[GC] Summary:");
for (int i = 0; i <= SEG_TYPE_MAX; i++)
if (segcount[i])
debugC(kDebugLevelGC, "\t%d\t* %s", segcount[i], segnames[i]);
#endif
}
} // End of namespace Sci
|