diff --git a/ARMeilleure/CodeGen/CompiledFunction.cs b/ARMeilleure/CodeGen/CompiledFunction.cs index ab5e88eb..0560bf2e 100644 --- a/ARMeilleure/CodeGen/CompiledFunction.cs +++ b/ARMeilleure/CodeGen/CompiledFunction.cs @@ -48,9 +48,21 @@ namespace ARMeilleure.CodeGen /// A delegate of type pointing to the mapped function public T Map() { - IntPtr codePtr = JitCache.Map(this); + return MapWithPointer(out _); + } - return Marshal.GetDelegateForFunctionPointer(codePtr); + /// + /// Maps the onto the and returns a delegate of type + /// pointing to the mapped function. + /// + /// Type of delegate + /// Pointer to the function code in memory + /// A delegate of type pointing to the mapped function + public T MapWithPointer(out IntPtr codePointer) + { + codePointer = JitCache.Map(this); + + return Marshal.GetDelegateForFunctionPointer(codePointer); } } } \ No newline at end of file diff --git a/ARMeilleure/Instructions/NativeInterface.cs b/ARMeilleure/Instructions/NativeInterface.cs index 2ac748a9..57964cc8 100644 --- a/ARMeilleure/Instructions/NativeInterface.cs +++ b/ARMeilleure/Instructions/NativeInterface.cs @@ -191,7 +191,7 @@ namespace ARMeilleure.Instructions { TranslatedFunction function = Context.Translator.GetOrTranslate(address, GetContext().ExecutionMode); - return (ulong)function.FuncPtr.ToInt64(); + return (ulong)function.FuncPointer.ToInt64(); } public static void InvalidateCacheLine(ulong address) diff --git a/ARMeilleure/Translation/PTC/Ptc.cs b/ARMeilleure/Translation/PTC/Ptc.cs index aeb5868c..de2294b2 100644 --- a/ARMeilleure/Translation/PTC/Ptc.cs +++ b/ARMeilleure/Translation/PTC/Ptc.cs @@ -745,9 +745,9 @@ namespace ARMeilleure.Translation.PTC bool highCq) { var cFunc = new CompiledFunction(code, unwindInfo, RelocInfo.Empty); - var gFunc = cFunc.Map(); + var gFunc = cFunc.MapWithPointer(out IntPtr gFuncPointer); - return new TranslatedFunction(gFunc, callCounter, guestSize, highCq); + return new TranslatedFunction(gFunc, gFuncPointer, callCounter, guestSize, highCq); } private void UpdateInfo(InfoEntry infoEntry) diff --git a/ARMeilleure/Translation/TranslatedFunction.cs b/ARMeilleure/Translation/TranslatedFunction.cs index 04dd769c..71eec08a 100644 --- a/ARMeilleure/Translation/TranslatedFunction.cs +++ b/ARMeilleure/Translation/TranslatedFunction.cs @@ -1,6 +1,5 @@ using ARMeilleure.Common; using System; -using System.Runtime.InteropServices; namespace ARMeilleure.Translation { @@ -8,18 +7,18 @@ namespace ARMeilleure.Translation { private readonly GuestFunction _func; // Ensure that this delegate will not be garbage collected. + public IntPtr FuncPointer { get; } public Counter CallCounter { get; } public ulong GuestSize { get; } public bool HighCq { get; } - public IntPtr FuncPtr { get; } - public TranslatedFunction(GuestFunction func, Counter callCounter, ulong guestSize, bool highCq) + public TranslatedFunction(GuestFunction func, IntPtr funcPointer, Counter callCounter, ulong guestSize, bool highCq) { _func = func; + FuncPointer = funcPointer; CallCounter = callCounter; GuestSize = guestSize; HighCq = highCq; - FuncPtr = Marshal.GetFunctionPointerForDelegate(func); } public ulong Execute(State.ExecutionContext context) diff --git a/ARMeilleure/Translation/Translator.cs b/ARMeilleure/Translation/Translator.cs index cbf6baa0..0c05b2b4 100644 --- a/ARMeilleure/Translation/Translator.cs +++ b/ARMeilleure/Translation/Translator.cs @@ -211,7 +211,7 @@ namespace ARMeilleure.Translation if (oldFunc != func) { - JitCache.Unmap(func.FuncPtr); + JitCache.Unmap(func.FuncPointer); func = oldFunc; } @@ -230,7 +230,7 @@ namespace ARMeilleure.Translation { if (FunctionTable.IsValid(guestAddress) && (Optimizations.AllowLcqInFunctionTable || func.HighCq)) { - Volatile.Write(ref FunctionTable.GetValue(guestAddress), (ulong)func.FuncPtr); + Volatile.Write(ref FunctionTable.GetValue(guestAddress), (ulong)func.FuncPointer); } } @@ -292,11 +292,11 @@ namespace ARMeilleure.Translation _ptc.WriteCompiledFunction(address, funcSize, hash, highCq, compiledFunc); } - GuestFunction func = compiledFunc.Map(); + GuestFunction func = compiledFunc.MapWithPointer(out IntPtr funcPointer); Allocators.ResetAll(); - return new TranslatedFunction(func, counter, funcSize, highCq); + return new TranslatedFunction(func, funcPointer, counter, funcSize, highCq); } private void BackgroundTranslate() @@ -537,7 +537,7 @@ namespace ARMeilleure.Translation foreach (var func in functions) { - JitCache.Unmap(func.FuncPtr); + JitCache.Unmap(func.FuncPointer); func.CallCounter?.Dispose(); } @@ -546,7 +546,7 @@ namespace ARMeilleure.Translation while (_oldFuncs.TryDequeue(out var kv)) { - JitCache.Unmap(kv.Value.FuncPtr); + JitCache.Unmap(kv.Value.FuncPointer); kv.Value.CallCounter?.Dispose(); }