using System;
using System.Runtime.InteropServices;
namespace Ryujinx.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 Reserve(ulong size)
return MemoryManagementWindows.Reserve(sizeNint);
return MemoryManagementUnix.Reserve(size);
public static bool Commit(IntPtr address, ulong size)
return MemoryManagementWindows.Commit(address, sizeNint);
return MemoryManagementUnix.Commit(address, size);
public static bool Decommit(IntPtr address, ulong size)
return MemoryManagementWindows.Decommit(address, sizeNint);
return MemoryManagementUnix.Decommit(address, size);
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool throwOnFail)
bool result;
result = MemoryManagementWindows.Reprotect(address, sizeNint, permission);
result = MemoryManagementUnix.Reprotect(address, size, permission);
if (!result && throwOnFail)
throw new MemoryProtectionException(permission);
public static bool Free(IntPtr address)
return MemoryManagementWindows.Free(address);
return MemoryManagementUnix.Free(address);
public static IntPtr CreateSharedMemory(ulong size, bool reserve)
return MemoryManagementWindows.CreateSharedMemory(sizeNint, reserve);
return MemoryManagementUnix.CreateSharedMemory(size, reserve);
public static void DestroySharedMemory(IntPtr handle)
MemoryManagementWindows.DestroySharedMemory(handle);
MemoryManagementUnix.DestroySharedMemory(handle);
public static IntPtr MapSharedMemory(IntPtr handle)
return MemoryManagementWindows.MapSharedMemory(handle);
return MemoryManagementUnix.MapSharedMemory(handle);
public static void UnmapSharedMemory(IntPtr address)
MemoryManagementWindows.UnmapSharedMemory(address);
MemoryManagementUnix.UnmapSharedMemory(address);
public static IntPtr Remap(IntPtr target, IntPtr source, ulong size)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ||
return MemoryManagementUnix.Remap(target, source, size);