diff options
| author | 2016-09-18 09:38:01 +0900 | |
|---|---|---|
| committer | 2016-09-18 09:38:01 +0900 | |
| commit | dc8479928c5aee4c6ad6fe4f59006fb604cee701 (patch) | |
| tree | 569a7f13128450bbab973236615587ff00bced5f /src/common/memory_util.cpp | |
| parent | Travis: Import Dolphin’s clang-format hook. (diff) | |
| download | yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.tar.gz yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.tar.xz yuzu-dc8479928c5aee4c6ad6fe4f59006fb604cee701.zip | |
Sources: Run clang-format on everything.
Diffstat (limited to 'src/common/memory_util.cpp')
| -rw-r--r-- | src/common/memory_util.cpp | 90 |
1 files changed, 39 insertions, 51 deletions
diff --git a/src/common/memory_util.cpp b/src/common/memory_util.cpp index 07c7f79c8..7d352f00f 100644 --- a/src/common/memory_util.cpp +++ b/src/common/memory_util.cpp | |||
| @@ -2,31 +2,29 @@ | |||
| 2 | // Licensed under GPLv2 or any later version | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | |||
| 6 | #include "common/logging/log.h" | ||
| 7 | #include "common/memory_util.h" | 5 | #include "common/memory_util.h" |
| 6 | #include "common/logging/log.h" | ||
| 8 | 7 | ||
| 9 | #ifdef _WIN32 | 8 | #ifdef _WIN32 |
| 10 | #include <windows.h> | 9 | #include <windows.h> |
| 11 | #include <psapi.h> | 10 | #include <psapi.h> |
| 12 | #include "common/common_funcs.h" | 11 | #include "common/common_funcs.h" |
| 13 | #include "common/string_util.h" | 12 | #include "common/string_util.h" |
| 14 | #else | 13 | #else |
| 15 | #include <cstdlib> | 14 | #include <cstdlib> |
| 16 | #include <sys/mman.h> | 15 | #include <sys/mman.h> |
| 17 | #endif | 16 | #endif |
| 18 | 17 | ||
| 19 | #if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) | 18 | #if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) |
| 20 | #include <unistd.h> | 19 | #include <unistd.h> |
| 21 | #define PAGE_MASK (getpagesize() - 1) | 20 | #define PAGE_MASK (getpagesize() - 1) |
| 22 | #define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) | 21 | #define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK)) |
| 23 | #endif | 22 | #endif |
| 24 | 23 | ||
| 25 | // This is purposely not a full wrapper for virtualalloc/mmap, but it | 24 | // This is purposely not a full wrapper for virtualalloc/mmap, but it |
| 26 | // provides exactly the primitive operations that Dolphin needs. | 25 | // provides exactly the primitive operations that Dolphin needs. |
| 27 | 26 | ||
| 28 | void* AllocateExecutableMemory(size_t size, bool low) | 27 | void* AllocateExecutableMemory(size_t size, bool low) { |
| 29 | { | ||
| 30 | #if defined(_WIN32) | 28 | #if defined(_WIN32) |
| 31 | void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); | 29 | void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE); |
| 32 | #else | 30 | #else |
| @@ -39,31 +37,27 @@ void* AllocateExecutableMemory(size_t size, bool low) | |||
| 39 | // effect of discarding already mapped pages that happen to be in the | 37 | // effect of discarding already mapped pages that happen to be in the |
| 40 | // requested virtual memory range (such as the emulated RAM, sometimes). | 38 | // requested virtual memory range (such as the emulated RAM, sometimes). |
| 41 | if (low && (!map_hint)) | 39 | if (low && (!map_hint)) |
| 42 | map_hint = (char*)round_page(512*1024*1024); /* 0.5 GB rounded up to the next page */ | 40 | map_hint = (char*)round_page(512 * 1024 * 1024); /* 0.5 GB rounded up to the next page */ |
| 43 | #endif | 41 | #endif |
| 44 | void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, | 42 | void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_PRIVATE |
| 45 | MAP_ANON | MAP_PRIVATE | ||
| 46 | #if defined(ARCHITECTURE_X64) && defined(MAP_32BIT) | 43 | #if defined(ARCHITECTURE_X64) && defined(MAP_32BIT) |
| 47 | | (low ? MAP_32BIT : 0) | 44 | | (low ? MAP_32BIT : 0) |
| 48 | #endif | 45 | #endif |
| 49 | , -1, 0); | 46 | , |
| 47 | -1, 0); | ||
| 50 | #endif /* defined(_WIN32) */ | 48 | #endif /* defined(_WIN32) */ |
| 51 | 49 | ||
| 52 | #ifdef _WIN32 | 50 | #ifdef _WIN32 |
| 53 | if (ptr == nullptr) | 51 | if (ptr == nullptr) { |
| 54 | { | ||
| 55 | #else | 52 | #else |
| 56 | if (ptr == MAP_FAILED) | 53 | if (ptr == MAP_FAILED) { |
| 57 | { | ||
| 58 | ptr = nullptr; | 54 | ptr = nullptr; |
| 59 | #endif | 55 | #endif |
| 60 | LOG_ERROR(Common_Memory, "Failed to allocate executable memory"); | 56 | LOG_ERROR(Common_Memory, "Failed to allocate executable memory"); |
| 61 | } | 57 | } |
| 62 | #if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) | 58 | #if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT) |
| 63 | else | 59 | else { |
| 64 | { | 60 | if (low) { |
| 65 | if (low) | ||
| 66 | { | ||
| 67 | map_hint += size; | 61 | map_hint += size; |
| 68 | map_hint = (char*)round_page(map_hint); /* round up to the next page */ | 62 | map_hint = (char*)round_page(map_hint); /* round up to the next page */ |
| 69 | } | 63 | } |
| @@ -78,13 +72,11 @@ void* AllocateExecutableMemory(size_t size, bool low) | |||
| 78 | return ptr; | 72 | return ptr; |
| 79 | } | 73 | } |
| 80 | 74 | ||
| 81 | void* AllocateMemoryPages(size_t size) | 75 | void* AllocateMemoryPages(size_t size) { |
| 82 | { | ||
| 83 | #ifdef _WIN32 | 76 | #ifdef _WIN32 |
| 84 | void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE); | 77 | void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE); |
| 85 | #else | 78 | #else |
| 86 | void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, | 79 | void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0); |
| 87 | MAP_ANON | MAP_PRIVATE, -1, 0); | ||
| 88 | 80 | ||
| 89 | if (ptr == MAP_FAILED) | 81 | if (ptr == MAP_FAILED) |
| 90 | ptr = nullptr; | 82 | ptr = nullptr; |
| @@ -96,10 +88,9 @@ void* AllocateMemoryPages(size_t size) | |||
| 96 | return ptr; | 88 | return ptr; |
| 97 | } | 89 | } |
| 98 | 90 | ||
| 99 | void* AllocateAlignedMemory(size_t size,size_t alignment) | 91 | void* AllocateAlignedMemory(size_t size, size_t alignment) { |
| 100 | { | ||
| 101 | #ifdef _WIN32 | 92 | #ifdef _WIN32 |
| 102 | void* ptr = _aligned_malloc(size,alignment); | 93 | void* ptr = _aligned_malloc(size, alignment); |
| 103 | #else | 94 | #else |
| 104 | void* ptr = nullptr; | 95 | void* ptr = nullptr; |
| 105 | #ifdef ANDROID | 96 | #ifdef ANDROID |
| @@ -116,10 +107,8 @@ void* AllocateAlignedMemory(size_t size,size_t alignment) | |||
| 116 | return ptr; | 107 | return ptr; |
| 117 | } | 108 | } |
| 118 | 109 | ||
| 119 | void FreeMemoryPages(void* ptr, size_t size) | 110 | void FreeMemoryPages(void* ptr, size_t size) { |
| 120 | { | 111 | if (ptr) { |
| 121 | if (ptr) | ||
| 122 | { | ||
| 123 | #ifdef _WIN32 | 112 | #ifdef _WIN32 |
| 124 | if (!VirtualFree(ptr, 0, MEM_RELEASE)) | 113 | if (!VirtualFree(ptr, 0, MEM_RELEASE)) |
| 125 | LOG_ERROR(Common_Memory, "FreeMemoryPages failed!\n%s", GetLastErrorMsg()); | 114 | LOG_ERROR(Common_Memory, "FreeMemoryPages failed!\n%s", GetLastErrorMsg()); |
| @@ -129,20 +118,17 @@ void FreeMemoryPages(void* ptr, size_t size) | |||
| 129 | } | 118 | } |
| 130 | } | 119 | } |
| 131 | 120 | ||
| 132 | void FreeAlignedMemory(void* ptr) | 121 | void FreeAlignedMemory(void* ptr) { |
| 133 | { | 122 | if (ptr) { |
| 134 | if (ptr) | ||
| 135 | { | ||
| 136 | #ifdef _WIN32 | 123 | #ifdef _WIN32 |
| 137 | _aligned_free(ptr); | 124 | _aligned_free(ptr); |
| 138 | #else | 125 | #else |
| 139 | free(ptr); | 126 | free(ptr); |
| 140 | #endif | 127 | #endif |
| 141 | } | 128 | } |
| 142 | } | 129 | } |
| 143 | 130 | ||
| 144 | void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) | 131 | void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) { |
| 145 | { | ||
| 146 | #ifdef _WIN32 | 132 | #ifdef _WIN32 |
| 147 | DWORD oldValue; | 133 | DWORD oldValue; |
| 148 | if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) | 134 | if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue)) |
| @@ -152,19 +138,19 @@ void WriteProtectMemory(void* ptr, size_t size, bool allowExecute) | |||
| 152 | #endif | 138 | #endif |
| 153 | } | 139 | } |
| 154 | 140 | ||
| 155 | void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) | 141 | void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute) { |
| 156 | { | ||
| 157 | #ifdef _WIN32 | 142 | #ifdef _WIN32 |
| 158 | DWORD oldValue; | 143 | DWORD oldValue; |
| 159 | if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue)) | 144 | if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, |
| 145 | &oldValue)) | ||
| 160 | LOG_ERROR(Common_Memory, "UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); | 146 | LOG_ERROR(Common_Memory, "UnWriteProtectMemory failed!\n%s", GetLastErrorMsg()); |
| 161 | #else | 147 | #else |
| 162 | mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); | 148 | mprotect(ptr, size, |
| 149 | allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ); | ||
| 163 | #endif | 150 | #endif |
| 164 | } | 151 | } |
| 165 | 152 | ||
| 166 | std::string MemUsage() | 153 | std::string MemUsage() { |
| 167 | { | ||
| 168 | #ifdef _WIN32 | 154 | #ifdef _WIN32 |
| 169 | #pragma comment(lib, "psapi") | 155 | #pragma comment(lib, "psapi") |
| 170 | DWORD processID = GetCurrentProcessId(); | 156 | DWORD processID = GetCurrentProcessId(); |
| @@ -175,10 +161,12 @@ std::string MemUsage() | |||
| 175 | // Print information about the memory usage of the process. | 161 | // Print information about the memory usage of the process. |
| 176 | 162 | ||
| 177 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); | 163 | hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID); |
| 178 | if (nullptr == hProcess) return "MemUsage Error"; | 164 | if (nullptr == hProcess) |
| 165 | return "MemUsage Error"; | ||
| 179 | 166 | ||
| 180 | if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) | 167 | if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc))) |
| 181 | Ret = Common::StringFromFormat("%s K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); | 168 | Ret = Common::StringFromFormat( |
| 169 | "%s K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str()); | ||
| 182 | 170 | ||
| 183 | CloseHandle(hProcess); | 171 | CloseHandle(hProcess); |
| 184 | return Ret; | 172 | return Ret; |