[Scummvm-cvs-logs] SF.net SVN: scummvm:[39944] scummvm/trunk/engines/saga

fingolfin at users.sourceforge.net fingolfin at users.sourceforge.net
Sat Apr 11 23:38:41 CEST 2009


Revision: 39944
          http://scummvm.svn.sourceforge.net/scummvm/?rev=39944&view=rev
Author:   fingolfin
Date:     2009-04-11 21:38:41 +0000 (Sat, 11 Apr 2009)

Log Message:
-----------
SAGA: changed _threadList back to a list of ScriptThread objs, instead of ptrs to instances.

Modified Paths:
--------------
    scummvm/trunk/engines/saga/events.cpp
    scummvm/trunk/engines/saga/script.cpp
    scummvm/trunk/engines/saga/script.h
    scummvm/trunk/engines/saga/sfuncs.cpp
    scummvm/trunk/engines/saga/sthread.cpp

Modified: scummvm/trunk/engines/saga/events.cpp
===================================================================
--- scummvm/trunk/engines/saga/events.cpp	2009-04-11 21:38:14 UTC (rev 39943)
+++ scummvm/trunk/engines/saga/events.cpp	2009-04-11 21:38:41 UTC (rev 39944)
@@ -273,7 +273,6 @@
 }
 
 int Events::handleOneShot(Event *event) {
-	ScriptThread *sthread;
 	Rect rect;
 
 
@@ -478,24 +477,20 @@
 	case kScriptEvent:
 		switch (event->op) {
 		case kEventExecBlocking:
-		case kEventExecNonBlocking:
+		case kEventExecNonBlocking: {
 			debug(6, "Exec module number %ld script entry number %ld", event->param, event->param2);
 
-			sthread = _vm->_script->createThread(event->param, event->param2);
-			if (sthread == NULL) {
-				_vm->_console->DebugPrintf("Thread creation failed.\n");
-				break;
-			}
+			ScriptThread &sthread = _vm->_script->createThread(event->param, event->param2);
+			sthread._threadVars[kThreadVarAction] = event->param3;
+			sthread._threadVars[kThreadVarObject] = event->param4;
+			sthread._threadVars[kThreadVarWithObject] = event->param5;
+			sthread._threadVars[kThreadVarActor] = event->param6;
 
-			sthread->_threadVars[kThreadVarAction] = event->param3;
-			sthread->_threadVars[kThreadVarObject] = event->param4;
-			sthread->_threadVars[kThreadVarWithObject] = event->param5;
-			sthread->_threadVars[kThreadVarActor] = event->param6;
-
 			if (event->op == kEventExecBlocking)
 				_vm->_script->completeThread();
 
 			break;
+			}
 		case kEventThreadWake:
 			_vm->_script->wakeUpThreads(event->param);
 			break;

Modified: scummvm/trunk/engines/saga/script.cpp
===================================================================
--- scummvm/trunk/engines/saga/script.cpp	2009-04-11 21:38:14 UTC (rev 39943)
+++ scummvm/trunk/engines/saga/script.cpp	2009-04-11 21:38:41 UTC (rev 39944)
@@ -220,11 +220,7 @@
 
 // Shut down script module gracefully; free all allocated module resources
 Script::~Script() {
-	ScriptThreadList::iterator threadIterator = _threadList.begin();
-	while (threadIterator != _threadList.end()) {
-		delete *threadIterator;
-		threadIterator = _threadList.erase(threadIterator);
-	}
+
 }
 
 // Script opcodes

Modified: scummvm/trunk/engines/saga/script.h
===================================================================
--- scummvm/trunk/engines/saga/script.h	2009-04-11 21:38:14 UTC (rev 39943)
+++ scummvm/trunk/engines/saga/script.h	2009-04-11 21:38:41 UTC (rev 39944)
@@ -42,7 +42,6 @@
 
 #define ITE_SCRIPT_FUNCTION_MAX 78
 #define IHNM_SCRIPT_FUNCTION_MAX 105
-#define DEFAULT_THREAD_STACK_SIZE 256
 
 enum AddressTypes {
 	kAddressCommon     = 0,	// offset from global variables
@@ -169,8 +168,7 @@
 
 class ScriptThread {
 public:
-	uint16 *_stackBuf;
-	uint16 _stackSize;					// stack size in uint16
+	int16 *_stackBuf;
 
 	uint16 _stackTopIndex;
 	uint16 _frameIndex;
@@ -196,6 +194,10 @@
 
 	int32 _frameWait;
 
+	enum {
+		THREAD_STACK_SIZE = 256
+	};
+
 public:
 	byte *baseAddress(byte addrMode) {
 		switch (addrMode) {
@@ -215,25 +217,25 @@
 	}
 
 	int16 stackTop() {
-		return (int16)_stackBuf[_stackTopIndex];
+		return _stackBuf[_stackTopIndex];
 	}
 
 	uint pushedSize() {
-		return _stackSize - _stackTopIndex - 2;
+		return THREAD_STACK_SIZE - _stackTopIndex - 2;
 	}
 
 	void push(int16 value) {
 		if (_stackTopIndex <= 0) {
 			error("ScriptThread::push() stack overflow");
 		}
-		_stackBuf[--_stackTopIndex] = (uint16)value;
+		_stackBuf[--_stackTopIndex] = value;
 	}
 
 	int16 pop() {
-		if (_stackTopIndex >= _stackSize) {
+		if (_stackTopIndex >= THREAD_STACK_SIZE) {
 			error("ScriptThread::pop() stack underflow");
 		}
-		return (int16)_stackBuf[_stackTopIndex++];
+		return _stackBuf[_stackTopIndex++];
 	}
 
 
@@ -260,14 +262,42 @@
 
 	ScriptThread() {
 		memset(this, 0xFE, sizeof(*this));
-		_stackBuf = NULL;
+		_flags = kTFlagNone;
+		_stackBuf = 0;
 	}
+
+	// copy constructor
+	ScriptThread(const ScriptThread& s) {
+		memcpy(this, &s, sizeof(*this));
+
+		// Verify that s doesn't have a non-zero _stackBuf, for else
+		// we would have to clone  that buffer, too, which we currently
+		// don't do. This case should never occur anyway, though (at
+		// least as long as the thread handling code does not change).
+		assert(!_stackBuf);
+	}
+
+	// assignment operator
+	ScriptThread& operator=(const ScriptThread &s) {
+		if (this == &s)
+			return *this;
+
+		free(_stackBuf);
+		memcpy(this, &s, sizeof(*this));
+
+		// Verify that s doesn't have a non-zero _stackBuf, for else
+		// we would have to clone  that buffer, too, which we currently
+		// don't do. This case should never occur anyway, though (at
+		// least as long as the thread handling code does not change).
+		assert(!_stackBuf);
+	}
+
 	~ScriptThread() {
 		free(_stackBuf);
 	}
 };
 
-typedef Common::List<ScriptThread*> ScriptThreadList;
+typedef Common::List<ScriptThread> ScriptThreadList;
 
 #define SCRIPTOP_PARAMS ScriptThread *thread, MemoryReadStream *scriptS, bool &stopParsing, bool &breakOut
 #define SCRIPTFUNC_PARAMS ScriptThread *thread, int nArgs, bool &disContinue
@@ -381,7 +411,7 @@
 	VoiceLUT _globalVoiceLUT;
 
 public:
-	ScriptThread *createThread(uint16 scriptModuleNumber, uint16 scriptEntryPointNumber);
+	ScriptThread &createThread(uint16 scriptModuleNumber, uint16 scriptEntryPointNumber);
 	int executeThread(ScriptThread *thread, int entrypointNumber);
 	void executeThreads(uint msec);
 	void completeThread(void);
@@ -397,7 +427,7 @@
 	void loadModuleBase(ModuleData &module, const byte *resourcePointer, size_t resourceLength);
 
 	// runThread returns true if we should break running of other threads
-	bool runThread(ScriptThread *thread);
+	bool runThread(ScriptThread &thread);
 	void setThreadEntrypoint(ScriptThread *thread, int entrypointNumber);
 
 public:

Modified: scummvm/trunk/engines/saga/sfuncs.cpp
===================================================================
--- scummvm/trunk/engines/saga/sfuncs.cpp	2009-04-11 21:38:14 UTC (rev 39943)
+++ scummvm/trunk/engines/saga/sfuncs.cpp	2009-04-11 21:38:41 UTC (rev 39944)
@@ -394,15 +394,14 @@
 
 // Script function #13 (0x0D)
 void Script::sfKillActorThreads(SCRIPTFUNC_PARAMS) {
-	ScriptThread *anotherThread;
 	ScriptThreadList::iterator threadIterator;
 	int16 actorId = thread->pop();
 
 	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
-		anotherThread = *threadIterator;
-		if ((anotherThread != thread) && (anotherThread->_threadVars[kThreadVarActor] == actorId)) {
-			anotherThread->_flags &= ~kTFlagWaiting;
-			anotherThread->_flags |= kTFlagAborted;
+		ScriptThread &anotherThread = *threadIterator;
+		if ((&anotherThread != thread) && (anotherThread._threadVars[kThreadVarActor] == actorId)) {
+			anotherThread._flags &= ~kTFlagWaiting;
+			anotherThread._flags |= kTFlagAborted;
 		}
 	}
 }

Modified: scummvm/trunk/engines/saga/sthread.cpp
===================================================================
--- scummvm/trunk/engines/saga/sthread.cpp	2009-04-11 21:38:14 UTC (rev 39943)
+++ scummvm/trunk/engines/saga/sthread.cpp	2009-04-11 21:38:41 UTC (rev 39944)
@@ -37,42 +37,40 @@
 
 namespace Saga {
 
-ScriptThread *Script::createThread(uint16 scriptModuleNumber, uint16 scriptEntryPointNumber) {
-	ScriptThread *newThread = new ScriptThread();
-
+ScriptThread &Script::createThread(uint16 scriptModuleNumber, uint16 scriptEntryPointNumber) {
 	loadModule(scriptModuleNumber);
 	if (_modules[scriptModuleNumber].entryPointsCount <= scriptEntryPointNumber) {
 		error("Script::createThread wrong scriptEntryPointNumber");
 	}
 
-	newThread->_flags = kTFlagNone;
-	newThread->_stackSize = DEFAULT_THREAD_STACK_SIZE;
-	newThread->_stackBuf = (uint16 *)malloc(newThread->_stackSize * sizeof(uint16));
-	newThread->_stackTopIndex = newThread->_stackSize - 2;
-	newThread->_instructionOffset = _modules[scriptModuleNumber].entryPoints[scriptEntryPointNumber].offset;
-	newThread->_commonBase = _commonBuffer;
-	newThread->_staticBase = _commonBuffer + _modules[scriptModuleNumber].staticOffset;
-	newThread->_moduleBase = _modules[scriptModuleNumber].moduleBase;
-	newThread->_moduleBaseSize = _modules[scriptModuleNumber].moduleBaseSize;
-	newThread->_strings = &_modules[scriptModuleNumber].strings;
+	ScriptThread newThread;
+	newThread._instructionOffset = _modules[scriptModuleNumber].entryPoints[scriptEntryPointNumber].offset;
+	newThread._commonBase = _commonBuffer;
+	newThread._staticBase = _commonBuffer + _modules[scriptModuleNumber].staticOffset;
+	newThread._moduleBase = _modules[scriptModuleNumber].moduleBase;
+	newThread._moduleBaseSize = _modules[scriptModuleNumber].moduleBaseSize;
+	newThread._strings = &_modules[scriptModuleNumber].strings;
 
 	if (_vm->getGameId() == GID_IHNM)
-		newThread->_voiceLUT = &_globalVoiceLUT;
+		newThread._voiceLUT = &_globalVoiceLUT;
 	else
-		newThread->_voiceLUT = &_modules[scriptModuleNumber].voiceLUT;
+		newThread._voiceLUT = &_modules[scriptModuleNumber].voiceLUT;
 
 	_threadList.push_front(newThread);
 
-	return newThread;
+	ScriptThread &tmp = *_threadList.begin();
+	tmp._stackBuf = (int16 *)malloc(ScriptThread::THREAD_STACK_SIZE * sizeof(int16));
+	tmp._stackTopIndex = ScriptThread::THREAD_STACK_SIZE - 2;
+	return tmp;
 }
 
 void Script::wakeUpActorThread(int waitType, void *threadObj) {
 	ScriptThreadList::iterator threadIterator;
 
 	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
-		ScriptThread *thread = *threadIterator;
-		if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType) && (thread->_threadObj == threadObj)) {
-			thread->_flags &= ~kTFlagWaiting;
+		ScriptThread &thread = *threadIterator;
+		if ((thread._flags & kTFlagWaiting) && (thread._waitType == waitType) && (thread._threadObj == threadObj)) {
+			thread._flags &= ~kTFlagWaiting;
 		}
 	}
 }
@@ -81,9 +79,9 @@
 	ScriptThreadList::iterator threadIterator;
 
 	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
-		ScriptThread *thread = *threadIterator;
-		if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType)) {
-			thread->_flags &= ~kTFlagWaiting;
+		ScriptThread &thread = *threadIterator;
+		if ((thread._flags & kTFlagWaiting) && (thread._waitType == waitType)) {
+			thread._flags &= ~kTFlagWaiting;
 		}
 	}
 }
@@ -92,10 +90,10 @@
 	ScriptThreadList::iterator threadIterator;
 
 	for (threadIterator = _threadList.begin(); threadIterator != _threadList.end(); ++threadIterator) {
-		ScriptThread *thread = *threadIterator;
-		if ((thread->_flags & kTFlagWaiting) && (thread->_waitType == waitType)) {
-			thread->_waitType = kWaitTypeDelay;
-			thread->_sleepTime = sleepTime;
+		ScriptThread &thread = *threadIterator;
+		if ((thread._flags & kTFlagWaiting) && (thread._waitType == waitType)) {
+			thread._waitType = kWaitTypeDelay;
+			thread._sleepTime = sleepTime;
 		}
 	}
 }
@@ -110,55 +108,54 @@
 	threadIterator = _threadList.begin();
 
 	while (threadIterator != _threadList.end()) {
-		ScriptThread *thread = *threadIterator;
+		ScriptThread &thread = *threadIterator;
 
-		if (thread->_flags & (kTFlagFinished | kTFlagAborted)) {
-			if (thread->_flags & kTFlagFinished)
+		if (thread._flags & (kTFlagFinished | kTFlagAborted)) {
+			if (thread._flags & kTFlagFinished)
 				setPointerVerb();
 
 			if (_vm->getGameId() == GID_IHNM) {
-				thread->_flags &= ~kTFlagFinished;
-				thread->_flags |= kTFlagAborted;
+				thread._flags &= ~kTFlagFinished;
+				thread._flags |= kTFlagAborted;
 				++threadIterator;
 			} else {
-				delete *threadIterator;
 				threadIterator = _threadList.erase(threadIterator);
 			}
 			continue;
 		}
 
-		if (thread->_flags & kTFlagWaiting) {
+		if (thread._flags & kTFlagWaiting) {
 
-			switch (thread->_waitType) {
+			switch (thread._waitType) {
 			case kWaitTypeDelay:
-				if (thread->_sleepTime < msec) {
-					thread->_sleepTime = 0;
+				if (thread._sleepTime < msec) {
+					thread._sleepTime = 0;
 				} else {
-					thread->_sleepTime -= msec;
+					thread._sleepTime -= msec;
 				}
 
-				if (thread->_sleepTime == 0)
-					thread->_flags &= ~kTFlagWaiting;
+				if (thread._sleepTime == 0)
+					thread._flags &= ~kTFlagWaiting;
 				break;
 
 			case kWaitTypeWalk:
 				{
 					ActorData *actor;
-					actor = (ActorData *)thread->_threadObj;
+					actor = (ActorData *)thread._threadObj;
 					if (actor->_currentAction == kActionWait) {
-						thread->_flags &= ~kTFlagWaiting;
+						thread._flags &= ~kTFlagWaiting;
 					}
 				}
 				break;
 
 			case kWaitTypeWaitFrames: // IHNM
-				if (thread->_frameWait < _vm->_frameCount)
-					thread->_flags &= ~kTFlagWaiting;
+				if (thread._frameWait < _vm->_frameCount)
+					thread._flags &= ~kTFlagWaiting;
 				break;
 			}
 		}
 
-		if (!(thread->_flags & kTFlagWaiting)) {
+		if (!(thread._flags & kTFlagWaiting)) {
 			if (runThread(thread)) {
 				break;
 			}
@@ -175,8 +172,8 @@
 	threadIterator = _threadList.begin();
 
 	while (threadIterator != _threadList.end()) {
-		ScriptThread *thread = *threadIterator;
-		thread->_flags |= kTFlagAborted;
+		ScriptThread &thread = *threadIterator;
+		thread._flags |= kTFlagAborted;
 		++threadIterator;
 	}
 	executeThreads(0);
@@ -189,44 +186,44 @@
 		executeThreads(0);
 }
 
-bool Script::runThread(ScriptThread *thread) {
+bool Script::runThread(ScriptThread &thread) {
 	uint16 savedInstructionOffset;
 	bool stopParsing = false;
 	bool breakOut = false;
 	int operandChar;
 
-	MemoryReadStream scriptS(thread->_moduleBase, thread->_moduleBaseSize);
+	MemoryReadStream scriptS(thread._moduleBase, thread._moduleBaseSize);
 
-	scriptS.seek(thread->_instructionOffset);
+	scriptS.seek(thread._instructionOffset);
 
 	for (uint instructionCount = 0; instructionCount < STHREAD_TIMESLICE; instructionCount++) {
-		if (thread->_flags & (kTFlagAsleep))
+		if (thread._flags & (kTFlagAsleep))
 			break;
 
-		savedInstructionOffset = thread->_instructionOffset;
+		savedInstructionOffset = thread._instructionOffset;
 		operandChar = scriptS.readByte();
 
-		debug(8, "Executing thread offset: %u (%x) stack: %d", thread->_instructionOffset, operandChar, thread->pushedSize());
+		debug(8, "Executing thread offset: %u (%x) stack: %d", thread._instructionOffset, operandChar, thread.pushedSize());
 
 		stopParsing = false;
 		debug(4, "Calling op %s", this->_scriptOpsList[operandChar].scriptOpName);
-		(this->*_scriptOpsList[operandChar].scriptOp)(thread, &scriptS, stopParsing, breakOut);
+		(this->*_scriptOpsList[operandChar].scriptOp)(&thread, &scriptS, stopParsing, breakOut);
 		if (stopParsing)
 			return breakOut;
 
-		if (thread->_flags & (kTFlagFinished | kTFlagAborted)) {
-			error("Wrong flags %d in thread", thread->_flags);
+		if (thread._flags & (kTFlagFinished | kTFlagAborted)) {
+			error("Wrong flags %d in thread", thread._flags);
 		}
 
 		// Set instruction offset only if a previous instruction didn't branch
-		if (savedInstructionOffset == thread->_instructionOffset) {
-			thread->_instructionOffset = scriptS.pos();
+		if (savedInstructionOffset == thread._instructionOffset) {
+			thread._instructionOffset = scriptS.pos();
 		} else {
-			if (thread->_instructionOffset >= scriptS.size()) {
+			if (thread._instructionOffset >= scriptS.size()) {
 				error("Script::runThread() Out of range script execution");
 			}
 
-			scriptS.seek(thread->_instructionOffset);
+			scriptS.seek(thread._instructionOffset);
 		}
 
 		if (breakOut)


This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.




More information about the Scummvm-git-logs mailing list