memory_util.cpp 5.1 KB

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