Make writing to RO mapped memory perform COW
Jeffrey Pfau jeffrey@endrift.com
Sun, 20 Jul 2014 01:36:18 -0700
6 files changed,
14 insertions(+),
14 deletions(-)
M
src/gba/gba-savedata.c
→
src/gba/gba-savedata.c
@@ -85,7 +85,7 @@ end = savedata->vf->seek(savedata->vf, 0, SEEK_END);
if (end < SIZE_CART_FLASH512) { savedata->vf->truncate(savedata->vf, SIZE_CART_FLASH1M); } - savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MEMORY_WRITE); + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_FLASH1M, MAP_WRITE); } savedata->currentBank = savedata->data;@@ -110,7 +110,7 @@ end = savedata->vf->seek(savedata->vf, 0, SEEK_END);
if (end < SIZE_CART_EEPROM) { savedata->vf->truncate(savedata->vf, SIZE_CART_EEPROM); } - savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, MEMORY_WRITE); + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_EEPROM, MAP_WRITE); } if (end < SIZE_CART_EEPROM) { memset(&savedata->data[end], 0xFF, SIZE_CART_EEPROM - end);@@ -133,7 +133,7 @@ end = savedata->vf->seek(savedata->vf, 0, SEEK_END);
if (end < SIZE_CART_SRAM) { savedata->vf->truncate(savedata->vf, SIZE_CART_SRAM); } - savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_SRAM, MEMORY_WRITE); + savedata->data = savedata->vf->map(savedata->vf, SIZE_CART_SRAM, MAP_WRITE); } if (end < SIZE_CART_SRAM) {
M
src/gba/gba-serialize.c
→
src/gba/gba-serialize.c
@@ -99,7 +99,7 @@ return true;
} struct GBASerializedState* GBAMapState(struct VFile* vf) { - return vf->map(vf, sizeof(struct GBASerializedState), MEMORY_WRITE); + return vf->map(vf, sizeof(struct GBASerializedState), MAP_WRITE); } void GBAUnmapState(struct VFile* vf, struct GBASerializedState* state) {
M
src/gba/gba.c
→
src/gba/gba.c
@@ -386,7 +386,7 @@ void GBALoadROM(struct GBA* gba, struct VFile* vf, struct VFile* sav, const char* fname) {
gba->romVf = vf; gba->pristineRomSize = vf->seek(vf, 0, SEEK_END); vf->seek(vf, 0, SEEK_SET); - gba->pristineRom = vf->map(vf, SIZE_CART0, MEMORY_READ); + gba->pristineRom = vf->map(vf, SIZE_CART0, MAP_READ); gba->memory.rom = gba->pristineRom; gba->activeFile = fname; gba->memory.romSize = gba->pristineRomSize;@@ -398,7 +398,7 @@ }
void GBALoadBIOS(struct GBA* gba, struct VFile* vf) { gba->biosVf = vf; - gba->memory.bios = vf->map(vf, SIZE_BIOS, MEMORY_READ); + gba->memory.bios = vf->map(vf, SIZE_BIOS, MAP_READ); gba->memory.fullBios = 1; uint32_t checksum = GBAChecksum(gba->memory.bios, SIZE_BIOS); GBALog(gba, GBA_LOG_DEBUG, "BIOS Checksum: 0x%X", checksum);
M
src/util/memory.h
→
src/util/memory.h
@@ -3,9 +3,6 @@ #define MEMORY_H
#include "common.h" -#define MEMORY_READ 1 -#define MEMORY_WRITE 2 - void* anonymousMemoryMap(size_t size); void mappedMemoryFree(void* memory, size_t size);
M
src/util/vfs.c
→
src/util/vfs.c
@@ -105,7 +105,7 @@ #ifndef _WIN32
static void* _vfdMap(struct VFile* vf, size_t size, int flags) { struct VFileFD* vfd = (struct VFileFD*) vf; int mmapFlags = MAP_PRIVATE; - if (flags & MEMORY_WRITE) { + if (flags & MAP_WRITE) { mmapFlags = MAP_SHARED; } return mmap(0, size, PROT_READ | PROT_WRITE, mmapFlags, vfd->fd, 0);@@ -118,9 +118,9 @@ }
#else static void* _vfdMap(struct VFile* vf, size_t size, int flags) { struct VFileFD* vfd = (struct VFileFD*) vf; - int createFlags = PAGE_READONLY; - int mapFiles = FILE_MAP_READ; - if (flags & MEMORY_WRITE) { + int createFlags = PAGE_WRITECOPY; + int mapFiles = FILE_MAP_COPY; + if (flags & MAP_WRITE) { createFlags = PAGE_READWRITE; mapFiles = FILE_MAP_WRITE; }
M
src/util/vfs.h
→
src/util/vfs.h
@@ -3,7 +3,10 @@ #define VFS_H
#include "common.h" -#include "memory.h" +enum { + MAP_READ = 1, + MAP_WRITE = 2 +}; struct VFile { bool (*close)(struct VFile* vf);