MemoryManagement.cs 6.1 KB

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