From 79a3a8d9a6beaec9af75e5908e4cc5b791178f6f Mon Sep 17 00:00:00 2001 From: PatrickvL Date: Fri, 12 Jul 2019 18:17:23 +0200 Subject: [PATCH] Extracted functions for MapPersistedMemory and UnmapPersistedMemory --- src/gui/WndMain.cpp | 47 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/gui/WndMain.cpp b/src/gui/WndMain.cpp index 3bf27c5f2..d34d01a01 100644 --- a/src/gui/WndMain.cpp +++ b/src/gui/WndMain.cpp @@ -65,9 +65,36 @@ static int gameLogoWidth, gameLogoHeight; static int splashLogoWidth, splashLogoHeight; + static HANDLE hPersistedMemory = NULL; static LPVOID PersistedMemoryAddr = nullptr; +void MapPersistedMemory() +{ + assert(((hPersistedMemory == NULL) == (PersistedMemoryAddr == nullptr)) && "Persistent memory handle and address must both be unset (or already set)!"); + + if (hPersistedMemory == NULL) { + hPersistedMemory = OpenFileMapping(FILE_MAP_READ, FALSE, "PersistentMemory"); + assert(hPersistedMemory != NULL); + + PersistedMemoryAddr = MapViewOfFile(hPersistedMemory, FILE_MAP_READ, 0, 0, 0); + assert(PersistedMemoryAddr != nullptr); + } +} + +void UnmapPersistedMemory() +{ + assert(((hPersistedMemory != NULL) == (PersistedMemoryAddr != nullptr)) && "Persistent memory handle and address must both be set (or already unset)!"); + + if (hPersistedMemory != NULL) { + UnmapViewOfFile(PersistedMemoryAddr); + PersistedMemoryAddr = nullptr; + + CloseHandle(hPersistedMemory); + hPersistedMemory = NULL; + } +} + bool g_SaveOnExit = true; void ClearSymbolCache(const char sStorageLocation[MAX_PATH]) @@ -355,16 +382,9 @@ LRESULT CALLBACK WndMain::WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lP case ID_GUI_VM_PERSIST_MEM: { if (lParam) { - hPersistedMemory = OpenFileMapping(FILE_MAP_READ, FALSE, "PersistentMemory"); - assert(hPersistedMemory != NULL); - PersistedMemoryAddr = MapViewOfFile(hPersistedMemory, FILE_MAP_READ, 0, 0, 0); - assert(PersistedMemoryAddr != nullptr); - } - else { - UnmapViewOfFile(PersistedMemoryAddr); - CloseHandle(hPersistedMemory); - PersistedMemoryAddr = nullptr; - hPersistedMemory = NULL; + MapPersistedMemory(); + } else { + UnmapPersistedMemory(); } } break; @@ -2343,12 +2363,7 @@ void WndMain::CrashMonitor(DWORD dwChildProcID) g_EmuShared->GetBootFlags(&iBootFlags); if (!iBootFlags) { - if (hPersistedMemory != NULL) { - UnmapViewOfFile(PersistedMemoryAddr); - CloseHandle(hPersistedMemory); - PersistedMemoryAddr = nullptr; - hPersistedMemory = NULL; - } + UnmapPersistedMemory(); if (dwExitCode == EXIT_SUCCESS) {// StopEmulation return; }