SCI: Cleanup for some SegManager internals

- rename segGet and getSegment to getScriptSegment; the two can be
  distinguished by the parameter count.
- rename type SCRIPT_GET to ScriptLoadType to conform with our code
  formatting conventions
- rename get_class_address to getClassAddress
- some cleanup

svn-id: r43981
This commit is contained in:
Max Horn 2009-09-06 12:58:16 +00:00
parent d04b5d2c9b
commit fdbb167ea3
10 changed files with 51 additions and 51 deletions

View file

@ -2844,7 +2844,7 @@ int parse_reg_t(EngineState *s, const char *str, reg_t *dest) { // Returns 0 on
if (*endptr) if (*endptr)
return 1; return 1;
dest->segment = s->segMan->segGet(script_nr); dest->segment = s->segMan->getScriptSegment(script_nr);
if (!dest->segment) { if (!dest->segment) {
return 1; return 1;

View file

@ -326,7 +326,7 @@ int script_init_engine(EngineState *s) {
s->segMan = new SegManager(s->resMan); s->segMan = new SegManager(s->resMan);
s->gc_countdown = GC_INTERVAL - 1; s->gc_countdown = GC_INTERVAL - 1;
SegmentId script_000_segment = s->segMan->getSegment(0, SCRIPT_GET_LOCK); SegmentId script_000_segment = s->segMan->getScriptSegment(0, SCRIPT_GET_LOCK);
if (script_000_segment <= 0) { if (script_000_segment <= 0) {
debug(2, "Failed to instantiate script.000"); debug(2, "Failed to instantiate script.000");

View file

@ -268,7 +268,7 @@ reg_t kScriptID(EngineState *s, int, int argc, reg_t *argv) {
if (argv[0].segment) if (argv[0].segment)
return argv[0]; return argv[0];
SegmentId scriptSeg = s->segMan->getSegment(script, SCRIPT_GET_LOAD); SegmentId scriptSeg = s->segMan->getScriptSegment(script, SCRIPT_GET_LOAD);
Script *scr; Script *scr;
if (!scriptSeg) if (!scriptSeg)
@ -297,7 +297,7 @@ reg_t kDisposeScript(EngineState *s, int, int argc, reg_t *argv) {
if (argv[0].segment) if (argv[0].segment)
return s->r_acc; return s->r_acc;
int id = s->segMan->segGet(script); int id = s->segMan->getScriptSegment(script);
Script *scr = s->segMan->getScriptIfLoaded(id); Script *scr = s->segMan->getScriptIfLoaded(id);
if (scr) { if (scr) {
if (s->_executionStack.back().addr.pc.segment != id) if (s->_executionStack.back().addr.pc.segment != id)

View file

@ -146,7 +146,7 @@ reg_t kSetSynonyms(EngineState *s, int, int argc, reg_t *argv) {
int synonyms_nr = 0; int synonyms_nr = 0;
script = GET_SEL32V(objpos, number); script = GET_SEL32V(objpos, number);
seg = s->segMan->segGet(script); seg = s->segMan->getScriptSegment(script);
if (seg >= 0) if (seg >= 0)
synonyms_nr = s->segMan->getScript(seg)->getSynonymsNr(); synonyms_nr = s->segMan->getScript(seg)->getSynonymsNr();

View file

@ -319,9 +319,9 @@ void CloneTable::freeAtAddress(SegManager *segMan, reg_t addr) {
//-------------------- locals -------------------- //-------------------- locals --------------------
reg_t LocalVariables::findCanonicAddress(SegManager *segMan, reg_t addr) { reg_t LocalVariables::findCanonicAddress(SegManager *segMan, reg_t addr) {
// Reference the owning script // Reference the owning script
SegmentId owner_seg = segMan->segGet(script_id); SegmentId owner_seg = segMan->getScriptSegment(script_id);
assert(owner_seg >= 0); assert(owner_seg > 0);
return make_reg(owner_seg, 0); return make_reg(owner_seg, 0);
} }

View file

@ -714,7 +714,7 @@ EngineState *gamestate_restore(EngineState *s, Common::SeekableReadStream *fh) {
reconstruct_scripts(retval, retval->segMan); reconstruct_scripts(retval, retval->segMan);
retval->segMan->reconstructClones(); retval->segMan->reconstructClones();
retval->game_obj = s->game_obj; retval->game_obj = s->game_obj;
retval->script_000 = retval->segMan->getScript(retval->segMan->getSegment(0, SCRIPT_GET_DONT_LOAD)); retval->script_000 = retval->segMan->getScript(retval->segMan->getScriptSegment(0, SCRIPT_GET_DONT_LOAD));
retval->gc_countdown = GC_INTERVAL - 1; retval->gc_countdown = GC_INTERVAL - 1;
retval->sys_strings_segment = retval->segMan->findSegmentByType(MEM_OBJ_SYS_STRINGS); retval->sys_strings_segment = retval->segMan->findSegmentByType(MEM_OBJ_SYS_STRINGS);
retval->sys_strings = (SystemStrings *)GET_SEGMENT(*retval->segMan, retval->sys_strings_segment, MEM_OBJ_SYS_STRINGS); retval->sys_strings = (SystemStrings *)GET_SEGMENT(*retval->segMan, retval->sys_strings_segment, MEM_OBJ_SYS_STRINGS);

View file

@ -235,7 +235,7 @@ bool SegManager::scriptIsMarkedAsDeleted(SegmentId seg) {
int SegManager::deallocateScript(int script_nr) { int SegManager::deallocateScript(int script_nr) {
SegmentId seg = segGet(script_nr); SegmentId seg = getScriptSegment(script_nr);
deallocate(seg, true); deallocate(seg, true);
@ -262,13 +262,12 @@ MemObject *SegManager::memObjAllocate(SegmentId segid, int hash_id, MemObjectTyp
} }
// return the seg if script_id is valid and in the map, else -1 // return the seg if script_id is valid and in the map, else -1
SegmentId SegManager::segGet(int script_id) const { SegmentId SegManager::getScriptSegment(int script_id) const {
return id_seg_map->lookupKey(script_id); return id_seg_map->lookupKey(script_id);
} }
Script *SegManager::getScript(const SegmentId seg) { Script *SegManager::getScript(const SegmentId seg) {
// FIXME: We accept segment 0, but that is actually an invalid segment... if (seg <= 0 || (uint)seg >= _heap.size()) {
if (seg < 0 || (uint)seg >= _heap.size()) {
error("SegManager::getScript(): seg id %x out of bounds", seg); error("SegManager::getScript(): seg id %x out of bounds", seg);
} }
if (!_heap[seg]) { if (!_heap[seg]) {
@ -282,7 +281,7 @@ Script *SegManager::getScript(const SegmentId seg) {
Script *SegManager::getScriptIfLoaded(const SegmentId seg) { Script *SegManager::getScriptIfLoaded(const SegmentId seg) {
// FIXME: We accept segment 0, but that is actually an invalid segment... // FIXME: We accept segment 0, but that is actually an invalid segment...
if (seg < 0 || (uint)seg >= _heap.size() || !_heap[seg] || _heap[seg]->getType() != MEM_OBJ_SCRIPT) if (seg <= 0 || (uint)seg >= _heap.size() || !_heap[seg] || _heap[seg]->getType() != MEM_OBJ_SCRIPT)
return 0; return 0;
return (Script *)_heap[seg]; return (Script *)_heap[seg];
} }
@ -299,8 +298,7 @@ SegmentId SegManager::findSegmentByType(int type) {
// false - invalid seg // false - invalid seg
// true - valid seg // true - valid seg
bool SegManager::check(SegmentId seg) { bool SegManager::check(SegmentId seg) {
// FIXME: We accept segment 0, but that is actually an invalid segment... if (seg <= 0 || (uint)seg >= _heap.size()) {
if (seg < 0 || (uint)seg >= _heap.size()) {
return false; return false;
} }
if (!_heap[seg]) { if (!_heap[seg]) {
@ -443,13 +441,13 @@ void SegManager::heapRelocate(reg_t block) {
} }
} }
SegmentId SegManager::getSegment(int script_nr, SCRIPT_GET load) { SegmentId SegManager::getScriptSegment(int script_nr, ScriptLoadType load) {
SegmentId segment; SegmentId segment;
if ((load & SCRIPT_GET_LOAD) == SCRIPT_GET_LOAD) if ((load & SCRIPT_GET_LOAD) == SCRIPT_GET_LOAD)
script_instantiate(_resMan, this, script_nr); script_instantiate(_resMan, this, script_nr);
segment = segGet(script_nr); segment = getScriptSegment(script_nr);
if (segment > 0) { if (segment > 0) {
if ((load & SCRIPT_GET_LOCK) == SCRIPT_GET_LOCK) if ((load & SCRIPT_GET_LOCK) == SCRIPT_GET_LOCK)
@ -457,19 +455,19 @@ SegmentId SegManager::getSegment(int script_nr, SCRIPT_GET load) {
return segment; return segment;
} else } else
return 0; return -1;
} }
#define INST_LOOKUP_CLASS(id) ((id == 0xffff) ? NULL_REG : get_class_address(id, SCRIPT_GET_LOCK, NULL_REG)) #define INST_LOOKUP_CLASS(id) ((id == 0xffff) ? NULL_REG : getClassAddress(id, SCRIPT_GET_LOCK, NULL_REG))
reg_t SegManager::get_class_address(int classnr, SCRIPT_GET lock, reg_t caller) { reg_t SegManager::getClassAddress(int classnr, ScriptLoadType lock, reg_t caller) {
if (classnr < 0 || (int)_classtable.size() <= classnr || _classtable[classnr].script < 0) { if (classnr < 0 || (int)_classtable.size() <= classnr || _classtable[classnr].script < 0) {
error("[VM] Attempt to dereference class %x, which doesn't exist (max %x)", classnr, _classtable.size()); error("[VM] Attempt to dereference class %x, which doesn't exist (max %x)", classnr, _classtable.size());
return NULL_REG; return NULL_REG;
} else { } else {
Class *the_class = &_classtable[classnr]; Class *the_class = &_classtable[classnr];
if (!the_class->reg.segment) { if (!the_class->reg.segment) {
getSegment(the_class->script, lock); getScriptSegment(the_class->script, lock);
if (!the_class->reg.segment) { if (!the_class->reg.segment) {
error("[VM] Trying to instantiate class %x by instantiating script 0x%x (%03d) failed;" error("[VM] Trying to instantiate class %x by instantiating script 0x%x (%03d) failed;"

View file

@ -44,24 +44,24 @@ namespace Sci {
: NULL): NULL) : NULL): NULL)
/** /**
* Parameters for getSegment() * Parameters for getScriptSegment().
*/ */
typedef enum { enum ScriptLoadType {
SCRIPT_GET_DONT_LOAD = 0, /**< Fail if not loaded */ SCRIPT_GET_DONT_LOAD = 0, /**< Fail if not loaded */
SCRIPT_GET_LOAD = 1, /**< Load, if neccessary */ SCRIPT_GET_LOAD = 1, /**< Load, if neccessary */
SCRIPT_GET_LOCK = 3 /**< Load, if neccessary, and lock */ SCRIPT_GET_LOCK = 3 /**< Load, if neccessary, and lock */
} SCRIPT_GET; };
class SegManager : public Common::Serializable { class SegManager : public Common::Serializable {
public: public:
/** /**
* Initialize the segment manager * Initialize the segment manager.
*/ */
SegManager(ResourceManager *resMan); SegManager(ResourceManager *resMan);
/** /**
* Deallocate all memory associated with the segment manager * Deallocate all memory associated with the segment manager.
*/ */
~SegManager(); ~SegManager();
@ -70,7 +70,7 @@ public:
// 1. Scripts // 1. Scripts
/** /**
* Allocate a script into the segment manager * Allocate a script into the segment manager.
* @param script_nr The number of the script to load * @param script_nr The number of the script to load
* @param seg_id The segment ID of the newly allocated segment, * @param seg_id The segment ID of the newly allocated segment,
* on success * on success
@ -81,7 +81,7 @@ public:
// The script must then be initialised; see section (1b.), below. // The script must then be initialised; see section (1b.), below.
/** /**
* Forcefully deallocate a previously allocated script * Forcefully deallocate a previously allocated script.
* @param script_nr number of the script to deallocate * @param script_nr number of the script to deallocate
* @return 1 on success, 0 on failure * @return 1 on success, 0 on failure
*/ */
@ -95,7 +95,7 @@ public:
/** /**
* Validate whether the specified public function is exported by * Validate whether the specified public function is exported by
* the script in the specified segment * the script in the specified segment.
* @param pubfunct Index of the function to validate * @param pubfunct Index of the function to validate
* @param seg Segment ID of the script the check is to * @param seg Segment ID of the script the check is to
* be performed for * be performed for
@ -105,12 +105,23 @@ public:
uint16 validateExportFunc(int pubfunct, SegmentId seg); uint16 validateExportFunc(int pubfunct, SegmentId seg);
/** /**
* Get the segment ID associated with a script number * Determines the segment occupied by a certain script, if any.
* @param script_nr Number of the script to look up * @param script_nr Number of the script to look up
* @return The associated segment ID, or -1 if no * @return The script's segment ID, or -1 on failure
* matching segment exists
*/ */
SegmentId segGet(int script_nr) const; SegmentId getScriptSegment(int script_nr) const;
/**
* Determines the segment occupied by a certain script. Optionally
* load it, or load & lock it.
* @param[in] script_nr Number of the script to look up
* @param[in] load flag determining whether to load/lock the script
* @return The script's segment ID, or -1 on failure
*/
SegmentId getScriptSegment(int script_nr, ScriptLoadType load);
// TODO: document this
reg_t getClassAddress(int classnr, ScriptLoadType lock, reg_t caller);
/** /**
* Return a pointer to the specified script. * Return a pointer to the specified script.
@ -335,15 +346,6 @@ public:
*/ */
byte *dereference(reg_t reg, int *size); byte *dereference(reg_t reg, int *size);
/**
* Determines the segment occupied by a certain script
* @param[in] script_id The script in question
* @param[in] load One of SCRIPT_GET_*
* @return The script's segment, or 0 on failure
*/
SegmentId getSegment(int script_nr, SCRIPT_GET load);
reg_t get_class_address(int classnr, SCRIPT_GET lock, reg_t caller);
void heapRelocate(reg_t block); void heapRelocate(reg_t block);
void scriptRelocateExportsSci11(SegmentId seg); void scriptRelocateExportsSci11(SegmentId seg);

View file

@ -204,7 +204,7 @@ static void validate_write_var(reg_t *r, reg_t *stack_base, int type, int max, i
#define GET_OP_SIGNED_FLEX() ((opcode & 1)? GET_OP_SIGNED_BYTE() : GET_OP_SIGNED_WORD()) #define GET_OP_SIGNED_FLEX() ((opcode & 1)? GET_OP_SIGNED_BYTE() : GET_OP_SIGNED_WORD())
ExecStack *execute_method(EngineState *s, uint16 script, uint16 pubfunct, StackPtr sp, reg_t calling_obj, uint16 argc, StackPtr argp) { ExecStack *execute_method(EngineState *s, uint16 script, uint16 pubfunct, StackPtr sp, reg_t calling_obj, uint16 argc, StackPtr argp) {
int seg = s->segMan->segGet(script); int seg = s->segMan->getScriptSegment(script);
Script *scr = s->segMan->getScriptIfLoaded(seg); Script *scr = s->segMan->getScriptIfLoaded(seg);
if (!scr) // Script not present yet? if (!scr) // Script not present yet?
@ -212,7 +212,7 @@ ExecStack *execute_method(EngineState *s, uint16 script, uint16 pubfunct, StackP
else else
scr->unmarkDeleted(); scr->unmarkDeleted();
int temp = s->segMan->validateExportFunc(pubfunct, seg); const int temp = s->segMan->validateExportFunc(pubfunct, seg);
if (!temp) { if (!temp) {
error("Request for invalid exported function 0x%x of script 0x%x", pubfunct, script); error("Request for invalid exported function 0x%x of script 0x%x", pubfunct, script);
return NULL; return NULL;
@ -1042,7 +1042,7 @@ void run_vm(EngineState *s, int restoring) {
break; break;
case 0x28: // class case 0x28: // class
s->r_acc = s->segMan->get_class_address((unsigned)opparams[0], SCRIPT_GET_LOCK, s->r_acc = s->segMan->getClassAddress((unsigned)opparams[0], SCRIPT_GET_LOCK,
scriptState.xs->addr.pc); scriptState.xs->addr.pc);
break; break;
@ -1062,7 +1062,7 @@ void run_vm(EngineState *s, int restoring) {
break; break;
case 0x2b: // super case 0x2b: // super
r_temp = s->segMan->get_class_address(opparams[0], SCRIPT_GET_LOAD, scriptState.xs->addr.pc); r_temp = s->segMan->getClassAddress(opparams[0], SCRIPT_GET_LOAD, scriptState.xs->addr.pc);
if (!r_temp.segment) if (!r_temp.segment)
error("[VM]: Invalid superclass in object"); error("[VM]: Invalid superclass in object");
@ -1506,11 +1506,11 @@ SelectorType lookup_selector(SegManager *segMan, reg_t obj_location, Selector se
} }
reg_t script_lookup_export(SegManager *segMan, int script_nr, int export_index) { reg_t script_lookup_export(SegManager *segMan, int script_nr, int export_index) {
SegmentId seg = segMan->getSegment(script_nr, SCRIPT_GET_DONT_LOAD); SegmentId seg = segMan->getScriptSegment(script_nr, SCRIPT_GET_DONT_LOAD);
return make_reg(seg, segMan->validateExportFunc(export_index, seg)); return make_reg(seg, segMan->validateExportFunc(export_index, seg));
} }
#define INST_LOOKUP_CLASS(id) ((id == 0xffff)? NULL_REG : segMan->get_class_address(id, SCRIPT_GET_LOCK, reg)) #define INST_LOOKUP_CLASS(id) ((id == 0xffff)? NULL_REG : segMan->getClassAddress(id, SCRIPT_GET_LOCK, reg))
int script_instantiate_common(ResourceManager *resMan, SegManager *segMan, int script_nr, Resource **script, Resource **heap, int *was_new) { int script_instantiate_common(ResourceManager *resMan, SegManager *segMan, int script_nr, Resource **script, Resource **heap, int *was_new) {
int seg_id; int seg_id;
@ -1533,7 +1533,7 @@ int script_instantiate_common(ResourceManager *resMan, SegManager *segMan, int s
return 0; return 0;
} }
seg_id = segMan->segGet(script_nr); seg_id = segMan->getScriptSegment(script_nr);
Script *scr = segMan->getScriptIfLoaded(seg_id); Script *scr = segMan->getScriptIfLoaded(seg_id);
if (scr) { if (scr) {
if (!scr->isMarkedAsDeleted()) { if (!scr->isMarkedAsDeleted()) {
@ -1821,7 +1821,7 @@ void script_uninstantiate_sci0(SegManager *segMan, int script_nr, SegmentId seg)
} }
void script_uninstantiate(SegManager *segMan, int script_nr) { void script_uninstantiate(SegManager *segMan, int script_nr) {
SegmentId segment = segMan->segGet(script_nr); SegmentId segment = segMan->getScriptSegment(script_nr);
Script *scr = segMan->getScriptIfLoaded(segment); Script *scr = segMan->getScriptIfLoaded(segment);
if (!scr) { // Is it already loaded? if (!scr) { // Is it already loaded?

View file

@ -344,7 +344,7 @@ ExecStack *send_selector(EngineState *s, reg_t send_obj, reg_t work_obj,
StackPtr sp, int framesize, StackPtr argp); StackPtr sp, int framesize, StackPtr argp);
#define SCI_XS_CALLEE_LOCALS -1 #define SCI_XS_CALLEE_LOCALS ((SegmentId)-1)
/** /**
* Adds an entry to the top of the execution stack. * Adds an entry to the top of the execution stack.