aboutsummaryrefslogtreecommitdiff
path: root/backends
diff options
context:
space:
mode:
authorYotam Barnoy2010-08-24 11:24:34 +0000
committerYotam Barnoy2010-08-24 11:24:34 +0000
commitb88f341b8000bd5a8844f7fc4b69692459302f19 (patch)
treed700721b72d51940dee7713b3a494f0dbacd6a0c /backends
parent347accf2d77729f95f4cefe73df10d198e915e85 (diff)
downloadscummvm-rg350-b88f341b8000bd5a8844f7fc4b69692459302f19.tar.gz
scummvm-rg350-b88f341b8000bd5a8844f7fc4b69692459302f19.tar.bz2
scummvm-rg350-b88f341b8000bd5a8844f7fc4b69692459302f19.zip
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
Diffstat (limited to 'backends')
-rw-r--r--backends/fs/psp/psp-fs.cpp12
-rw-r--r--backends/fs/psp/psp-stream.cpp314
-rw-r--r--backends/fs/psp/psp-stream.h52
-rw-r--r--backends/platform/psp/tests.cpp174
4 files changed, 332 insertions, 220 deletions
diff --git a/backends/fs/psp/psp-fs.cpp b/backends/fs/psp/psp-fs.cpp
index d72c5108dd..4857a385b7 100644
--- a/backends/fs/psp/psp-fs.cpp
+++ b/backends/fs/psp/psp-fs.cpp
@@ -247,11 +247,19 @@ AbstractFSNode *PSPFilesystemNode::getParent() const {
}
Common::SeekableReadStream *PSPFilesystemNode::createReadStream() {
- return PSPIoStream::makeFromPath(getPath(), false);
+ const uint32 READ_BUFFER_SIZE = 1024;
+
+ Common::SeekableReadStream *stream = PspIoStream::makeFromPath(getPath(), false);
+
+ return new PspIoBufferedReadStream(stream, READ_BUFFER_SIZE, DisposeAfterUse::YES);
}
Common::WriteStream *PSPFilesystemNode::createWriteStream() {
- return PSPIoStream::makeFromPath(getPath(), true);
+ const uint32 WRITE_BUFFER_SIZE = 1024;
+
+ Common::WriteStream *stream = PspIoStream::makeFromPath(getPath(), true);
+
+ return new PspIoBufferedWriteStream(stream, WRITE_BUFFER_SIZE, DisposeAfterUse::YES);
}
#endif //#ifdef __PSP__
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__ */
diff --git a/backends/fs/psp/psp-stream.h b/backends/fs/psp/psp-stream.h
index ca9d40c720..be3a1220de 100644
--- a/backends/fs/psp/psp-stream.h
+++ b/backends/fs/psp/psp-stream.h
@@ -33,18 +33,33 @@
#include "common/stream.h"
#include "common/str.h"
-/*
+class PspIoBufferedReadStream : public Common::BufferedSeekableReadStream {
+public:
+ PspIoBufferedReadStream(SeekableReadStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::YES) : BufferedSeekableReadStream(parentStream, bufSize, disposeParentStream) {}
+protected:
+ virtual void allocBuf(uint32 bufSize) { _buf = (byte *)memalign(64, bufSize); } // want 64 byte alignment for cache
+ virtual void deallocBuf() { free(_buf); }
+};
+
+class PspIoBufferedWriteStream : public Common::BufferedWriteStream {
+public:
+ PspIoBufferedWriteStream(WriteStream *parentStream, uint32 bufSize, DisposeAfterUse::Flag disposeParentStream = DisposeAfterUse::YES) : BufferedWriteStream(parentStream, bufSize, disposeParentStream) {}
+protected:
+ virtual void allocBuf(uint32 bufSize) { _buf = (byte *)memalign(64, bufSize); }
+ virtual void deallocBuf() { free(_buf); }
+};
+
+/**
* Class to handle special suspend/resume needs of PSP IO Streams
*/
-class PSPIoStream : public Common::SeekableReadStream, public Common::WriteStream, public Common::NonCopyable, public Suspendable {
+class PspIoStream : public Common::SeekableReadStream, public Common::WriteStream, public Common::NonCopyable, public Suspendable {
protected:
SceUID _handle; // file handle
Common::String _path;
int _fileSize;
bool _writeMode; // for resuming in the right mode
- int _physicalPos; // position in the real file
+ int _physicalPos; // physical position in file
int _pos; // position. Sometimes virtual
- bool _inCache; // whether we're in cache (virtual) mode
bool _eos; // EOS flag
enum {
@@ -52,36 +67,26 @@ protected:
ResumeError = 3
};
- enum {
- CACHE_SIZE = 1024,
- MIN_READ_SIZE = 1024 // reading less than 1024 takes exactly the same time as 1024
- };
-
- // For caching
- char *_cache;
- int _cacheStartOffset; // starting offset of the cache. -1 when cache is invalid
-
- mutable int _ferror; // file error state
+ // debug stuff
+ mutable int _error; // file error state
int _errorSuspend; // for debugging
mutable int _errorSource;
int _errorPos;
SceUID _errorHandle;
int _suspendCount;
-
- bool synchronizePhysicalPos(); // synchronize the physical and virtual positions
- bool isOffsetInCache(uint32 pos); // check if an offset is found in cache
- bool isCacheValid() { return _cacheStartOffset != -1; }
+
+ bool physicalSeekFromCur(int32 offset);
public:
/**
* Given a path, invoke fopen on that path and wrap the result in a
- * PSPIoStream instance.
+ * PspIoStream instance.
*/
- static PSPIoStream *makeFromPath(const Common::String &path, bool writeMode);
+ static PspIoStream *makeFromPath(const Common::String &path, bool writeMode);
- PSPIoStream(const Common::String &path, bool writeMode);
- virtual ~PSPIoStream();
+ PspIoStream(const Common::String &path, bool writeMode);
+ virtual ~PspIoStream();
void * open(); // open the file pointed to by the file path
@@ -96,7 +101,8 @@ public:
virtual int32 size() const;
virtual bool seek(int32 offs, int whence = SEEK_SET);
virtual uint32 read(void *dataPtr, uint32 dataSize);
-
+
+ // for suspending
int suspend(); /* Suspendable interface (power manager) */
int resume(); /* " " */
};
diff --git a/backends/platform/psp/tests.cpp b/backends/platform/psp/tests.cpp
index d1bdb9e640..f65c13ae83 100644
--- a/backends/platform/psp/tests.cpp
+++ b/backends/platform/psp/tests.cpp
@@ -43,7 +43,9 @@
#include "backends/platform/psp/rtc.h"
#include "backends/platform/psp/thread.h"
#include "backends/platform/psp/memory.h"
-
+#include "common/stream.h"
+#include "common/file.h"
+#include "common/fs.h"
#define UNCACHED(x) ((byte *)(((uint32)(x)) | 0x40000000)) /* make an uncached access */
#define CACHED(x) ((byte *)(((uint32)(x)) & 0xBFFFFFFF)) /* make an uncached access into a cached one */
@@ -445,6 +447,7 @@ void PspSpeedTests::fastCopySpeed() {
class PspUnitTests {
public:
void testFastCopy();
+ bool testFileSystem();
private:
enum {
@@ -453,6 +456,7 @@ private:
void fastCopySpecificSize(byte *dst, byte *src, uint32 bytes, bool swap = false);
void fastCopyDifferentSizes(byte *dst, byte *src, bool swap = false);
+
};
void PspUnitTests::testFastCopy() {
@@ -537,6 +541,171 @@ void PspUnitTests::fastCopySpecificSize(byte *dst, byte *src, uint32 bytes, bool
}
}
+// This function leaks. For now I don't care
+bool PspUnitTests::testFileSystem() {
+ // create memory
+ const uint32 BufSize = 32 * 1024;
+ char* buffer = new char[BufSize];
+ int i;
+ Common::WriteStream *wrStream;
+ Common::SeekableReadStream *rdStream;
+
+ PSP_INFO_PRINT("testing fileSystem...\n");
+
+ // fill buffer
+ for (i=0; i<(int)BufSize; i += 4) {
+ buffer[i] = 'A';
+ buffer[i + 1] = 'B';
+ buffer[i + 2] = 'C';
+ buffer[i + 3] = 'D';
+ }
+
+ // create a file
+ const char *path = "./file.test";
+ Common::FSNode file(path);
+
+ PSP_INFO_PRINT("creating write stream...\n");
+
+ wrStream = file.createWriteStream();
+ if (!wrStream) {
+ PSP_ERROR("%s couldn't be created.\n", path);
+ return false;
+ }
+
+ // write contents
+ char* index = buffer;
+ int32 totalLength = BufSize;
+ int32 curLength = 50;
+
+ PSP_INFO_PRINT("writing...\n");
+
+ while(totalLength - curLength > 0) {
+ if ((int)wrStream->write(index, curLength) != curLength) {
+ PSP_ERROR("couldn't write %d bytes\n", curLength);
+ return false;
+ }
+ totalLength -= curLength;
+ index += curLength;
+ //curLength *= 2;
+ //PSP_INFO_PRINT("write\n");
+ }
+
+ // write the rest
+ if ((int)wrStream->write(index, totalLength) != totalLength) {
+ PSP_ERROR("couldn't write %d bytes\n", curLength);
+ return false;
+ }
+
+ delete wrStream;
+
+ PSP_INFO_PRINT("reading...\n");
+
+ rdStream = file.createReadStream();
+ if (!rdStream) {
+ PSP_ERROR("%s couldn't be created.\n", path);
+ return false;
+ }
+
+ // seek to beginning
+ if (!rdStream->seek(0, SEEK_SET)) {
+ PSP_ERROR("couldn't seek to the beginning after writing the file\n");
+ return false;
+ }
+
+ // read the contents
+ char *readBuffer = new char[BufSize + 4];
+ memset(readBuffer, 0, (BufSize + 4));
+ index = readBuffer;
+ while (rdStream->read(index, 100) == 100) {
+ index += 100;
+ }
+
+ if (!rdStream->eos()) {
+ PSP_ERROR("didn't find EOS at end of stream\n");
+ return false;
+ }
+
+ // compare
+ for (i=0; i<(int)BufSize; i++)
+ if (buffer[i] != readBuffer[i]) {
+ PSP_ERROR("reading/writing mistake at %x. Got %x instead of %x\n", i, readBuffer[i], buffer[i]);
+ return false;
+ }
+
+ // Check for exceeding limit
+ for (i=0; i<4; i++) {
+ if (readBuffer[BufSize + i]) {
+ PSP_ERROR("read exceeded limits. %d = %x\n", BufSize + i, readBuffer[BufSize + i]);
+ }
+ }
+
+ delete rdStream;
+
+ PSP_INFO_PRINT("writing...\n");
+
+ wrStream = file.createWriteStream();
+ if (!wrStream) {
+ PSP_ERROR("%s couldn't be created.\n", path);
+ return false;
+ }
+
+ const char *phrase = "Jello is really fabulous";
+ uint32 phraseLen = strlen(phrase);
+
+ int ret;
+ if ((ret = wrStream->write(phrase, phraseLen)) != (int)phraseLen) {
+ PSP_ERROR("couldn't write phrase. Got %d instead of %d\n", ret, phraseLen);
+ return false;
+ }
+
+ PSP_INFO_PRINT("reading...\n");
+
+ delete wrStream;
+ rdStream = file.createReadStream();
+ if (!rdStream) {
+ PSP_ERROR("%s couldn't be created.\n", path);
+ return false;
+ }
+
+ char *readPhrase = new char[phraseLen + 2];
+ memset(readPhrase, 0, phraseLen + 2);
+
+ if ((ret = rdStream->read(readPhrase, phraseLen) != phraseLen)) {
+ PSP_ERROR("read error on phrase. Got %d instead of %d\n", ret, phraseLen);
+ return false;
+ }
+
+ for (i=0; i<(int)phraseLen; i++) {
+ if (readPhrase[i] != phrase[i]) {
+ PSP_ERROR("bad read/write in phrase. At %d, %x != %x\n", i, readPhrase[i], phrase[i]);
+ return false;
+ }
+ }
+
+ // check for exceeding
+ if (readPhrase[i] != 0) {
+ PSP_ERROR("found excessive copy in phrase. %c at %d\n", readPhrase[i], i);
+ return false;
+ }
+
+ PSP_INFO_PRINT("trying to read end...\n");
+
+ // seek to end
+ if (!rdStream->seek(0, SEEK_END)) {
+ PSP_ERROR("couldn't seek to end for append\n");
+ return false;
+ };
+
+ // try to read
+ if (rdStream->read(readPhrase, 2) || !rdStream->eos()) {
+ PSP_ERROR("was able to read at end of file\n");
+ return false;
+ }
+
+ PSP_INFO_PRINT("ok\n");
+ return true;
+}
+
void psp_tests() {
PSP_INFO_PRINT("in tests\n");
@@ -558,7 +727,8 @@ void psp_tests() {
// Unit tests
PspUnitTests unitTests;
- unitTests.testFastCopy();
+ //unitTests.testFastCopy();
+ unitTests.testFileSystem();
#endif
}