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

@@ -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();