MemoryManagement.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. namespace Ryujinx.Memory
  3. {
  4. public static class MemoryManagement
  5. {
  6. public static IntPtr Allocate(ulong size)
  7. {
  8. if (OperatingSystem.IsWindows())
  9. {
  10. IntPtr sizeNint = new IntPtr((long)size);
  11. return MemoryManagementWindows.Allocate(sizeNint);
  12. }
  13. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  14. {
  15. return MemoryManagementUnix.Allocate(size);
  16. }
  17. else
  18. {
  19. throw new PlatformNotSupportedException();
  20. }
  21. }
  22. public static IntPtr Reserve(ulong size, bool viewCompatible)
  23. {
  24. if (OperatingSystem.IsWindows())
  25. {
  26. IntPtr sizeNint = new IntPtr((long)size);
  27. return MemoryManagementWindows.Reserve(sizeNint, viewCompatible);
  28. }
  29. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  30. {
  31. return MemoryManagementUnix.Reserve(size);
  32. }
  33. else
  34. {
  35. throw new PlatformNotSupportedException();
  36. }
  37. }
  38. public static bool Commit(IntPtr address, ulong size)
  39. {
  40. if (OperatingSystem.IsWindows())
  41. {
  42. IntPtr sizeNint = new IntPtr((long)size);
  43. return MemoryManagementWindows.Commit(address, sizeNint);
  44. }
  45. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  46. {
  47. return MemoryManagementUnix.Commit(address, size);
  48. }
  49. else
  50. {
  51. throw new PlatformNotSupportedException();
  52. }
  53. }
  54. public static bool Decommit(IntPtr address, ulong size)
  55. {
  56. if (OperatingSystem.IsWindows())
  57. {
  58. IntPtr sizeNint = new IntPtr((long)size);
  59. return MemoryManagementWindows.Decommit(address, sizeNint);
  60. }
  61. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  62. {
  63. return MemoryManagementUnix.Decommit(address, size);
  64. }
  65. else
  66. {
  67. throw new PlatformNotSupportedException();
  68. }
  69. }
  70. public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr address, ulong size, bool force4KBMap)
  71. {
  72. if (OperatingSystem.IsWindows())
  73. {
  74. IntPtr sizeNint = new IntPtr((long)size);
  75. if (force4KBMap)
  76. {
  77. MemoryManagementWindows.MapView4KB(sharedMemory, srcOffset, address, sizeNint);
  78. }
  79. else
  80. {
  81. MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, sizeNint);
  82. }
  83. }
  84. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  85. {
  86. MemoryManagementUnix.MapView(sharedMemory, srcOffset, address, size);
  87. }
  88. else
  89. {
  90. throw new PlatformNotSupportedException();
  91. }
  92. }
  93. public static void UnmapView(IntPtr sharedMemory, IntPtr address, ulong size, bool force4KBMap)
  94. {
  95. if (OperatingSystem.IsWindows())
  96. {
  97. IntPtr sizeNint = new IntPtr((long)size);
  98. if (force4KBMap)
  99. {
  100. MemoryManagementWindows.UnmapView4KB(address, sizeNint);
  101. }
  102. else
  103. {
  104. MemoryManagementWindows.UnmapView(sharedMemory, address, sizeNint);
  105. }
  106. }
  107. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  108. {
  109. MemoryManagementUnix.UnmapView(address, size);
  110. }
  111. else
  112. {
  113. throw new PlatformNotSupportedException();
  114. }
  115. }
  116. public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool force4KBMap, bool throwOnFail)
  117. {
  118. bool result;
  119. if (OperatingSystem.IsWindows())
  120. {
  121. IntPtr sizeNint = new IntPtr((long)size);
  122. if (forView && force4KBMap)
  123. {
  124. result = MemoryManagementWindows.Reprotect4KB(address, sizeNint, permission, forView);
  125. }
  126. else
  127. {
  128. result = MemoryManagementWindows.Reprotect(address, sizeNint, permission, forView);
  129. }
  130. }
  131. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  132. {
  133. result = MemoryManagementUnix.Reprotect(address, size, permission);
  134. }
  135. else
  136. {
  137. throw new PlatformNotSupportedException();
  138. }
  139. if (!result && throwOnFail)
  140. {
  141. throw new MemoryProtectionException(permission);
  142. }
  143. }
  144. public static bool Free(IntPtr address)
  145. {
  146. if (OperatingSystem.IsWindows())
  147. {
  148. return MemoryManagementWindows.Free(address);
  149. }
  150. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  151. {
  152. return MemoryManagementUnix.Free(address);
  153. }
  154. else
  155. {
  156. throw new PlatformNotSupportedException();
  157. }
  158. }
  159. public static IntPtr CreateSharedMemory(ulong size, bool reserve)
  160. {
  161. if (OperatingSystem.IsWindows())
  162. {
  163. IntPtr sizeNint = new IntPtr((long)size);
  164. return MemoryManagementWindows.CreateSharedMemory(sizeNint, reserve);
  165. }
  166. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  167. {
  168. return MemoryManagementUnix.CreateSharedMemory(size, reserve);
  169. }
  170. else
  171. {
  172. throw new PlatformNotSupportedException();
  173. }
  174. }
  175. public static void DestroySharedMemory(IntPtr handle)
  176. {
  177. if (OperatingSystem.IsWindows())
  178. {
  179. MemoryManagementWindows.DestroySharedMemory(handle);
  180. }
  181. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  182. {
  183. MemoryManagementUnix.DestroySharedMemory(handle);
  184. }
  185. else
  186. {
  187. throw new PlatformNotSupportedException();
  188. }
  189. }
  190. public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
  191. {
  192. if (OperatingSystem.IsWindows())
  193. {
  194. return MemoryManagementWindows.MapSharedMemory(handle);
  195. }
  196. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  197. {
  198. return MemoryManagementUnix.MapSharedMemory(handle, size);
  199. }
  200. else
  201. {
  202. throw new PlatformNotSupportedException();
  203. }
  204. }
  205. public static void UnmapSharedMemory(IntPtr address, ulong size)
  206. {
  207. if (OperatingSystem.IsWindows())
  208. {
  209. MemoryManagementWindows.UnmapSharedMemory(address);
  210. }
  211. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  212. {
  213. MemoryManagementUnix.UnmapSharedMemory(address, size);
  214. }
  215. else
  216. {
  217. throw new PlatformNotSupportedException();
  218. }
  219. }
  220. }
  221. }