(retro_file) Implement RFILE_HINT_UNBUFFERED

This commit is contained in:
Higor Eurípedes 2015-11-14 07:17:21 -03:00
parent 6d977f08a9
commit 533f41259a
3 changed files with 91 additions and 45 deletions

View File

@ -79,9 +79,11 @@ struct RFILE
SceUID fd;
#elif defined(__CELLOS_LV2__)
int fd;
#elif defined(HAVE_BUFFERED_IO)
FILE *fd;
#else
unsigned hints;
#if defined(HAVE_BUFFERED_IO)
FILE *fp;
#endif
int fd;
#endif
};
@ -92,9 +94,11 @@ int retro_get_fd(RFILE *stream)
return -1;
#if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__)
return stream->fd;
#elif defined(HAVE_BUFFERED_IO)
return fileno(stream->fd);
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fileno(stream->fp);
#endif
return stream->fd;
#endif
}
@ -113,6 +117,8 @@ RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
(void)mode_int;
(void)flags;
stream->hints = mode;
switch (mode)
{
case RFILE_MODE_READ:
@ -122,10 +128,13 @@ RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
#elif defined(__CELLOS_LV2__)
mode_int = 0777;
flags = CELL_FS_O_RDONLY;
#elif defined(HAVE_BUFFERED_IO)
mode_str = "rb";
#else
flags = O_RDONLY;
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
mode_str = "rb";
else
#endif
flags = O_RDONLY;
#endif
break;
case RFILE_MODE_WRITE:
@ -135,10 +144,13 @@ RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
#elif defined(__CELLOS_LV2__)
mode_int = 0777;
flags = CELL_FS_O_CREAT | CELL_FS_O_WRONLY | CELL_FS_O_TRUNC;
#elif defined(HAVE_BUFFERED_IO)
mode_str = "wb";
#else
flags = O_WRONLY | O_CREAT | O_TRUNC | S_IRUSR | S_IWUSR;
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
mode_str = "wb";
else
#endif
flags = O_WRONLY | O_CREAT | O_TRUNC | S_IRUSR | S_IWUSR;
#endif
break;
case RFILE_MODE_READ_WRITE:
@ -148,13 +160,18 @@ RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
#elif defined(__CELLOS_LV2__)
mode_int = 0777;
flags = CELL_FS_O_RDWR;
#elif defined(HAVE_BUFFERED_IO)
mode_str = "w+";
#else
flags = O_RDWR;
#ifdef _WIN32
flags |= O_BINARY;
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
mode_str = "w+";
else
#endif
{
flags = O_RDWR;
#ifdef _WIN32
flags |= O_BINARY;
#endif
}
#endif
break;
}
@ -163,16 +180,24 @@ RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len)
stream->fd = sceIoOpen(path, flags, mode_int);
#elif defined(__CELLOS_LV2__)
cellFsOpen(path, flags, &stream->fd, NULL, 0);
#elif defined(HAVE_BUFFERED_IO)
stream->fd = fopen(path, mode_str);
#else
stream->fd = open(path, flags);
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
{
stream->fp = fopen(path, mode_str);
if (!stream->fp)
goto error;
}
else
#endif
{
stream->fd = open(path, flags);
if (stream->fd == -1)
goto error;
}
#endif
#if defined(HAVE_BUFFERED_IO)
if (!stream->fd)
goto error;
#else
#if defined(VITA) || defined(PSP) || defined(__CELLOS_LV2__)
if (stream->fd == -1)
goto error;
#endif
@ -202,13 +227,16 @@ ssize_t retro_fseek(RFILE *stream, ssize_t offset, int whence)
if (cellFsLseek(stream->fd, offset, whence, &pos) != CELL_FS_SUCCEEDED)
return -1;
return 0;
#elif defined(HAVE_BUFFERED_IO)
return fseek(stream->fd, (long)offset, whence);
#else
ret = lseek(stream->fd, offset, whence);
if (ret == -1)
return -1;
return 0;
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fseek(stream->fp, (long)offset, whence);
else
#endif
{
ret = lseek(stream->fd, offset, whence);
return ret == -1 ? -1 : 0;
}
#endif
}
@ -223,10 +251,13 @@ ssize_t retro_ftell(RFILE *stream)
if (cellFsLseek(stream->fd, 0, CELL_FS_SEEK_CUR, &pos) != CELL_FS_SUCCEEDED)
return -1;
return 0;
#elif defined(HAVE_BUFFERED_IO)
return ftell(stream->fd);
#else
return lseek(stream->fd, 0, SEEK_CUR);
#else
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return ftell(stream->fp);
else
#endif
return lseek(stream->fd, 0, SEEK_CUR);
#endif
}
@ -246,10 +277,13 @@ ssize_t retro_fread(RFILE *stream, void *s, size_t len)
if (cellFsRead(stream->fd, s, len, &bytes_written) != CELL_FS_SUCCEEDED)
return -1;
return bytes_written;
#elif defined(HAVE_BUFFERED_IO)
return fread(s, 1, len, stream->fd);
#else
return read(stream->fd, s, len);
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fread(s, 1, len, stream->fp);
else
#endif
return read(stream->fd, s, len);
#endif
}
@ -264,10 +298,13 @@ ssize_t retro_fwrite(RFILE *stream, const void *s, size_t len)
if (cellFsWrite(stream->fd, s, len, &bytes_written) != CELL_FS_SUCCEEDED)
return -1;
return bytes_written;
#elif defined(HAVE_BUFFERED_IO)
return fwrite(s, 1, len, stream->fd);
#else
return write(stream->fd, s, len);
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
return fwrite(s, 1, len, stream->fp);
else
#endif
return write(stream->fd, s, len);
#endif
}
@ -282,12 +319,17 @@ int retro_fclose(RFILE *stream)
#elif defined(__CELLOS_LV2__)
if (stream->fd > 0)
cellFsClose(stream->fd);
#elif defined(HAVE_BUFFERED_IO)
if (stream->fd)
fclose(stream->fd);
#else
if (stream->fd > 0)
close(stream->fd);
#if defined(HAVE_BUFFERED_IO)
if ((stream->hints & RFILE_HINT_UNBUFFERED) == 0)
{
if (stream->fp)
fclose(stream->fp);
}
else
#endif
if (stream->fd > 0)
close(stream->fd);
#endif
free(stream);

View File

@ -40,7 +40,11 @@ enum
{
RFILE_MODE_READ = 0,
RFILE_MODE_WRITE,
RFILE_MODE_READ_WRITE
RFILE_MODE_READ_WRITE,
/* There is no garantee these requests will be attended. */
RFILE_HINT_UNBUFFERED = 1<<8,
RFILE_HINT_MMAP = 1<<7
};
RFILE *retro_fopen(const char *path, unsigned mode, ssize_t len);

View File

@ -401,7 +401,7 @@ void libretrodb_cursor_close(libretrodb_cursor_t *cursor)
int libretrodb_cursor_open(libretrodb_t *db, libretrodb_cursor_t *cursor,
libretrodb_query_t *q)
{
cursor->fd = retro_fopen(db->path, RFILE_MODE_READ, -1);
cursor->fd = retro_fopen(db->path, RFILE_MODE_READ | RFILE_HINT_UNBUFFERED, -1);
if (!cursor->fd)
return -errno;