aboutsummaryrefslogtreecommitdiff
path: root/backends/fs/psp/psp-stream.cpp
blob: d3d6311836cb79a9930f74fe7a687dfca50dc9ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */
#ifdef __PSP__

#include <SDL/SDL_thread.h>
#include <SDL/SDL_mutex.h>

#include "backends/platform/psp/powerman.h"
#include "backends/fs/psp/psp-stream.h"

#include <errno.h>

#define MIN2(a,b) ((a < b) ? a : b)
#define MIN3(a,b,c) ( (a < b) ? (a < c ? a : c) : (b < c ? b : c) )

//#define __PSP_PRINT_TO_FILE__
//#define __PSP_DEBUG_FUNCS__ /* For debugging function calls */
//#define __PSP_DEBUG_PRINT__	/* For debug printouts */
#include "backends/platform/psp/trace.h"

PSPIoStream::PSPIoStream(const Common::String &path, bool writeMode)
		: StdioStream((void *)1), _path(path), _writeMode(writeMode),
		  _ferror(false), _pos(0),
		  _physicalPos(0), _fileSize(0), _inCache(false),
		  _cacheStartOffset(-1), _cache(0),
		  _errorSuspend(0), _errorSource(0),
		  _errorPos(0), _errorHandle(0), _suspendCount(0) {
	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() {
	DEBUG_ENTER_FUNC();

	if (PowerMan.beginCriticalSection() == PowerManager::Blocked)
		PSP_DEBUG_PRINT_FUNC("Suspended\n");

	PowerMan.unregisterSuspend(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
	
	if (_cache)
		free(_cache);

	PowerMan.endCriticalSection();
}

/* Function to open the file pointed to by the path.
 *
 */
void *PSPIoStream::open() {
	DEBUG_ENTER_FUNC();
	if (PowerMan.beginCriticalSection() == PowerManager::Blocked) {
		// No need to open. Just return the _handle resume() already opened.
		PSP_DEBUG_PRINT_FUNC("Suspended\n");
	}

	_handle = fopen(_path.c_str(), _writeMode ? "wb" : "rb"); 	// open

	if (_handle) {
		// Get the file size
		fseek((FILE *)_handle, 0, SEEK_END);	// go to the end
		_fileSize = ftell((FILE *)_handle);
		fseek((FILE *)_handle, 0, SEEK_SET);	// back to the beginning
	
		// Allocate the cache
		_cache = (char *)memalign(64, CACHE_SIZE);
	}

	PowerMan.registerSuspend(this);	 // Register with the powermanager to be suspended

	PowerMan.endCriticalSection();

	return _handle;
}

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,
				  _errorPos, _errorHandle, _suspendCount);

	return _ferror;
}

void PSPIoStream::clearErr() {
	_ferror = false;
}

bool PSPIoStream::eos() const {
	return (_pos >= _fileSize);
}

int32 PSPIoStream::pos() const {
	return _pos;
}

int32 PSPIoStream::size() const {
	return _fileSize;
}

bool PSPIoStream::seek(int32 offs, int whence) {
	DEBUG_ENTER_FUNC();
	PSP_DEBUG_PRINT_FUNC("offset[%d], whence[%d], _pos[%d], _physPos[%d]\n", offs, whence, _pos, _physicalPos);
	bool success = true;
	
	int32 posToSearchFor = 0;
	switch (whence) {
	case SEEK_CUR:
		posToSearchFor = _pos;
		break;
	case SEEK_END:
		posToSearchFor = _fileSize;	// unsure. Does it take us here or to EOS - 1?
		break;
	}
	posToSearchFor += offs;
	
	// Check for bad values
	if (posToSearchFor < 0 || posToSearchFor > _fileSize) {
		_ferror = true;
		return false;
	}
	
	// See if we can find it in cache
	if (isOffsetInCache(posToSearchFor)) {
		PSP_DEBUG_PRINT("seek offset[%d] found in cache. Cache starts[%d]\n", posToSearchFor, _cacheStartOffset);
		_inCache = true;		
	} else {	// not in cache
		_inCache = false;		
	}	
	_pos = posToSearchFor;		
	return success;
}

// for debugging
/*
void printBuffer(byte *ptr, uint32 len) {
	for (int i = 0; i < len; i++) {
		PSP_INFO_PRINT("%x ", *ptr);
		ptr++;
	}	
	PSP_INFO_PRINT("\n");
}*/

