Implement new nbio interface implementations

This commit is contained in:
twinaphex 2017-11-25 05:51:33 +01:00
parent 7a772b9cd4
commit d4c3108b88
7 changed files with 596 additions and 333 deletions

View File

@ -184,6 +184,9 @@ OBJ += frontend/frontend.o \
setting_list.o \ setting_list.o \
list_special.o \ list_special.o \
$(LIBRETRO_COMM_DIR)/file/nbio/nbio_stdio.o \ $(LIBRETRO_COMM_DIR)/file/nbio/nbio_stdio.o \
$(LIBRETRO_COMM_DIR)/file/nbio/nbio_linux.o \
$(LIBRETRO_COMM_DIR)/file/nbio/nbio_unixmmap.o \
$(LIBRETRO_COMM_DIR)/file/nbio/nbio_windowsmmap.o \
$(LIBRETRO_COMM_DIR)/file/nbio/nbio_intf.o \ $(LIBRETRO_COMM_DIR)/file/nbio/nbio_intf.o \
$(LIBRETRO_COMM_DIR)/file/file_path.o \ $(LIBRETRO_COMM_DIR)/file/file_path.o \
file_path_special.o \ file_path_special.o \

View File

@ -822,6 +822,9 @@ FILE
#include "../list_special.c" #include "../list_special.c"
#include "../libretro-common/string/stdstring.c" #include "../libretro-common/string/stdstring.c"
#include "../libretro-common/file/nbio/nbio_stdio.c" #include "../libretro-common/file/nbio/nbio_stdio.c"
#include "../libretro-common/file/nbio/nbio_linux.c"
#include "../libretro-common/file/nbio/nbio_unixmmap.c"
#include "../libretro-common/file/nbio/nbio_windowsmmap.c"
#include "../libretro-common/file/nbio/nbio_intf.c" #include "../libretro-common/file/nbio/nbio_intf.c"
/*============================================================ /*============================================================

View File

@ -1,11 +1,44 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nbio_intf.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <file/nbio.h> #include <file/nbio.h>
extern nbio_intf_t nbio_linux;
extern nbio_intf_t nbio_mmap_unix;
extern nbio_intf_t nbio_mmap_win32;
extern nbio_intf_t nbio_stdio; extern nbio_intf_t nbio_stdio;
#if defined(HAVE_MMAP) && defined(_linux__)
static nbio_intf_t *internal_nbio = &nbio_linux;
#elif defined(HAVE_MMAP) && defined(BSD) && !defined(__MACH__)
static nbio_intf_t *internal_nbio = &nbio_mmap_unix;
#elif defined(HAVE_MMAP) && defined(_WIN32)
static nbio_intf_t *internal_nbio = &nbio_mmap_win32;
#else
static nbio_intf_t *internal_nbio = &nbio_stdio; static nbio_intf_t *internal_nbio = &nbio_stdio;
#endif
struct nbio_t* nbio_open(const char * filename, unsigned mode) struct nbio_t* nbio_open(const char * filename, unsigned mode)
{ {

View File

@ -1,3 +1,27 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nbio_linux.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if defined(__linux__)
#define _GNU_SOURCE #define _GNU_SOURCE
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -23,7 +47,6 @@ struct nbio_t
size_t len; size_t len;
}; };
/* there's also a Unix AIO thingy, but it's not in glibc /* there's also a Unix AIO thingy, but it's not in glibc
* and we don't want more dependencies */ * and we don't want more dependencies */
@ -53,7 +76,7 @@ static int io_getevents(aio_context_t ctx, long min_nr, long nr,
return syscall(__NR_io_getevents, ctx, min_nr, nr, events, timeout); return syscall(__NR_io_getevents, ctx, min_nr, nr, events, timeout);
} }
struct nbio_t* nbio_open(const char * filename, unsigned mode) static struct nbio_t* nbio_linux_open(const char * filename, unsigned mode)
{ {
static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC }; static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC };
@ -101,17 +124,17 @@ static void nbio_begin_op(struct nbio_t* handle, uint16_t op)
handle->busy = true; handle->busy = true;
} }
void nbio_begin_read(struct nbio_t* handle) static void nbio_linux_begin_read(struct nbio_t* handle)
{ {
nbio_begin_op(handle, IOCB_CMD_PREAD); nbio_begin_op(handle, IOCB_CMD_PREAD);
} }
void nbio_begin_write(struct nbio_t* handle) static void nbio_linux_begin_write(struct nbio_t* handle)
{ {
nbio_begin_op(handle, IOCB_CMD_PWRITE); nbio_begin_op(handle, IOCB_CMD_PWRITE);
} }
bool nbio_iterate(struct nbio_t* handle) static bool nbio_linux_iterate(struct nbio_t* handle)
{ {
if (handle->busy) if (handle->busy)
{ {
@ -122,7 +145,7 @@ bool nbio_iterate(struct nbio_t* handle)
return !handle->busy; return !handle->busy;
} }
void nbio_resize(struct nbio_t* handle, size_t len) static void nbio_linux_resize(struct nbio_t* handle, size_t len)
{ {
if (!handle) if (!handle)
return; return;
@ -146,7 +169,7 @@ void nbio_resize(struct nbio_t* handle, size_t len)
handle->len = len; handle->len = len;
} }
void* nbio_get_ptr(struct nbio_t* handle, size_t* len) static void *nbio_linux_get_ptr(struct nbio_t* handle, size_t* len)
{ {
if (!handle) if (!handle)
return NULL; return NULL;
@ -157,7 +180,7 @@ void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
return NULL; return NULL;
} }
void nbio_cancel(struct nbio_t* handle) static void nbio_linux_cancel(struct nbio_t* handle)
{ {
if (!handle) if (!handle)
return; return;
@ -170,7 +193,7 @@ void nbio_cancel(struct nbio_t* handle)
} }
} }
void nbio_free(struct nbio_t* handle) static void nbio_linux_free(struct nbio_t* handle)
{ {
if (!handle) if (!handle)
return; return;
@ -180,3 +203,17 @@ void nbio_free(struct nbio_t* handle)
free(handle->ptr); free(handle->ptr);
free(handle); free(handle);
} }
nbio_intf_t nbio_linux = {
nbio_linux_open,
nbio_linux_begin_read,
nbio_linux_begin_write,
nbio_linux_iterate,
nbio_linux_resize,
nbio_linux_get_ptr,
nbio_linux_cancel,
nbio_linux_free,
"nbio_linux",
};
#endif

