using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace ARMeilleure.Memory
{
public static class MemoryManagement
public static IntPtr Allocate(ulong size)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
IntPtr sizeNint = new IntPtr((long)size);
return MemoryManagementWindows.Allocate(sizeNint);
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
return MemoryManagementUnix.Allocate(size);
else
throw new PlatformNotSupportedException();
public static IntPtr AllocateWriteTracked(ulong size)
return MemoryManagementWindows.AllocateWriteTracked(sizeNint);
public static bool Commit(IntPtr address, ulong size)
return MemoryManagementWindows.Commit(address, sizeNint);
return MemoryManagementUnix.Commit(address, size);
public static void Reprotect(IntPtr address, ulong size, MemoryProtection permission)
bool result;
result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
result = MemoryManagementUnix.Reprotect(address, size, permission);
if (!result)
throw new MemoryProtectionException(permission);
public static IntPtr Reserve(ulong size)
return MemoryManagementWindows.Reserve(sizeNint);
return MemoryManagementUnix.Reserve(size);
public static bool Free(IntPtr address)
return MemoryManagementWindows.Free(address);
return MemoryManagementUnix.Free(address);