From a503f9223a4b87320f30f7264994a55a265a4f78 Mon Sep 17 00:00:00 2001 From: Yotam Barnoy Date: Sun, 22 Aug 2010 10:48:13 +0000 Subject: PSP: switched from stdio to psp functions -- dramatic speed improvement Turns out that stdio is about 30x(!) slower than using the PSP's functions. Very significant optimization. svn-id: r52271 --- backends/fs/psp/psp-stream.cpp | 69 ++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 46 deletions(-) (limited to 'backends/fs/psp/psp-stream.cpp') diff --git a/backends/fs/psp/psp-stream.cpp b/backends/fs/psp/psp-stream.cpp index 67c73beeaa..8843bfd2fb 100644 --- a/backends/fs/psp/psp-stream.cpp +++ b/backends/fs/psp/psp-stream.cpp @@ -24,16 +24,11 @@ */ #ifdef __PSP__ -#include #include -#include -#include #include "backends/platform/psp/powerman.h" #include "backends/fs/psp/psp-stream.h" -#include - #define MIN2(a,b) ((a < b) ? a : b) #define MIN3(a,b,c) ( (a < b) ? (a < c ? a : c) : (b < c ? b : c) ) @@ -65,7 +60,7 @@ void printBuffer(byte *ptr, uint32 len) { PSPIoStream::PSPIoStream(const Common::String &path, bool writeMode) - : StdioStream((void *)1), _path(path), _writeMode(writeMode), + : _handle(0), _path(path), _writeMode(writeMode), _ferror(false), _pos(0), _physicalPos(0), _fileSize(0), _inCache(false), _eos(false), _cacheStartOffset(-1), _cache(0), @@ -74,8 +69,6 @@ PSPIoStream::PSPIoStream(const Common::String &path, bool writeMode) DEBUG_ENTER_FUNC(); // assert(!path.empty()); // do we need this? - - _handle = (void *)0; // Need to do this since base class asserts not 0. } PSPIoStream::~PSPIoStream() { @@ -87,7 +80,7 @@ PSPIoStream::~PSPIoStream() { PowerMan.unregisterForSuspend(this); // Unregister with powermanager to be suspended // Must do this before fclose() or resume() will reopen. - fclose((FILE *)_handle); // We don't need a critical section. Worst case, the handle gets closed on its own + sceIoClose(_handle); // We don't need a critical section. Worst case, the handle gets closed on its own if (_cache) free(_cache); @@ -105,7 +98,7 @@ void *PSPIoStream::open() { PSP_DEBUG_PRINT_FUNC("Suspended\n"); } - _handle = fopen(_path.c_str(), _writeMode ? "wb" : "rb"); // open + _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); // open if (_handle) { // Get the file size. This way is much faster than going to the end of the file and back @@ -122,7 +115,7 @@ void *PSPIoStream::open() { PowerMan.endCriticalSection(); - return _handle; + return (void *)_handle; } bool PSPIoStream::err() const { @@ -188,7 +181,7 @@ bool PSPIoStream::seek(int32 offs, int whence) { } else { // not in cache _inCache = false; } - _pos = posToSearchFor; + _pos = posToSearchFor; return true; } @@ -245,11 +238,10 @@ uint32 PSPIoStream::read(void *ptr, uint32 len) { PSP_DEBUG_PRINT("filling cache with 0x%x bytes from physicalPos[0x%x]. cacheStart[0x%x], pos[0x%x], fileSize[0x%x]\n", lenToCopyToCache, _physicalPos, _cacheStartOffset, _pos, _fileSize); - size_t ret = fread(_cache, 1, lenToCopyToCache, (FILE *)_handle); - if (ret != lenToCopyToCache) { + int ret = sceIoRead(_handle, _cache, lenToCopyToCache); + if (ret != (int)lenToCopyToCache) { PSP_ERROR("in filling cache, failed to get 0x%x bytes. Only got 0x%x\n", lenToCopyToCache, ret); _ferror = true; - clearerr((FILE *)_handle); } _cacheStartOffset = _physicalPos; _inCache = true; @@ -264,15 +256,14 @@ uint32 PSPIoStream::read(void *ptr, uint32 len) { } else { // Too big for cache. No caching PSP_DEBUG_PRINT("reading 0x%x bytes from file to %p. Pos[0x%x], physPos[0x%x]\n", lenFromFile, destPtr, _pos, _physicalPos); - size_t ret = fread(destPtr, 1, lenFromFile, (FILE *)_handle); + int ret = sceIoRead(_handle, destPtr, lenFromFile); _physicalPos += ret; // Update pos _pos = _physicalPos; - if (ret != lenFromFile) { // error + if (ret != (int)lenFromFile) { // error PSP_ERROR("fread returned [0x%x] instead of len[0x%x]\n", ret, lenFromFile); _ferror = true; - clearerr((FILE *)_handle); _errorSource = 4; } _inCache = false; @@ -290,8 +281,10 @@ uint32 PSPIoStream::read(void *ptr, uint32 len) { // TODO: Test if seeking backwards/forwards has any effect on performance inline bool PSPIoStream::synchronizePhysicalPos() { if (_pos != _physicalPos) { - if (fseek((FILE *)_handle, _pos - _physicalPos, SEEK_CUR) != 0) + if (sceIoLseek32(_handle, _pos - _physicalPos, PSP_SEEK_CUR) < 0) { + _ferror = true; return false; + } _physicalPos = _pos; } @@ -314,26 +307,25 @@ uint32 PSPIoStream::write(const void *ptr, uint32 len) { PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x]\n", _path.c_str(), len); - if (_ferror) + if (!len || _ferror) // we actually get calls with len=0 return 0; _eos = false; // we can't have eos with write synchronizePhysicalPos(); - size_t ret = fwrite(ptr, 1, len, (FILE *)_handle); + int ret = sceIoWrite(_handle, ptr, len); // If we're making the file bigger, adjust the size - if (_physicalPos + (int)ret > _fileSize) + if (_physicalPos + ret > _fileSize) _fileSize = _physicalPos + ret; + _physicalPos += ret; _pos = _physicalPos; _inCache = false; _cacheStartOffset = -1; // invalidate cache - if (ret != len) { // Set error + if (ret != (int)len) { // Set error _ferror = true; - clearerr((FILE *)_handle); - _pos = ftell((FILE *)_handle); // Update pos _errorSource = 5; PSP_ERROR("fwrite returned[0x%x] instead of len[0x%x]\n", ret, len); } @@ -344,23 +336,8 @@ uint32 PSPIoStream::write(const void *ptr, uint32 len) { } bool PSPIoStream::flush() { - DEBUG_ENTER_FUNC(); - // Enter critical section - if (PowerMan.beginCriticalSection()) - PSP_DEBUG_PRINT_FUNC("Suspended\n"); - int ret = fflush((FILE *)_handle); - - if (ret != 0) { - _ferror = true; - clearerr((FILE *)_handle); - _errorSource = 6; - PSP_ERROR("fflush returned ret[%d]\n", ret); - } - - PowerMan.endCriticalSection(); - - return (ret == 0); + return true; } // For the PSP, since we're building in suspend support, we moved opening @@ -393,8 +370,8 @@ int PSPIoStream::suspend() { } if (_handle > 0) { - fclose((FILE *)_handle); // close our file descriptor - _handle = (void *)0xFFFFFFFF; // Set handle to non-null invalid value so makeFromPath doesn't return error + sceIoClose(_handle); // close our file descriptor + _handle = 0xFFFFFFFF; // Set handle to non-null invalid value so makeFromPath doesn't return error } return 0; @@ -409,19 +386,19 @@ int PSPIoStream::resume() { _suspendCount--; // We reopen our file descriptor - _handle = fopen(_path.c_str(), _writeMode ? "wb" : "rb"); + _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); // open if (_handle <= 0) { PSP_ERROR("Couldn't reopen file %s\n", _path.c_str()); } // Resume our previous position if (_handle > 0 && _pos > 0) { - ret = fseek((FILE *)_handle, _pos, SEEK_SET); + ret = sceIoLseek32(_handle, _pos, PSP_SEEK_SET); _physicalPos = _pos; _inCache = false; - if (ret != 0) { // Check for problem + if (ret < 0) { // Check for problem _errorSuspend = ResumeError; _errorPos = _pos; _errorHandle = _handle; -- cgit v1.2.3 From b88f341b8000bd5a8844f7fc4b69692459302f19 Mon Sep 17 00:00:00 2001 From: Yotam Barnoy Date: Tue, 24 Aug 2010 11:24:34 +0000 Subject: PSP: switched to using BufferedSeekableReadStream and BufferedWriteStream The last PSP optimization made reading much faster, but writing isn't buffered so saving the config file was VERY slow. I decided the cleanest way to do this would be to add BWS and use BSRS. svn-id: r52327 --- backends/fs/psp/psp-stream.cpp | 314 ++++++++++++++++------------------------- 1 file changed, 121 insertions(+), 193 deletions(-) (limited to 'backends/fs/psp/psp-stream.cpp') diff --git a/backends/fs/psp/psp-stream.cpp b/backends/fs/psp/psp-stream.cpp index 8843bfd2fb..74d631a2f7 100644 --- a/backends/fs/psp/psp-stream.cpp +++ b/backends/fs/psp/psp-stream.cpp @@ -58,58 +58,53 @@ void printBuffer(byte *ptr, uint32 len) { } #endif +// Class PspIoStream ------------------------------------------------ -PSPIoStream::PSPIoStream(const Common::String &path, bool writeMode) - : _handle(0), _path(path), _writeMode(writeMode), - _ferror(false), _pos(0), - _physicalPos(0), _fileSize(0), _inCache(false), _eos(false), - _cacheStartOffset(-1), _cache(0), - _errorSuspend(0), _errorSource(0), - _errorPos(0), _errorHandle(0), _suspendCount(0) { +PspIoStream::PspIoStream(const Common::String &path, bool writeMode) + : _handle(0), _path(path), _fileSize(0), _writeMode(writeMode), + _physicalPos(0), _pos(0), _eos(false), _error(false), + _errorSuspend(0), _errorSource(0), _errorPos(0), _errorHandle(0), _suspendCount(0) { DEBUG_ENTER_FUNC(); - // assert(!path.empty()); // do we need this? + //assert(!path.empty()); // do we need this? } -PSPIoStream::~PSPIoStream() { +PspIoStream::~PspIoStream() { DEBUG_ENTER_FUNC(); if (PowerMan.beginCriticalSection()) - PSP_DEBUG_PRINT_FUNC("Suspended\n"); - - PowerMan.unregisterForSuspend(this); // Unregister with powermanager to be suspended - // Must do this before fclose() or resume() will reopen. - - sceIoClose(_handle); // We don't need a critical section. Worst case, the handle gets closed on its own + PSP_DEBUG_PRINT_FUNC("suspended\n"); + + PowerMan.unregisterForSuspend(this); // Unregister with powermanager to be suspended + // Must do this before fclose() or resume() will reopen. + sceIoClose(_handle); - if (_cache) - free(_cache); - PowerMan.endCriticalSection(); } /* Function to open the file pointed to by the path. * */ -void *PSPIoStream::open() { +void *PspIoStream::open() { DEBUG_ENTER_FUNC(); + if (PowerMan.beginCriticalSection()) { - // No need to open. Just return the _handle resume() already opened. - PSP_DEBUG_PRINT_FUNC("Suspended\n"); + // No need to open? Just return the _handle resume() already opened + PSP_DEBUG_PRINT_FUNC("suspended\n"); } - _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); // open - - if (_handle) { - // Get the file size. This way is much faster than going to the end of the file and back - SceIoStat stat; - sceIoGetstat(_path.c_str(), &stat); - _fileSize = *((uint32 *)(void *)&stat.st_size); // 4GB file is big enough for us - PSP_DEBUG_PRINT("%s filesize = %d\n", _path.c_str(), _fileSize); + _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); + if (!_handle) { + _error = true; + _handle = NULL; + } - // Allocate the cache - _cache = (char *)memalign(64, CACHE_SIZE); - } + // Get the file size. This way is much faster than going to the end of the file and back + SceIoStat stat; + sceIoGetstat(_path.c_str(), &stat); + _fileSize = *((uint32 *)(void *)&stat.st_size); // 4GB file (32 bits) is big enough for us + + PSP_DEBUG_PRINT("%s filesize[%d]\n", _path.c_str(), _fileSize); PowerMan.registerForSuspend(this); // Register with the powermanager to be suspended @@ -118,35 +113,48 @@ void *PSPIoStream::open() { return (void *)_handle; } -bool PSPIoStream::err() const { +bool PspIoStream::err() const { DEBUG_ENTER_FUNC(); - if (_ferror) // We dump since no printing to screen with suspend - PSP_ERROR("mem_ferror[%d], source[%d], suspend error[%d], pos[%d], \ - _errorPos[%d], _errorHandle[%p], suspendCount[%d]\n", - _ferror, _errorSource, _errorSuspend, _pos, + if (_error) // We dump since no printing to screen with suspend callback + PSP_ERROR("mem_error[%d], source[%d], suspend error[%d], pos[%d]," + "_errorPos[%d], _errorHandle[%p], suspendCount[%d]\n", + _error, _errorSource, _errorSuspend, _pos, _errorPos, _errorHandle, _suspendCount); - return _ferror; + return _error; } -void PSPIoStream::clearErr() { - _ferror = false; +void PspIoStream::clearErr() { + _error = false; } -bool PSPIoStream::eos() const { +bool PspIoStream::eos() const { return _eos; } -int32 PSPIoStream::pos() const { +int32 PspIoStream::pos() const { return _pos; } -int32 PSPIoStream::size() const { +int32 PspIoStream::size() const { return _fileSize; } -bool PSPIoStream::seek(int32 offs, int whence) { +bool PspIoStream::physicalSeekFromCur(int32 offset) { + + int ret = sceIoLseek32(_handle, offset, PSP_SEEK_CUR); + + if (ret < 0) { + _error = true; + PSP_ERROR("failed to seek in file[%s] to [%x]. Error[%x]\n", _path.c_str(), offset, ret); + return false; + } + _physicalPos += offset; + return true; +} + +bool PspIoStream::seek(int32 offs, int whence) { DEBUG_ENTER_FUNC(); PSP_DEBUG_PRINT_FUNC("offset[0x%x], whence[%d], _pos[0x%x], _physPos[0x%x]\n", offs, whence, _pos, _physicalPos); _eos = false; @@ -157,195 +165,116 @@ bool PSPIoStream::seek(int32 offs, int whence) { posToSearchFor = _pos; break; case SEEK_END: - posToSearchFor = _fileSize; // unsure. Does it take us here or to EOS - 1? + posToSearchFor = _fileSize; break; } posToSearchFor += offs; - + // Check for bad values if (posToSearchFor < 0) { - _ferror = true; + _error = true; return false; - } - - if (posToSearchFor > _fileSize) { - _ferror = true; + } else if (posToSearchFor > _fileSize) { + _error = true; _eos = true; return false; } - // See if we can find it in cache - if (isOffsetInCache(posToSearchFor)) { - PSP_DEBUG_PRINT("seek offset[0x%x] found in cache. Cache starts[0x%x]\n", posToSearchFor, _cacheStartOffset); - _inCache = true; - } else { // not in cache - _inCache = false; - } _pos = posToSearchFor; + return true; } -uint32 PSPIoStream::read(void *ptr, uint32 len) { +uint32 PspIoStream::read(void *ptr, uint32 len) { DEBUG_ENTER_FUNC(); - PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p]\n", _path.c_str(), len, ptr); + PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p], _pos[%x], _physPos[%x]\n", _path.c_str(), len, ptr, _pos, _physicalPos); - if (_ferror || _eos) + if (_error || _eos || len <= 0) return 0; - - byte *destPtr = (byte *)ptr; - uint32 lenFromFile = len; // how much we read from the actual file - uint32 lenFromCache = 0; // how much we read from cache + uint32 lenRemainingInFile = _fileSize - _pos; - - if (lenFromFile > lenRemainingInFile) { - lenFromFile = lenRemainingInFile; + + // check for getting EOS + if (len > lenRemainingInFile) { + len = lenRemainingInFile; _eos = true; } - - // Are we in cache? - if (_inCache && isCacheValid()) { - uint32 offsetInCache = _pos - _cacheStartOffset; - // We can read at most what's in the cache or the remaining size of the file - lenFromCache = MIN2(lenFromFile, CACHE_SIZE - offsetInCache); // unsure - - PSP_DEBUG_PRINT("reading 0x%x bytes from cache to %p. pos[0x%x] physPos[0x%x] cacheStart[0x%x]\n", lenFromCache, destPtr, _pos, _physicalPos, _cacheStartOffset); - - memcpy(destPtr, &_cache[offsetInCache], lenFromCache); - _pos += lenFromCache; - - if (lenFromCache < lenFromFile) { // there's more to copy from the file - lenFromFile -= lenFromCache; - lenRemainingInFile -= lenFromCache; // since we moved pos - destPtr += lenFromCache; - } else { // we're done -#ifdef DEBUG_BUFFERS - printBuffer((byte *)ptr, len); -#endif - - return lenFromCache; // how much we actually read - } - } if (PowerMan.beginCriticalSection()) - PSP_DEBUG_PRINT_FUNC("Suspended\n"); - + PSP_DEBUG_PRINT_FUNC("suspended\n"); - synchronizePhysicalPos(); // we need to update our physical position + // check if we need to seek + if (_pos != _physicalPos) + PSP_DEBUG_PRINT("seeking from %x to %x\n", _physicalPos, _pos); + if (!physicalSeekFromCur(_pos - _physicalPos)) { + _error = true; + return 0; + } - if (lenFromFile <= MIN_READ_SIZE) { // We load the cache in case the read is small enough - // This optimization is based on the principle that reading 1 byte is as expensive as 1000 bytes - uint32 lenToCopyToCache = MIN2((uint32)MIN_READ_SIZE, lenRemainingInFile); // at most remaining file size - - PSP_DEBUG_PRINT("filling cache with 0x%x bytes from physicalPos[0x%x]. cacheStart[0x%x], pos[0x%x], fileSize[0x%x]\n", lenToCopyToCache, _physicalPos, _cacheStartOffset, _pos, _fileSize); - - int ret = sceIoRead(_handle, _cache, lenToCopyToCache); - if (ret != (int)lenToCopyToCache) { - PSP_ERROR("in filling cache, failed to get 0x%x bytes. Only got 0x%x\n", lenToCopyToCache, ret); - _ferror = true; - } - _cacheStartOffset = _physicalPos; - _inCache = true; - - _physicalPos += ret; - - PSP_DEBUG_PRINT("copying 0x%x bytes from cache to %p\n", lenFromFile, destPtr); - - // Copy to the destination buffer from cache - memcpy(destPtr, _cache, lenFromFile); - _pos += lenFromFile; - - } else { // Too big for cache. No caching - PSP_DEBUG_PRINT("reading 0x%x bytes from file to %p. Pos[0x%x], physPos[0x%x]\n", lenFromFile, destPtr, _pos, _physicalPos); - int ret = sceIoRead(_handle, destPtr, lenFromFile); - - _physicalPos += ret; // Update pos - _pos = _physicalPos; - - if (ret != (int)lenFromFile) { // error - PSP_ERROR("fread returned [0x%x] instead of len[0x%x]\n", ret, lenFromFile); - _ferror = true; - _errorSource = 4; - } - _inCache = false; - } + int ret = sceIoRead(_handle, ptr, len); PowerMan.endCriticalSection(); - -#ifdef DEBUG_BUFFERS - printBuffer((byte *)ptr, len); -#endif - - return lenFromCache + lenFromFile; // total of what was copied -} - -// TODO: Test if seeking backwards/forwards has any effect on performance -inline bool PSPIoStream::synchronizePhysicalPos() { - if (_pos != _physicalPos) { - if (sceIoLseek32(_handle, _pos - _physicalPos, PSP_SEEK_CUR) < 0) { - _ferror = true; - return false; - } - _physicalPos = _pos; - } - return true; -} - -inline bool PSPIoStream::isOffsetInCache(uint32 offset) { - if (_cacheStartOffset != -1 && - offset >= (uint32)_cacheStartOffset && - offset < (uint32)(_cacheStartOffset + CACHE_SIZE)) - return true; - return false; -} + _physicalPos += ret; // Update position + _pos = _physicalPos; + + if (ret != (int)len) { // error + PSP_ERROR("sceIoRead returned [0x%x] instead of len[0x%x]\n", ret, len); + _error = true; + _errorSource = 4; + } + return ret; +} -uint32 PSPIoStream::write(const void *ptr, uint32 len) { +uint32 PspIoStream::write(const void *ptr, uint32 len) { DEBUG_ENTER_FUNC(); - // Check if we can access the file - if (PowerMan.beginCriticalSection()) - PSP_DEBUG_PRINT_FUNC("Suspended\n"); + PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p], _pos[%x], _physPos[%x] buf[%x %x %x %x..%x %x]\n", _path.c_str(), len, ptr, _pos, _physicalPos, ((byte *)ptr)[0], ((byte *)ptr)[1], ((byte *)ptr)[2], ((byte *)ptr)[3], ((byte *)ptr)[len - 2], + ((byte *)ptr)[len - 1]); - PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x]\n", _path.c_str(), len); - - if (!len || _ferror) // we actually get calls with len=0 + if (!len || _error) // we actually get some calls with len == 0! return 0; + + _eos = false; // we can't have eos with write - _eos = false; // we can't have eos with write - synchronizePhysicalPos(); + if (PowerMan.beginCriticalSection()) + PSP_DEBUG_PRINT_FUNC("suspended\n"); + + // check if we need to seek + if (_pos != _physicalPos) + if (!physicalSeekFromCur(_pos - _physicalPos)) { + _error = true; + return 0; + } int ret = sceIoWrite(_handle, ptr, len); - - // If we're making the file bigger, adjust the size - if (_physicalPos + ret > _fileSize) - _fileSize = _physicalPos + ret; - - _physicalPos += ret; - _pos = _physicalPos; - _inCache = false; - _cacheStartOffset = -1; // invalidate cache - - if (ret != (int)len) { // Set error - _ferror = true; + + PowerMan.endCriticalSection(); + + if (ret != (int)len) { + _error = true; _errorSource = 5; - PSP_ERROR("fwrite returned[0x%x] instead of len[0x%x]\n", ret, len); + PSP_ERROR("sceIoWrite returned[0x%x] instead of len[0x%x]\n", ret, len); } - PowerMan.endCriticalSection(); - + _physicalPos += ret; + _pos = _physicalPos; + + if (_pos > _fileSize) + _fileSize = _pos; + return ret; } -bool PSPIoStream::flush() { - +bool PspIoStream::flush() { return true; } // For the PSP, since we're building in suspend support, we moved opening -// the actual file to an open function since we need an actual PSPIoStream object to suspend. +// the actual file to an open function since we need an actual PspIoStream object to suspend. // -PSPIoStream *PSPIoStream::makeFromPath(const Common::String &path, bool writeMode) { +PspIoStream *PspIoStream::makeFromPath(const Common::String &path, bool writeMode) { DEBUG_ENTER_FUNC(); - PSPIoStream *stream = new PSPIoStream(path, writeMode); + PspIoStream *stream = new PspIoStream(path, writeMode); if (stream->open() <= 0) { delete stream; @@ -359,7 +288,7 @@ PSPIoStream *PSPIoStream::makeFromPath(const Common::String &path, bool writeMod * Function to suspend the IO stream (called by PowerManager) * we can have no output here */ -int PSPIoStream::suspend() { +int PspIoStream::suspend() { DEBUG_ENTER_FUNC(); _suspendCount++; @@ -380,7 +309,7 @@ int PSPIoStream::suspend() { /* * Function to resume the IO stream (called by Power Manager) */ -int PSPIoStream::resume() { +int PspIoStream::resume() { DEBUG_ENTER_FUNC(); int ret = 0; _suspendCount--; @@ -388,15 +317,15 @@ int PSPIoStream::resume() { // We reopen our file descriptor _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); // open if (_handle <= 0) { - PSP_ERROR("Couldn't reopen file %s\n", _path.c_str()); + _errorSuspend = ResumeError; + _errorPos = _pos; } - // Resume our previous position + // Resume our previous position if needed if (_handle > 0 && _pos > 0) { ret = sceIoLseek32(_handle, _pos, PSP_SEEK_SET); _physicalPos = _pos; - _inCache = false; if (ret < 0) { // Check for problem _errorSuspend = ResumeError; @@ -407,5 +336,4 @@ int PSPIoStream::resume() { return ret; } - #endif /* __PSP__ */ -- cgit v1.2.3 From 89b34faa5aa85f8e7f7e5ec54f14246e639c9e6f Mon Sep 17 00:00:00 2001 From: Yotam Barnoy Date: Wed, 25 Aug 2010 13:26:30 +0000 Subject: PSP: fixed flag to open() I was missing a flag (PSP_O_TRUNC) causing the config file to be opened on top of the old file, causing file corruption. svn-id: r52387 --- backends/fs/psp/psp-stream.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'backends/fs/psp/psp-stream.cpp') diff --git a/backends/fs/psp/psp-stream.cpp b/backends/fs/psp/psp-stream.cpp index 74d631a2f7..3b0737ba63 100644 --- a/backends/fs/psp/psp-stream.cpp +++ b/backends/fs/psp/psp-stream.cpp @@ -93,7 +93,7 @@ void *PspIoStream::open() { PSP_DEBUG_PRINT_FUNC("suspended\n"); } - _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_RDWR | PSP_O_CREAT : PSP_O_RDONLY, 0777); + _handle = sceIoOpen(_path.c_str(), _writeMode ? PSP_O_WRONLY | PSP_O_CREAT | PSP_O_TRUNC : PSP_O_RDONLY, 0777); if (!_handle) { _error = true; _handle = NULL; @@ -228,8 +228,7 @@ uint32 PspIoStream::read(void *ptr, uint32 len) { uint32 PspIoStream::write(const void *ptr, uint32 len) { DEBUG_ENTER_FUNC(); - PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p], _pos[%x], _physPos[%x] buf[%x %x %x %x..%x %x]\n", _path.c_str(), len, ptr, _pos, _physicalPos, ((byte *)ptr)[0], ((byte *)ptr)[1], ((byte *)ptr)[2], ((byte *)ptr)[3], ((byte *)ptr)[len - 2], - ((byte *)ptr)[len - 1]); + PSP_DEBUG_PRINT_FUNC("filename[%s], len[0x%x], ptr[%p], _pos[%x], _physPos[%x]\n", _path.c_str(), len, ptr, _pos, _physicalPos); if (!len || _error) // we actually get some calls with len == 0! return 0; -- cgit v1.2.3 From 8388e0dfea4ae0d80e51368acd12685c740c5bb5 Mon Sep 17 00:00:00 2001 From: Jordi Vilalta Prat Date: Tue, 12 Oct 2010 02:18:11 +0000 Subject: JANITORAL: Clean trailing whitespaces. svn-id: r53160 --- backends/fs/psp/psp-stream.cpp | 66 +++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 33 deletions(-) (limited to 'backends/fs/psp/psp-stream.cpp') diff --git a/backends/fs/psp/psp-stream.cpp b/backends/fs/psp/psp-stream.cpp index 3b0737ba63..10b80a0639 100644 --- a/backends/fs/psp/psp-stream.cpp +++ b/backends/fs/psp/psp-stream.cpp @@ -43,17 +43,17 @@ #ifdef DEBUG_BUFFERS void printBuffer(byte *ptr, uint32 len) { uint32 printLen = len <= 10 ? len : 10; - + for (int i = 0; i < printLen; i++) { - PSP_INFO_PRINT("%x ", ptr[i]); + PSP_INFO_PRINT("%x ", ptr[i]); } - + if (len > 10) { PSP_INFO_PRINT("... "); for (int i = len - 10; i < len; i++) PSP_INFO_PRINT("%x ", ptr[i]); } - + PSP_INFO_PRINT("\n"); } #endif @@ -62,7 +62,7 @@ void printBuffer(byte *ptr, uint32 len) { PspIoStream::PspIoStream(const Common::String &path, bool writeMode) : _handle(0), _path(path), _fileSize(0), _writeMode(writeMode), - _physicalPos(0), _pos(0), _eos(false), _error(false), + _physicalPos(0), _pos(0), _eos(false), _error(false), _errorSuspend(0), _errorSource(0), _errorPos(0), _errorHandle(0), _suspendCount(0) { DEBUG_ENTER_FUNC(); @@ -74,11 +74,11 @@ PspIoStream::~PspIoStream() { if (PowerMan.beginCriticalSection()) PSP_DEBUG_PRINT_FUNC("suspended\n"); - + PowerMan.unregisterForSuspend(this); // Unregister with powermanager to be suspended // Must do this before fclose() or resume() will reopen. sceIoClose(_handle); - + PowerMan.endCriticalSection(); } @@ -87,7 +87,7 @@ PspIoStream::~PspIoStream() { */ void *PspIoStream::open() { DEBUG_ENTER_FUNC(); - + if (PowerMan.beginCriticalSection()) { // No need to open? Just return the _handle resume() already opened PSP_DEBUG_PRINT_FUNC("suspended\n"); @@ -97,13 +97,13 @@ void *PspIoStream::open() { if (!_handle) { _error = true; _handle = NULL; - } - + } + // Get the file size. This way is much faster than going to the end of the file and back SceIoStat stat; sceIoGetstat(_path.c_str(), &stat); _fileSize = *((uint32 *)(void *)&stat.st_size); // 4GB file (32 bits) is big enough for us - + PSP_DEBUG_PRINT("%s filesize[%d]\n", _path.c_str(), _fileSize); PowerMan.registerForSuspend(this); // Register with the powermanager to be suspended @@ -115,7 +115,7 @@ void *PspIoStream::open() { bool PspIoStream::err() const { DEBUG_ENTER_FUNC(); - + if (_error) // We dump since no printing to screen with suspend callback PSP_ERROR("mem_error[%d], source[%d], suspend error[%d], pos[%d]," "_errorPos[%d], _errorHandle[%p], suspendCount[%d]\n", @@ -142,9 +142,9 @@ int32 PspIoStream::size() const { } bool PspIoStream::physicalSeekFromCur(int32 offset) { - + int ret = sceIoLseek32(_handle, offset, PSP_SEEK_CUR); - + if (ret < 0) { _error = true; PSP_ERROR("failed to seek in file[%s] to [%x]. Error[%x]\n", _path.c_str(), offset, ret); @@ -158,7 +158,7 @@ bool PspIoStream::seek(int32 offs, int whence) { DEBUG_ENTER_FUNC(); PSP_DEBUG_PRINT_FUNC("offset[0x%x], whence[%d], _pos[0x%x], _physPos[0x%x]\n", offs, whence, _pos, _physicalPos); _eos = false; - + int32 posToSearchFor = 0; switch (whence) { case SEEK_CUR: @@ -179,9 +179,9 @@ bool PspIoStream::seek(int32 offs, int whence) { _eos = true; return false; } - + _pos = posToSearchFor; - + return true; } @@ -198,33 +198,33 @@ uint32 PspIoStream::read(void *ptr, uint32 len) { if (len > lenRemainingInFile) { len = lenRemainingInFile; _eos = true; - } + } if (PowerMan.beginCriticalSection()) PSP_DEBUG_PRINT_FUNC("suspended\n"); - + // check if we need to seek if (_pos != _physicalPos) PSP_DEBUG_PRINT("seeking from %x to %x\n", _physicalPos, _pos); if (!physicalSeekFromCur(_pos - _physicalPos)) { _error = true; return 0; - } - + } + int ret = sceIoRead(_handle, ptr, len); PowerMan.endCriticalSection(); - + _physicalPos += ret; // Update position _pos = _physicalPos; - + if (ret != (int)len) { // error PSP_ERROR("sceIoRead returned [0x%x] instead of len[0x%x]\n", ret, len); _error = true; - _errorSource = 4; + _errorSource = 4; } return ret; -} +} uint32 PspIoStream::write(const void *ptr, uint32 len) { DEBUG_ENTER_FUNC(); @@ -234,7 +234,7 @@ uint32 PspIoStream::write(const void *ptr, uint32 len) { return 0; _eos = false; // we can't have eos with write - + if (PowerMan.beginCriticalSection()) PSP_DEBUG_PRINT_FUNC("suspended\n"); @@ -244,11 +244,11 @@ uint32 PspIoStream::write(const void *ptr, uint32 len) { _error = true; return 0; } - + int ret = sceIoWrite(_handle, ptr, len); - + PowerMan.endCriticalSection(); - + if (ret != (int)len) { _error = true; _errorSource = 5; @@ -257,10 +257,10 @@ uint32 PspIoStream::write(const void *ptr, uint32 len) { _physicalPos += ret; _pos = _physicalPos; - + if (_pos > _fileSize) - _fileSize = _pos; - + _fileSize = _pos; + return ret; } @@ -323,7 +323,7 @@ int PspIoStream::resume() { // Resume our previous position if needed if (_handle > 0 && _pos > 0) { ret = sceIoLseek32(_handle, _pos, PSP_SEEK_SET); - + _physicalPos = _pos; if (ret < 0) { // Check for problem -- cgit v1.2.3