From 6af056c89d16ae506aab0c7a29ae43edf8105925 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Mon, 16 Feb 2009 01:58:30 +0000 Subject: Reimplemented reg_t_hashmap using Common::HashMap svn-id: r38340 --- engines/sci/engine/gc.cpp | 72 +++++++++++++++-------------------- engines/sci/engine/gc.h | 24 ++++++++++-- engines/sci/engine/scriptdebug.cpp | 14 +++---- engines/sci/include/reg_t_hashmap.h | 71 ---------------------------------- engines/sci/module.mk | 1 - engines/sci/sci.h | 25 ++++++++++++ engines/sci/scicore/reg_t_hashmap.cpp | 41 -------------------- 7 files changed, 81 insertions(+), 167 deletions(-) delete mode 100644 engines/sci/include/reg_t_hashmap.h delete mode 100644 engines/sci/scicore/reg_t_hashmap.cpp (limited to 'engines') diff --git a/engines/sci/engine/gc.cpp b/engines/sci/engine/gc.cpp index 484c0ec41d..295455cad9 100644 --- a/engines/sci/engine/gc.cpp +++ b/engines/sci/engine/gc.cpp @@ -45,9 +45,8 @@ new_worklist() { } static void -worklist_push(worklist_t **wlp, reg_t_hash_map_ptr hashmap, reg_t reg) { +worklist_push(worklist_t **wlp, reg_t_hash_map *hashmap, reg_t reg) { worklist_t *wl = *wlp; - char added; if (!reg.segment) /* No numbers */ return; @@ -56,11 +55,12 @@ worklist_push(worklist_t **wlp, reg_t_hash_map_ptr hashmap, reg_t reg) { sciprintf("[GC] Adding "PREG"\n", PRINT_REG(reg)); #endif - reg_t_hash_map_check_value(hashmap, reg, 1, &added); - - if (!added) + + if (hashmap->contains(reg)) return; /* already dealt with it */ + hashmap->setVal(reg, true); + if (!wl || wl->used == WORKLIST_CHUNK_SIZE) *wlp = wl = fresh_worklist(wl); @@ -101,41 +101,29 @@ free_worklist(worklist_t *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 * +normalise_hashmap_ptrs(reg_t_hash_map *nonnormal_map, seg_interface_t **interfaces, int interfaces_nr) { + 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) { + seg_interface_t *interfce; + reg_t reg = i->_key; + interfce = (reg.segment < interfaces_nr) + ? interfaces[reg.segment] + : NULL; + + if (interfce) { + reg = interfce->find_canonic_address(interfce, reg); + normal_map->setVal(reg, true); + } } -} - -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; + return normal_map; } typedef struct { - reg_t_hash_map_ptr nonnormal_map; + reg_t_hash_map *nonnormal_map; worklist_t **worklist_ref; } worklist_manager_t; @@ -145,12 +133,12 @@ add_outgoing_refs(void *pre_wm, reg_t addr) { worklist_push(wm->worklist_ref, wm->nonnormal_map, addr); } -reg_t_hash_map_ptr +reg_t_hash_map * 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; + reg_t_hash_map *nonnormal_map = new reg_t_hash_map(); + reg_t_hash_map *normal_map = NULL; worklist_t *worklist = new_worklist(); worklist_manager_t worklist_manager; int i; @@ -248,7 +236,7 @@ find_all_used_references(state_t *s) { if (interfaces[i]) interfaces[i]->deallocate_self(interfaces[i]); sci_free(interfaces); - free_reg_t_hash_map(nonnormal_map); + delete nonnormal_map; return normal_map; } @@ -259,15 +247,15 @@ typedef struct { char *segnames[MEM_OBJ_MAX + 1]; int segcount[MEM_OBJ_MAX + 1]; #endif - reg_t_hash_map_ptr use_map; + reg_t_hash_map *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; + reg_t_hash_map *use_map = deallocator->use_map; - if (0 > reg_t_hash_map_check_value(use_map, addr, 0, NULL)) { + if (!use_map->contains(addr)) { /* Not found -> we can free it */ deallocator->interfce->free_at_address(deallocator->interfce, addr); #ifdef DEBUG_GC @@ -306,7 +294,7 @@ run_gc(state_t *s) { deallocator.interfce->deallocate_self(deallocator.interfce); } - free_reg_t_hash_map(deallocator.use_map); + delete deallocator.use_map; #ifdef DEBUG_GC { diff --git a/engines/sci/engine/gc.h b/engines/sci/engine/gc.h index 4e64c0b424..a5c5f093df 100644 --- a/engines/sci/engine/gc.h +++ b/engines/sci/engine/gc.h @@ -22,14 +22,32 @@ #ifndef GC_H_ #define GC_H_ -#include "sci/include/reg_t_hashmap.h" +#include "common/hashmap.h" +#include "sci/include/vm_types.h" #include "sci/include/engine.h" -reg_t_hash_map_ptr + +struct reg_t_EqualTo { + bool operator()(const reg_t& x, const reg_t& y) const { + return (x.segment == y.segment) && (x.offset == y.offset); + } +}; + +struct reg_t_Hash { + uint operator()(const reg_t& x) const { + return (x.segment << 3) | x.offset; + } +}; + +// The reg_t_hash_map is actually really a hashset +typedef Common::HashMap reg_t_hash_map; + + +reg_t_hash_map * find_all_used_references(state_t *s); /* Finds all used references and normalises them to their memory addresses ** Parameters: (state_t *) s: The state to gather all information from -** Returns : (reg_t_hash_map_ptr) A hash map containing entries for all used references +** Returns : (reg_t_hash_map *) A hash map containing entries for all used references */ void diff --git a/engines/sci/engine/scriptdebug.cpp b/engines/sci/engine/scriptdebug.cpp index b93f6749dd..cc173ec1ac 100644 --- a/engines/sci/engine/scriptdebug.cpp +++ b/engines/sci/engine/scriptdebug.cpp @@ -34,7 +34,6 @@ #include "sci/engine/kernel_types.h" #include "sci/include/sci_midi.h" #include "sci/include/sci_widgets.h" -#include "sci/include/reg_t_hashmap.h" #include "common/util.h" @@ -3278,19 +3277,16 @@ c_gc(state_t *s) { return 0; } -static void -print_all_of_them(void *_1, reg_t reg, int _2) { - sciprintf(" - "PREG"\n", PRINT_REG(reg)); -} - static int c_gc_list_reachable(state_t *s) { - reg_t_hash_map_ptr use_map = find_all_used_references(s); + reg_t_hash_map *use_map = find_all_used_references(s); sciprintf("Reachable references (normalised):\n"); - apply_to_reg_t_hash_map(use_map, NULL, print_all_of_them); + for (reg_t_hash_map::iterator i = use_map->begin(); i != use_map->end(); ++i) { + sciprintf(" - "PREG"\n", PRINT_REG(i->_key)); + } - free_reg_t_hash_map(use_map); + delete use_map; return 0; } diff --git a/engines/sci/include/reg_t_hashmap.h b/engines/sci/include/reg_t_hashmap.h deleted file mode 100644 index b451f675f2..0000000000 --- a/engines/sci/include/reg_t_hashmap.h +++ /dev/null @@ -1,71 +0,0 @@ -/*************************************************************************** - int_hashmap.h Copyright (C) 2001 Christoph Reichenbach - - - This program may be modified and copied freely according to the terms of - the GNU general public license (GPL), as long as the above copyright - notice and the licensing information contained herein are preserved. - - Please refer to www.gnu.org for licensing details. - - This work is provided AS IS, without warranty of any kind, expressed or - implied, including but not limited to the warranties of merchantibility, - noninfringement, and fitness for a specific purpose. The author will not - be held liable for any damage caused by this work or derivatives of it. - - By using this source code, you agree to the licensing terms as stated - above. - - - Please contact the maintainer for bug reports or inquiries. - - Current Maintainer: - - Christoph Reichenbach (CR) - -***************************************************************************/ - -#ifndef _REG_T_HASHMAP_H_ -#define _REG_T_HASHMAP_H_ - -#define DCS_REGT_HASH_MAX 512 - -#include "vm.h" -#ifdef HASH_MAX -# undef HASH_MAX -# undef COMP -# undef HASH -#endif - -#define HASH_MAX DCS_REGT_HASH_MAX -#define COMP(x, y) compare_reg_t(x, y) -#define HASH(x) (((x.segment << 3) | x.offset) & 0x1ff) -#undef MUST_FREE - -#include "hashmap.h" -DECLARE_STRUCTS(reg_t) -DECLARE_FUNCTIONS(reg_t) - -#ifndef BUILD_MAP_FUNCTIONS -# undef HASH_MAX -# undef COMP -# undef HASH -#endif - -/* see hashmap.h for descriptions of these functions */ -reg_t_hash_map_ptr -new_reg_t_hash_map(void); - -void -free_reg_t_hash_map(reg_t_hash_map_ptr); - -int -reg_t_hash_map_check_value(reg_t_hash_map_ptr, reg_t value, char add_p, char *added); - -int -reg_t_hash_map_remove_value(reg_t_hash_map_ptr, reg_t value); - -void -apply_to_reg_t_hash_map(reg_t_hash_map_ptr map, void *param, void (*note)(void *param, reg_t name, int value)); - -#endif /* _INT_HASHMAP_H_ */ diff --git a/engines/sci/module.mk b/engines/sci/module.mk index c90f0011dd..2d1f9843cf 100644 --- a/engines/sci/module.mk +++ b/engines/sci/module.mk @@ -58,7 +58,6 @@ MODULE_OBJS = \ scicore/exe_lzexe.o \ scicore/exe_raw.o \ scicore/int_hashmap.o \ - scicore/reg_t_hashmap.o \ scicore/resource.o \ scicore/resource_map.o \ scicore/resource_patch.o \ diff --git a/engines/sci/sci.h b/engines/sci/sci.h index f7a0c3b7c0..cad4d9910c 100644 --- a/engines/sci/sci.h +++ b/engines/sci/sci.h @@ -1,3 +1,28 @@ +/* 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: https://scummvm.svn.sourceforge.net/svnroot/scummvm/scummvm/trunk/engines/agi/agi.cpp $ + * $Id: agi.cpp 36279 2009-02-12 15:13:52Z thebluegr $ + * + */ + #ifndef SCI_H #define SCI_H diff --git a/engines/sci/scicore/reg_t_hashmap.cpp b/engines/sci/scicore/reg_t_hashmap.cpp deleted file mode 100644 index 3d1e37e454..0000000000 --- a/engines/sci/scicore/reg_t_hashmap.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/*************************************************************************** - int_hashmap. Copyright (C) 2001 Christoph Reichenbach - - - This program may be modified and copied freely according to the terms of - the GNU general public license (GPL), as long as the above copyright - notice and the licensing information contained herein are preserved. - - Please refer to www.gnu.org for licensing details. - - This work is provided AS IS, without warranty of any kind, expressed or - implied, including but not limited to the warranties of merchantibility, - noninfringement, and fitness for a specific purpose. The author will not - be held liable for any damage caused by this work or derivatives of it. - - By using this source code, you agree to the licensing terms as stated - above. - - - Please contact the maintainer for bug reports or inquiries. - - Current Maintainer: - - Christoph Reichenbach (CR) - -***************************************************************************/ - -#define BUILD_MAP_FUNCTIONS -#include "sci/include/reg_t_hashmap.h" - -#include "sci/scicore/hashmap.cpp" - -static inline int -compare_reg_t (reg_t lhs, reg_t rhs) { - if (lhs.segment == rhs.segment) - return lhs.offset - rhs.offset; - else - return lhs.segment - rhs.segment; -} - -DEFINE_FUNCTIONS(reg_t) -- cgit v1.2.3