memory_util.cpp 5.1 KB

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