MemoryManagement.cs 7.3 KB

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