misc: Replace references to IntPtr/UIntPtr with nint/nuint + code cleanups.

This commit is contained in:
Evan Husted 2024-10-26 08:46:41 -05:00
parent a09d314817
commit dfb4854d19
172 changed files with 902 additions and 914 deletions

View file

@ -13,13 +13,13 @@ namespace Ryujinx.Memory
private readonly bool _isMirror;
private readonly bool _viewCompatible;
private readonly bool _forJit;
private IntPtr _sharedMemory;
private IntPtr _pointer;
private nint _sharedMemory;
private nint _pointer;
/// <summary>
/// Pointer to the memory block data.
/// </summary>
public IntPtr Pointer => _pointer;
public nint Pointer => _pointer;
/// <summary>
/// Size of the memory block.
@ -68,7 +68,7 @@ namespace Ryujinx.Memory
/// <param name="sharedMemory">Shared memory to use as backing storage for this block</param>
/// <exception cref="SystemException">Throw when there's an error while mapping the shared memory</exception>
/// <exception cref="PlatformNotSupportedException">Throw when the current platform is not supported</exception>
private MemoryBlock(ulong size, IntPtr sharedMemory)
private MemoryBlock(ulong size, nint sharedMemory)
{
_pointer = MemoryManagement.MapSharedMemory(sharedMemory, size);
Size = size;
@ -86,7 +86,7 @@ namespace Ryujinx.Memory
/// <exception cref="PlatformNotSupportedException">Throw when the current platform is not supported</exception>
public MemoryBlock CreateMirror()
{
if (_sharedMemory == IntPtr.Zero)
if (_sharedMemory == nint.Zero)
{
throw new NotSupportedException("Mirroring is not supported on the memory block because the Mirrorable flag was not set.");
}
@ -134,7 +134,7 @@ namespace Ryujinx.Memory
/// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
public void MapView(MemoryBlock srcBlock, ulong srcOffset, ulong dstOffset, ulong size)
{
if (srcBlock._sharedMemory == IntPtr.Zero)
if (srcBlock._sharedMemory == nint.Zero)
{
throw new ArgumentException("The source memory block is not mirrorable, and thus cannot be mapped on the current block.");
}
@ -273,9 +273,9 @@ namespace Ryujinx.Memory
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe ref T GetRef<T>(ulong offset) where T : unmanaged
{
IntPtr ptr = _pointer;
nint ptr = _pointer;
ObjectDisposedException.ThrowIf(ptr == IntPtr.Zero, this);
ObjectDisposedException.ThrowIf(ptr == nint.Zero, this);
int size = Unsafe.SizeOf<T>();
@ -298,14 +298,14 @@ namespace Ryujinx.Memory
/// <exception cref="ObjectDisposedException">Throw when the memory block has already been disposed</exception>
/// <exception cref="InvalidMemoryRegionException">Throw when either <paramref name="offset"/> or <paramref name="size"/> are out of range</exception>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public IntPtr GetPointer(ulong offset, ulong size) => GetPointerInternal(offset, size);
public nint GetPointer(ulong offset, ulong size) => GetPointerInternal(offset, size);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private IntPtr GetPointerInternal(ulong offset, ulong size)
private nint GetPointerInternal(ulong offset, ulong size)
{
IntPtr ptr = _pointer;
nint ptr = _pointer;
ObjectDisposedException.ThrowIf(ptr == IntPtr.Zero, this);
ObjectDisposedException.ThrowIf(ptr == nint.Zero, this);
ulong endOffset = offset + size;
@ -364,9 +364,9 @@ namespace Ryujinx.Memory
/// <param name="pointer">Native pointer</param>
/// <param name="offset">Offset to add</param>
/// <returns>Native pointer with the added offset</returns>
private static IntPtr PtrAddr(IntPtr pointer, ulong offset)
private static nint PtrAddr(nint pointer, ulong offset)
{
return new IntPtr(pointer.ToInt64() + (long)offset);
return new nint(pointer.ToInt64() + (long)offset);
}
/// <summary>
@ -386,10 +386,10 @@ namespace Ryujinx.Memory
private void FreeMemory()
{
IntPtr ptr = Interlocked.Exchange(ref _pointer, IntPtr.Zero);
nint ptr = Interlocked.Exchange(ref _pointer, nint.Zero);
// If pointer is null, the memory was already freed or never allocated.
if (ptr != IntPtr.Zero)
if (ptr != nint.Zero)
{
if (_usesSharedMemory)
{
@ -403,9 +403,9 @@ namespace Ryujinx.Memory
if (!_isMirror)
{
IntPtr sharedMemory = Interlocked.Exchange(ref _sharedMemory, IntPtr.Zero);
nint sharedMemory = Interlocked.Exchange(ref _sharedMemory, nint.Zero);
if (sharedMemory != IntPtr.Zero)
if (sharedMemory != nint.Zero)
{
MemoryManagement.DestroySharedMemory(sharedMemory);
}

View file

@ -4,11 +4,11 @@ namespace Ryujinx.Memory
{
public static class MemoryManagement
{
public static IntPtr Allocate(ulong size, bool forJit)
public static nint Allocate(ulong size, bool forJit)
{
if (OperatingSystem.IsWindows())
{
return MemoryManagementWindows.Allocate((IntPtr)size);
return MemoryManagementWindows.Allocate((nint)size);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -20,11 +20,11 @@ namespace Ryujinx.Memory
}
}
public static IntPtr Reserve(ulong size, bool forJit, bool viewCompatible)
public static nint Reserve(ulong size, bool forJit, bool viewCompatible)
{
if (OperatingSystem.IsWindows())
{
return MemoryManagementWindows.Reserve((IntPtr)size, viewCompatible);
return MemoryManagementWindows.Reserve((nint)size, viewCompatible);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -36,11 +36,11 @@ namespace Ryujinx.Memory
}
}
public static void Commit(IntPtr address, ulong size, bool forJit)
public static void Commit(nint address, ulong size, bool forJit)
{
if (OperatingSystem.IsWindows())
{
MemoryManagementWindows.Commit(address, (IntPtr)size);
MemoryManagementWindows.Commit(address, (nint)size);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -52,11 +52,11 @@ namespace Ryujinx.Memory
}
}
public static void Decommit(IntPtr address, ulong size)
public static void Decommit(nint address, ulong size)
{
if (OperatingSystem.IsWindows())
{
MemoryManagementWindows.Decommit(address, (IntPtr)size);
MemoryManagementWindows.Decommit(address, (nint)size);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -68,11 +68,11 @@ namespace Ryujinx.Memory
}
}
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr address, ulong size, MemoryBlock owner)
public static void MapView(nint sharedMemory, ulong srcOffset, nint address, ulong size, MemoryBlock owner)
{
if (OperatingSystem.IsWindows())
{
MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, (IntPtr)size, owner);
MemoryManagementWindows.MapView(sharedMemory, srcOffset, address, (nint)size, owner);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -84,11 +84,11 @@ namespace Ryujinx.Memory
}
}
public static void UnmapView(IntPtr sharedMemory, IntPtr address, ulong size, MemoryBlock owner)
public static void UnmapView(nint sharedMemory, nint address, ulong size, MemoryBlock owner)
{
if (OperatingSystem.IsWindows())
{
MemoryManagementWindows.UnmapView(sharedMemory, address, (IntPtr)size, owner);
MemoryManagementWindows.UnmapView(sharedMemory, address, (nint)size, owner);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -100,13 +100,13 @@ namespace Ryujinx.Memory
}
}
public static void Reprotect(IntPtr address, ulong size, MemoryPermission permission, bool forView, bool throwOnFail)
public static void Reprotect(nint address, ulong size, MemoryPermission permission, bool forView, bool throwOnFail)
{
bool result;
if (OperatingSystem.IsWindows())
{
result = MemoryManagementWindows.Reprotect(address, (IntPtr)size, permission, forView);
result = MemoryManagementWindows.Reprotect(address, (nint)size, permission, forView);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -123,11 +123,11 @@ namespace Ryujinx.Memory
}
}
public static bool Free(IntPtr address, ulong size)
public static bool Free(nint address, ulong size)
{
if (OperatingSystem.IsWindows())
{
return MemoryManagementWindows.Free(address, (IntPtr)size);
return MemoryManagementWindows.Free(address, (nint)size);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -139,11 +139,11 @@ namespace Ryujinx.Memory
}
}
public static IntPtr CreateSharedMemory(ulong size, bool reserve)
public static nint CreateSharedMemory(ulong size, bool reserve)
{
if (OperatingSystem.IsWindows())
{
return MemoryManagementWindows.CreateSharedMemory((IntPtr)size, reserve);
return MemoryManagementWindows.CreateSharedMemory((nint)size, reserve);
}
else if (OperatingSystem.IsLinux() || OperatingSystem.IsMacOS())
{
@ -155,7 +155,7 @@ namespace Ryujinx.Memory
}
}
public static void DestroySharedMemory(IntPtr handle)
public static void DestroySharedMemory(nint handle)
{
if (OperatingSystem.IsWindows())
{
@ -171,7 +171,7 @@ namespace Ryujinx.Memory
}
}
public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
public static nint MapSharedMemory(nint handle, ulong size)
{
if (OperatingSystem.IsWindows())
{
@ -187,7 +187,7 @@ namespace Ryujinx.Memory
}
}
public static void UnmapSharedMemory(IntPtr address, ulong size)
public static void UnmapSharedMemory(nint address, ulong size)
{
if (OperatingSystem.IsWindows())
{

View file

@ -10,19 +10,19 @@ namespace Ryujinx.Memory
[SupportedOSPlatform("macos")]
static class MemoryManagementUnix
{
private static readonly ConcurrentDictionary<IntPtr, ulong> _allocations = new();
private static readonly ConcurrentDictionary<nint, ulong> _allocations = new();
public static IntPtr Allocate(ulong size, bool forJit)
public static nint Allocate(ulong size, bool forJit)
{
return AllocateInternal(size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, forJit);
}
public static IntPtr Reserve(ulong size, bool forJit)
public static nint Reserve(ulong size, bool forJit)
{
return AllocateInternal(size, MmapProts.PROT_NONE, forJit);
}
private static IntPtr AllocateInternal(ulong size, MmapProts prot, bool forJit, bool shared = false)
private static nint AllocateInternal(ulong size, MmapProts prot, bool forJit, bool shared = false)
{
MmapFlags flags = MmapFlags.MAP_ANONYMOUS;
@ -50,7 +50,7 @@ namespace Ryujinx.Memory
}
}
IntPtr ptr = Mmap(IntPtr.Zero, size, prot, flags, -1, 0);
nint ptr = Mmap(nint.Zero, size, prot, flags, -1, 0);
if (ptr == MAP_FAILED)
{
@ -66,7 +66,7 @@ namespace Ryujinx.Memory
return ptr;
}
public static void Commit(IntPtr address, ulong size, bool forJit)
public static void Commit(nint address, ulong size, bool forJit)
{
MmapProts prot = MmapProts.PROT_READ | MmapProts.PROT_WRITE;
@ -81,7 +81,7 @@ namespace Ryujinx.Memory
}
}
public static void Decommit(IntPtr address, ulong size)
public static void Decommit(nint address, ulong size)
{
// Must be writable for madvise to work properly.
if (mprotect(address, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE) != 0)
@ -100,7 +100,7 @@ namespace Ryujinx.Memory
}
}
public static bool Reprotect(IntPtr address, ulong size, MemoryPermission permission)
public static bool Reprotect(nint address, ulong size, MemoryPermission permission)
{
return mprotect(address, size, GetProtection(permission)) == 0;
}
@ -119,7 +119,7 @@ namespace Ryujinx.Memory
};
}
public static bool Free(IntPtr address)
public static bool Free(nint address)
{
if (_allocations.TryRemove(address, out ulong size))
{
@ -129,12 +129,12 @@ namespace Ryujinx.Memory
return false;
}
public static bool Unmap(IntPtr address, ulong size)
public static bool Unmap(nint address, ulong size)
{
return munmap(address, size) == 0;
}
public unsafe static IntPtr CreateSharedMemory(ulong size, bool reserve)
public unsafe static nint CreateSharedMemory(ulong size, bool reserve)
{
int fd;
@ -144,13 +144,13 @@ namespace Ryujinx.Memory
fixed (byte* pMemName = memName)
{
fd = shm_open((IntPtr)pMemName, 0x2 | 0x200 | 0x800 | 0x400, 384); // O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600
fd = shm_open((nint)pMemName, 0x2 | 0x200 | 0x800 | 0x400, 384); // O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0600
if (fd == -1)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
if (shm_unlink((IntPtr)pMemName) != 0)
if (shm_unlink((nint)pMemName) != 0)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
@ -162,20 +162,20 @@ namespace Ryujinx.Memory
fixed (byte* pFileName = fileName)
{
fd = mkstemp((IntPtr)pFileName);
fd = mkstemp((nint)pFileName);
if (fd == -1)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
if (unlink((IntPtr)pFileName) != 0)
if (unlink((nint)pFileName) != 0)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
}
}
if (ftruncate(fd, (IntPtr)size) != 0)
if (ftruncate(fd, (nint)size) != 0)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
@ -183,27 +183,27 @@ namespace Ryujinx.Memory
return fd;
}
public static void DestroySharedMemory(IntPtr handle)
public static void DestroySharedMemory(nint handle)
{
close(handle.ToInt32());
}
public static IntPtr MapSharedMemory(IntPtr handle, ulong size)
public static nint MapSharedMemory(nint handle, ulong size)
{
return Mmap(IntPtr.Zero, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_SHARED, handle.ToInt32(), 0);
return Mmap(nint.Zero, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_SHARED, handle.ToInt32(), 0);
}
public static void UnmapSharedMemory(IntPtr address, ulong size)
public static void UnmapSharedMemory(nint address, ulong size)
{
munmap(address, size);
}
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, ulong size)
public static void MapView(nint sharedMemory, ulong srcOffset, nint location, ulong size)
{
Mmap(location, size, MmapProts.PROT_READ | MmapProts.PROT_WRITE, MmapFlags.MAP_FIXED | MmapFlags.MAP_SHARED, sharedMemory.ToInt32(), (long)srcOffset);
}
public static void UnmapView(IntPtr location, ulong size)
public static void UnmapView(nint location, ulong size)
{
Mmap(location, size, MmapProts.PROT_NONE, MmapFlags.MAP_FIXED | MmapFlags.MAP_PRIVATE | MmapFlags.MAP_ANONYMOUS | MmapFlags.MAP_NORESERVE, -1, 0);
}

View file

@ -12,16 +12,16 @@ namespace Ryujinx.Memory
private static readonly PlaceholderManager _placeholders = new();
public static IntPtr Allocate(IntPtr size)
public static nint Allocate(nint size)
{
return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit);
}
public static IntPtr Reserve(IntPtr size, bool viewCompatible)
public static nint Reserve(nint size, bool viewCompatible)
{
if (viewCompatible)
{
IntPtr baseAddress = AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);
nint baseAddress = AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);
_placeholders.ReserveRange((ulong)baseAddress, (ulong)size);
@ -31,11 +31,11 @@ namespace Ryujinx.Memory
return AllocateInternal(size, AllocationType.Reserve);
}
private static IntPtr AllocateInternal(IntPtr size, AllocationType flags = 0)
private static nint AllocateInternal(nint size, AllocationType flags = 0)
{
IntPtr ptr = WindowsApi.VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
nint ptr = WindowsApi.VirtualAlloc(nint.Zero, size, flags, MemoryProtection.ReadWrite);
if (ptr == IntPtr.Zero)
if (ptr == nint.Zero)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
@ -43,11 +43,11 @@ namespace Ryujinx.Memory
return ptr;
}
private static IntPtr AllocateInternal2(IntPtr size, AllocationType flags = 0)
private static nint AllocateInternal2(nint size, AllocationType flags = 0)
{
IntPtr ptr = WindowsApi.VirtualAlloc2(WindowsApi.CurrentProcessHandle, IntPtr.Zero, size, flags, MemoryProtection.NoAccess, IntPtr.Zero, 0);
nint ptr = WindowsApi.VirtualAlloc2(WindowsApi.CurrentProcessHandle, nint.Zero, size, flags, MemoryProtection.NoAccess, nint.Zero, 0);
if (ptr == IntPtr.Zero)
if (ptr == nint.Zero)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
@ -55,15 +55,15 @@ namespace Ryujinx.Memory
return ptr;
}
public static void Commit(IntPtr location, IntPtr size)
public static void Commit(nint location, nint size)
{
if (WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) == IntPtr.Zero)
if (WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) == nint.Zero)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
}
public static void Decommit(IntPtr location, IntPtr size)
public static void Decommit(nint location, nint size)
{
if (!WindowsApi.VirtualFree(location, size, AllocationType.Decommit))
{
@ -71,17 +71,17 @@ namespace Ryujinx.Memory
}
}
public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, MemoryBlock owner)
public static void MapView(nint sharedMemory, ulong srcOffset, nint location, nint size, MemoryBlock owner)
{
_placeholders.MapView(sharedMemory, srcOffset, location, size, owner);
}
public static void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner)
public static void UnmapView(nint sharedMemory, nint location, nint size, MemoryBlock owner)
{
_placeholders.UnmapView(sharedMemory, location, size, owner);
}
public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
public static bool Reprotect(nint address, nint size, MemoryPermission permission, bool forView)
{
if (forView)
{
@ -93,26 +93,26 @@ namespace Ryujinx.Memory
}
}
public static bool Free(IntPtr address, IntPtr size)
public static bool Free(nint address, nint size)
{
_placeholders.UnreserveRange((ulong)address, (ulong)size);
return WindowsApi.VirtualFree(address, IntPtr.Zero, AllocationType.Release);
return WindowsApi.VirtualFree(address, nint.Zero, AllocationType.Release);
}
public static IntPtr CreateSharedMemory(IntPtr size, bool reserve)
public static nint CreateSharedMemory(nint size, bool reserve)
{
var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
IntPtr handle = WindowsApi.CreateFileMapping(
nint handle = WindowsApi.CreateFileMapping(
WindowsApi.InvalidHandleValue,
IntPtr.Zero,
nint.Zero,
FileMapProtection.PageReadWrite | prot,
(uint)(size.ToInt64() >> 32),
(uint)size.ToInt64(),
null);
if (handle == IntPtr.Zero)
if (handle == nint.Zero)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
@ -120,7 +120,7 @@ namespace Ryujinx.Memory
return handle;
}
public static void DestroySharedMemory(IntPtr handle)
public static void DestroySharedMemory(nint handle)
{
if (!WindowsApi.CloseHandle(handle))
{
@ -128,11 +128,11 @@ namespace Ryujinx.Memory
}
}
public static IntPtr MapSharedMemory(IntPtr handle)
public static nint MapSharedMemory(nint handle)
{
IntPtr ptr = WindowsApi.MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);
nint ptr = WindowsApi.MapViewOfFile(handle, 4 | 2, 0, 0, nint.Zero);
if (ptr == IntPtr.Zero)
if (ptr == nint.Zero)
{
throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
}
@ -140,7 +140,7 @@ namespace Ryujinx.Memory
return ptr;
}
public static void UnmapSharedMemory(IntPtr address)
public static void UnmapSharedMemory(nint address)
{
if (!WindowsApi.UnmapViewOfFile(address))
{

View file

@ -44,7 +44,7 @@ namespace Ryujinx.Memory
O_SYNC = 256,
}
public const IntPtr MAP_FAILED = -1;
public const nint MAP_FAILED = -1;
private const int MAP_ANONYMOUS_LINUX_GENERIC = 0x20;
private const int MAP_NORESERVE_LINUX_GENERIC = 0x4000;
@ -57,37 +57,37 @@ namespace Ryujinx.Memory
public const int MADV_REMOVE = 9;
[LibraryImport("libc", EntryPoint = "mmap", SetLastError = true)]
private static partial IntPtr Internal_mmap(IntPtr address, ulong length, MmapProts prot, int flags, int fd, long offset);
private static partial nint Internal_mmap(nint address, ulong length, MmapProts prot, int flags, int fd, long offset);
[LibraryImport("libc", SetLastError = true)]
public static partial int mprotect(IntPtr address, ulong length, MmapProts prot);
public static partial int mprotect(nint address, ulong length, MmapProts prot);
[LibraryImport("libc", SetLastError = true)]
public static partial int munmap(IntPtr address, ulong length);
public static partial int munmap(nint address, ulong length);
[LibraryImport("libc", SetLastError = true)]
public static partial IntPtr mremap(IntPtr old_address, ulong old_size, ulong new_size, int flags, IntPtr new_address);
public static partial nint mremap(nint old_address, ulong old_size, ulong new_size, int flags, nint new_address);
[LibraryImport("libc", SetLastError = true)]
public static partial int madvise(IntPtr address, ulong size, int advice);
public static partial int madvise(nint address, ulong size, int advice);
[LibraryImport("libc", SetLastError = true)]
public static partial int mkstemp(IntPtr template);
public static partial int mkstemp(nint template);
[LibraryImport("libc", SetLastError = true)]
public static partial int unlink(IntPtr pathname);
public static partial int unlink(nint pathname);
[LibraryImport("libc", SetLastError = true)]
public static partial int ftruncate(int fildes, IntPtr length);
public static partial int ftruncate(int fildes, nint length);
[LibraryImport("libc", SetLastError = true)]
public static partial int close(int fd);
[LibraryImport("libc", SetLastError = true)]
public static partial int shm_open(IntPtr name, int oflag, uint mode);
public static partial int shm_open(nint name, int oflag, uint mode);
[LibraryImport("libc", SetLastError = true)]
public static partial int shm_unlink(IntPtr name);
public static partial int shm_unlink(nint name);
private static int MmapFlagsToSystemFlags(MmapFlags flags)
{
@ -164,7 +164,7 @@ namespace Ryujinx.Memory
return result;
}
public static IntPtr Mmap(IntPtr address, ulong length, MmapProts prot, MmapFlags flags, int fd, long offset)
public static nint Mmap(nint address, ulong length, MmapProts prot, MmapFlags flags, int fd, long offset)
{
return Internal_mmap(address, length, prot, MmapFlagsToSystemFlags(flags), fd, offset);
}

View file

@ -18,7 +18,7 @@ namespace Ryujinx.Memory.WindowsShared
private readonly MappingTree<ulong> _mappings;
private readonly MappingTree<MemoryPermission> _protections;
private readonly IntPtr _partialUnmapStatePtr;
private readonly nint _partialUnmapStatePtr;
private readonly Thread _partialUnmapTrimThread;
/// <summary>
@ -100,7 +100,7 @@ namespace Ryujinx.Memory.WindowsShared
if (IsMapped(node.Value))
{
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)node.Start, 2))
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (nint)node.Start, 2))
{
throw new WindowsApiException("UnmapViewOfFile2");
}
@ -126,7 +126,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="location">Address to map the view into</param>
/// <param name="size">Size of the view in bytes</param>
/// <param name="owner">Memory block that owns the mapping</param>
public void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, MemoryBlock owner)
public void MapView(nint sharedMemory, ulong srcOffset, nint location, nint size, MemoryBlock owner)
{
ref var partialUnmapLock = ref GetPartialUnmapState().PartialUnmapLock;
partialUnmapLock.AcquireReaderLock();
@ -151,7 +151,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="size">Size of the view in bytes</param>
/// <param name="updateProtection">Indicates if the memory protections should be updated after the map</param>
/// <exception cref="WindowsApiException">Thrown when the Windows API returns an error mapping the memory</exception>
private void MapViewInternal(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, bool updateProtection)
private void MapViewInternal(nint sharedMemory, ulong srcOffset, nint location, nint size, bool updateProtection)
{
SplitForMap((ulong)location, (ulong)size, srcOffset);
@ -163,10 +163,10 @@ namespace Ryujinx.Memory.WindowsShared
size,
0x4000,
MemoryProtection.ReadWrite,
IntPtr.Zero,
nint.Zero,
0);
if (ptr == IntPtr.Zero)
if (ptr == nint.Zero)
{
throw new WindowsApiException("MapViewOfFile3");
}
@ -210,8 +210,8 @@ namespace Ryujinx.Memory.WindowsShared
if (overlapStartsBefore && overlapEndsAfter)
{
CheckFreeResult(WindowsApi.VirtualFree(
(IntPtr)address,
(IntPtr)size,
(nint)address,
(nint)size,
AllocationType.Release | AllocationType.PreservePlaceholder));
_mappings.Add(new RangeNode<ulong>(overlapStart, address, overlapValue));
@ -222,8 +222,8 @@ namespace Ryujinx.Memory.WindowsShared
ulong overlappedSize = overlapEnd - address;
CheckFreeResult(WindowsApi.VirtualFree(
(IntPtr)address,
(IntPtr)overlappedSize,
(nint)address,
(nint)overlappedSize,
AllocationType.Release | AllocationType.PreservePlaceholder));
_mappings.Add(new RangeNode<ulong>(overlapStart, address, overlapValue));
@ -233,8 +233,8 @@ namespace Ryujinx.Memory.WindowsShared
ulong overlappedSize = endAddress - overlapStart;
CheckFreeResult(WindowsApi.VirtualFree(
(IntPtr)overlapStart,
(IntPtr)overlappedSize,
(nint)overlapStart,
(nint)overlappedSize,
AllocationType.Release | AllocationType.PreservePlaceholder));
_mappings.Add(new RangeNode<ulong>(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize)));
@ -255,7 +255,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="location">Address to unmap</param>
/// <param name="size">Size of the region to unmap in bytes</param>
/// <param name="owner">Memory block that owns the mapping</param>
public void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner)
public void UnmapView(nint sharedMemory, nint location, nint size, MemoryBlock owner)
{
ref var partialUnmapLock = ref GetPartialUnmapState().PartialUnmapLock;
partialUnmapLock.AcquireReaderLock();
@ -283,7 +283,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="owner">Memory block that owns the mapping</param>
/// <param name="updateProtection">Indicates if the memory protections should be updated after the unmap</param>
/// <exception cref="WindowsApiException">Thrown when the Windows API returns an error unmapping or remapping the memory</exception>
private void UnmapViewInternal(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner, bool updateProtection)
private void UnmapViewInternal(nint sharedMemory, nint location, nint size, MemoryBlock owner, bool updateProtection)
{
ulong startAddress = (ulong)location;
ulong unmapSize = (ulong)size;
@ -327,7 +327,7 @@ namespace Ryujinx.Memory.WindowsShared
{
partialUnmapState.PartialUnmapsCount++;
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (nint)overlap.Start, 2))
{
throw new WindowsApiException("UnmapViewOfFile2");
}
@ -336,7 +336,7 @@ namespace Ryujinx.Memory.WindowsShared
{
ulong remapSize = startAddress - overlap.Start;
MapViewInternal(sharedMemory, overlap.Value, (IntPtr)overlap.Start, (IntPtr)remapSize, updateProtection: false);
MapViewInternal(sharedMemory, overlap.Value, (nint)overlap.Start, (nint)remapSize, updateProtection: false);
RestoreRangeProtection(overlap.Start, remapSize);
}
@ -347,7 +347,7 @@ namespace Ryujinx.Memory.WindowsShared
ulong remapAddress = overlap.Start + overlappedSize;
ulong remapSize = overlap.End - endAddress;
MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize, updateProtection: false);
MapViewInternal(sharedMemory, remapBackingOffset, (nint)remapAddress, (nint)remapSize, updateProtection: false);
RestoreRangeProtection(remapAddress, remapSize);
}
}
@ -356,7 +356,7 @@ namespace Ryujinx.Memory.WindowsShared
partialUnmapLock.DowngradeFromWriterLock();
}
}
else if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
else if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (nint)overlap.Start, 2))
{
throw new WindowsApiException("UnmapViewOfFile2");
}
@ -441,8 +441,8 @@ namespace Ryujinx.Memory.WindowsShared
size = endAddress - address;
CheckFreeResult(WindowsApi.VirtualFree(
(IntPtr)address,
(IntPtr)size,
(nint)address,
(nint)size,
AllocationType.Release | AllocationType.CoalescePlaceholders));
}
}
@ -454,7 +454,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="size">Size of the region to reprotect in bytes</param>
/// <param name="permission">New permissions</param>
/// <returns>True if the reprotection was successful, false otherwise</returns>
public bool ReprotectView(IntPtr address, IntPtr size, MemoryPermission permission)
public bool ReprotectView(nint address, nint size, MemoryPermission permission)
{
ref var partialUnmapLock = ref GetPartialUnmapState().PartialUnmapLock;
partialUnmapLock.AcquireReaderLock();
@ -478,7 +478,7 @@ namespace Ryujinx.Memory.WindowsShared
/// <param name="throwOnError">Throw an exception instead of returning an error if the operation fails</param>
/// <returns>True if the reprotection was successful or if <paramref name="throwOnError"/> is true, false otherwise</returns>
/// <exception cref="WindowsApiException">If <paramref name="throwOnError"/> is true, it is thrown when the Windows API returns an error reprotecting the memory</exception>
private bool ReprotectViewInternal(IntPtr address, IntPtr size, MemoryPermission permission, bool throwOnError)
private bool ReprotectViewInternal(nint address, nint size, MemoryPermission permission, bool throwOnError)
{
ulong reprotectAddress = (ulong)address;
ulong reprotectSize = (ulong)size;
@ -514,7 +514,7 @@ namespace Ryujinx.Memory.WindowsShared
mappedSize -= delta;
}
if (!WindowsApi.VirtualProtect((IntPtr)mappedAddress, (IntPtr)mappedSize, WindowsApi.GetProtection(permission), out _))
if (!WindowsApi.VirtualProtect((nint)mappedAddress, (nint)mappedSize, WindowsApi.GetProtection(permission), out _))
{
if (throwOnError)
{
@ -729,7 +729,7 @@ namespace Ryujinx.Memory.WindowsShared
protEndAddress = endAddress;
}
ReprotectViewInternal((IntPtr)protAddress, (IntPtr)(protEndAddress - protAddress), protection.Value, true);
ReprotectViewInternal((nint)protAddress, (nint)(protEndAddress - protAddress), protection.Value, true);
}
}
}