View File

@ -1,239 +1,261 @@
#include <stdio.h> /* Copyright (C) 2010-2017 The RetroArch team
#include <stdlib.h> *
* ---------------------------------------------------------------------------------------
#include <file/nbio.h> * The following license statement only applies to this file (nbio_stdio.c).
#include <encodings/utf.h> * ---------------------------------------------------------------------------------------
*
/* Assume W-functions do not work below VC2005 and Xbox platforms */ * Permission is hereby granted, free of charge,
#if defined(_MSC_VER) && _MSC_VER < 1400 || defined(_XBOX) * to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
#ifndef LEGACY_WIN32 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
#define LEGACY_WIN32 * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
#endif *
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#endif *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
struct nbio_t * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
{ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
FILE* f; * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
void* data; * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
size_t progress; * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
size_t len; */
/*
* possible values: #include <stdio.h>
* NBIO_READ, NBIO_WRITE - obvious #include <stdlib.h>
* -1 - currently doing nothing
* -2 - the pointer was reallocated since the last operation #include <file/nbio.h>
*/ #include <encodings/utf.h>
signed char op;
signed char mode; /* Assume W-functions do not work below VC2005 and Xbox platforms */
}; #if defined(_MSC_VER) && _MSC_VER < 1400 || defined(_XBOX)
#if !defined(_WIN32) || defined(LEGACY_WIN32) #ifndef LEGACY_WIN32
static const char * modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" }; #define LEGACY_WIN32
#else #endif
static const wchar_t * modes[]={ L"rb", L"wb", L"r+b", L"rb", L"wb", L"r+b" };
#endif #endif
static struct nbio_t* nbio_stdio_open(const char * filename, unsigned mode) struct nbio_t
{ {
void *buf = NULL; FILE* f;
struct nbio_t* handle = NULL; void* data;
size_t len = 0; size_t progress;
#if !defined(_WIN32) || defined(LEGACY_WIN32) size_t len;
FILE* f = fopen(filename, modes[mode]); /*
#else * possible values:
wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename); * NBIO_READ, NBIO_WRITE - obvious
FILE* f = _wfopen(filename_wide, modes[mode]); * -1 - currently doing nothing
* -2 - the pointer was reallocated since the last operation
if (filename_wide) */
free(filename_wide); signed char op;
#endif signed char mode;
if (!f) };
return NULL;
#if !defined(_WIN32) || defined(LEGACY_WIN32)
handle = (struct nbio_t*)malloc(sizeof(struct nbio_t)); static const char *stdio_modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" };
#else
if (!handle) static const wchar_t *stdio_modes[]={ L"rb", L"wb", L"r+b", L"rb", L"wb", L"r+b" };
goto error; #endif
handle->f = f; static struct nbio_t* nbio_stdio_open(const char * filename, unsigned mode)
{
switch (mode) void *buf = NULL;
{ struct nbio_t* handle = NULL;
case NBIO_WRITE: size_t len = 0;
case BIO_WRITE: #if !defined(_WIN32) || defined(LEGACY_WIN32)
break; FILE* f = fopen(filename, stdio_modes[mode]);
default: #else
fseek(handle->f, 0, SEEK_END); wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename);
len = ftell(handle->f); FILE* f = _wfopen(filename_wide, stdio_modes[mode]);
break;
} if (filename_wide)
free(filename_wide);
handle->mode = mode; #endif
if (!f)
if (len) return NULL;
buf = malloc(len);
handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
if (len && !buf)
goto error; if (!handle)
goto error;
handle->data = buf;
handle->len = len; handle->f = f;
handle->progress = handle->len;
handle->op = -2; switch (mode)
{
return handle; case NBIO_WRITE:
case BIO_WRITE:
error: break;
if (handle) default:
free(handle); fseek(handle->f, 0, SEEK_END);
fclose(f); len = ftell(handle->f);
return NULL; break;
} }
static void nbio_stdio_begin_read(struct nbio_t* handle) handle->mode = mode;
{
if (!handle) if (len)
return; buf = malloc(len);
if (handle->op >= 0) if (len && !buf)
{ goto error;
puts("ERROR - attempted file read operation while busy");
abort(); handle->data = buf;
} handle->len = len;
handle->progress = handle->len;
fseek(handle->f, 0, SEEK_SET); handle->op = -2;
handle->op = NBIO_READ; return handle;
handle->progress = 0;
} error:
if (handle)
static void nbio_stdio_begin_write(struct nbio_t* handle) free(handle);
{ fclose(f);
if (!handle) return NULL;
return; }
if (handle->op >= 0) static void nbio_stdio_begin_read(struct nbio_t* handle)
{ {
puts("ERROR - attempted file write operation while busy"); if (!handle)
abort(); return;
}
if (handle->op >= 0)
fseek(handle->f, 0, SEEK_SET); {
handle->op = NBIO_WRITE; puts("ERROR - attempted file read operation while busy");
handle->progress = 0; abort();
} }
static bool nbio_stdio_iterate(struct nbio_t* handle) fseek(handle->f, 0, SEEK_SET);
{
size_t amount = 65536; handle->op = NBIO_READ;
handle->progress = 0;
if (!handle) }
return false;
static void nbio_stdio_begin_write(struct nbio_t* handle)
if (amount > handle->len - handle->progress) {
amount = handle->len - handle->progress; if (!handle)
return;
switch (handle->op)
{ if (handle->op >= 0)
case NBIO_READ: {
if (handle->mode == BIO_READ) puts("ERROR - attempted file write operation while busy");
{ abort();
amount = handle->len; }
fread((char*)handle->data, 1, amount, handle->f);
} fseek(handle->f, 0, SEEK_SET);
else handle->op = NBIO_WRITE;
fread((char*)handle->data + handle->progress, 1, amount, handle->f); handle->progress = 0;
break; }
case NBIO_WRITE:
if (handle->mode == BIO_WRITE) static bool nbio_stdio_iterate(struct nbio_t* handle)
{ {
size_t written = 0; size_t amount = 65536;
amount = handle->len;
written = fwrite((char*)handle->data, 1, amount, handle->f); if (!handle)
if (written != amount) return false;
return false;
} if (amount > handle->len - handle->progress)
else amount = handle->len - handle->progress;
fwrite((char*)handle->data + handle->progress, 1, amount, handle->f);
break; switch (handle->op)
} {
case NBIO_READ:
handle->progress += amount; if (handle->mode == BIO_READ)
{
if (handle->progress == handle->len) amount = handle->len;
handle->op = -1; fread((char*)handle->data, 1, amount, handle->f);
return (handle->op < 0); }
} else
fread((char*)handle->data + handle->progress, 1, amount, handle->f);
static void nbio_stdio_resize(struct nbio_t* handle, size_t len) break;
{ case NBIO_WRITE:
if (!handle) if (handle->mode == BIO_WRITE)
return; {
size_t written = 0;
if (handle->op >= 0) amount = handle->len;
{ written = fwrite((char*)handle->data, 1, amount, handle->f);
puts("ERROR - attempted file resize operation while busy"); if (written != amount)
abort(); return false;
} }
if (len < handle->len) else
{ fwrite((char*)handle->data + handle->progress, 1, amount, handle->f);
puts("ERROR - attempted file shrink operation, not implemented"); break;
abort(); }
}
handle->progress += amount;
handle->len = len;
handle->data = realloc(handle->data, handle->len); if (handle->progress == handle->len)
handle->op = -1; handle->op = -1;
handle->progress = handle->len; return (handle->op < 0);
} }
static void *nbio_stdio_get_ptr(struct nbio_t* handle, size_t* len) static void nbio_stdio_resize(struct nbio_t* handle, size_t len)
{ {
if (!handle) if (!handle)
return NULL; return;
if (len)
*len = handle->len; if (handle->op >= 0)
if (handle->op == -1) {
return handle->data; puts("ERROR - attempted file resize operation while busy");
return NULL; abort();
} }
if (len < handle->len)
static void nbio_stdio_cancel(struct nbio_t* handle) {
{ puts("ERROR - attempted file shrink operation, not implemented");
if (!handle) abort();
return; }
handle->op = -1; handle->len = len;
handle->progress = handle->len; handle->data = realloc(handle->data, handle->len);
} handle->op = -1;
handle->progress = handle->len;
static void nbio_stdio_free(struct nbio_t* handle) }
{
if (!handle) static void *nbio_stdio_get_ptr(struct nbio_t* handle, size_t* len)
return; {
if (handle->op >= 0) if (!handle)
{ return NULL;
puts("ERROR - attempted free() while busy"); if (len)
abort(); *len = handle->len;
} if (handle->op == -1)
fclose(handle->f); return handle->data;
free(handle->data); return NULL;
}
handle->f = NULL;
handle->data = NULL; static void nbio_stdio_cancel(struct nbio_t* handle)
free(handle); {
} if (!handle)
return;
nbio_intf_t nbio_stdio = {
nbio_stdio_open, handle->op = -1;
nbio_stdio_begin_read, handle->progress = handle->len;
nbio_stdio_begin_write, }
nbio_stdio_iterate,
nbio_stdio_resize, static void nbio_stdio_free(struct nbio_t* handle)
nbio_stdio_get_ptr, {
nbio_stdio_cancel, if (!handle)
nbio_stdio_free, return;
"nbio_stdio", if (handle->op >= 0)
}; {
puts("ERROR - attempted free() while busy");
abort();
}
fclose(handle->f);
free(handle->data);
handle->f = NULL;
handle->data = NULL;
free(handle);
}
nbio_intf_t nbio_stdio = {
nbio_stdio_open,
nbio_stdio_begin_read,
nbio_stdio_begin_write,
nbio_stdio_iterate,
nbio_stdio_resize,
nbio_stdio_get_ptr,
nbio_stdio_cancel,
nbio_stdio_free,
"nbio_stdio",
};

