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

@@ -28,7 +28,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
[SupportedOSPlatform("windows")]
[LibraryImport("kernel32.dll", SetLastError = true)]
public static partial IntPtr FlushInstructionCache(IntPtr hProcess, IntPtr lpAddress, UIntPtr dwSize);
public static partial nint FlushInstructionCache(nint hProcess, nint lpAddress, nuint dwSize);
public static void Initialize(IJitMemoryAllocator allocator)
{
@@ -57,7 +57,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
}
}
public unsafe static IntPtr Map(ReadOnlySpan<byte> code)
public unsafe static nint Map(ReadOnlySpan<byte> code)
{
lock (_lock)
{
@@ -65,7 +65,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
int funcOffset = Allocate(code.Length);
IntPtr funcPtr = _jitRegion.Pointer + funcOffset;
nint funcPtr = _jitRegion.Pointer + funcOffset;
if (OperatingSystem.IsMacOS() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
{
@@ -73,7 +73,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
{
fixed (byte* codePtr = code)
{
JitSupportDarwin.Copy(funcPtr, (IntPtr)codePtr, (ulong)code.Length);
JitSupportDarwin.Copy(funcPtr, (nint)codePtr, (ulong)code.Length);
}
}
}
@@ -85,7 +85,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
if (OperatingSystem.IsWindows() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
{
FlushInstructionCache(Process.GetCurrentProcess().Handle, funcPtr, (UIntPtr)code.Length);
FlushInstructionCache(Process.GetCurrentProcess().Handle, funcPtr, (nuint)code.Length);
}
else
{
@@ -99,7 +99,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
}
}
public static void Unmap(IntPtr pointer)
public static void Unmap(nint pointer)
{
lock (_lock)
{

View File

@@ -68,7 +68,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
}
}
public void Invalidate(IntPtr basePointer, ulong size)
public void Invalidate(nint basePointer, ulong size)
{
if (_needsInvalidation)
{

View File

@@ -8,9 +8,9 @@ namespace Ryujinx.Cpu.LightningJit.Cache
static partial class JitSupportDarwin
{
[LibraryImport("libarmeilleure-jitsupport", EntryPoint = "armeilleure_jit_memcpy")]
public static partial void Copy(IntPtr dst, IntPtr src, ulong n);
public static partial void Copy(nint dst, nint src, ulong n);
[LibraryImport("libc", EntryPoint = "sys_icache_invalidate", SetLastError = true)]
public static partial void SysIcacheInvalidate(IntPtr start, IntPtr len);
public static partial void SysIcacheInvalidate(nint start, nint len);
}
}

View File

@@ -23,7 +23,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
private readonly CacheMemoryAllocator _cacheAllocator;
public CacheMemoryAllocator Allocator => _cacheAllocator;
public IntPtr Pointer => _region.Block.Pointer;
public nint Pointer => _region.Block.Pointer;
public MemoryCache(IJitMemoryAllocator allocator, ulong size)
{
@@ -110,10 +110,10 @@ namespace Ryujinx.Cpu.LightningJit.Cache
{
public readonly int Offset;
public readonly int Size;
public readonly IntPtr FuncPtr;
public readonly nint FuncPtr;
private int _useCount;
public ThreadLocalCacheEntry(int offset, int size, IntPtr funcPtr)
public ThreadLocalCacheEntry(int offset, int size, nint funcPtr)
{
Offset = offset;
Size = size;
@@ -140,9 +140,9 @@ namespace Ryujinx.Cpu.LightningJit.Cache
_lock = new();
}
public unsafe IntPtr Map(IntPtr framePointer, ReadOnlySpan<byte> code, ulong guestAddress, ulong guestSize)
public unsafe nint Map(nint framePointer, ReadOnlySpan<byte> code, ulong guestAddress, ulong guestSize)
{
if (TryGetThreadLocalFunction(guestAddress, out IntPtr funcPtr))
if (TryGetThreadLocalFunction(guestAddress, out nint funcPtr))
{
return funcPtr;
}
@@ -167,7 +167,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
}
}
public unsafe IntPtr MapPageAligned(ReadOnlySpan<byte> code)
public unsafe nint MapPageAligned(ReadOnlySpan<byte> code)
{
lock (_lock)
{
@@ -179,7 +179,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
Debug.Assert((funcOffset & ((int)MemoryBlock.GetPageSize() - 1)) == 0);
IntPtr funcPtr = _sharedCache.Pointer + funcOffset;
nint funcPtr = _sharedCache.Pointer + funcOffset;
code.CopyTo(new Span<byte>((void*)funcPtr, code.Length));
_sharedCache.ReprotectAsRx(funcOffset, sizeAligned);
@@ -188,7 +188,7 @@ namespace Ryujinx.Cpu.LightningJit.Cache
}
}
private bool TryGetThreadLocalFunction(ulong guestAddress, out IntPtr funcPtr)
private bool TryGetThreadLocalFunction(ulong guestAddress, out nint funcPtr)
{
if ((_threadLocalCache ??= new()).TryGetValue(guestAddress, out var entry))
{
@@ -209,12 +209,12 @@ namespace Ryujinx.Cpu.LightningJit.Cache
return true;
}
funcPtr = IntPtr.Zero;
funcPtr = nint.Zero;
return false;
}
private void ClearThreadLocalCache(IntPtr framePointer)
private void ClearThreadLocalCache(nint framePointer)
{
// Try to delete functions that are already on the shared cache
// and no longer being executed.
@@ -296,14 +296,14 @@ namespace Ryujinx.Cpu.LightningJit.Cache
_threadLocalCache = null;
}
private unsafe IntPtr AddThreadLocalFunction(ReadOnlySpan<byte> code, ulong guestAddress)
private unsafe nint AddThreadLocalFunction(ReadOnlySpan<byte> code, ulong guestAddress)
{
int alignedSize = BitUtils.AlignUp(code.Length, (int)MemoryBlock.GetPageSize());
int funcOffset = _localCache.Allocate(alignedSize);
Debug.Assert((funcOffset & (int)(MemoryBlock.GetPageSize() - 1)) == 0);
IntPtr funcPtr = _localCache.Pointer + funcOffset;
nint funcPtr = _localCache.Pointer + funcOffset;
code.CopyTo(new Span<byte>((void*)funcPtr, code.Length));
(_threadLocalCache ??= new()).Add(guestAddress, new(funcOffset, code.Length, funcPtr));