MemoryManagement.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 void Reprotect(IntPtr address, ulong size, MemoryPermission permission)
  59. {
  60. bool result;
  61. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  62. {
  63. IntPtr sizeNint = new IntPtr((long)size);
  64. result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
  65. }
  66. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  67. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  68. {
  69. result = MemoryManagementUnix.Reprotect(address, size, permission);
  70. }
  71. else
  72. {
  73. throw new PlatformNotSupportedException();
  74. }
  75. if (!result)
  76. {
  77. throw new MemoryProtectionException(permission);
  78. }
  79. }
  80. public static bool Free(IntPtr address)
  81. {
  82. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  83. {
  84. return MemoryManagementWindows.Free(address);
  85. }
  86. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  87. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  88. {
  89. return MemoryManagementUnix.Free(address);
  90. }
  91. else
  92. {
  93. throw new PlatformNotSupportedException();
  94. }
  95. }
  96. }
  97. }