MemoryManagement.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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)
  22. {
  23. if (OperatingSystem.IsWindows())
  24. {
  25. return MemoryManagementWindows.Reserve((IntPtr)size, viewCompatible);
  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. MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, (IntPtr)size, owner);
  71. }
  72. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  73. {
  74. MemoryManagementUnix.MapView(sharedMemory, srcOffset, address, size);
  75. }
  76. else
  77. {
  78. throw new PlatformNotSupportedException();
  79. }
  80. }
  81. public static void UnmapView(IntPtr sharedMemory, IntPtr address, ulong size, MemoryBlock owner)
  82. {
  83. if (OperatingSystem.IsWindows())
  84. {
  85. MemoryManagementWindows.UnmapView(sharedMemory, address, (IntPtr)size, owner);
  86. }
  87. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  88. {
  89. MemoryManagementUnix.UnmapView(address, size);
  90. }
  91. else
  92. {
  93. throw new PlatformNotSupportedException();
  94. }
  95. }
  96. public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool throwOnFail)
  97. {
  98. bool result;
  99. if (OperatingSystem.IsWindows())
  100. {
  101. result = MemoryManagementWindows.Reprotect(address, (IntPtr)size, permission, forView);
  102. }
  103. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  104. {
  105. result = MemoryManagementUnix.Reprotect(address, size, permission);
  106. }
  107. else
  108. {
  109. throw new PlatformNotSupportedException();
  110. }
  111. if (!result && throwOnFail)
  112. {
  113. throw new MemoryProtectionException(permission);
  114. }
  115. }
  116. public static bool Free(IntPtr address, ulong size)
  117. {
  118. if (OperatingSystem.IsWindows())
  119. {
  120. return MemoryManagementWindows.Free(address, (IntPtr)size);
  121. }
  122. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  123. {
  124. return MemoryManagementUnix.Free(address);
  125. }
  126. else
  127. {
  128. throw new PlatformNotSupportedException();
  129. }
  130. }
  131. public static IntPtr CreateSharedMemory(ulong size, bool reserve)
  132. {
  133. if (OperatingSystem.IsWindows())
  134. {
  135. return MemoryManagementWindows.CreateSharedMemory((IntPtr)size, reserve);
  136. }
  137. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  138. {
  139. return MemoryManagementUnix.CreateSharedMemory(size, reserve);
  140. }
  141. else
  142. {
  143. throw new PlatformNotSupportedException();
  144. }
  145. }
  146. public static void DestroySharedMemory(IntPtr handle)
  147. {
  148. if (OperatingSystem.IsWindows())
  149. {
  150. MemoryManagementWindows.DestroySharedMemory(handle);
  151. }
  152. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  153. {
  154. MemoryManagementUnix.DestroySharedMemory(handle);
  155. }
  156. else
  157. {
  158. throw new PlatformNotSupportedException();
  159. }
  160. }
  161. public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
  162. {
  163. if (OperatingSystem.IsWindows())
  164. {
  165. return MemoryManagementWindows.MapSharedMemory(handle);
  166. }
  167. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  168. {
  169. return MemoryManagementUnix.MapSharedMemory(handle, size);
  170. }
  171. else
  172. {
  173. throw new PlatformNotSupportedException();
  174. }
  175. }
  176. public static void UnmapSharedMemory(IntPtr address, ulong size)
  177. {
  178. if (OperatingSystem.IsWindows())
  179. {
  180. MemoryManagementWindows.UnmapSharedMemory(address);
  181. }
  182. else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
  183. {
  184. MemoryManagementUnix.UnmapSharedMemory(address, size);
  185. }
  186. else
  187. {
  188. throw new PlatformNotSupportedException();
  189. }
  190. }
  191. }
  192. }