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

@ -92,7 +92,7 @@ namespace ARMeilleure.Translation
else
{
int index = Delegates.GetDelegateIndex(info);
IntPtr funcPtr = Delegates.GetDelegateFuncPtrByIndex(index);
nint funcPtr = Delegates.GetDelegateFuncPtrByIndex(index);
OperandType returnType = GetOperandType(info.ReturnType);

View file

@ -31,7 +31,7 @@ namespace ARMeilleure.Translation.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)
{
@ -65,7 +65,7 @@ namespace ARMeilleure.Translation.Cache
}
}
public static IntPtr Map(CompiledFunction func)
public static nint Map(CompiledFunction func)
{
byte[] code = func.Code;
@ -75,7 +75,7 @@ namespace ARMeilleure.Translation.Cache
int funcOffset = Allocate(code.Length);
IntPtr funcPtr = _jitRegion.Pointer + funcOffset;
nint funcPtr = _jitRegion.Pointer + funcOffset;
if (OperatingSystem.IsMacOS() && RuntimeInformation.ProcessArchitecture == Architecture.Arm64)
{
@ -83,7 +83,7 @@ namespace ARMeilleure.Translation.Cache
{
fixed (byte* codePtr = code)
{
JitSupportDarwin.Copy(funcPtr, (IntPtr)codePtr, (ulong)code.Length);
JitSupportDarwin.Copy(funcPtr, (nint)codePtr, (ulong)code.Length);
}
}
}
@ -95,7 +95,7 @@ namespace ARMeilleure.Translation.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
{
@ -109,7 +109,7 @@ namespace ARMeilleure.Translation.Cache
}
}
public static void Unmap(IntPtr pointer)
public static void Unmap(nint pointer)
{
lock (_lock)
{

View file

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

View file

@ -40,7 +40,7 @@ namespace ARMeilleure.Translation.Cache
PushMachframe = 10,
}
private unsafe delegate RuntimeFunction* GetRuntimeFunctionCallback(ulong controlPc, IntPtr context);
private unsafe delegate RuntimeFunction* GetRuntimeFunctionCallback(ulong controlPc, nint context);
[LibraryImport("kernel32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
@ -49,7 +49,7 @@ namespace ARMeilleure.Translation.Cache
ulong baseAddress,
uint length,
GetRuntimeFunctionCallback callback,
IntPtr context,
nint context,
[MarshalAs(UnmanagedType.LPWStr)] string outOfProcessCallbackDll);
private static GetRuntimeFunctionCallback _getRuntimeFunctionCallback;
@ -60,7 +60,7 @@ namespace ARMeilleure.Translation.Cache
private unsafe static UnwindInfo* _unwindInfo;
public static void InstallFunctionTableHandler(IntPtr codeCachePointer, uint codeCacheLength, IntPtr workBufferPtr)
public static void InstallFunctionTableHandler(nint codeCachePointer, uint codeCacheLength, nint workBufferPtr)
{
ulong codeCachePtr = (ulong)codeCachePointer.ToInt64();
@ -91,7 +91,7 @@ namespace ARMeilleure.Translation.Cache
}
}
private static unsafe RuntimeFunction* FunctionTableHandler(ulong controlPc, IntPtr context)
private static unsafe RuntimeFunction* FunctionTableHandler(ulong controlPc, nint context)
{
int offset = (int)((long)controlPc - context.ToInt64());

View file

@ -8,9 +8,9 @@ namespace ARMeilleure.Translation
private readonly Delegate _dlg; // Ensure that this delegate will not be garbage collected.
#pragma warning restore IDE0052
public IntPtr FuncPtr { get; }
public nint FuncPtr { get; }
public DelegateInfo(Delegate dlg, IntPtr funcPtr)
public DelegateInfo(Delegate dlg, nint funcPtr)
{
_dlg = dlg;
FuncPtr = funcPtr;

View file

@ -9,7 +9,7 @@ namespace ARMeilleure.Translation
{
static class Delegates
{
public static bool TryGetDelegateFuncPtrByIndex(int index, out IntPtr funcPtr)
public static bool TryGetDelegateFuncPtrByIndex(int index, out nint funcPtr)
{
if (index >= 0 && index < _delegates.Count)
{
@ -25,7 +25,7 @@ namespace ARMeilleure.Translation
}
}
public static IntPtr GetDelegateFuncPtrByIndex(int index)
public static nint GetDelegateFuncPtrByIndex(int index)
{
if (index < 0 || index >= _delegates.Count)
{
@ -35,7 +35,7 @@ namespace ARMeilleure.Translation
return _delegates.Values[index].FuncPtr; // O(1).
}
public static IntPtr GetDelegateFuncPtr(MethodInfo info)
public static nint GetDelegateFuncPtr(MethodInfo info)
{
ArgumentNullException.ThrowIfNull(info);
@ -65,7 +65,7 @@ namespace ARMeilleure.Translation
return index;
}
private static void SetDelegateInfo(Delegate dlg, IntPtr funcPtr)
private static void SetDelegateInfo(Delegate dlg, nint funcPtr)
{
string key = GetKey(dlg.Method);

View file

@ -2,6 +2,6 @@ using System;
namespace ARMeilleure.Translation
{
delegate void DispatcherFunction(IntPtr nativeContext, ulong startAddress);
delegate ulong WrapperFunction(IntPtr nativeContext, ulong startAddress);
delegate void DispatcherFunction(nint nativeContext, ulong startAddress);
delegate ulong WrapperFunction(nint nativeContext, ulong startAddress);
}

View file

@ -97,7 +97,7 @@ namespace ARMeilleure.Translation
public virtual Operand Call(MethodInfo info, params Operand[] callArgs)
{
IntPtr funcPtr = Delegates.GetDelegateFuncPtr(info);
nint funcPtr = Delegates.GetDelegateFuncPtr(info);
OperandType returnType = GetOperandType(info.ReturnType);

View file

@ -2,5 +2,5 @@ using System;
namespace ARMeilleure.Translation
{
delegate ulong GuestFunction(IntPtr nativeContextPtr);
delegate ulong GuestFunction(nint nativeContextPtr);
}

View file

@ -268,11 +268,11 @@ namespace ARMeilleure.Translation.PTC
return false;
}
IntPtr intPtr = IntPtr.Zero;
nint intPtr = nint.Zero;
try
{
intPtr = Marshal.AllocHGlobal(new IntPtr(outerHeader.UncompressedStreamSize));
intPtr = Marshal.AllocHGlobal(new nint(outerHeader.UncompressedStreamSize));
using UnmanagedMemoryStream stream = new((byte*)intPtr.ToPointer(), outerHeader.UncompressedStreamSize, outerHeader.UncompressedStreamSize, FileAccess.ReadWrite);
try
@ -373,7 +373,7 @@ namespace ARMeilleure.Translation.PTC
}
finally
{
if (intPtr != IntPtr.Zero)
if (intPtr != nint.Zero)
{
Marshal.FreeHGlobal(intPtr);
}
@ -455,11 +455,11 @@ namespace ARMeilleure.Translation.PTC
outerHeader.SetHeaderHash();
IntPtr intPtr = IntPtr.Zero;
nint intPtr = nint.Zero;
try
{
intPtr = Marshal.AllocHGlobal(new IntPtr(outerHeader.UncompressedStreamSize));
intPtr = Marshal.AllocHGlobal(new nint(outerHeader.UncompressedStreamSize));
using UnmanagedMemoryStream stream = new((byte*)intPtr.ToPointer(), outerHeader.UncompressedStreamSize, outerHeader.UncompressedStreamSize, FileAccess.ReadWrite);
stream.Seek((long)Unsafe.SizeOf<InnerHeader>(), SeekOrigin.Begin);
@ -513,7 +513,7 @@ namespace ARMeilleure.Translation.PTC
}
finally
{
if (intPtr != IntPtr.Zero)
if (intPtr != nint.Zero)
{
Marshal.FreeHGlobal(intPtr);
}
@ -664,7 +664,7 @@ namespace ARMeilleure.Translation.PTC
foreach (RelocEntry relocEntry in relocEntries)
{
IntPtr? imm = null;
nint? imm = null;
Symbol symbol = relocEntry.Symbol;
if (symbol.Type == SymbolType.FunctionTable)
@ -675,7 +675,7 @@ namespace ARMeilleure.Translation.PTC
{
unsafe
{
imm = (IntPtr)Unsafe.AsPointer(ref translator.FunctionTable.GetValue(guestAddress));
imm = (nint)Unsafe.AsPointer(ref translator.FunctionTable.GetValue(guestAddress));
}
}
}
@ -683,7 +683,7 @@ namespace ARMeilleure.Translation.PTC
{
int index = (int)symbol.Value;
if (Delegates.TryGetDelegateFuncPtrByIndex(index, out IntPtr funcPtr))
if (Delegates.TryGetDelegateFuncPtrByIndex(index, out nint funcPtr))
{
imm = funcPtr;
}
@ -698,7 +698,7 @@ namespace ARMeilleure.Translation.PTC
unsafe
{
imm = (IntPtr)Unsafe.AsPointer(ref callCounter.Value);
imm = (nint)Unsafe.AsPointer(ref callCounter.Value);
}
}
else if (symbol == DispatchStubSymbol)
@ -744,7 +744,7 @@ namespace ARMeilleure.Translation.PTC
bool highCq)
{
var cFunc = new CompiledFunction(code, unwindInfo, RelocInfo.Empty);
var gFunc = cFunc.MapWithPointer<GuestFunction>(out IntPtr gFuncPointer);
var gFunc = cFunc.MapWithPointer<GuestFunction>(out nint gFuncPointer);
return new TranslatedFunction(gFunc, gFuncPointer, callCounter, guestSize, highCq);
}

View file

@ -7,12 +7,12 @@ namespace ARMeilleure.Translation
{
private readonly GuestFunction _func; // Ensure that this delegate will not be garbage collected.
public IntPtr FuncPointer { get; }
public nint FuncPointer { get; }
public Counter<uint> CallCounter { get; }
public ulong GuestSize { get; }
public bool HighCq { get; }
public TranslatedFunction(GuestFunction func, IntPtr funcPointer, Counter<uint> callCounter, ulong guestSize, bool highCq)
public TranslatedFunction(GuestFunction func, nint funcPointer, Counter<uint> callCounter, ulong guestSize, bool highCq)
{
_func = func;
FuncPointer = funcPointer;

View file

@ -298,7 +298,7 @@ namespace ARMeilleure.Translation
_ptc.WriteCompiledFunction(address, funcSize, hash, highCq, compiledFunc);
}
GuestFunction func = compiledFunc.MapWithPointer<GuestFunction>(out IntPtr funcPointer);
GuestFunction func = compiledFunc.MapWithPointer<GuestFunction>(out nint funcPointer);
Allocators.ResetAll();

View file

@ -15,12 +15,12 @@ namespace ARMeilleure.Translation
/// </summary>
class TranslatorStubs : IDisposable
{
private readonly Lazy<IntPtr> _slowDispatchStub;
private readonly Lazy<nint> _slowDispatchStub;
private bool _disposed;
private readonly AddressTable<ulong> _functionTable;
private readonly Lazy<IntPtr> _dispatchStub;
private readonly Lazy<nint> _dispatchStub;
private readonly Lazy<DispatcherFunction> _dispatchLoop;
private readonly Lazy<WrapperFunction> _contextWrapper;
@ -28,7 +28,7 @@ namespace ARMeilleure.Translation
/// Gets the dispatch stub.
/// </summary>
/// <exception cref="ObjectDisposedException"><see cref="TranslatorStubs"/> instance was disposed</exception>
public IntPtr DispatchStub
public nint DispatchStub
{
get
{
@ -42,7 +42,7 @@ namespace ARMeilleure.Translation
/// Gets the slow dispatch stub.
/// </summary>
/// <exception cref="ObjectDisposedException"><see cref="TranslatorStubs"/> instance was disposed</exception>
public IntPtr SlowDispatchStub
public nint SlowDispatchStub
{
get
{
@ -140,7 +140,7 @@ namespace ARMeilleure.Translation
/// Generates a <see cref="DispatchStub"/>.
/// </summary>
/// <returns>Generated <see cref="DispatchStub"/></returns>
private IntPtr GenerateDispatchStub()
private nint GenerateDispatchStub()
{
var context = new EmitterContext();
@ -198,7 +198,7 @@ namespace ARMeilleure.Translation
/// Generates a <see cref="SlowDispatchStub"/>.
/// </summary>
/// <returns>Generated <see cref="SlowDispatchStub"/></returns>
private IntPtr GenerateSlowDispatchStub()
private nint GenerateSlowDispatchStub()
{
var context = new EmitterContext();

View file

@ -9,7 +9,7 @@ namespace ARMeilleure.Translation
{
public static class TranslatorTestMethods
{
public delegate int FpFlagsPInvokeTest(IntPtr managedMethod);
public delegate int FpFlagsPInvokeTest(nint managedMethod);
private static bool SetPlatformFtz(EmitterContext context, bool ftz)
{