Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
inactive123 committed Nov 25, 2017
1 parent 0868c4e commit 0f6c453
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
41 changes: 24 additions & 17 deletions libretro-common/file/nbio/nbio_windowsmmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,30 @@ struct nbio_t
struct nbio_t* nbio_open(const char * filename, unsigned mode)
{
static const DWORD dispositions[] = { OPEN_EXISTING, CREATE_ALWAYS, OPEN_ALWAYS, OPEN_EXISTING, CREATE_ALWAYS };

bool is_write = (mode == NBIO_WRITE || mode == NBIO_UPDATE || mode == BIO_WRITE);
DWORD access = (is_write ? GENERIC_READ|GENERIC_WRITE : GENERIC_READ);
HANDLE file = CreateFile(filename, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL);

HANDLE mem;

void* ptr;
LARGE_INTEGER len;
struct nbio_t* handle;
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);
HANDLE file = CreateFile(filename, access, FILE_SHARE_ALL, NULL, dispositions[mode], FILE_ATTRIBUTE_NORMAL, NULL);

if (file == INVALID_HANDLE_VALUE) return NULL;
if (file == INVALID_HANDLE_VALUE)
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 = malloc(sizeof(struct nbio_t));

handle->file = file;
handle->is_write = is_write;
handle->len = len.QuadPart;
handle->ptr = ptr;
handle->len = len.QuadPart;
handle->ptr = ptr;

return handle;
}

Expand All @@ -66,8 +66,10 @@ void nbio_resize(struct nbio_t* handle, size_t 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 */
/* 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");
abort();
}
Expand All @@ -87,7 +89,7 @@ void nbio_resize(struct nbio_t* handle, size_t len)
handle->ptr = MapViewOfFile(mem, handle->is_write ? (FILE_MAP_READ|FILE_MAP_WRITE) : FILE_MAP_READ, 0, 0, len);
CloseHandle(mem);

if (handle->ptr == NULL)
if (!handle->ptr)
{
puts("ERROR - couldn't resize file (MapViewOfFile)");
abort();
Expand All @@ -96,7 +98,10 @@ void nbio_resize(struct nbio_t* handle, size_t len)

void* nbio_get_ptr(struct nbio_t* handle, size_t* len)
{
if (len) *len = handle->len;
if (!handle)
return NULL;
if (len)
*len = handle->len;
return handle->ptr;
}

Expand All @@ -107,6 +112,8 @@ void nbio_cancel(struct nbio_t* handle)

void nbio_free(struct nbio_t* handle)
{
if (!handle)
return;
CloseHandle(handle->file);
UnmapViewOfFile(handle->ptr);
free(handle);
Expand Down
55 changes: 28 additions & 27 deletions libretro-common/memmap/memmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
#endif

#ifdef _WIN32
void* mmap(void *addr, size_t len, int prot, int flags, int fildes, size_t offset)
void* mmap(void *addr, size_t len, int prot, int flags,
int fildes, size_t offset)
{
void *map = (void*)NULL;
HANDLE handle = INVALID_HANDLE_VALUE;
Expand All @@ -57,36 +58,34 @@ void* mmap(void *addr, size_t len, int prot, int flags, int fildes, size_t offse
{
case PROT_READ:
default:
{
handle = CreateFileMapping((HANDLE) _get_osfhandle(fildes), 0, PAGE_READONLY, 0,
len, 0);
if (!handle)
break;
map = (void*)MapViewOfFile(handle, FILE_MAP_READ, 0, 0, len);
CloseHandle(handle);
handle = CreateFileMapping((HANDLE)
_get_osfhandle(fildes), 0, PAGE_READONLY, 0,
len, 0);
if (!handle)
break;
}
map = (void*)MapViewOfFile(handle, FILE_MAP_READ, 0, 0, len);
CloseHandle(handle);
break;
case PROT_WRITE:
{
handle = CreateFileMapping((HANDLE) _get_osfhandle(fildes),0,PAGE_READWRITE,0,
len, 0);
if (!handle)
break;
map = (void*)MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, len);
CloseHandle(handle);
handle = CreateFileMapping((HANDLE)
_get_osfhandle(fildes),0,PAGE_READWRITE,0,
len, 0);
if (!handle)
break;
}
map = (void*)MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, len);
CloseHandle(handle);
break;
case PROT_READWRITE:
{
handle = CreateFileMapping((HANDLE) _get_osfhandle(fildes),0,PAGE_READWRITE,0,
len, 0);
if (!handle)
break;
map = (void*)MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, len);
CloseHandle(handle);
handle = CreateFileMapping((HANDLE)
_get_osfhandle(fildes),0,PAGE_READWRITE,0,
len, 0);
if (!handle)
break;
}
map = (void*)MapViewOfFile(handle, FILE_MAP_ALL_ACCESS, 0, 0, len);
CloseHandle(handle);
break;
}

if (map == (void*)NULL)
return((void*)MAP_FAILED);
return((void*) ((int8_t*)map + offset));
Expand All @@ -110,7 +109,8 @@ int mprotect(void *addr, size_t len, int prot)
}

#elif !defined(HAVE_MMAN)
void* mmap(void *addr, size_t len, int prot, int flags, int fildes, size_t offset)
void* mmap(void *addr, size_t len, int prot, int flags,
int fildes, size_t offset)
{
return malloc(len);
}
Expand All @@ -123,7 +123,8 @@ int munmap(void *addr, size_t len)

int mprotect(void *addr, size_t len, int prot)
{
/* stub - not really needed at this point since this codepath has no dynarecs */
/* stub - not really needed at this point
* since this codepath has no dynarecs. */
return 0;
}

Expand Down

0 comments on commit 0f6c453

Please sign in to comment.