avoid using file stat when possible. it's slow.

This commit is contained in:
Daryl Borth 2018-08-28 15:02:35 -06:00
parent b738b86176
commit 16181ef6b9
6 changed files with 1515 additions and 1560 deletions

View File

@ -23,6 +23,7 @@
#include <di/di.h>
#include <ogc/dvd.h>
#include <iso9660.h>
#include <fat.h>
#include "vbagx.h"
#include "vbasupport.h"
@ -35,7 +36,7 @@
#define THREAD_SLEEP 100
unsigned char *savebuffer;
unsigned char *savebuffer = NULL;
static mutex_t bufferLock = LWP_MUTEX_NULL;
FILE * file; // file pointer - the only one we should ever use!
bool unmountRequired[7] = { false, false, false, false, false, false, false };
@ -473,22 +474,6 @@ static char *GetExt(char *file)
return ext;
}
bool GetFileSize(int i)
{
if(browserList[i].length > 0)
return true;
struct stat filestat;
char path[MAXPATHLEN+1];
snprintf(path, MAXPATHLEN, "%s%s", browser.dir, browserList[i].filename);
if(stat(path, &filestat) < 0)
return false;
browserList[i].length = filestat.st_size;
return true;
}
void FindAndSelectLastLoadedFile ()
{
int indexFound = -1;
@ -674,7 +659,6 @@ ParseDirectory(bool waitParse, bool filter)
AddBrowserEntry();
sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level");
browserList[0].length = 0;
browserList[0].isdir = 1; // flag this as a dir
browserList[0].icon = ICON_FOLDER;
browser.numEntries++;
@ -776,7 +760,7 @@ LoadSzFile(char * filepath, unsigned char * rbuffer)
* LoadFile
***************************************************************************/
size_t
LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
LoadFile (char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent)
{
char zipbuffer[2048];
size_t size = 0, offset = 0, readsize = 0;
@ -828,7 +812,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
if (IsZipFile (zipbuffer))
{
size = UnZipBuffer ((unsigned char *)rbuffer); // unzip
size = UnZipBuffer ((unsigned char *)rbuffer, buffersize); // unzip
}
else
{
@ -836,27 +820,23 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size = ftello(file);
fseeko(file,0,SEEK_SET);
while(!feof(file))
{
// If the size requested is *less* than the filesize, only read that much - we don't want to overrun the buffer
int toread = 4096;
if (length > 0 && offset+toread > length) {
toread = length - offset;
}
ShowProgress ("Loading...", offset, size);
readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk
if(readsize <= 0)
break; // reading finished (or failed)
offset += readsize;
if (length > 0 && offset >= length) {
break;
}
if(size > buffersize) {
size = 0;
}
else {
while(!feof(file))
{
ShowProgress ("Loading...", offset, size);
readsize = fread (rbuffer + offset, 1, 4096, file); // read in next chunk
if(readsize <= 0)
break; // reading finished (or failed)
offset += readsize;
}
size = offset;
CancelAction();
}
size = offset;
CancelAction();
}
}
retry = 0;
@ -871,19 +851,7 @@ LoadFile (char * rbuffer, char *filepath, size_t length, bool silent)
size_t LoadFile(char * filepath, bool silent)
{
struct stat filestat;
if(stat(filepath, &filestat) != 0) {
return 0;
}
int size = filestat.st_size;
if(size >= SAVEBUFFERSIZE) {
return 0;
}
return LoadFile((char *)savebuffer, filepath, 0, silent);
return LoadFile((char *)savebuffer, filepath, 0, SAVEBUFFERSIZE, silent);
}
/****************************************************************************
@ -952,7 +920,6 @@ SaveFile (char * buffer, char *filepath, size_t datasize, bool silent)
// go back to checking if devices were inserted/removed
ResumeDeviceThread();
if(!silent)
CancelAction();
return written;

View File

@ -11,11 +11,8 @@
#ifndef _FILEOP_H_
#define _FILEOP_H_
#include <gccore.h>
#include <stdio.h>
#include <string.h>
#include <ogcsys.h>
#include <fat.h>
#include <unistd.h>
#define SAVEBUFFERSIZE (1024 * 1024 * 2) // leave room for IPS/UPS files and larger images
@ -31,13 +28,12 @@ char * StripDevice(char * path);
bool ChangeInterface(int device, bool silent);
bool ChangeInterface(char * filepath, bool silent);
void CreateAppPath(char * origpath);
bool GetFileSize(int i);
void FindAndSelectLastLoadedFile();
int ParseDirectory(bool waitParse = false, bool filter = true);
bool CreateDirectory(char * path);
void AllocSaveBuffer();
void FreeSaveBuffer();
size_t LoadFile(char * rbuffer, char *filepath, size_t length, bool silent);
size_t LoadFile(char * rbuffer, char *filepath, size_t length, size_t buffersize, bool silent);
size_t LoadFile(char * filepath, bool silent);
size_t LoadSzFile(char * filepath, unsigned char * rbuffer);
size_t SaveFile(char * buffer, char *filepath, size_t datasize, bool silent);

View File

@ -97,7 +97,7 @@ IsZipFile (char *buffer)
******************************************************************************/
size_t
UnZipBuffer (unsigned char *outbuffer)
UnZipBuffer (unsigned char *outbuffer, size_t buffersize)
{
PKZIPHEADER pkzip;
size_t zipoffset = 0;
@ -122,6 +122,10 @@ UnZipBuffer (unsigned char *outbuffer)
pkzip.uncompressedSize = FLIP32 (pkzip.uncompressedSize);
if(pkzip.uncompressedSize > buffersize) {
return 0;
}
ShowProgress ("Loading...", 0, pkzip.uncompressedSize);
/*** Prepare the zip stream ***/
@ -211,7 +215,7 @@ GetFirstZipFilename ()
return NULL;
// read start of ZIP
if(LoadFile (tempbuffer, filepath, ZIPCHUNK, NOTSILENT) < 35)
if(LoadFile (tempbuffer, filepath, ZIPCHUNK, ZIPCHUNK, NOTSILENT) < 35)
return NULL;
tempbuffer[28] = 0; // truncate - filename length is 2 bytes long (bytes 26-27)
@ -428,7 +432,6 @@ int SzParse(char * filepath)
sprintf(browserList[0].filename, "..");
sprintf(browserList[0].displayname, "Up One Level");
browserList[0].isdir = 1;
browserList[0].length = filelen;
browserList[0].icon = ICON_FOLDER;
// get contents and parse them into file list structure

View File

@ -12,7 +12,7 @@
int IsZipFile (char *buffer);
char * GetFirstZipFilename();
size_t UnZipBuffer (unsigned char *outbuffer);
size_t UnZipBuffer (unsigned char *outbuffer, size_t buffersize);
int SzParse(char * filepath);
size_t SzExtractFile(int i, unsigned char *buffer);
void SzClose();

View File

@ -420,7 +420,7 @@ bool SaveBatteryOrState(char * filepath, int action, bool silent)
const char* generic_goomba_error = "Cannot save SRAM in Goomba format (did not load correctly.)";
// check for goomba sram format
char* old_sram = (char*)malloc(GOOMBA_COLOR_SRAM_SIZE);
size_t br = LoadFile(old_sram, filepath, GOOMBA_COLOR_SRAM_SIZE, true);
size_t br = LoadFile(old_sram, filepath, GOOMBA_COLOR_SRAM_SIZE, GOOMBA_COLOR_SRAM_SIZE, true);
if (br >= GOOMBA_COLOR_SRAM_SIZE && goomba_is_sram(old_sram)) {
void* cleaned = goomba_cleanup(old_sram);
if (cleaned == NULL) {
@ -1005,12 +1005,12 @@ void LoadPNGBorder(const char* fallback)
char error[1024]; error[1023] = 0;
int r;
bool borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 1024*1024, SILENT);
bool borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 0, 1024*1024, SILENT);
if (!borderLoaded) {
// Try default border.png
free(borderPath);
borderPath = AllocAndGetPNGBorderPath(fallback);
borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 1024*1024, SILENT);
borderLoaded = LoadFile((char*)png_tmp_buf, borderPath, 0, 1024*1024, SILENT);
}
if (!borderLoaded) goto cleanup;
@ -1060,12 +1060,7 @@ bool LoadGBROM()
{
gbEmulatorType = GCSettings.GBHardware;
if (browserList[browser.selIndex].length > 1024*1024*8)
{
InfoPrompt("ROM size is too large (> 8 MB)");
return false;
}
gbRom = (u8 *)malloc(1024*1024*8); // 32 MB is too much for sure
gbRom = (u8 *)malloc(1024*1024*8);
if (!gbRom)
{
InfoPrompt("Unable to allocate 8 MB of memory");
@ -1082,7 +1077,7 @@ bool LoadGBROM()
if(!MakeFilePath(filepath, FILE_ROM))
return false;
gbRomSize = LoadFile ((char *)gbRom, filepath, browserList[browser.selIndex].length, NOTSILENT);
gbRomSize = LoadFile ((char *)gbRom, filepath, 0, (1024*1024*8), NOTSILENT);
}
else
{
@ -1175,12 +1170,6 @@ bool LoadVBAROM()
return false;
}
if(!GetFileSize(browser.selIndex))
{
ErrorPrompt("Error loading game!");
return false;
}
srcWidth = 0;
srcHeight = 0;

View File

@ -165,7 +165,7 @@ int VMCPULoadROM()
if(!MakeFilePath(filepath, FILE_ROM))
return 0;
GBAROMSize = LoadFile ((char *)rom, filepath, browserList[browser.selIndex].length, NOTSILENT);
GBAROMSize = LoadFile ((char *)rom, filepath, 0, (1024*1024*32), NOTSILENT);
}
else
{