View File

@ -1,3 +1,27 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nbio_unixmmap.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if defined(HAVE_MMAP) && defined(__linux__)
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -19,7 +43,7 @@ struct nbio_t
void* ptr; void* ptr;
}; };
struct nbio_t* nbio_open(const char * filename, unsigned mode) static struct nbio_t* nbio_mmap_unix_open(const char * filename, unsigned mode)
{ {
static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC }; static const int o_flags[] = { O_RDONLY, O_RDWR|O_CREAT|O_TRUNC, O_RDWR, O_RDONLY, O_RDWR|O_CREAT|O_TRUNC };
static const int map_flags[] = { PROT_READ, PROT_WRITE|PROT_READ, PROT_WRITE|PROT_READ, PROT_READ, PROT_WRITE|PROT_READ }; static const int map_flags[] = { PROT_READ, PROT_WRITE|PROT_READ, PROT_WRITE|PROT_READ, PROT_READ, PROT_WRITE|PROT_READ };
@ -49,22 +73,22 @@ struct nbio_t* nbio_open(const char * filename, unsigned mode)
return handle; return handle;
} }
void nbio_begin_read(struct nbio_t* handle) static void nbio_mmap_unix_begin_read(struct nbio_t* handle)
{ {
/* not needed */ /* not needed */
} }
void nbio_begin_write(struct nbio_t* handle) static void nbio_mmap_unix_begin_write(struct nbio_t* handle)
{ {
/* not needed */ /* not needed */
} }
bool nbio_iterate(struct nbio_t* handle) static bool nbio_mmap_unix_iterate(struct nbio_t* handle)
{ {
return true; /* not needed */ return true; /* not needed */
} }
void nbio_resize(struct nbio_t* handle, size_t len) static void nbio_mmap_unix_resize(struct nbio_t* handle, size_t len)
{ {
if (len < handle->len) if (len < handle->len)
{ {
@ -94,7 +118,7 @@ void nbio_resize(struct nbio_t* handle, size_t len)
} }
} }
void* nbio_get_ptr(struct nbio_t* handle, size_t* len) static void *nbio_mmap_unix_get_ptr(struct nbio_t* handle, size_t* len)
{ {
if (!handle) if (!handle)
return NULL; return NULL;
@ -103,12 +127,12 @@ void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
return handle->ptr; return handle->ptr;
} }
void nbio_cancel(struct nbio_t* handle) static void nbio_mmap_unix_cancel(struct nbio_t* handle)
{ {
/* not needed */ /* not needed */
} }
void nbio_free(struct nbio_t* handle) static void nbio_mmap_unix_free(struct nbio_t* handle)
{ {
if (!handle) if (!handle)
return; return;
@ -116,3 +140,17 @@ void nbio_free(struct nbio_t* handle)
munmap(handle->ptr, handle->len); munmap(handle->ptr, handle->len);
free(handle); free(handle);
} }
nbio_intf_t nbio_mmap_unix = {
nbio_mmap_unix_open,
nbio_mmap_unix_begin_read,
nbio_mmap_unix_begin_write,
nbio_mmap_unix_iterate,
nbio_mmap_unix_resize,
nbio_mmap_unix_get_ptr,
nbio_mmap_unix_cancel,
nbio_mmap_unix_free,
"nbio_mmap_unix",
};
#endif

