From 34692ab826abc8f8faa61bdb2280b742424528f1 Mon Sep 17 00:00:00 2001 From: "Jasper St. Pierre" Date: Sat, 7 Dec 2013 15:14:29 -0500 Subject: Remove unnecessary Src/ folders --- Source/Core/Common/MemoryUtil.cpp | 197 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 Source/Core/Common/MemoryUtil.cpp (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp new file mode 100644 index 0000000000..ce1c5d8a54 --- /dev/null +++ b/Source/Core/Common/MemoryUtil.cpp @@ -0,0 +1,197 @@ +// Copyright 2013 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + + +#include "Common.h" +#include "MemoryUtil.h" +#include "StringUtil.h" + +#ifdef _WIN32 +#include +#include +#else +#include +#include +#endif + +#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) +#include +#define PAGE_MASK (getpagesize() - 1) +#define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) +#endif + +// This is purposely not a full wrapper for virtualalloc/mmap, but it +// provides exactly the primitive operations that Dolphin needs. + +void* AllocateExecutableMemory(size_t size, bool low) +{ +#if defined(_WIN32) + void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); +#else + static char *map_hint = 0; +#if defined(__x86_64__) && !defined(MAP_32BIT) + // This OS has no flag to enforce allocation below the 4 GB boundary, + // but if we hint that we want a low address it is very likely we will + // get one. + // An older version of this code used MAP_FIXED, but that has the side + // effect of discarding already mapped pages that happen to be in the + // requested virtual memory range (such as the emulated RAM, sometimes). + if (low && (!map_hint)) + map_hint = (char*)round_page(512*1024*1024); /* 0.5 GB rounded up to the next page */ +#endif + void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_ANON | MAP_PRIVATE +#if defined(__x86_64__) && defined(MAP_32BIT) + | (low ? MAP_32BIT : 0) +#endif + , -1, 0); +#endif /* defined(_WIN32) */ + + // printf("Mapped executable memory at %p (size %ld)\n", ptr, + // (unsigned long)size); + +#if defined(__FreeBSD__) + if (ptr == MAP_FAILED) + { + ptr = NULL; +#else + if (ptr == NULL) + { +#endif + PanicAlert("Failed to allocate executable memory"); + } +#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) + else + { + if (low) + { + map_hint += size; + map_hint = (char*)round_page(map_hint); /* round up to the next page */ + // printf("Next map will (hopefully) be at %p\n", map_hint); + } + } +#endif + +#if defined(_M_X64) + if ((u64)ptr >= 0x80000000 && low == true) + PanicAlert("Executable memory ended up above 2GB!"); +#endif + + return ptr; +} + +void* AllocateMemoryPages(size_t size) +{ +#ifdef _WIN32 + void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); +#else + void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); +#endif + + // printf("Mapped memory at %p (size %ld)\n", ptr, + // (unsigned long)size); + + if (ptr == NULL) + PanicAlert("Failed to allocate raw memory"); + + return ptr; +} + +void* AllocateAlignedMemory(size_t size,size_t alignment) +{ +#ifdef _WIN32 + void* ptr = _aligned_malloc(size,alignment); +#else + void* ptr = NULL; +#ifdef ANDROID + ptr = memalign(alignment, size); +#else + if (posix_memalign(&ptr, alignment, size) != 0) + ERROR_LOG(MEMMAP, "Failed to allocate aligned memory"); +#endif +#endif + + // printf("Mapped memory at %p (size %ld)\n", ptr, + // (unsigned long)size); + + if (ptr == NULL) + PanicAlert("Failed to allocate aligned memory"); + + return ptr; +} + +void FreeMemoryPages(void* ptr, size_t size) +{ + if (ptr) + { +#ifdef _WIN32 + + if (!VirtualFree(ptr, 0, MEM_RELEASE)) + PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); + ptr = NULL; // Is this our responsibility? + +#else + munmap(ptr, size); +#endif + } +} + +void FreeAlignedMemory(void* ptr) +{ + if (ptr) + { +#ifdef _WIN32 + _aligned_free(ptr); +#else + free(ptr); +#endif + } +} + +void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) +{ +#ifdef _WIN32 + DWORD oldValue; + if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) + PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); +#else + mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); +#endif +} + +void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) +{ +#ifdef _WIN32 + DWORD oldValue; + if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) + PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); +#else + mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); +#endif +} + +std::string MemUsage() +{ +#ifdef _WIN32 +#pragma comment(lib, "psapi") + DWORD processID = GetCurrentProcessId(); + HANDLE hProcess; + PROCESS_MEMORY_COUNTERS pmc; + std::string Ret; + + // Print information about the memory usage of the process. + + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); + if (NULL == hProcess) return "MemUsage Error"; + + if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) + Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); + + CloseHandle(hProcess); + return Ret; +#else + return ""; +#endif +} -- cgit v1.2.3 From 3fd87a7636ff434118a5d7f7334550be8db55c0b Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 16 Feb 2014 23:51:41 -0500 Subject: Second and final pass of clearing out tabs. --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index ce1c5d8a54..242fdbcb81 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -58,7 +58,7 @@ void* AllocateExecutableMemory(size_t size, bool low) #else if (ptr == NULL) { -#endif +#endif PanicAlert("Failed to allocate executable memory"); } #if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) -- cgit v1.2.3 From 2afe2152712981e21d6bda6f029292ed2b1cf91e Mon Sep 17 00:00:00 2001 From: Lioncash Date: Mon, 17 Feb 2014 05:18:15 -0500 Subject: Convert all includes to relative paths. --- Source/Core/Common/MemoryUtil.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 242fdbcb81..e65f0a1cbb 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -3,9 +3,9 @@ // Refer to the license.txt file included. -#include "Common.h" -#include "MemoryUtil.h" -#include "StringUtil.h" +#include "Common/Common.h" +#include "Common/MemoryUtil.h" +#include "Common/StringUtil.h" #ifdef _WIN32 #include -- cgit v1.2.3 From 83b7bb64aa5e1ee6b18eaa5d08c17bb5b6c57048 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Thu, 20 Feb 2014 04:11:52 +0100 Subject: Make Common/ mostly IWYU clean (and fix errors in rest of the project detected by this change). --- Source/Core/Common/MemoryUtil.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index e65f0a1cbb..cc6d569036 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -2,17 +2,19 @@ // Licensed under GPLv2 // Refer to the license.txt file included. +#include +#include +#include #include "Common/Common.h" -#include "Common/MemoryUtil.h" -#include "Common/StringUtil.h" #ifdef _WIN32 #include #include +#include "Common/StringUtil.h" #else -#include #include +#include #endif #if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) -- cgit v1.2.3 From 5f0a8008f45f2c30a1aed044a428c5b4ce09ce88 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 23 Feb 2014 08:22:32 +0100 Subject: Convert MemoryUtil.cpp to Unix-style line endings --- Source/Core/Common/MemoryUtil.cpp | 398 +++++++++++++++++++------------------- 1 file changed, 199 insertions(+), 199 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index cc6d569036..3d3e17cc8a 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -1,199 +1,199 @@ -// Copyright 2013 Dolphin Emulator Project -// Licensed under GPLv2 -// Refer to the license.txt file included. - -#include -#include -#include - -#include "Common/Common.h" - -#ifdef _WIN32 -#include -#include -#include "Common/StringUtil.h" -#else -#include -#include -#endif - -#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) -#include -#define PAGE_MASK (getpagesize() - 1) -#define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) -#endif - -// This is purposely not a full wrapper for virtualalloc/mmap, but it -// provides exactly the primitive operations that Dolphin needs. - -void* AllocateExecutableMemory(size_t size, bool low) -{ -#if defined(_WIN32) - void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); -#else - static char *map_hint = 0; -#if defined(__x86_64__) && !defined(MAP_32BIT) - // This OS has no flag to enforce allocation below the 4 GB boundary, - // but if we hint that we want a low address it is very likely we will - // get one. - // An older version of this code used MAP_FIXED, but that has the side - // effect of discarding already mapped pages that happen to be in the - // requested virtual memory range (such as the emulated RAM, sometimes). - if (low && (!map_hint)) - map_hint = (char*)round_page(512*1024*1024); /* 0.5 GB rounded up to the next page */ -#endif - void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_ANON | MAP_PRIVATE -#if defined(__x86_64__) && defined(MAP_32BIT) - | (low ? MAP_32BIT : 0) -#endif - , -1, 0); -#endif /* defined(_WIN32) */ - - // printf("Mapped executable memory at %p (size %ld)\n", ptr, - // (unsigned long)size); - -#if defined(__FreeBSD__) - if (ptr == MAP_FAILED) - { - ptr = NULL; -#else - if (ptr == NULL) - { -#endif - PanicAlert("Failed to allocate executable memory"); - } -#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) - else - { - if (low) - { - map_hint += size; - map_hint = (char*)round_page(map_hint); /* round up to the next page */ - // printf("Next map will (hopefully) be at %p\n", map_hint); - } - } -#endif - -#if defined(_M_X64) - if ((u64)ptr >= 0x80000000 && low == true) - PanicAlert("Executable memory ended up above 2GB!"); -#endif - - return ptr; -} - -void* AllocateMemoryPages(size_t size) -{ -#ifdef _WIN32 - void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); -#else - void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); -#endif - - // printf("Mapped memory at %p (size %ld)\n", ptr, - // (unsigned long)size); - - if (ptr == NULL) - PanicAlert("Failed to allocate raw memory"); - - return ptr; -} - -void* AllocateAlignedMemory(size_t size,size_t alignment) -{ -#ifdef _WIN32 - void* ptr = _aligned_malloc(size,alignment); -#else - void* ptr = NULL; -#ifdef ANDROID - ptr = memalign(alignment, size); -#else - if (posix_memalign(&ptr, alignment, size) != 0) - ERROR_LOG(MEMMAP, "Failed to allocate aligned memory"); -#endif -#endif - - // printf("Mapped memory at %p (size %ld)\n", ptr, - // (unsigned long)size); - - if (ptr == NULL) - PanicAlert("Failed to allocate aligned memory"); - - return ptr; -} - -void FreeMemoryPages(void* ptr, size_t size) -{ - if (ptr) - { -#ifdef _WIN32 - - if (!VirtualFree(ptr, 0, MEM_RELEASE)) - PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); - ptr = NULL; // Is this our responsibility? - -#else - munmap(ptr, size); -#endif - } -} - -void FreeAlignedMemory(void* ptr) -{ - if (ptr) - { -#ifdef _WIN32 - _aligned_free(ptr); -#else - free(ptr); -#endif - } -} - -void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) -{ -#ifdef _WIN32 - DWORD oldValue; - if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) - PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); -#else - mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); -#endif -} - -void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) -{ -#ifdef _WIN32 - DWORD oldValue; - if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) - PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); -#else - mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); -#endif -} - -std::string MemUsage() -{ -#ifdef _WIN32 -#pragma comment(lib, "psapi") - DWORD processID = GetCurrentProcessId(); - HANDLE hProcess; - PROCESS_MEMORY_COUNTERS pmc; - std::string Ret; - - // Print information about the memory usage of the process. - - hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); - if (NULL == hProcess) return "MemUsage Error"; - - if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) - Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); - - CloseHandle(hProcess); - return Ret; -#else - return ""; -#endif -} +// Copyright 2013 Dolphin Emulator Project +// Licensed under GPLv2 +// Refer to the license.txt file included. + +#include +#include +#include + +#include "Common/Common.h" + +#ifdef _WIN32 +#include +#include +#include "Common/StringUtil.h" +#else +#include +#include +#endif + +#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) +#include +#define PAGE_MASK (getpagesize() - 1) +#define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) +#endif + +// This is purposely not a full wrapper for virtualalloc/mmap, but it +// provides exactly the primitive operations that Dolphin needs. + +void* AllocateExecutableMemory(size_t size, bool low) +{ +#if defined(_WIN32) + void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); +#else + static char *map_hint = 0; +#if defined(__x86_64__) && !defined(MAP_32BIT) + // This OS has no flag to enforce allocation below the 4 GB boundary, + // but if we hint that we want a low address it is very likely we will + // get one. + // An older version of this code used MAP_FIXED, but that has the side + // effect of discarding already mapped pages that happen to be in the + // requested virtual memory range (such as the emulated RAM, sometimes). + if (low && (!map_hint)) + map_hint = (char*)round_page(512*1024*1024); /* 0.5 GB rounded up to the next page */ +#endif + void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, + MAP_ANON | MAP_PRIVATE +#if defined(__x86_64__) && defined(MAP_32BIT) + | (low ? MAP_32BIT : 0) +#endif + , -1, 0); +#endif /* defined(_WIN32) */ + + // printf("Mapped executable memory at %p (size %ld)\n", ptr, + // (unsigned long)size); + +#if defined(__FreeBSD__) + if (ptr == MAP_FAILED) + { + ptr = NULL; +#else + if (ptr == NULL) + { +#endif + PanicAlert("Failed to allocate executable memory"); + } +#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) + else + { + if (low) + { + map_hint += size; + map_hint = (char*)round_page(map_hint); /* round up to the next page */ + // printf("Next map will (hopefully) be at %p\n", map_hint); + } + } +#endif + +#if defined(_M_X64) + if ((u64)ptr >= 0x80000000 && low == true) + PanicAlert("Executable memory ended up above 2GB!"); +#endif + + return ptr; +} + +void* AllocateMemoryPages(size_t size) +{ +#ifdef _WIN32 + void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); +#else + void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE, -1, 0); +#endif + + // printf("Mapped memory at %p (size %ld)\n", ptr, + // (unsigned long)size); + + if (ptr == NULL) + PanicAlert("Failed to allocate raw memory"); + + return ptr; +} + +void* AllocateAlignedMemory(size_t size,size_t alignment) +{ +#ifdef _WIN32 + void* ptr = _aligned_malloc(size,alignment); +#else + void* ptr = NULL; +#ifdef ANDROID + ptr = memalign(alignment, size); +#else + if (posix_memalign(&ptr, alignment, size) != 0) + ERROR_LOG(MEMMAP, "Failed to allocate aligned memory"); +#endif +#endif + + // printf("Mapped memory at %p (size %ld)\n", ptr, + // (unsigned long)size); + + if (ptr == NULL) + PanicAlert("Failed to allocate aligned memory"); + + return ptr; +} + +void FreeMemoryPages(void* ptr, size_t size) +{ + if (ptr) + { +#ifdef _WIN32 + + if (!VirtualFree(ptr, 0, MEM_RELEASE)) + PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); + ptr = NULL; // Is this our responsibility? + +#else + munmap(ptr, size); +#endif + } +} + +void FreeAlignedMemory(void* ptr) +{ + if (ptr) + { +#ifdef _WIN32 + _aligned_free(ptr); +#else + free(ptr); +#endif + } +} + +void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) +{ +#ifdef _WIN32 + DWORD oldValue; + if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) + PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); +#else + mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); +#endif +} + +void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) +{ +#ifdef _WIN32 + DWORD oldValue; + if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) + PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); +#else + mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); +#endif +} + +std::string MemUsage() +{ +#ifdef _WIN32 +#pragma comment(lib, "psapi") + DWORD processID = GetCurrentProcessId(); + HANDLE hProcess; + PROCESS_MEMORY_COUNTERS pmc; + std::string Ret; + + // Print information about the memory usage of the process. + + hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); + if (NULL == hProcess) return "MemUsage Error"; + + if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) + Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); + + CloseHandle(hProcess); + return Ret; +#else + return ""; +#endif +} -- cgit v1.2.3 From 315a8ba1c04bf8cffd9b04096be898ac462fa89e Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 23 Feb 2014 23:03:39 +0100 Subject: Various changes suggested by cppcheck - remove unused variables - reduce the scope where it makes sense - correct limits (did you know that strcat()'s last parameter does not include the \0 that is always added?) - set some free()'d pointers to NULL --- Source/Core/Common/MemoryUtil.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 3d3e17cc8a..cfdc3ad730 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -129,11 +129,10 @@ void FreeMemoryPages(void* ptr, size_t size) if (ptr) { #ifdef _WIN32 - if (!VirtualFree(ptr, 0, MEM_RELEASE)) + { PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); - ptr = NULL; // Is this our responsibility? - + } #else munmap(ptr, size); #endif @@ -145,9 +144,9 @@ void FreeAlignedMemory(void* ptr) if (ptr) { #ifdef _WIN32 - _aligned_free(ptr); + _aligned_free(ptr); #else - free(ptr); + free(ptr); #endif } } -- cgit v1.2.3 From 4f02132f9323a147c68b7dc1a79a62be33c1f170 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 2 Mar 2014 05:21:50 -0600 Subject: Make our architecture defines less stupid. Our defines were never clear between what meant 64bit or x86_64 This makes a clear cut between bitness and architecture. This commit also has the side effect of bringing up aarch64 compiling support. --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index cfdc3ad730..441ba2f10b 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -75,7 +75,7 @@ void* AllocateExecutableMemory(size_t size, bool low) } #endif -#if defined(_M_X64) +#if _ARCH_64 if ((u64)ptr >= 0x80000000 && low == true) PanicAlert("Executable memory ended up above 2GB!"); #endif -- cgit v1.2.3 From d802d392811be44d34ae9cd23f616db93e54c50f Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 9 Mar 2014 21:14:26 +0100 Subject: clang-modernize -use-nullptr and s/\bNULL\b/nullptr/g for *.cpp/h/mm files not compiled on my machine --- Source/Core/Common/MemoryUtil.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 441ba2f10b..e664752910 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -31,7 +31,7 @@ void* AllocateExecutableMemory(size_t size, bool low) #if defined(_WIN32) void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else - static char *map_hint = 0; + static char *map_hint = nullptr; #if defined(__x86_64__) && !defined(MAP_32BIT) // This OS has no flag to enforce allocation below the 4 GB boundary, // but if we hint that we want a low address it is very likely we will @@ -56,9 +56,9 @@ void* AllocateExecutableMemory(size_t size, bool low) #if defined(__FreeBSD__) if (ptr == MAP_FAILED) { - ptr = NULL; + ptr = nullptr; #else - if (ptr == NULL) + if (ptr == nullptr) { #endif PanicAlert("Failed to allocate executable memory"); @@ -88,14 +88,14 @@ void* AllocateMemoryPages(size_t size) #ifdef _WIN32 void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); #else - void* ptr = mmap(0, size, PROT_READ | PROT_WRITE, + void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); #endif // printf("Mapped memory at %p (size %ld)\n", ptr, // (unsigned long)size); - if (ptr == NULL) + if (ptr == nullptr) PanicAlert("Failed to allocate raw memory"); return ptr; @@ -106,7 +106,7 @@ void* AllocateAlignedMemory(size_t size,size_t alignment) #ifdef _WIN32 void* ptr = _aligned_malloc(size,alignment); #else - void* ptr = NULL; + void* ptr = nullptr; #ifdef ANDROID ptr = memalign(alignment, size); #else @@ -118,7 +118,7 @@ void* AllocateAlignedMemory(size_t size,size_t alignment) // printf("Mapped memory at %p (size %ld)\n", ptr, // (unsigned long)size); - if (ptr == NULL) + if (ptr == nullptr) PanicAlert("Failed to allocate aligned memory"); return ptr; @@ -185,7 +185,7 @@ std::string MemUsage() // Print information about the memory usage of the process. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); - if (NULL == hProcess) return "MemUsage Error"; + if (nullptr == hProcess) return "MemUsage Error"; if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); -- cgit v1.2.3 From 664c8d30a055f4762a2a60be77c1c8eaec1a5d85 Mon Sep 17 00:00:00 2001 From: Pierre Bourdon Date: Sat, 29 Mar 2014 11:05:44 +0100 Subject: Remove all trailing whitespaces from our codebase. --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index e664752910..8e8d27b520 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -52,7 +52,7 @@ void* AllocateExecutableMemory(size_t size, bool low) // printf("Mapped executable memory at %p (size %ld)\n", ptr, // (unsigned long)size); - + #if defined(__FreeBSD__) if (ptr == MAP_FAILED) { -- cgit v1.2.3 From 87e671404aa75bf3f1ba013daa0a13165ab4827c Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 1 Jun 2014 23:45:44 -0500 Subject: Make MemoryUtil.cpp use the correct x86_64 define. MemoryUtil.cpp was incorrectly using the old __x86_64__ define when it should be using _M_X86_64. It was also using _ARCH_64 when it shouldn't have which was causing an errant PanicAlert to come up in my development. --- Source/Core/Common/MemoryUtil.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 8e8d27b520..bb3ddf58a4 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -17,7 +17,7 @@ #include #endif -#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) +#if !defined(_WIN32) && defined(_M_X86_64) && !defined(MAP_32BIT) #include #define PAGE_MASK (getpagesize() - 1) #define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) @@ -32,7 +32,7 @@ void* AllocateExecutableMemory(size_t size, bool low) void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else static char *map_hint = nullptr; -#if defined(__x86_64__) && !defined(MAP_32BIT) +#if defined(_M_X86_64) && !defined(MAP_32BIT) // This OS has no flag to enforce allocation below the 4 GB boundary, // but if we hint that we want a low address it is very likely we will // get one. @@ -44,7 +44,7 @@ void* AllocateExecutableMemory(size_t size, bool low) #endif void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE -#if defined(__x86_64__) && defined(MAP_32BIT) +#if defined(_M_X86_64) && defined(MAP_32BIT) | (low ? MAP_32BIT : 0) #endif , -1, 0); @@ -63,7 +63,7 @@ void* AllocateExecutableMemory(size_t size, bool low) #endif PanicAlert("Failed to allocate executable memory"); } -#if !defined(_WIN32) && defined(__x86_64__) && !defined(MAP_32BIT) +#if !defined(_WIN32) && defined(_M_X86_64) && !defined(MAP_32BIT) else { if (low) @@ -75,7 +75,7 @@ void* AllocateExecutableMemory(size_t size, bool low) } #endif -#if _ARCH_64 +#if _M_X86_64 if ((u64)ptr >= 0x80000000 && low == true) PanicAlert("Executable memory ended up above 2GB!"); #endif -- cgit v1.2.3 From 22e1aa5bb4a159d6d66a321f978917614aa36331 Mon Sep 17 00:00:00 2001 From: degasus Date: Tue, 8 Jul 2014 14:29:26 +0200 Subject: mark all local functions as static --- Source/Core/Common/MemoryUtil.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index bb3ddf58a4..0ae08ced89 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -7,6 +7,7 @@ #include #include "Common/Common.h" +#include "Common/MemoryUtil.h" #ifdef _WIN32 #include -- cgit v1.2.3 From 33450c80c3ca39ef1875f9c72898ba5749d06142 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Thu, 31 Jul 2014 00:47:44 -0500 Subject: Fixes a check for what mmap returns. On error mmap returns MAP_FAILED(-1) not null. FreeBSD was checking the return correctly, Linux was not. This was noticed by triad attempting to run Dolphin under valgrind and not getting a memory space under the 2GB limit(Because -1 wraps around on unsigned obviously) --- Source/Core/Common/MemoryUtil.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 0ae08ced89..3a10d031cf 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -54,13 +54,13 @@ void* AllocateExecutableMemory(size_t size, bool low) // printf("Mapped executable memory at %p (size %ld)\n", ptr, // (unsigned long)size); -#if defined(__FreeBSD__) - if (ptr == MAP_FAILED) +#ifdef _WIN32 + if (ptr == nullptr) { - ptr = nullptr; #else - if (ptr == nullptr) + if (ptr == MAP_FAILED) { + ptr = nullptr; #endif PanicAlert("Failed to allocate executable memory"); } -- cgit v1.2.3 From be3428cf8e7698e4efe9c50cd6499516e7f36332 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 10 Aug 2014 04:50:58 -0400 Subject: Core: Fix case where a panic alert wouldn't be shown in MemoryUtil.cpp --- Source/Core/Common/MemoryUtil.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 3a10d031cf..7a075692ce 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -91,10 +91,10 @@ void* AllocateMemoryPages(size_t size) #else void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); -#endif - // printf("Mapped memory at %p (size %ld)\n", ptr, - // (unsigned long)size); + if (ptr == MAP_FAILED) + ptr = nullptr; +#endif if (ptr == nullptr) PanicAlert("Failed to allocate raw memory"); -- cgit v1.2.3 From 9a61cfc650c159b8e6b57059fc9f335dd29fe3dd Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 10 Aug 2014 05:28:00 -0400 Subject: Core: Actually show MemoryUtil.cpp allocation error messages on Linux --- Source/Core/Common/MemoryUtil.cpp | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 7a075692ce..7d5eaa0b28 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -129,14 +129,20 @@ void FreeMemoryPages(void* ptr, size_t size) { if (ptr) { + bool error_occurred = false; + #ifdef _WIN32 if (!VirtualFree(ptr, 0, MEM_RELEASE)) - { - PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); - } + error_occurred = true; #else - munmap(ptr, size); + int retval = munmap(ptr, size); + + if (retval != 0) + error_occurred = true; #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) { + bool error_occurred = false; + #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) - PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); + error_occurred = true; #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 + + if (error_occurred) + PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); } void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) { + bool error_occurred = false; + #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) - PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); + error_occurred = true; #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 + + if (error_occurred) + PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); } std::string MemUsage() -- cgit v1.2.3 From fbc64984ca7de7db10b1a8a4f49002f260c93569 Mon Sep 17 00:00:00 2001 From: Rohit Nirmal Date: Sun, 7 Sep 2014 20:06:58 -0500 Subject: Include CommonTypes.h instead of Common.h. --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 7d5eaa0b28..f7e1d7d902 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -6,7 +6,7 @@ #include #include -#include "Common/Common.h" +#include "Common/CommonTypes.h" #include "Common/MemoryUtil.h" #ifdef _WIN32 -- cgit v1.2.3 From 7ad90275934b63dcc4726a7c49ee9a64758dda3b Mon Sep 17 00:00:00 2001 From: comex Date: Mon, 15 Sep 2014 23:03:07 -0400 Subject: Be pedantic about stack overflow on Linux and OS X. Add some magic to the fault handler to handle stack overflow due to BLR optimization, and disable the optimization if fastmem is not enabled. --- Source/Core/Common/MemoryUtil.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index f7e1d7d902..a741deef4f 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -158,6 +158,25 @@ void FreeAlignedMemory(void* ptr) } } +void ReadProtectMemory(void* ptr, size_t size) +{ + bool error_occurred = false; + +#ifdef _WIN32 + DWORD oldValue; + if (!VirtualProtect(ptr, size, PAGE_NOACCESS, &oldValue)) + error_occurred = true; +#else + int retval = mprotect(ptr, size, PROT_NONE); + + if (retval != 0) + error_occurred = true; +#endif + + if (error_occurred) + PanicAlert("ReadProtectMemory failed!\n%s", GetLastErrorMsg()); +} + void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) { bool error_occurred = false; -- cgit v1.2.3 From 46057db37d086c47ff6c69f200f66648fa0e6c84 Mon Sep 17 00:00:00 2001 From: Rohit Nirmal Date: Thu, 18 Sep 2014 23:17:41 -0500 Subject: Fix build failing when disabling precompiled headers. --- Source/Core/Common/MemoryUtil.cpp | 3 +++ 1 file changed, 3 insertions(+) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index a741deef4f..fd88726814 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -6,8 +6,11 @@ #include #include +#include "Common/CommonFuncs.h" #include "Common/CommonTypes.h" #include "Common/MemoryUtil.h" +#include "Common/MsgHandler.h" +#include "Common/Logging/Log.h" #ifdef _WIN32 #include -- cgit v1.2.3 From f98359ef5c4da25d0a6bf2ea09f2a96618e7c768 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sat, 3 Jan 2015 11:46:26 +0100 Subject: MemoryUtil: add comment for Valgrind Valgrind doesn't support the mmap() flag MAP_32BIT. Adding a simple CMake option would force a recompile, so just add this comment. --- Source/Core/Common/MemoryUtil.cpp | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index fd88726814..87cc7812e0 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -21,6 +21,10 @@ #include #endif +// Valgrind doesn't support MAP_32BIT. +// Uncomment the following line to be able to run Dolphin in Valgrind. +//#undef MAP_32BIT + #if !defined(_WIN32) && defined(_M_X86_64) && !defined(MAP_32BIT) #include #define PAGE_MASK (getpagesize() - 1) -- cgit v1.2.3 From 52b1a5459773c16554bf1a3fc583706054e0e4cc Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Mon, 12 Jan 2015 01:24:23 +0100 Subject: MemoryUtil: mention Valgrind in mmap error message --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 87cc7812e0..e36babb03d 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -69,7 +69,7 @@ void* AllocateExecutableMemory(size_t size, bool low) { ptr = nullptr; #endif - PanicAlert("Failed to allocate executable memory"); + PanicAlert("Failed to allocate executable memory. If you are running Dolphin in Valgrind, try '#undef MAP_32BIT'."); } #if !defined(_WIN32) && defined(_M_X86_64) && !defined(MAP_32BIT) else -- cgit v1.2.3 From 08e50ec6dda9434e1ea804237d4c1de4f0e26438 Mon Sep 17 00:00:00 2001 From: Ryan Houdek Date: Sun, 18 Jan 2015 16:34:42 -0600 Subject: [Android] Remove posix_memalign workaround. Google fixed the issue of posix_memalign not being available. It now works fine in r10d of the NDK. --- Source/Core/Common/MemoryUtil.cpp | 4 ---- 1 file changed, 4 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index e36babb03d..14d58aa45f 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -115,12 +115,8 @@ void* AllocateAlignedMemory(size_t size,size_t alignment) void* ptr = _aligned_malloc(size,alignment); #else void* ptr = nullptr; -#ifdef ANDROID - ptr = memalign(alignment, size); -#else if (posix_memalign(&ptr, alignment, size) != 0) ERROR_LOG(MEMMAP, "Failed to allocate aligned memory"); -#endif #endif // printf("Mapped memory at %p (size %ld)\n", ptr, -- cgit v1.2.3 From 93b16a4a2d5f3e6d467d1315c461aff12852b26c Mon Sep 17 00:00:00 2001 From: Stevoisiak Date: Sun, 15 Feb 2015 14:43:31 -0500 Subject: Formatting/Whitespace Cleanup Various fixes to formatting and whitespace --- Source/Core/Common/MemoryUtil.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 14d58aa45f..8ab189854f 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -109,10 +109,10 @@ void* AllocateMemoryPages(size_t size) return ptr; } -void* AllocateAlignedMemory(size_t size,size_t alignment) +void* AllocateAlignedMemory(size_t size, size_t alignment) { #ifdef _WIN32 - void* ptr = _aligned_malloc(size,alignment); + void* ptr = _aligned_malloc(size, alignment); #else void* ptr = nullptr; if (posix_memalign(&ptr, alignment, size) != 0) -- cgit v1.2.3 From ef6bdf674f556c0ce9df383a3dc2e8bdc7483a5f Mon Sep 17 00:00:00 2001 From: degasus Date: Tue, 7 Apr 2015 22:15:21 +0200 Subject: Common: Remove TLS usage in GetLastErrorMsg Just return std::string as this code isn't performance critical. --- Source/Core/Common/MemoryUtil.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 8ab189854f..db60a475ed 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -145,7 +145,7 @@ void FreeMemoryPages(void* ptr, size_t size) #endif if (error_occurred) - PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg()); + PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg().c_str()); } } @@ -177,7 +177,7 @@ void ReadProtectMemory(void* ptr, size_t size) #endif if (error_occurred) - PanicAlert("ReadProtectMemory failed!\n%s", GetLastErrorMsg()); + PanicAlert("ReadProtectMemory failed!\n%s", GetLastErrorMsg().c_str()); } void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) @@ -196,7 +196,7 @@ void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) #endif if (error_occurred) - PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg()); + PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str()); } void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) @@ -215,7 +215,7 @@ void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) #endif if (error_occurred) - PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); + PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str()); } std::string MemUsage() -- cgit v1.2.3 From 562b9d4a9f3710a65a2987a3c1a88e00778c6d67 Mon Sep 17 00:00:00 2001 From: degasus Date: Sat, 16 May 2015 15:26:11 +0200 Subject: VideoCommon: Abort texture prefetching on low RAM There is no nice way to correctly "detect" the "used" memory, so we just say we're fine to use 50% of the physical memory for custom textures. This will fix out-of-memory crashes, but we still might run into swapping issues. --- Source/Core/Common/MemoryUtil.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index db60a475ed..36de6b23a0 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -19,6 +19,12 @@ #else #include #include +#include +#ifdef __APPLE__ +#include +#else +#include +#endif #endif // Valgrind doesn't support MAP_32BIT. @@ -241,3 +247,26 @@ std::string MemUsage() return ""; #endif } + + +size_t MemPhysical() +{ +#ifdef _WIN32 + MEMORYSTATUSEX memInfo; + memInfo.dwLength = sizeof(MEMORYSTATUSEX); + GlobalMemoryStatusEx(&memInfo); + return memInfo.ullTotalPhys; +#elif defined(__APPLE__) + int mib[2]; + size_t physical_memory; + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + size_t length = sizeof(size_t); + sysctl(mib, 2, &physical_memory, &length, NULL, 0); + return physical_memory; +#else + struct sysinfo memInfo; + sysinfo (&memInfo); + return (size_t)memInfo.totalram * memInfo.mem_unit; +#endif +} -- cgit v1.2.3 From cefcb0ace9d363b3679b4e93bcc9ec05f1e5f4f8 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Mon, 18 May 2015 01:08:10 +0200 Subject: Update license headers to GPLv2+ --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index db60a475ed..663ff3296f 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -1,5 +1,5 @@ // Copyright 2013 Dolphin Emulator Project -// Licensed under GPLv2 +// Licensed under GPLv2+ // Refer to the license.txt file included. #include -- cgit v1.2.3 From 30ebb2459eb97ba544547183854775df8460b475 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sun, 24 May 2015 06:55:12 +0200 Subject: Set copyright year to when a file was created --- Source/Core/Common/MemoryUtil.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 663ff3296f..9455212f01 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -1,4 +1,4 @@ -// Copyright 2013 Dolphin Emulator Project +// Copyright 2008 Dolphin Emulator Project // Licensed under GPLv2+ // Refer to the license.txt file included. -- cgit v1.2.3 From d4538c762ff1d42b69c325abea9bd0ce3854f486 Mon Sep 17 00:00:00 2001 From: Tillmann Karras Date: Sat, 2 May 2015 11:57:17 +0200 Subject: MemoryUtil: get executable pages near static data and clean up a bit. --- Source/Core/Common/MemoryUtil.cpp | 120 ++++++-------------------------------- 1 file changed, 19 insertions(+), 101 deletions(-) (limited to 'Source/Core/Common/MemoryUtil.cpp') diff --git a/Source/Core/Common/MemoryUtil.cpp b/Source/Core/Common/MemoryUtil.cpp index 510b258b09..e4563e3409 100644 --- a/Source/Core/Common/MemoryUtil.cpp +++ b/Source/Core/Common/MemoryUtil.cpp @@ -27,71 +27,29 @@ #endif #endif -// Valgrind doesn't support MAP_32BIT. -// Uncomment the following line to be able to run Dolphin in Valgrind. -//#undef MAP_32BIT - -#if !defined(_WIN32) && defined(_M_X86_64) && !defined(MAP_32BIT) -#include -#define PAGE_MASK (getpagesize() - 1) -#define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) -#endif - -// This is purposely not a full wrapper for virtualalloc/mmap, but it -// provides exactly the primitive operations that Dolphin needs. - -void* AllocateExecutableMemory(size_t size, bool low) +void* AllocateExecutableMemory(size_t size, void* map_hint) { #if defined(_WIN32) void* ptr = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else - static char *map_hint = nullptr; -#if defined(_M_X86_64) && !defined(MAP_32BIT) - // This OS has no flag to enforce allocation below the 4 GB boundary, - // but if we hint that we want a low address it is very likely we will - // get one. - // An older version of this code used MAP_FIXED, but that has the side - // effect of discarding already mapped pages that happen to be in the - // requested virtual memory range (such as the emulated RAM, sometimes). - if (low && (!map_hint)) - map_hint = (char*)round_page(512*1024*1024); /* 0.5 GB rounded up to the next page */ -#endif void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, - MAP_ANON | MAP_PRIVATE -#if defined(_M_X86_64) && defined(MAP_32BIT) - | (low ? MAP_32BIT : 0) -#endif - , -1, 0); + MAP_ANON | MAP_PRIVATE, -1, 0); #endif /* defined(_WIN32) */ - // printf("Mapped executable memory at %p (size %ld)\n", ptr, - // (unsigned long)size); - #ifdef _WIN32 if (ptr == nullptr) - { #else if (ptr == MAP_FAILED) - { - ptr = nullptr; #endif - PanicAlert("Failed to allocate executable memory. If you are running Dolphin in Valgrind, try '#undef MAP_32BIT'."); - } -#if !defined(_WIN32) && defined(_M_X86_64) && !defined(MAP_32BIT) - else { - if (low) - { - map_hint += size; - map_hint = (char*)round_page(map_hint); /* round up to the next page */ - // printf("Next map will (hopefully) be at %p\n", map_hint); - } + ptr = nullptr; + PanicAlert("Failed to allocate executable memory."); } -#endif -#if _M_X86_64 - if ((u64)ptr >= 0x80000000 && low == true) - PanicAlert("Executable memory ended up above 2GB!"); +#ifdef _X86_64 + ptrdiff_t ofs = (u8*)ptr - (u8*)map_hint; + if (ofs < -0x80000000ll || ofs + size > 0x80000000ll) + PanicAlert("Executable range can't be used for RIP-relative addressing."); #endif return ptr; @@ -117,18 +75,12 @@ void* AllocateMemoryPages(size_t size) void* AllocateAlignedMemory(size_t size, size_t alignment) { + void* ptr = nullptr; #ifdef _WIN32 - void* ptr = _aligned_malloc(size, alignment); + if (!(ptr = _aligned_malloc(size, alignment))) #else - void* ptr = nullptr; - if (posix_memalign(&ptr, alignment, size) != 0) - ERROR_LOG(MEMMAP, "Failed to allocate aligned memory"); + if (posix_memalign(&ptr, alignment, size)) #endif - - // printf("Mapped memory at %p (size %ld)\n", ptr, - // (unsigned long)size); - - if (ptr == nullptr) PanicAlert("Failed to allocate aligned memory"); return ptr; @@ -136,23 +88,12 @@ void* AllocateAlignedMemory(size_t size, size_t alignment) void FreeMemoryPages(void* ptr, size_t size) { - if (ptr) - { - bool error_occurred = false; - #ifdef _WIN32 - if (!VirtualFree(ptr, 0, MEM_RELEASE)) - error_occurred = true; + if (ptr && !VirtualFree(ptr, 0, MEM_RELEASE)) #else - int retval = munmap(ptr, size); - - if (retval != 0) - error_occurred = true; + if (ptr && munmap(ptr, size)) #endif - - if (error_occurred) - PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg().c_str()); - } + PanicAlert("FreeMemoryPages failed!\n%s", GetLastErrorMsg().c_str()); } void FreeAlignedMemory(void* ptr) @@ -169,58 +110,34 @@ void FreeAlignedMemory(void* ptr) void ReadProtectMemory(void* ptr, size_t size) { - bool error_occurred = false; - #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, PAGE_NOACCESS, &oldValue)) - error_occurred = true; #else - int retval = mprotect(ptr, size, PROT_NONE); - - if (retval != 0) - error_occurred = true; + if (mprotect(ptr, size, PROT_NONE)) #endif - - if (error_occurred) PanicAlert("ReadProtectMemory failed!\n%s", GetLastErrorMsg().c_str()); } void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) { - bool error_occurred = false; - #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) - error_occurred = true; #else - int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ); - - if (retval != 0) - error_occurred = true; + if (mprotect(ptr, size, PROT_READ | (allowExecute ? PROT_EXEC : 0))) #endif - - if (error_occurred) PanicAlert("WriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str()); } void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) { - bool error_occurred = false; - #ifdef _WIN32 DWORD oldValue; if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) - error_occurred = true; #else - int retval = mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); - - if (retval != 0) - error_occurred = true; + if (mprotect(ptr, size, PROT_READ | PROT_WRITE | (allowExecute ? PROT_EXEC : 0))) #endif - - if (error_occurred) PanicAlert("UnWriteProtectMemory failed!\n%s", GetLastErrorMsg().c_str()); } @@ -236,7 +153,8 @@ std::string MemUsage() // Print information about the memory usage of the process. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); - if (nullptr == hProcess) return "MemUsage Error"; + if (nullptr == hProcess) + return "MemUsage Error"; if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) Ret = StringFromFormat("%s K", ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); -- cgit v1.2.3