[Scummvm-git-logs] scummvm master -> 7a0edc66b5d63128e5f1171708283bcc05a6184e

Strangerke noreply at scummvm.org
Wed Feb 19 23:35:17 UTC 2025


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
7a0edc66b5 M4: Simplify code in f_stream_open, some cleanup


Commit: 7a0edc66b5d63128e5f1171708283bcc05a6184e
    https://github.com/scummvm/scummvm/commit/7a0edc66b5d63128e5f1171708283bcc05a6184e
Author: Strangerke (arnaud.boutonne at gmail.com)
Date: 2025-02-20T00:34:58+01:00

Commit Message:
M4: Simplify code in f_stream_open, some cleanup

Changed paths:
    engines/m4/fileio/fstream.cpp


diff --git a/engines/m4/fileio/fstream.cpp b/engines/m4/fileio/fstream.cpp
index 8771450f342..1173cf8b861 100644
--- a/engines/m4/fileio/fstream.cpp
+++ b/engines/m4/fileio/fstream.cpp
@@ -56,10 +56,8 @@ bool f_stream_Init() {
 }
 
 void f_stream_Shutdown(void) {
-	strmRequest *myStream;
-
 	// Loop through the list, closing all stream requests, which also deallocs the request
-	myStream = _G(firstStream);
+	strmRequest *myStream = _G(firstStream);
 	while (myStream) {
 		_G(firstStream) = _G(firstStream)->next;
 		f_stream_Close(myStream);
@@ -70,10 +68,7 @@ void f_stream_Shutdown(void) {
 strmRequest *f_stream_Open(SysFile *srcFile, int32 fileOffset, int32 strmMinBuffSize, int32 strmBuffSize,
 	int32 numBlocksToRead, int32 *blockSizeArray, int32 initialRead, bool wrapStream) {
 
-	strmRequest *newStream;
-	int32 bytesRead, i, bytesToRead;
-	bool finished;
-	int32 memAvail;
+	int32 bytesToRead;
 
 	// Parameter verification        
 	if (!srcFile) {
@@ -85,61 +80,17 @@ strmRequest *f_stream_Open(SysFile *srcFile, int32 fileOffset, int32 strmMinBuff
 	}
 
 	// Allocate a new stream request struct
-	if ((newStream = (strmRequest *)mem_alloc(sizeof(strmRequest), STR_STRMREQ)) == nullptr) {
+	strmRequest *newStream = (strmRequest *)mem_alloc(sizeof(strmRequest), STR_STRMREQ);
+	if (newStream == nullptr) {
 		error_show(FL, 'OOM!', "%d", sizeof(strmRequest));
-		return nullptr;
 	}
 
-	// Try to get memory
+	// Get memory. If there's not enough memory, a exception will be triggered in NewHandle
 	newStream->strmHandle = NewHandle(strmBuffSize, "stream buff");
-	if (newStream->strmHandle) {
-		goto got_mem;
-	}
-
-	// Maximize available memory
-	MaxMem((Size *)&memAvail);	// param on PC is max mem avail in one block
-
-	// try to get requested size
-	if (memAvail >= strmBuffSize) {
-		/*
-				if ((newStream->strmBuff = (uint8*)mem_alloc(strmBuffSize, STR_STRMBUFF)) != nullptr) {
-					goto got_mem;
-				}
-		*/
-		// try to get memory
-		newStream->strmHandle = NewHandle(strmBuffSize, "stream buff");
-		if (newStream->strmHandle) {
-			goto got_mem;
-		}
-	}
 
-	// try to get what's left if it's enough
-	// get a compromise between free and requested.
-	// if we get it all, system gets unstable...
-	if (memAvail > strmMinBuffSize) {
-		int32 alloc_me = ((memAvail - strmMinBuffSize) / 2) + strmMinBuffSize;
-		/*
-				if ((newStream->strmBuff = (uint8*)mem_alloc(alloc_me, STR_STRMBUFF)) != nullptr) {
-					strmBuffSize = alloc_me;
-					goto got_mem;
-				}
-		*/
-		// try to get memory
-		newStream->strmHandle = NewHandle(alloc_me, "stream buff");
-		if (newStream->strmHandle) {
-			strmBuffSize = alloc_me;
-			goto got_mem;
-		}
-
-	}
-
-	// sorry, bud.
-	error_show(FL, 'FSOM', "want: %d, have: %d", strmMinBuffSize, memAvail);
-
-got_mem:
 	//lock the buffer - to be locked until the stream is closed
 	HLock(newStream->strmHandle);
-	newStream->strmBuff = (uint8 *) * (newStream->strmHandle);
+	newStream->strmBuff = (uint8 *) *(newStream->strmHandle);
 
 	// Initialize the stream request
 	newStream->strmSize = strmBuffSize;
@@ -155,7 +106,6 @@ got_mem:
 
 	// If the streaming should begin part way into the file, seek to the beginning of where to start streaming
 	if (fileOffset > 0) {
-		// If (fseek(newStream->srcFile, fileOffset, SEEK_SET) != 0) {
 		if (!newStream->srcFile->seek(fileOffset)) {
 			HUnLock(newStream->strmHandle);
 			DisposeHandle(newStream->strmHandle);
@@ -171,9 +121,9 @@ got_mem:
 		if (newStream->blockSizeArray) {
 			// Calculate the total number of bytes to read in initially
 			initialRead = (int32)imath_min(initialRead, numBlocksToRead);
-			finished = false;
+			bool finished = false;
 			bytesToRead = 0;
-			i = 0;
+			int32 i = 0;
 			while ((i < initialRead) && (!finished)) {
 				if ((bytesToRead + blockSizeArray[i]) <= strmBuffSize) {
 					bytesToRead += blockSizeArray[i];
@@ -200,7 +150,7 @@ got_mem:
 		if (bytesToRead > 0) {
 
 			// Read in the initial bytes to read
-			bytesRead = newStream->srcFile->read(newStream->strmHead, bytesToRead);
+			int32 bytesRead = newStream->srcFile->read(newStream->strmHead, bytesToRead);
 
 			//did we actually read that many?  If not, close the file
 			if (bytesRead < bytesToRead) {
@@ -229,11 +179,10 @@ got_mem:
 
 
 static bool UnwrapStream(strmRequest *myStream) {
-	int32 bytesToMove = 0, bytesAvail;
-	uint8 *tempBuff;
+	int32 bytesToMove = 0;
 
 	// Using tempBuff as a flag to determine whether data needs to be temporarily stored
-	tempBuff = nullptr;
+	uint8 *tempBuff = nullptr;
 
 	// Since strmTail is never allowed to be equal to strmWrap if it is > strmHead, there must be a
 	// Non-zero amount of data at the end which we must move.
@@ -250,7 +199,7 @@ static bool UnwrapStream(strmRequest *myStream) {
 	}
 
 	// Move the data at the end of the buffer to the beginning and reset the strmWrap pointer
-	bytesAvail = (byte *)myStream->strmWrap - (byte *)myStream->strmTail;
+	int32 bytesAvail = (byte *)myStream->strmWrap - (byte *)myStream->strmTail;
 	memmove(myStream->strmBuff, myStream->strmTail, bytesAvail);
 	myStream->strmTail = myStream->strmBuff;
 	myStream->strmHead = (uint8 *)((byte *)(myStream->strmTail) + bytesAvail);
@@ -281,7 +230,7 @@ void f_stream_DumpPreviouslyRead(strmRequest *myStream) {
 
 
 int32 f_stream_Read(strmRequest *myStream, uint8 **dest, int32 numBytes) {
-	int32   bytesAvail, bytesNeeded, bytesRead;
+	int32 bytesAvail;
 
 	// Parameter verification
 	if (!myStream)
@@ -322,53 +271,53 @@ int32 f_stream_Read(strmRequest *myStream, uint8 **dest, int32 numBytes) {
 		}
 
 		return numBytes;
-	} else {
-		// Else we will have to read more data from disc
-		// If this has happened, since we "unwrapped" the stream buff, we can guarantee that strmTail < strmHead
-
-		// Calculate how much more must be read in
-		bytesNeeded = numBytes - bytesAvail;
+	}
 
-		// Make sure we have enough room at the end of the buffer to accommodate
-		if ((int32)(myStream->endStrmBuff - myStream->strmHead) < bytesNeeded) {
-			// We need to memmove the contents of the stream to the beginning of the buff to allow
-			// F_stream_read() to return a pointer to a contiguous block
+	// Else we will have to read more data from disc
+	// If this has happened, since we "unwrapped" the stream buff, we can guarantee that strmTail < strmHead
 
-			// Move the data to the beginning of the stream buffer, and reset the head and tail pointers
-			memmove((void *)myStream->strmBuff, myStream->strmTail, bytesAvail);
-			myStream->strmTail = myStream->strmBuff;
-			myStream->strmHead = (uint8 *)((byte *)myStream->strmTail + bytesAvail);
-		}
+	// Calculate how much more must be read in
+	int32 bytesNeeded = numBytes - bytesAvail;
 
-		// If the client is using a blockSizeArray, hopefully bytesNeeded will be equal to the next blockSize
-		if (myStream->blockSizeArray && (*myStream->blockSizeArray == bytesNeeded) && (myStream->numBlocksToRead > 0)) {
-			myStream->blockSizeArray++;
-			myStream->numBlocksToRead--;
-		} else {
-			// Otherwise we just trashed the whole point of using a calculated blockSizeArray
-			myStream->blockSizeArray = nullptr;
-			myStream->numBlocksToRead = -1;
-			myStream->nextReadSize = numBytes;
-		}
+	// Make sure we have enough room at the end of the buffer to accommodate
+	if ((int32)(myStream->endStrmBuff - myStream->strmHead) < bytesNeeded) {
+		// We need to memmove the contents of the stream to the beginning of the buff to allow
+		// F_stream_read() to return a pointer to a contiguous block
 
-		// Read in the bytesNeeded
-		bytesRead = myStream->srcFile->read(myStream->strmHead, bytesNeeded);
+		// Move the data to the beginning of the stream buffer, and reset the head and tail pointers
+		memmove((void *)myStream->strmBuff, myStream->strmTail, bytesAvail);
+		myStream->strmTail = myStream->strmBuff;
+		myStream->strmHead = (uint8 *)((byte *)myStream->strmTail + bytesAvail);
+	}
 
-		if (bytesRead < bytesNeeded) {
-			// If we could not read that much in, close the srcFile
-			delete myStream->srcFile;
-			myStream->srcFile = nullptr;
-		}
+	// If the client is using a blockSizeArray, hopefully bytesNeeded will be equal to the next blockSize
+	if (myStream->blockSizeArray && (*myStream->blockSizeArray == bytesNeeded) && (myStream->numBlocksToRead > 0)) {
+		myStream->blockSizeArray++;
+		myStream->numBlocksToRead--;
+	} else {
+		// Otherwise we just trashed the whole point of using a calculated blockSizeArray
+		myStream->blockSizeArray = nullptr;
+		myStream->numBlocksToRead = -1;
+		myStream->nextReadSize = numBytes;
+	}
 
-		// Set the destination pointer and update the stream pointers
-		*dest = (uint8 *)myStream->strmTail;
-		myStream->strmLastRead = myStream->strmTail;
-		myStream->strmHead += bytesRead;
-		myStream->strmTail = myStream->strmHead;
+	// Read in the bytesNeeded
+	int32 bytesRead = myStream->srcFile->read(myStream->strmHead, bytesNeeded);
 
-		// Return the number of bytes successfully available
-		return (bytesRead + bytesAvail);
+	if (bytesRead < bytesNeeded) {
+		// If we could not read that much in, close the srcFile
+		delete myStream->srcFile;
+		myStream->srcFile = nullptr;
 	}
+
+	// Set the destination pointer and update the stream pointers
+	*dest = (uint8 *)myStream->strmTail;
+	myStream->strmLastRead = myStream->strmTail;
+	myStream->strmHead += bytesRead;
+	myStream->strmTail = myStream->strmHead;
+
+	// Return the number of bytes successfully available
+	return (bytesRead + bytesAvail);
 }
 
 void f_stream_Close(strmRequest *myStream) {
@@ -378,9 +327,7 @@ void f_stream_Close(strmRequest *myStream) {
 	}
 
 	// Close the stream and throw out the stream buffer
-	if (myStream->srcFile) {
-		delete myStream->srcFile;
-	}
+	delete myStream->srcFile;
 
 	// Kill the stream buffer
 	HUnLock(myStream->strmHandle);
@@ -405,10 +352,8 @@ void f_stream_Close(strmRequest *myStream) {
 }
 
 void f_stream_Process(int32 numToProcess) {
-	strmRequest *myStream;
 	int32 buffEndBytesAvail = 0, buffStartBytesAvail = 0;
 	int32 bytesRead, bytesAvail, nextReadSize;
-	bool buffWrap, useBlockSizeArray;
 
 	// No sense wasting time if there are no stream requests to process
 	if (!_G(firstStream)) {
@@ -416,13 +361,13 @@ void f_stream_Process(int32 numToProcess) {
 	}
 
 	// Loop through until either the end of the list of requests, or we've serviced the "numToProcess"
-	myStream = _G(firstStream);
+	strmRequest *myStream = _G(firstStream);
 	while (myStream && (numToProcess > 0)) {
 
 		// Make sure we still have an open srcFile
 		if (myStream->srcFile && (myStream->numBlocksToRead != 0)) {
-			buffWrap = false;
-			useBlockSizeArray = false;
+			bool buffWrap = false;
+			bool useBlockSizeArray = false;
 
 			// Calculate the amount of empty space in the stream buff
 			// If all the empty space in the stream buff is between the head and the lastRead...




More information about the Scummvm-git-logs mailing list