MemoryManagement.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 IntPtr Allocate(ulong size)
  9. {
  10. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  11. {
  12. IntPtr sizeNint = new IntPtr((long)size);
  13. return MemoryManagementWindows.Allocate(sizeNint);
  14. }
  15. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  16. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  17. {
  18. return MemoryManagementUnix.Allocate(size);
  19. }
  20. else
  21. {
  22. throw new PlatformNotSupportedException();
  23. }
  24. }
  25. public static IntPtr AllocateWriteTracked(ulong size)
  26. {
  27. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  28. {
  29. IntPtr sizeNint = new IntPtr((long)size);
  30. return MemoryManagementWindows.AllocateWriteTracked(sizeNint);
  31. }
  32. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  33. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  34. {
  35. return MemoryManagementUnix.Allocate(size);
  36. }
  37. else
  38. {
  39. throw new PlatformNotSupportedException();
  40. }
  41. }
  42. public static bool Commit(IntPtr address, ulong size)
  43. {
  44. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  45. {
  46. IntPtr sizeNint = new IntPtr((long)size);
  47. return MemoryManagementWindows.Commit(address, sizeNint);
  48. }
  49. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  50. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  51. {
  52. return MemoryManagementUnix.Commit(address, size);
  53. }
  54. else
  55. {
  56. throw new PlatformNotSupportedException();
  57. }
  58. }
  59. public static void Reprotect(IntPtr address, ulong size, MemoryProtection permission)
  60. {
  61. bool result;
  62. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  63. {
  64. IntPtr sizeNint = new IntPtr((long)size);
  65. result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
  66. }
  67. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  68. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  69. {
  70. result = MemoryManagementUnix.Reprotect(address, size, permission);
  71. }
  72. else
  73. {
  74. throw new PlatformNotSupportedException();
  75. }
  76. if (!result)
  77. {
  78. throw new MemoryProtectionException(permission);
  79. }
  80. }
  81. public static IntPtr Reserve(ulong size)
  82. {
  83. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  84. {
  85. IntPtr sizeNint = new IntPtr((long)size);
  86. return MemoryManagementWindows.Reserve(sizeNint);
  87. }
  88. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  89. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  90. {
  91. return MemoryManagementUnix.Reserve(size);
  92. }
  93. else
  94. {
  95. throw new PlatformNotSupportedException();
  96. }
  97. }
  98. public static bool Free(IntPtr address)
  99. {
  100. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  101. {
  102. return MemoryManagementWindows.Free(address);
  103. }
  104. else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
  105. RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
  106. {
  107. return MemoryManagementUnix.Free(address);
  108. }
  109. else
  110. {
  111. throw new PlatformNotSupportedException();
  112. }
  113. }
  114. }
  115. }