Wii: Support fake-mapping on VFileFD

This commit is contained in:
Vicki Pfau 2022-03-19 16:38:12 -07:00
parent c8302da954
commit deeeb8ea56
2 changed files with 50 additions and 6 deletions

View File

@ -8,8 +8,8 @@ if(WIIDRC_LIBRARY)
add_definitions(-DWIIDRC)
endif()
set(OS_DEFINES _GNU_SOURCE COLOR_16_BIT COLOR_5_6_5 USE_VFS_FILE IOAPI_NO_64 FIXED_ROM_BUFFER)
list(APPEND CORE_VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-file.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-devlist.c)
set(OS_DEFINES _GNU_SOURCE COLOR_16_BIT COLOR_5_6_5 IOAPI_NO_64 FIXED_ROM_BUFFER)
list(APPEND CORE_VFS_SRC ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-fd.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-dirent.c ${CMAKE_SOURCE_DIR}/src/util/vfs/vfs-devlist.c)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

View File

@ -7,11 +7,13 @@
#include <fcntl.h>
#include <sys/stat.h>
#ifndef _WIN32
#ifdef _WIN32
#include <windows.h>
#elif defined(_POSIX_MAPPED_FILES)
#include <sys/mman.h>
#include <sys/time.h>
#else
#include <windows.h>
#include <mgba-util/memory.h>
#endif
#include <mgba-util/vector.h>
@ -31,6 +33,8 @@ struct VFileFD {
int fd;
#ifdef _WIN32
struct HandleMappingList handles;
#elif !defined(_POSIX_MAPPED_FILES)
bool writable;
#endif
};
@ -88,6 +92,8 @@ struct VFile* VFileFromFD(int fd) {
vfd->d.sync = _vfdSync;
#ifdef _WIN32
HandleMappingListInit(&vfd->handles, 4);
#elif !defined(_POSIX_MAPPED_FILES)
vfd->writable = false;
#endif
return &vfd->d;
@ -125,7 +131,7 @@ ssize_t _vfdWrite(struct VFile* vf, const void* buffer, size_t size) {
return write(vfd->fd, buffer, size);
}
#ifndef _WIN32
#ifdef _POSIX_MAPPED_FILES
static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
struct VFileFD* vfd = (struct VFileFD*) vf;
int mmapFlags = MAP_PRIVATE;
@ -144,7 +150,7 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
msync(memory, size, MS_SYNC);
munmap(memory, size);
}
#else
#elif defined(_WIN32)
static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
struct VFileFD* vfd = (struct VFileFD*) vf;
int createFlags = PAGE_WRITECOPY;
@ -183,6 +189,34 @@ static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
}
}
}
#else
static void* _vfdMap(struct VFile* vf, size_t size, int flags) {
struct VFileFD* vfd = (struct VFileFD*) vf;
if (flags & MAP_WRITE) {
vfd->writable = true;
}
void* mem = anonymousMemoryMap(size);
if (!mem) {
return 0;
}
off_t pos = lseek(vfd->fd, 0, SEEK_CUR);
lseek(vfd->fd, 0, SEEK_SET);
read(vfd->fd, mem, size);
lseek(vfd->fd, pos, SEEK_SET);
return mem;
}
static void _vfdUnmap(struct VFile* vf, void* memory, size_t size) {
struct VFileFD* vfd = (struct VFileFD*) vf;
if (vfd->writable) {
off_t pos = lseek(vfd->fd, 0, SEEK_CUR);
lseek(vfd->fd, 0, SEEK_SET);
write(vfd->fd, memory, size);
lseek(vfd->fd, pos, SEEK_SET);
}
mappedMemoryFree(memory, size);
}
#endif
static void _vfdTruncate(struct VFile* vf, size_t size) {
@ -210,7 +244,17 @@ static bool _vfdSync(struct VFile* vf, void* buffer, size_t size) {
futimes(vfd->fd, NULL);
#endif
if (buffer && size) {
#ifdef _POSIX_MAPPED_FILES
return msync(buffer, size, MS_ASYNC) == 0;
#else
off_t pos = lseek(vfd->fd, 0, SEEK_CUR);
lseek(vfd->fd, 0, SEEK_SET);
ssize_t res = write(vfd->fd, buffer, size);
lseek(vfd->fd, pos, SEEK_SET);
if (res < 0) {
return false;
}
#endif
}
return fsync(vfd->fd) == 0;
#else