MemoryManagement.cs 6.9 KB

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