Merge pull request #770 from lioncash/panic

Core: Fix case where a panic alert wouldn't be shown in MemoryUtil.cpp
This commit is contained in:
shuffle2 2014-09-03 00:10:12 -07:00
commit db84a22109
1 changed files with 33 additions and 11 deletions

View File

@ -91,10 +91,10 @@ void* AllocateMemoryPages(size_t size)
#else #else
void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE,
MAP_ANON | MAP_PRIVATE, -1, 0); MAP_ANON | MAP_PRIVATE, -1, 0);
#endif
// printf("Mapped memory at %p (size %ld)\n", ptr, if (ptr == MAP_FAILED)
// (unsigned long)size); ptr = nullptr;
#endif
if (ptr == nullptr) if (ptr == nullptr)
PanicAlert("Failed to allocate raw memory"); PanicAlert("Failed to allocate raw memory");
@ -129,14 +129,20 @@ void FreeMemoryPages(void* ptr, size_t size)
{ {
if (ptr) if (ptr)
{ {
bool error_occurred = false;
#ifdef _WIN32 #ifdef _WIN32
if (!VirtualFree(ptr, 0, MEM_RELEASE)) if (!VirtualFree(ptr, 0, MEM_RELEASE))
{ error_occurred = true;
PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg());
}
#else #else
munmap(ptr, size); int retval = munmap(ptr, size);
if (retval != 0)
error_occurred = true;
#endif #endif
if (error_occurred)
PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg());
} }
} }
@ -154,24 +160,40 @@ void FreeAlignedMemory(void* ptr)
void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) void WriteProtectMemory(void* ptr, size_t size, bool allowExecute)
{ {
bool error_occurred = false;
#ifdef _WIN32 #ifdef _WIN32
DWORD oldValue; DWORD oldValue;
if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue))
PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); error_occurred = true;
#else #else
mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ);
if (retval != 0)
error_occurred = true;
#endif #endif
if (error_occurred)
PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg());
} }
void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute)
{ {
bool error_occurred = false;
#ifdef _WIN32 #ifdef _WIN32
DWORD oldValue; DWORD oldValue;
if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue))
PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); error_occurred = true;
#else #else
mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ);
if (retval != 0)
error_occurred = true;
#endif #endif
if (error_occurred)
PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg());
} }
std::string MemUsage() std::string MemUsage()