View file

@ -7,42 +7,42 @@ namespace Ryujinx.Memory.WindowsShared
[SupportedOSPlatform("windows")]
static partial class WindowsApi
{
public static readonly IntPtr InvalidHandleValue = new(-1);
public static readonly IntPtr CurrentProcessHandle = new(-1);
public static readonly nint InvalidHandleValue = new(-1);
public static readonly nint CurrentProcessHandle = new(-1);
[LibraryImport("kernel32.dll", SetLastError = true)]
public static partial IntPtr VirtualAlloc(
IntPtr lpAddress,
IntPtr dwSize,
public static partial nint VirtualAlloc(
nint lpAddress,
nint dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect);
[LibraryImport("KernelBase.dll", SetLastError = true)]
public static partial IntPtr VirtualAlloc2(
IntPtr process,
IntPtr lpAddress,
IntPtr dwSize,
public static partial nint VirtualAlloc2(
nint process,
nint lpAddress,
nint dwSize,
AllocationType flAllocationType,
MemoryProtection flProtect,
IntPtr extendedParameters,
nint extendedParameters,
ulong parameterCount);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool VirtualProtect(
IntPtr lpAddress,
IntPtr dwSize,
nint lpAddress,
nint dwSize,
MemoryProtection flNewProtect,
out MemoryProtection lpflOldProtect);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool VirtualFree(IntPtr lpAddress, IntPtr dwSize, AllocationType dwFreeType);
public static partial bool VirtualFree(nint lpAddress, nint dwSize, AllocationType dwFreeType);
[LibraryImport("kernel32.dll", SetLastError = true, EntryPoint = "CreateFileMappingW")]
public static partial IntPtr CreateFileMapping(
IntPtr hFile,
IntPtr lpFileMappingAttributes,
public static partial nint CreateFileMapping(
nint hFile,
nint lpFileMappingAttributes,
FileMapProtection flProtect,
uint dwMaximumSizeHigh,
uint dwMaximumSizeLow,
@ -50,35 +50,35 @@ namespace Ryujinx.Memory.WindowsShared
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool CloseHandle(IntPtr hObject);
public static partial bool CloseHandle(nint hObject);
[LibraryImport("kernel32.dll", SetLastError = true)]
public static partial IntPtr MapViewOfFile(
IntPtr hFileMappingObject,
public static partial nint MapViewOfFile(
nint hFileMappingObject,
uint dwDesiredAccess,
uint dwFileOffsetHigh,
uint dwFileOffsetLow,
IntPtr dwNumberOfBytesToMap);
nint dwNumberOfBytesToMap);
[LibraryImport("KernelBase.dll", SetLastError = true)]
public static partial IntPtr MapViewOfFile3(
IntPtr hFileMappingObject,
IntPtr process,
IntPtr baseAddress,
public static partial nint MapViewOfFile3(
nint hFileMappingObject,
nint process,
nint baseAddress,
ulong offset,
IntPtr dwNumberOfBytesToMap,
nint dwNumberOfBytesToMap,
ulong allocationType,
MemoryProtection dwDesiredAccess,
IntPtr extendedParameters,
nint extendedParameters,
ulong parameterCount);
[LibraryImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool UnmapViewOfFile(IntPtr lpBaseAddress);
public static partial bool UnmapViewOfFile(nint lpBaseAddress);
[LibraryImport("KernelBase.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static partial bool UnmapViewOfFile2(IntPtr process, IntPtr lpBaseAddress, ulong unmapFlags);
public static partial bool UnmapViewOfFile2(nint process, nint lpBaseAddress, ulong unmapFlags);
[LibraryImport("kernel32.dll")]
public static partial uint GetLastError();