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, bool force4KBMap)
return MemoryManagementWindows.Reserve((IntPtr)size, viewCompatible, force4KBMap);
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)
if (owner.ForceWindows4KBView)
MemoryManagementWindows.MapView4KB(sharedMemory, srcOffset, address, (IntPtr)size);
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.UnmapView4KB(address, (IntPtr)size);
MemoryManagementWindows.UnmapView(sharedMemory, address, (IntPtr)size, owner);
MemoryManagementUnix.UnmapView(address, size);
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool force4KBMap, bool throwOnFail)
bool result;
if (forView && force4KBMap)
result = MemoryManagementWindows.Reprotect4KB(address, (IntPtr)size, permission, forView);
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, bool force4KBMap)
return MemoryManagementWindows.Free(address, (IntPtr)size, force4KBMap);
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);