memory_util.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "common/logging/log.h"
  5. #include "common/memory_util.h"
  6. #ifdef _WIN32
  7. #include <windows.h>
  8. #include <psapi.h>
  9. #include "common/common_funcs.h"
  10. #include "common/string_util.h"
  11. #else
  12. #include <cstdlib>
  13. #include <sys/mman.h>
  14. #endif
  15. #if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT)
  16. #include <unistd.h>
  17. #define PAGE_MASK (getpagesize() - 1)
  18. #define round_page(x) ((((unsigned long)(x)) + PAGE_MASK) & ~(PAGE_MASK))
  19. #endif
  20. // This is purposely not a full wrapper for virtualalloc/mmap, but it
  21. // provides exactly the primitive operations that Dolphin needs.
  22. void* AllocateExecutableMemory(size_t size, bool low)
  23. {
  24. #if defined(_WIN32)
  25. void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  26. #else
  27. static char* map_hint = nullptr;
  28. #if defined(ARCHITECTURE_X64) && !defined(MAP_32BIT)
  29. // This OS has no flag to enforce allocation below the 4 GB boundary,
  30. // but if we hint that we want a low address it is very likely we will
  31. // get one.
  32. // An older version of this code used MAP_FIXED, but that has the side
  33. // effect of discarding already mapped pages that happen to be in the
  34. // requested virtual memory range (such as the emulated RAM, sometimes).
  35. if (low && (!map_hint))
  36. map_hint = (char*)round_page(512*1024*1024); /* 0.5 GB rounded up to the next page */
  37. #endif
  38. void* ptr = mmap(map_hint, size, PROT_READ | PROT_WRITE | PROT_EXEC,
  39. MAP_ANON | MAP_PRIVATE
  40. #if defined(ARCHITECTURE_X64) && defined(MAP_32BIT)
  41. | (low ? MAP_32BIT : 0)
  42. #endif
  43. , -1, 0);
  44. #endif /* defined(_WIN32) */
  45. // printf("Mapped executable memory at %p (size %ld)\n", ptr,
  46. // (unsigned long)size);
  47. #ifdef _WIN32
  48. if (ptr == nullptr)
  49. {
  50. #else
  51. if (ptr == MAP_FAILED)
  52. {
  53. ptr = nullptr;
  54. #endif
  55. LOG_ERROR(Common_Memory, "Failed to allocate executable memory");
  56. }
  57. #if !defined(_WIN32) && defined(ARCHITECTURE_X64) && !defined(MAP_32BIT)
  58. else
  59. {
  60. if (low)
  61. {
  62. map_hint += size;
  63. map_hint = (char*)round_page(map_hint); /* round up to the next page */
  64. // printf("Next map will (hopefully) be at %p\n", map_hint);
  65. }
  66. }
  67. #endif
  68. #if EMU_ARCH_BITS == 64
  69. if ((u64)ptr >= 0x80000000 && low == true)
  70. LOG_ERROR(Common_Memory, "Executable memory ended up above 2GB!");
  71. #endif
  72. return ptr;
  73. }
  74. void* AllocateMemoryPages(size_t size)
  75. {
  76. #ifdef _WIN32
  77. void* ptr = VirtualAlloc(nullptr, size, MEM_COMMIT, PAGE_READWRITE);
  78. #else
  79. void* ptr = mmap(nullptr, size, PROT_READ | PROT_WRITE,
  80. MAP_ANON | MAP_PRIVATE, -1, 0);
  81. if (ptr == MAP_FAILED)
  82. ptr = nullptr;
  83. #endif
  84. // printf("Mapped memory at %p (size %ld)\n", ptr,
  85. // (unsigned long)size);
  86. if (ptr == nullptr)
  87. LOG_ERROR(Common_Memory, "Failed to allocate raw memory");
  88. return ptr;
  89. }
  90. void* AllocateAlignedMemory(size_t size,size_t alignment)
  91. {
  92. #ifdef _WIN32
  93. void* ptr = _aligned_malloc(size,alignment);
  94. #else
  95. void* ptr = nullptr;
  96. #ifdef ANDROID
  97. ptr = memalign(alignment, size);
  98. #else
  99. if (posix_memalign(&ptr, alignment, size) != 0)
  100. LOG_ERROR(Common_Memory, "Failed to allocate aligned memory");
  101. #endif
  102. #endif
  103. // printf("Mapped memory at %p (size %ld)\n", ptr,
  104. // (unsigned long)size);
  105. if (ptr == nullptr)
  106. LOG_ERROR(Common_Memory, "Failed to allocate aligned memory");
  107. return ptr;
  108. }
  109. void FreeMemoryPages(void* ptr, size_t size)
  110. {
  111. if (ptr)
  112. {
  113. #ifdef _WIN32
  114. if (!VirtualFree(ptr, 0, MEM_RELEASE))
  115. LOG_ERROR(Common_Memory, "FreeMemoryPages failed!\n%s", GetLastErrorMsg());
  116. ptr = nullptr; // Is this our responsibility?
  117. #else
  118. munmap(ptr, size);
  119. #endif
  120. }
  121. }
  122. void FreeAlignedMemory(void* ptr)
  123. {
  124. if (ptr)
  125. {
  126. #ifdef _WIN32
  127. _aligned_free(ptr);
  128. #else
  129. free(ptr);
  130. #endif
  131. }
  132. }
  133. void WriteProtectMemory(void* ptr, size_t size, bool allowExecute)
  134. {
  135. #ifdef _WIN32
  136. DWORD oldValue;
  137. if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READ : PAGE_READONLY, &oldValue))
  138. LOG_ERROR(Common_Memory, "WriteProtectMemory failed!\n%s", GetLastErrorMsg());
  139. #else
  140. mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_EXEC) : PROT_READ);
  141. #endif
  142. }
  143. void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute)
  144. {
  145. #ifdef _WIN32
  146. DWORD oldValue;
  147. if (!VirtualProtect(ptr, size, allowExecute ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, &oldValue))
  148. LOG_ERROR(Common_Memory, "UnWriteProtectMemory failed!\n%s", GetLastErrorMsg());
  149. #else
  150. mprotect(ptr, size, allowExecute ? (PROT_READ | PROT_WRITE | PROT_EXEC) : PROT_WRITE | PROT_READ);
  151. #endif
  152. }
  153. std::string MemUsage()
  154. {
  155. #ifdef _WIN32
  156. #pragma comment(lib, "psapi")
  157. DWORD processID = GetCurrentProcessId();
  158. HANDLE hProcess;
  159. PROCESS_MEMORY_COUNTERS pmc;
  160. std::string Ret;
  161. // Print information about the memory usage of the process.
  162. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID);
  163. if (nullptr == hProcess) return "MemUsage Error";
  164. if (GetProcessMemoryInfo(hProcess, &pmc, sizeof(pmc)))
  165. Ret = Common::StringFromFormat("%s K", Common::ThousandSeparate(pmc.WorkingSetSize / 1024, 7).c_str());
  166. CloseHandle(hProcess);
  167. return Ret;
  168. #else
  169. return "";
  170. #endif
  171. }