2009-02-17 15:02:16 +00:00
|
|
|
/* 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$
|
|
|
|
*
|
|
|
|
*/
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-15 08:34:13 +00:00
|
|
|
#include "sci/engine/gc.h"
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 10:23:36 +00:00
|
|
|
namespace Sci {
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
#define WORKLIST_CHUNK_SIZE 32
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
//#define DEBUG_GC
|
|
|
|
//#define DEBUG_GC_VERBOSE
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-21 22:06:42 +00:00
|
|
|
struct worklist_t {
|
2009-02-15 06:10:59 +00:00
|
|
|
int used;
|
|
|
|
reg_t entries[WORKLIST_CHUNK_SIZE];
|
2009-02-21 22:06:42 +00:00
|
|
|
worklist_t *next;
|
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
static worklist_t *fresh_worklist(worklist_t *old) {
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_t *retval = (worklist_t*)sci_malloc(sizeof(worklist_t));
|
|
|
|
retval->used = 0;
|
|
|
|
retval->next = old;
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
static worklist_t *new_worklist() {
|
2009-02-15 06:10:59 +00:00
|
|
|
return fresh_worklist(NULL);
|
|
|
|
}
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
static void worklist_push(worklist_t **wlp, reg_t_hash_map *hashmap, reg_t reg) {
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_t *wl = *wlp;
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
if (!reg.segment) // No numbers
|
2009-02-15 06:10:59 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef DEBUG_GC_VERBOSE
|
|
|
|
sciprintf("[GC] Adding "PREG"\n", PRINT_REG(reg));
|
|
|
|
#endif
|
|
|
|
|
2009-02-16 01:58:30 +00:00
|
|
|
if (hashmap->contains(reg))
|
2009-02-19 08:43:28 +00:00
|
|
|
return; // already dealt with it
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-16 01:58:30 +00:00
|
|
|
hashmap->setVal(reg, true);
|
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
if (!wl || wl->used == WORKLIST_CHUNK_SIZE)
|
|
|
|
*wlp = wl = fresh_worklist(wl);
|
|
|
|
|
|
|
|
wl->entries[wl->used++] = reg;
|
|
|
|
}
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
static int worklist_has_next(worklist_t *wl) {
|
2009-02-15 06:10:59 +00:00
|
|
|
return (wl && wl->used);
|
|
|
|
}
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
static reg_t worklist_pop(worklist_t **wlp) {
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_t *wl = *wlp;
|
|
|
|
reg_t retval;
|
|
|
|
|
|
|
|
if (!wl || !wl->used) {
|
2009-02-21 18:16:17 +00:00
|
|
|
error("Attempt to pop from empty worklist");
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retval = wl->entries[--wl->used];
|
|
|
|
|
|
|
|
if (!wl->used) {
|
|
|
|
*wlp = wl->next;
|
2009-02-17 13:51:52 +00:00
|
|
|
free(wl);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
static void free_worklist(worklist_t *wl) {
|
2009-02-15 06:10:59 +00:00
|
|
|
if (wl) {
|
|
|
|
if (wl->next)
|
|
|
|
free_worklist(wl->next);
|
2009-02-17 13:51:52 +00:00
|
|
|
free(wl);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-22 12:18:22 +00:00
|
|
|
static reg_t_hash_map *normalise_hashmap_ptrs(reg_t_hash_map *nonnormal_map, SegInterface **interfaces, int interfaces_nr) {
|
2009-02-16 01:58:30 +00:00
|
|
|
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) {
|
2009-02-21 23:27:24 +00:00
|
|
|
SegInterface *interfce;
|
2009-02-16 01:58:30 +00:00
|
|
|
reg_t reg = i->_key;
|
2009-02-19 08:43:28 +00:00
|
|
|
interfce = (reg.segment < interfaces_nr) ? interfaces[reg.segment] : NULL;
|
|
|
|
|
2009-02-16 01:58:30 +00:00
|
|
|
if (interfce) {
|
2009-02-22 12:18:22 +00:00
|
|
|
reg = interfce->findCanonicAddress(reg);
|
2009-02-16 01:58:30 +00:00
|
|
|
normal_map->setVal(reg, true);
|
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
2009-02-16 01:58:30 +00:00
|
|
|
return normal_map;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-02-21 22:06:42 +00:00
|
|
|
struct worklist_manager_t {
|
2009-02-16 01:58:30 +00:00
|
|
|
reg_t_hash_map *nonnormal_map;
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_t **worklist_ref;
|
2009-02-21 22:06:42 +00:00
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
void add_outgoing_refs(void *pre_wm, reg_t addr) {
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_manager_t *wm = (worklist_manager_t *) pre_wm;
|
|
|
|
worklist_push(wm->worklist_ref, wm->nonnormal_map, addr);
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
reg_t_hash_map *find_all_used_references(EngineState *s) {
|
2009-02-21 23:27:24 +00:00
|
|
|
SegManager *sm = s->seg_manager;
|
|
|
|
SegInterface **interfaces = (SegInterface **)sci_calloc(sizeof(SegInterface *), sm->heap_size);
|
2009-02-16 01:58:30 +00:00
|
|
|
reg_t_hash_map *nonnormal_map = new reg_t_hash_map();
|
|
|
|
reg_t_hash_map *normal_map = NULL;
|
2009-02-15 06:10:59 +00:00
|
|
|
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
|
2009-02-21 23:27:24 +00:00
|
|
|
interfaces[i] = sm->getSegInterface(i);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Initialise
|
|
|
|
// Init: Registers
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_push(&worklist, nonnormal_map, s->r_acc);
|
|
|
|
worklist_push(&worklist, nonnormal_map, s->r_prev);
|
2009-02-19 08:43:28 +00:00
|
|
|
// Init: Value Stack
|
|
|
|
// We do this one by hand since the stack doesn't know the current execution stack
|
2009-02-15 06:10:59 +00:00
|
|
|
{
|
2009-02-28 11:12:59 +00:00
|
|
|
ExecStack *xs = s->execution_stack + s->execution_stack_pos;
|
2009-02-15 06:10:59 +00:00
|
|
|
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
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Init: Execution Stack
|
2009-02-15 06:10:59 +00:00
|
|
|
for (i = 0; i <= s->execution_stack_pos; i++) {
|
2009-02-28 11:12:59 +00:00
|
|
|
ExecStack *es = s->execution_stack + i;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Init: Explicitly loaded scripts
|
2009-02-15 06:10:59 +00:00
|
|
|
for (i = 1; i < sm->heap_size; i++)
|
|
|
|
if (interfaces[i]
|
2009-02-22 12:18:22 +00:00
|
|
|
&& interfaces[i]->getType() == MEM_OBJ_SCRIPT) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Script *script = &(interfaces[i]->getMobj()->data.script);
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
if (script->lockers) { // Explicitly loaded?
|
2009-02-15 06:10:59 +00:00
|
|
|
int obj_nr;
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Locals, if present
|
2009-02-15 06:10:59 +00:00
|
|
|
worklist_push(&worklist, nonnormal_map, make_reg(script->locals_segment, 0));
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// All objects (may be classes, may be indirectly reachable)
|
2009-02-15 06:10:59 +00:00
|
|
|
for (obj_nr = 0; obj_nr < script->objects_nr; obj_nr++) {
|
2009-02-28 11:12:59 +00:00
|
|
|
Object *obj = script->objects + obj_nr;
|
2009-02-19 08:43:28 +00:00
|
|
|
worklist_push(&worklist, nonnormal_map, obj->pos);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_GC_VERBOSE
|
|
|
|
sciprintf("[GC] -- Finished explicitly loaded scripts, done with root set");
|
|
|
|
#endif
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Run Worklist Algorithm
|
2009-02-15 06:10:59 +00:00
|
|
|
while (worklist_has_next(worklist)) {
|
|
|
|
reg_t reg = worklist_pop(&worklist);
|
2009-02-19 08:43:28 +00:00
|
|
|
if (reg.segment != s->stack_segment) { // No need to repeat this one
|
2009-02-15 06:10:59 +00:00
|
|
|
#ifdef DEBUG_GC_VERBOSE
|
2009-02-15 22:28:12 +00:00
|
|
|
sciprintf("[GC] Checking "PREG"\n", PRINT_REG(reg));
|
2009-02-15 06:10:59 +00:00
|
|
|
#endif
|
2009-02-19 08:43:28 +00:00
|
|
|
if (reg.segment < sm->heap_size && interfaces[reg.segment])
|
2009-02-22 12:18:22 +00:00
|
|
|
interfaces[reg.segment]->listAllOutgoingReferences(s, reg,
|
2009-02-19 08:43:28 +00:00
|
|
|
&worklist_manager, add_outgoing_refs);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Normalise
|
2009-02-15 06:10:59 +00:00
|
|
|
normal_map = normalise_hashmap_ptrs(nonnormal_map, interfaces, sm->heap_size);
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
// Cleanup
|
2009-02-15 06:10:59 +00:00
|
|
|
for (i = 1; i < sm->heap_size; i++)
|
|
|
|
if (interfaces[i])
|
2009-02-22 12:18:22 +00:00
|
|
|
delete interfaces[i];
|
2009-02-19 08:43:28 +00:00
|
|
|
|
2009-02-17 13:51:52 +00:00
|
|
|
free(interfaces);
|
2009-02-16 01:58:30 +00:00
|
|
|
delete nonnormal_map;
|
2009-02-19 08:43:28 +00:00
|
|
|
|
2009-02-15 06:10:59 +00:00
|
|
|
return normal_map;
|
|
|
|
}
|
|
|
|
|
2009-02-21 22:06:42 +00:00
|
|
|
struct deallocator_t {
|
2009-02-21 23:27:24 +00:00
|
|
|
SegInterface *interfce;
|
2009-02-15 06:10:59 +00:00
|
|
|
#ifdef DEBUG_GC
|
|
|
|
char *segnames[MEM_OBJ_MAX + 1];
|
|
|
|
int segcount[MEM_OBJ_MAX + 1];
|
|
|
|
#endif
|
2009-02-16 01:58:30 +00:00
|
|
|
reg_t_hash_map *use_map;
|
2009-02-21 22:06:42 +00:00
|
|
|
};
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
void free_unless_used(void *pre_use_map, reg_t addr) {
|
|
|
|
deallocator_t *deallocator = (deallocator_t *)pre_use_map;
|
2009-02-16 01:58:30 +00:00
|
|
|
reg_t_hash_map *use_map = deallocator->use_map;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-16 01:58:30 +00:00
|
|
|
if (!use_map->contains(addr)) {
|
2009-02-19 08:43:28 +00:00
|
|
|
// Not found -> we can free it
|
2009-02-22 12:18:22 +00:00
|
|
|
deallocator->interfce->freeAtAddress(addr);
|
2009-02-15 06:10:59 +00:00
|
|
|
#ifdef DEBUG_GC
|
|
|
|
sciprintf("[GC] Deallocating "PREG"\n", PRINT_REG(addr));
|
|
|
|
deallocator->segcount[deallocator->interfce->type_id]++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-02-21 10:47:56 +00:00
|
|
|
void run_gc(EngineState *s) {
|
2009-02-15 06:10:59 +00:00
|
|
|
int seg_nr;
|
|
|
|
deallocator_t deallocator;
|
2009-02-21 23:27:24 +00:00
|
|
|
SegManager *sm = s->seg_manager;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
#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);
|
|
|
|
|
2009-02-19 08:43:28 +00:00
|
|
|
for (seg_nr = 1; seg_nr < sm->heap_size; seg_nr++) {
|
2009-02-15 06:10:59 +00:00
|
|
|
if (sm->heap[seg_nr] != NULL) {
|
2009-02-21 23:27:24 +00:00
|
|
|
deallocator.interfce = sm->getSegInterface(seg_nr);
|
2009-02-15 06:10:59 +00:00
|
|
|
#ifdef DEBUG_GC
|
|
|
|
deallocator.segnames[deallocator.interfce->type_id] = deallocator.interfce->type;
|
|
|
|
#endif
|
2009-02-22 12:18:22 +00:00
|
|
|
deallocator.interfce->listAllDeallocatable(&deallocator, free_unless_used);
|
|
|
|
delete deallocator.interfce;
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
2009-02-19 08:43:28 +00:00
|
|
|
}
|
2009-02-15 06:10:59 +00:00
|
|
|
|
2009-02-16 01:58:30 +00:00
|
|
|
delete deallocator.use_map;
|
2009-02-15 06:10:59 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_GC
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
sciprintf("[GC] Summary:\n");
|
|
|
|
for (i = 0; i <= MEM_OBJ_MAX; i++)
|
|
|
|
if (deallocator.segcount[i])
|
2009-02-19 08:43:28 +00:00
|
|
|
sciprintf("\t%d\t* %s\n", deallocator.segcount[i], deallocator.segnames[i]);
|
2009-02-15 06:10:59 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
2009-02-21 10:23:36 +00:00
|
|
|
|
|
|
|
} // End of namespace Sci
|