uint32 PSPIoStream::read(void *ptr, uint32 len) {
	DEBUG_ENTER_FUNC();
	PSP_DEBUG_PRINT_FUNC("filename[%s], len[%d], ptr[%p]\n", _path.c_str(), len, ptr);

	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;
	
	// 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 %d bytes from cache to %p. pos[%d] physPos[%d] cacheStart[%d]\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
			// debug
			//if (len < 10) printBuffer((byte *)ptr, len);
			return lenFromCache;			// how much we actually read
		}		
	}

	if (PowerMan.beginCriticalSection() == PowerManager::Blocked)
		PSP_DEBUG_PRINT_FUNC("Suspended\n");
	
	
	synchronizePhysicalPos();	// we need to update our physical position
	
	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 %d bytes from physicalPos[%d]. cacheStart[%d], pos[%d], fileSize[%d]\n", lenToCopyToCache, _physicalPos, _cacheStartOffset, _pos, _fileSize);
		
		size_t ret = fread(_cache, 1, lenToCopyToCache, (FILE *)_handle);
		if (ret != lenToCopyToCache) {
			PSP_ERROR("in filling cache, failed to get %d bytes. Only got %d\n", lenToCopyToCache, ret);
			_ferror = true;
			clearerr((FILE *)_handle);
		}
		_cacheStartOffset = _physicalPos;
		_inCache = true;
		
		_physicalPos += ret;
		
		PSP_DEBUG_PRINT("copying %d 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 %d bytes from file to %p. Pos[%d], physPos[%d]\n", lenFromFile, destPtr, _pos, _physicalPos);
		size_t ret = fread(destPtr, 1, lenFromFile, (FILE *)_handle);

		_physicalPos += ret;	// Update pos
		_pos = _physicalPos;

		if (ret != lenFromFile) {	// error
			PSP_ERROR("fread returned [%d] instead of len[%d]\n", ret, lenFromFile);
			_ferror = true;
			clearerr((FILE *)_handle);
			_errorSource = 4;			
		}
		_inCache = false;
	}

	PowerMan.endCriticalSection();

	// debug
	//if (len < 10) printBuffer((byte *)ptr, len);
	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 (fseek((FILE *)_handle, _pos - _physicalPos, SEEK_CUR) != 0)
			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;
}

uint32 PSPIoStream::write(const void *ptr, uint32 len) {
	DEBUG_ENTER_FUNC();
	// Check if we can access the file
	if (PowerMan.beginCriticalSection() == PowerManager::Blocked)
		PSP_DEBUG_PRINT_FUNC("Suspended\n");

	PSP_DEBUG_PRINT_FUNC("filename[%s], len[%d]\n", _path.c_str(), len);

	synchronizePhysicalPos();
	
	size_t ret = fwrite(ptr, 1, len, (FILE *)_handle);

	// If we're making the file bigger, adjust the size
	if (_physicalPos + (int)ret > _fileSize)
		_fileSize = _physicalPos + ret;
	_physicalPos += ret;
	_pos = _physicalPos;
	_inCache = false;
	_cacheStartOffset = -1;	// invalidate cache

	if (ret != len) {	// Set error
		_ferror = true;
		clearerr((FILE *)_handle);
		_pos = ftell((FILE *)_handle);	// Update pos
		_errorSource = 5;
		PSP_ERROR("fwrite returned[%d] instead of len[%d]\n", ret, len);
	}

	PowerMan.endCriticalSection();

	return ret;
}

bool PSPIoStream::flush() {
	DEBUG_ENTER_FUNC();
	// Enter critical section
	if (PowerMan.beginCriticalSection() == PowerManager::Blocked)
		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[%u]\n", ret);
	}

	PowerMan.endCriticalSection();

	return (ret == 0);
}

// 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.
//
PSPIoStream *PSPIoStream::makeFromPath(const Common::String &path, bool writeMode) {
	DEBUG_ENTER_FUNC();
	PSPIoStream *stream = new PSPIoStream(path, writeMode);

	if (stream->open() <= 0) {
		delete stream;
		stream = 0;
	}

	return stream;
}

/*
 *  Function to suspend the IO stream (called by PowerManager)
 *  we can have no output here
 */
int PSPIoStream::suspend() {
	DEBUG_ENTER_FUNC();
	_suspendCount++;

	if (_handle > 0 && _pos < 0) {	/* check for error */
		_errorSuspend = SuspendError;
		_errorPos = _pos;
		_errorHandle = _handle;
	}

	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
	}

	return 0;
}

/*
 *  Function to resume the IO stream (called by Power Manager)
 */
int PSPIoStream::resume() {
	DEBUG_ENTER_FUNC();
	int ret = 0;
	_suspendCount--;

	// We reopen our file descriptor
	_handle = fopen(_path.c_str(), _writeMode ? "wb" : "rb");
	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);
		
		_physicalPos = _pos;
		_inCache = false;

		if (ret != 0) {		// Check for problem
			_errorSuspend = ResumeError;
			_errorPos = _pos;
			_errorHandle = _handle;
		}
	}
	return ret;
}


#endif /* __PSP__ */