View File

@ -1,10 +1,32 @@
/* Copyright (C) 2010-2017 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (nbio_windowsmmap.c).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#if defined(HAVE_MMAP) && defined(_WIN32)
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <file/nbio.h> #include <file/nbio.h>
#include <encodings/utf.h> #include <encodings/utf.h>
#include <windows.h>
/* Assume W-functions do not work below VC2005 and Xbox platforms */ /* Assume W-functions do not work below VC2005 and Xbox platforms */
#if defined(_MSC_VER) && _MSC_VER < 1400 || defined(_XBOX) #if defined(_MSC_VER) && _MSC_VER < 1400 || defined(_XBOX)
@ -17,122 +39,227 @@
struct nbio_t struct nbio_t
{ {
HANDLE file; FILE* f;
bool is_write; void* data;
size_t progress;
size_t len; size_t len;
void* ptr; /*
* possible values:
* NBIO_READ, NBIO_WRITE - obvious
* -1 - currently doing nothing
* -2 - the pointer was reallocated since the last operation
*/
signed char op;
signed char mode;
}; };
#define FILE_SHARE_ALL (FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE)
struct nbio_t* nbio_open(const char * filename, unsigned mode)
{
static const DWORD dispositions[] = { OPEN_EXISTING, CREATE_ALWAYS, OPEN_ALWAYS, OPEN_EXISTING, CREATE_ALWAYS };
HANDLE mem;
LARGE_INTEGER len;
struct nbio_t* handle = NULL;
void* ptr = NULL;
bool is_write = (mode == NBIO_WRITE || mode == NBIO_UPDATE || mode == BIO_WRITE);
DWORD access = (is_write ? GENERIC_READ|GENERIC_WRITE : GENERIC_READ);
#if !defined(_WIN32) || defined(LEGACY_WIN32) #if !defined(_WIN32) || defined(LEGACY_WIN32)
HANDLE file = CreateFile(filename, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL); static const char * modes[]={ "rb", "wb", "r+b", "rb", "wb", "r+b" };
#else
static const wchar_t * modes[]={ L"rb", L"wb", L"r+b", L"rb", L"wb", L"r+b" };
#endif
static struct nbio_t* nbio_mmap_win32_open(const char * filename, unsigned mode)
{
void *buf = NULL;
struct nbio_t* handle = NULL;
size_t len = 0;
#if !defined(_WIN32) || defined(LEGACY_WIN32)
FILE* f = fopen(filename, modes[mode]);
#else #else
wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename); wchar_t *filename_wide = utf8_to_utf16_string_alloc(filename);
HANDLE file = CreateFileW(filename_wide, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL); FILE* f = _wfopen(filename_wide, modes[mode]);
if (filename_wide) if (filename_wide)
free(filename_wide); free(filename_wide);
#endif #endif
if (!f)
if (file == INVALID_HANDLE_VALUE)
return NULL; return NULL;
GetFileSizeEx(file, &len);
mem = CreateFileMapping(file, NULL, is_write ? PAGE_READWRITE : PAGE_READONLY, 0, 0, NULL);
ptr = MapViewOfFile(mem, is_write ? (FILE_MAP_READ|FILE_MAP_WRITE) : FILE_MAP_READ, 0, 0, len.QuadPart);
CloseHandle(mem);
handle = malloc(sizeof(struct nbio_t));
handle->file = file; handle = (struct nbio_t*)malloc(sizeof(struct nbio_t));
handle->is_write = is_write;
handle->len = len.QuadPart; if (!handle)
handle->ptr = ptr; goto error;
handle->f = f;
switch (mode)
{
case NBIO_WRITE:
case BIO_WRITE:
break;
default:
fseek(handle->f, 0, SEEK_END);
len = ftell(handle->f);
break;
}
handle->mode = mode;
if (len)
buf = malloc(len);
if (len && !buf)
goto error;
handle->data = buf;
handle->len = len;
handle->progress = handle->len;
handle->op = -2;
return handle; return handle;
error:
if (handle)
free(handle);
fclose(f);
return NULL;
} }
void nbio_begin_read(struct nbio_t* handle) static void nbio_mmap_win32_begin_read(struct nbio_t* handle)
{ {
/* not needed */ if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted file read operation while busy");
abort();
}
fseek(handle->f, 0, SEEK_SET);
handle->op = NBIO_READ;
handle->progress = 0;
} }
void nbio_begin_write(struct nbio_t* handle) static void nbio_mmap_win32_begin_write(struct nbio_t* handle)
{ {
/* not needed */ if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted file write operation while busy");
abort();
}
fseek(handle->f, 0, SEEK_SET);
handle->op = NBIO_WRITE;
handle->progress = 0;
} }
bool nbio_iterate(struct nbio_t* handle) static bool nbio_mmap_win32_iterate(struct nbio_t* handle)
{ {
return true; /* not needed */ size_t amount = 65536;
if (!handle)
return false;
if (amount > handle->len - handle->progress)
amount = handle->len - handle->progress;
switch (handle->op)
{
case NBIO_READ:
if (handle->mode == BIO_READ)
{
amount = handle->len;
fread((char*)handle->data, 1, amount, handle->f);
}
else
fread((char*)handle->data + handle->progress, 1, amount, handle->f);
break;
case NBIO_WRITE:
if (handle->mode == BIO_WRITE)
{
size_t written = 0;
amount = handle->len;
written = fwrite((char*)handle->data, 1, amount, handle->f);
if (written != amount)
return false;
}
else
fwrite((char*)handle->data + handle->progress, 1, amount, handle->f);
break;
}
handle->progress += amount;
if (handle->progress == handle->len)
handle->op = -1;
return (handle->op < 0);
} }
void nbio_resize(struct nbio_t* handle, size_t len) static void nbio_mmap_win32_resize(struct nbio_t* handle, size_t len)
{ {
LARGE_INTEGER len_li; if (!handle)
HANDLE mem; return;
if (handle->op >= 0)
{
puts("ERROR - attempted file resize operation while busy");
abort();
}
if (len < handle->len) if (len < handle->len)
{ {
/* this works perfectly fine if this check is removed,
* but it won't work on other nbio implementations */
/* therefore, it's blocked so nobody accidentally
* relies on it. */
puts("ERROR - attempted file shrink operation, not implemented"); puts("ERROR - attempted file shrink operation, not implemented");
abort(); abort();
} }
len_li.QuadPart = len; handle->len = len;
SetFilePointerEx(handle->file, len_li, NULL, FILE_BEGIN); handle->data = realloc(handle->data, handle->len);
handle->op = -1;
if (!SetEndOfFile(handle->file)) handle->progress = handle->len;
{
puts("ERROR - couldn't resize file (SetEndOfFile)");
abort(); /* this one returns void and I can't find any other way for it to report failure */
}
handle->len = len;
UnmapViewOfFile(handle->ptr);
mem = CreateFileMapping(handle->file, NULL, handle->is_write ? PAGE_READWRITE : PAGE_READONLY, 0, 0, NULL);
handle->ptr = MapViewOfFile(mem, handle->is_write ? (FILE_MAP_READ|FILE_MAP_WRITE) : FILE_MAP_READ, 0, 0, len);
CloseHandle(mem);
if (!handle->ptr)
{
puts("ERROR - couldn't resize file (MapViewOfFile)");
abort();
}
} }
void* nbio_get_ptr(struct nbio_t* handle, size_t* len) static void *nbio_mmap_win32_get_ptr(struct nbio_t* handle, size_t* len)
{ {
if (!handle) if (!handle)
return NULL; return NULL;
if (len) if (len)
*len = handle->len; *len = handle->len;
return handle->ptr; if (handle->op == -1)
return handle->data;
return NULL;
} }
void nbio_cancel(struct nbio_t* handle) static void nbio_mmap_win32_cancel(struct nbio_t* handle)
{
/* not needed */
}
void nbio_free(struct nbio_t* handle)
{ {
if (!handle) if (!handle)
return; return;
CloseHandle(handle->file);
UnmapViewOfFile(handle->ptr); handle->op = -1;
handle->progress = handle->len;
}
static void nbio_mmap_win32_free(struct nbio_t* handle)
{
if (!handle)
return;
if (handle->op >= 0)
{
puts("ERROR - attempted free() while busy");
abort();
}
fclose(handle->f);
free(handle->data);
handle->f = NULL;
handle->data = NULL;
free(handle); free(handle);
} }
nbio_intf_t nbio_mmap_win32 = {
nbio_mmap_win32_open,
nbio_mmap_win32_begin_read,
nbio_mmap_win32_begin_write,
nbio_mmap_win32_iterate,
nbio_mmap_win32_resize,
nbio_mmap_win32_get_ptr,
nbio_mmap_win32_cancel,
nbio_mmap_win32_free,
"nbio_mmap_win32",
};
#endif