MemoryManagement.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. namespace ARMeilleure.Memory
  5. {
  6. public static class MemoryManagement
  7. {
  8. public static bool HasWriteWatchSupport => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
  9. public static IntPtr Allocate(ulong size)
  10. {
  11. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  12. {
  13. IntPtr sizeNint = new IntPtr((long)size);
  14. return MemoryManagementWindows.Allocate(sizeNint);
  15. }
  16. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  17. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  18. {
  19. return MemoryManagementUnix.Allocate(size);
  20. }
  21. else
  22. {
  23. throw new PlatformNotSupportedException();
  24. }
  25. }
  26. public static IntPtr AllocateWriteTracked(ulong size)
  27. {
  28. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  29. {
  30. IntPtr sizeNint = new IntPtr((long)size);
  31. return MemoryManagementWindows.AllocateWriteTracked(sizeNint);
  32. }
  33. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  34. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  35. {
  36. return MemoryManagementUnix.Allocate(size);
  37. }
  38. else
  39. {
  40. throw new PlatformNotSupportedException();
  41. }
  42. }
  43. public static void Reprotect(IntPtr address, ulong size, MemoryProtection permission)
  44. {
  45. bool result;
  46. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  47. {
  48. IntPtr sizeNint = new IntPtr((long)size);
  49. result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
  50. }
  51. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  52. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  53. {
  54. result = MemoryManagementUnix.Reprotect(address, size, permission);
  55. }
  56. else
  57. {
  58. throw new PlatformNotSupportedException();
  59. }
  60. if (!result)
  61. {
  62. throw new MemoryProtectionException(permission);
  63. }
  64. }
  65. public static bool Free(IntPtr address)
  66. {
  67. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  68. {
  69. return MemoryManagementWindows.Free(address);
  70. }
  71. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  72. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  73. {
  74. return MemoryManagementUnix.Free(address);
  75. }
  76. else
  77. {
  78. throw new PlatformNotSupportedException();
  79. }
  80. }
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public static bool GetModifiedPages(
  83. IntPtr address,
  84. IntPtr size,
  85. IntPtr[] addresses,
  86. out ulong count)
  87. {
  88. // This is only supported on windows, but returning
  89. // false (failed) is also valid for platforms without
  90. // write tracking support on the OS.
  91. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  92. {
  93. return MemoryManagementWindows.GetModifiedPages(address, size, addresses, out count);
  94. }
  95. else
  96. {
  97. count = 0;
  98. return false;
  99. }
  100. }
  101. }
  102. }