aboutsummaryrefslogtreecommitdiff
path: root/engines/sci/engine
diff options
context:
space:
mode:
Diffstat (limited to 'engines/sci/engine')
-rw-r--r--engines/sci/engine/game.cpp4
-rw-r--r--engines/sci/engine/savegame.cpp8
-rw-r--r--engines/sci/engine/seg_manager.cpp30
-rw-r--r--engines/sci/engine/seg_manager.h2
-rw-r--r--engines/sci/engine/segment.cpp16
-rw-r--r--engines/sci/engine/segment.h33
6 files changed, 51 insertions, 42 deletions
diff --git a/engines/sci/engine/game.cpp b/engines/sci/engine/game.cpp
index 218b6bad4d..1c94b540a0 100644
--- a/engines/sci/engine/game.cpp
+++ b/engines/sci/engine/game.cpp
@@ -417,8 +417,8 @@ int game_init(EngineState *s) {
DataStack *stack;
stack = s->segMan->allocateStack(VM_STACK_SIZE, &s->stack_segment);
- s->stack_base = stack->entries;
- s->stack_top = s->stack_base + VM_STACK_SIZE;
+ s->stack_base = stack->_entries;
+ s->stack_top = stack->_entries + stack->_capacity;
if (!script_instantiate(s->resMan, s->segMan, 0)) {
warning("game_init(): Could not instantiate script 0");
diff --git a/engines/sci/engine/savegame.cpp b/engines/sci/engine/savegame.cpp
index 0c4cdaf9a9..03f249c20a 100644
--- a/engines/sci/engine/savegame.cpp
+++ b/engines/sci/engine/savegame.cpp
@@ -461,10 +461,10 @@ void DynMem::saveLoadWithSerializer(Common::Serializer &s) {
}
void DataStack::saveLoadWithSerializer(Common::Serializer &s) {
- s.syncAsUint32LE(nr);
+ s.syncAsUint32LE(_capacity);
if (s.isLoading()) {
//free(entries);
- entries = (reg_t *)calloc(nr, sizeof(reg_t));
+ _entries = (reg_t *)calloc(_capacity, sizeof(reg_t));
}
}
@@ -561,8 +561,8 @@ static void reconstruct_stack(EngineState *retval) {
DataStack *stack = (DataStack *)(retval->segMan->_heap[stack_seg]);
retval->stack_segment = stack_seg;
- retval->stack_base = stack->entries;
- retval->stack_top = retval->stack_base + VM_STACK_SIZE;
+ retval->stack_base = stack->_entries;
+ retval->stack_top = stack->_entries + stack->_capacity;
}
static void load_script(EngineState *s, Script *scr) {
diff --git a/engines/sci/engine/seg_manager.cpp b/engines/sci/engine/seg_manager.cpp
index a1c837512b..6449272bdc 100644
--- a/engines/sci/engine/seg_manager.cpp
+++ b/engines/sci/engine/seg_manager.cpp
@@ -93,12 +93,10 @@ SegmentId SegManager::findFreeSegment() const {
return seg;
}
-SegmentObj *SegManager::allocSegment(SegmentType type, SegmentId *segid) {
+SegmentObj *SegManager::allocSegment(SegmentObj *mem, SegmentId *segid) {
// Find a free segment
*segid = findFreeSegment();
- // Now allocate an object of the appropriate type...
- SegmentObj *mem = SegmentObj::createSegmentObj(type);
if (!mem)
error("SegManager: invalid mobj");
@@ -114,14 +112,14 @@ SegmentObj *SegManager::allocSegment(SegmentType type, SegmentId *segid) {
Script *SegManager::allocateScript(int script_nr, SegmentId *seg_id) {
// Check if the script already has an allocated segment. If it
- // does have one, return it.
+ // does, return that segment.
*seg_id = _scriptSegMap.getVal(script_nr, 0);
if (*seg_id > 0) {
return (Script *)_heap[*seg_id];
}
// allocate the SegmentObj
- SegmentObj *mem = allocSegment(SEG_TYPE_SCRIPT, seg_id);
+ SegmentObj *mem = allocSegment(new Script(), seg_id);
// Add the script to the "script id -> segment id" hashmap
_scriptSegMap[script_nr] = *seg_id;
@@ -572,7 +570,7 @@ LocalVariables *SegManager::allocLocalsSegment(Script *scr, int count) {
VERIFY(locals->getType() == SEG_TYPE_LOCALS, "Re-used locals segment did not consist of local variables");
VERIFY(locals->script_id == scr->_nr, "Re-used locals segment belonged to other script");
} else
- locals = (LocalVariables *)allocSegment(SEG_TYPE_LOCALS, &scr->_localsSegment);
+ locals = (LocalVariables *)allocSegment(new LocalVariables(), &scr->_localsSegment);
scr->_localsBlock = locals;
locals->script_id = scr->_nr;
@@ -704,23 +702,23 @@ static char *SegManager::dynprintf(char *msg, ...) {
*/
DataStack *SegManager::allocateStack(int size, SegmentId *segid) {
- SegmentObj *mobj = allocSegment(SEG_TYPE_STACK, segid);
+ SegmentObj *mobj = allocSegment(new DataStack(), segid);
DataStack *retval = (DataStack *)mobj;
- retval->entries = (reg_t *)calloc(size, sizeof(reg_t));
- retval->nr = size;
+ retval->_entries = (reg_t *)calloc(size, sizeof(reg_t));
+ retval->_capacity = size;
return retval;
}
SystemStrings *SegManager::allocateSysStrings(SegmentId *segid) {
- return (SystemStrings *)allocSegment(SEG_TYPE_SYS_STRINGS, segid);
+ return (SystemStrings *)allocSegment(new SystemStrings(), segid);
}
SegmentId SegManager::allocateStringFrags() {
SegmentId segid;
- allocSegment(SEG_TYPE_STRING_FRAG, &segid);
+ allocSegment(new StringFrag(), &segid);
return segid;
}
@@ -769,7 +767,7 @@ Clone *SegManager::allocateClone(reg_t *addr) {
int offset;
if (!Clones_seg_id) {
- table = (CloneTable *)allocSegment(SEG_TYPE_CLONES, &(Clones_seg_id));
+ table = (CloneTable *)allocSegment(new CloneTable(), &(Clones_seg_id));
} else
table = (CloneTable *)_heap[Clones_seg_id];
@@ -828,7 +826,7 @@ List *SegManager::allocateList(reg_t *addr) {
int offset;
if (!Lists_seg_id)
- allocSegment(SEG_TYPE_LISTS, &(Lists_seg_id));
+ allocSegment(new ListTable(), &(Lists_seg_id));
table = (ListTable *)_heap[Lists_seg_id];
offset = table->allocEntry();
@@ -842,7 +840,7 @@ Node *SegManager::allocateNode(reg_t *addr) {
int offset;
if (!Nodes_seg_id)
- allocSegment(SEG_TYPE_NODES, &(Nodes_seg_id));
+ allocSegment(new NodeTable(), &(Nodes_seg_id));
table = (NodeTable *)_heap[Nodes_seg_id];
offset = table->allocEntry();
@@ -856,7 +854,7 @@ Hunk *SegManager::alloc_Hunk(reg_t *addr) {
int offset;
if (!Hunks_seg_id)
- allocSegment(SEG_TYPE_HUNK, &(Hunks_seg_id));
+ allocSegment(new HunkTable(), &(Hunks_seg_id));
table = (HunkTable *)_heap[Hunks_seg_id];
offset = table->allocEntry();
@@ -912,7 +910,7 @@ char *SegManager::derefString(reg_t pointer, int entries) {
byte *SegManager::allocDynmem(int size, const char *descr, reg_t *addr) {
SegmentId seg;
- SegmentObj *mobj = allocSegment(SEG_TYPE_DYNMEM, &seg);
+ SegmentObj *mobj = allocSegment(new DynMem(), &seg);
*addr = make_reg(seg, 0);
DynMem &d = *(DynMem *)mobj;
diff --git a/engines/sci/engine/seg_manager.h b/engines/sci/engine/seg_manager.h
index 6fec14a3dd..8c6676d0ec 100644
--- a/engines/sci/engine/seg_manager.h
+++ b/engines/sci/engine/seg_manager.h
@@ -373,7 +373,7 @@ private:
SegmentId Hunks_seg_id; ///< ID of the (a) hunk segment
private:
- SegmentObj *allocSegment(SegmentType type, SegmentId *segid);
+ SegmentObj *allocSegment(SegmentObj *mem, SegmentId *segid);
LocalVariables *allocLocalsSegment(Script *scr, int count);
int deallocate(SegmentId seg, bool recursive);
int createClassTable();
diff --git a/engines/sci/engine/segment.cpp b/engines/sci/engine/segment.cpp
index dddf8169d8..036d0f6979 100644
--- a/engines/sci/engine/segment.cpp
+++ b/engines/sci/engine/segment.cpp
@@ -76,11 +76,11 @@ SegmentObj *SegmentObj::createSegmentObj(SegmentType type) {
}
assert(mem);
- mem->_type = type;
+ assert(mem->_type == type);
return mem;
}
-Script::Script() {
+Script::Script() : SegmentObj(SEG_TYPE_SCRIPT) {
_nr = 0;
_buf = NULL;
_bufSize = 0;
@@ -251,14 +251,14 @@ byte *LocalVariables::dereference(reg_t pointer, int *size) {
}
bool DataStack::isValidOffset(uint16 offset) const {
- return offset < nr * sizeof(reg_t);
+ return offset < _capacity * sizeof(reg_t);
}
byte *DataStack::dereference(reg_t pointer, int *size) {
if (size)
- *size = nr * sizeof(reg_t);
+ *size = _capacity * sizeof(reg_t);
- byte *base = (byte *)entries;
+ byte *base = (byte *)_entries;
return base + pointer.offset;
}
@@ -401,9 +401,9 @@ reg_t DataStack::findCanonicAddress(SegManager *segMan, reg_t addr) {
}
void DataStack::listAllOutgoingReferences(reg_t addr, void *param, NoteCallback note) {
- fprintf(stderr, "Emitting %d stack entries\n", nr);
- for (int i = 0; i < nr; i++)
- (*note)(param, entries[i]);
+ fprintf(stderr, "Emitting %d stack entries\n", _capacity);
+ for (int i = 0; i < _capacity; i++)
+ (*note)(param, _entries[i]);
fprintf(stderr, "DONE");
}
diff --git a/engines/sci/engine/segment.h b/engines/sci/engine/segment.h
index 450140b443..9e6e27dd85 100644
--- a/engines/sci/engine/segment.h
+++ b/engines/sci/engine/segment.h
@@ -59,6 +59,7 @@ public:
static SegmentObj *createSegmentObj(SegmentType type);
public:
+ SegmentObj(SegmentType type) : _type(type) {}
virtual ~SegmentObj() {}
inline SegmentType getType() const { return _type; }
@@ -113,6 +114,8 @@ public:
// TODO: Implement the following class
struct StringFrag : public SegmentObj {
+ StringFrag() : SegmentObj(SEG_TYPE_STRING_FRAG) {}
+
virtual bool isValidOffset(uint16 offset) const { return false; }
virtual void saveLoadWithSerializer(Common::Serializer &ser);
@@ -139,7 +142,7 @@ struct SystemStrings : public SegmentObj {
SystemString strings[SYS_STRINGS_MAX];
public:
- SystemStrings() {
+ SystemStrings() : SegmentObj(SEG_TYPE_SYS_STRINGS) {
memset(strings, 0, sizeof(strings));
}
~SystemStrings() {
@@ -168,7 +171,7 @@ struct LocalVariables : public SegmentObj {
Common::Array<reg_t> _locals;
public:
- LocalVariables() {
+ LocalVariables(): SegmentObj(SEG_TYPE_LOCALS) {
script_id = 0;
}
@@ -450,17 +453,17 @@ private:
/** Data stack */
struct DataStack : SegmentObj {
- int nr; /**< Number of stack entries */
- reg_t *entries;
+ int _capacity; /**< Number of stack entries */
+ reg_t *_entries;
public:
- DataStack() {
- nr = 0;
- entries = NULL;
+ DataStack() : SegmentObj(SEG_TYPE_STACK) {
+ _capacity = 0;
+ _entries = NULL;
}
~DataStack() {
- free(entries);
- entries = NULL;
+ free(_entries);
+ _entries = NULL;
}
virtual bool isValidOffset(uint16 offset) const;
@@ -509,7 +512,7 @@ struct Table : public SegmentObj {
Common::Array<Entry> _table;
public:
- Table() {
+ Table(SegmentType type) : SegmentObj(type) {
initTable();
}
@@ -562,6 +565,8 @@ public:
/* CloneTable */
struct CloneTable : public Table<Clone> {
+ CloneTable() : Table<Clone>(SEG_TYPE_CLONES) {}
+
virtual void freeAtAddress(SegManager *segMan, reg_t sub_addr);
virtual void listAllOutgoingReferences(reg_t object, void *param, NoteCallback note);
@@ -571,6 +576,8 @@ struct CloneTable : public Table<Clone> {
/* NodeTable */
struct NodeTable : public Table<Node> {
+ NodeTable() : Table<Node>(SEG_TYPE_NODES) {}
+
virtual void freeAtAddress(SegManager *segMan, reg_t sub_addr);
virtual void listAllOutgoingReferences(reg_t object, void *param, NoteCallback note);
@@ -580,6 +587,8 @@ struct NodeTable : public Table<Node> {
/* ListTable */
struct ListTable : public Table<List> {
+ ListTable() : Table<List>(SEG_TYPE_LISTS) {}
+
virtual void freeAtAddress(SegManager *segMan, reg_t sub_addr);
virtual void listAllOutgoingReferences(reg_t object, void *param, NoteCallback note);
@@ -589,6 +598,8 @@ struct ListTable : public Table<List> {
/* HunkTable */
struct HunkTable : public Table<Hunk> {
+ HunkTable() : Table<Hunk>(SEG_TYPE_HUNK) {}
+
virtual void freeEntry(int idx) {
Table<Hunk>::freeEntry(idx);
@@ -606,7 +617,7 @@ struct DynMem : public SegmentObj {
byte *_buf;
public:
- DynMem() : _size(0), _buf(0) {}
+ DynMem() : SegmentObj(SEG_TYPE_DYNMEM), _size(0), _buf(0) {}
~DynMem() {
free(_buf);
_buf = NULL;