using System;
namespace Ryujinx.Memory
{
public static class MemoryManagement
public static IntPtr Allocate(ulong size)
if (OperatingSystem.IsWindows())
return MemoryManagementWindows.Allocate((IntPtr)size);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
return MemoryManagementUnix.Allocate(size);
else
throw new PlatformNotSupportedException();
public static IntPtr Reserve(ulong size, bool viewCompatible)
return MemoryManagementWindows.Reserve((IntPtr)size, viewCompatible);
return MemoryManagementUnix.Reserve(size);
public static bool Commit(IntPtr address, ulong size)
return MemoryManagementWindows.Commit(address, (IntPtr)size);
return MemoryManagementUnix.Commit(address, size);
public static bool Decommit(IntPtr address, ulong size)
return MemoryManagementWindows.Decommit(address, (IntPtr)size);
return MemoryManagementUnix.Decommit(address, size);
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr address, ulong size, MemoryBlock owner)
MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, (IntPtr)size, owner);
MemoryManagementUnix.MapView(sharedMemory, srcOffset, address, size);
public static void UnmapView(IntPtr sharedMemory, IntPtr address, ulong size, MemoryBlock owner)
MemoryManagementWindows.UnmapView(sharedMemory, address, (IntPtr)size, owner);
MemoryManagementUnix.UnmapView(address, size);
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool throwOnFail)
bool result;
result = MemoryManagementWindows.Reprotect(address, (IntPtr)size, permission, forView);
result = MemoryManagementUnix.Reprotect(address, size, permission);
if (!result && throwOnFail)
throw new MemoryProtectionException(permission);
public static bool Free(IntPtr address, ulong size)
return MemoryManagementWindows.Free(address, (IntPtr)size);
return MemoryManagementUnix.Free(address);
public static IntPtr CreateSharedMemory(ulong size, bool reserve)
return MemoryManagementWindows.CreateSharedMemory((IntPtr)size, reserve);
return MemoryManagementUnix.CreateSharedMemory(size, reserve);
public static void DestroySharedMemory(IntPtr handle)
MemoryManagementWindows.DestroySharedMemory(handle);
MemoryManagementUnix.DestroySharedMemory(handle);
public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
return MemoryManagementWindows.MapSharedMemory(handle);
return MemoryManagementUnix.MapSharedMemory(handle, size);
public static void UnmapSharedMemory(IntPtr address, ulong size)
MemoryManagementWindows.UnmapSharedMemory(address);
MemoryManagementUnix.UnmapSharedMemory(address, size);