aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/JitCache.cs
diff options
context:
space:
mode:
Diffstat (limited to 'ARMeilleure/Translation/JitCache.cs')
-rw-r--r--ARMeilleure/Translation/JitCache.cs51
1 files changed, 24 insertions, 27 deletions
diff --git a/ARMeilleure/Translation/JitCache.cs b/ARMeilleure/Translation/JitCache.cs
index b004cc22..32d40c20 100644
--- a/ARMeilleure/Translation/JitCache.cs
+++ b/ARMeilleure/Translation/JitCache.cs
@@ -2,6 +2,7 @@ using ARMeilleure.CodeGen;
using ARMeilleure.Memory;
using System;
using System.Collections.Generic;
+using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ARMeilleure.Translation
@@ -12,35 +13,33 @@ namespace ARMeilleure.Translation
private const int PageMask = PageSize - 1;
private const int CodeAlignment = 4; // Bytes
-
private const int CacheSize = 2047 * 1024 * 1024;
private static ReservedRegion _jitRegion;
-
- private static IntPtr _basePointer => _jitRegion.Pointer;
-
private static int _offset;
+ private static readonly List<JitCacheEntry> _cacheEntries = new List<JitCacheEntry>();
- private static List<JitCacheEntry> _cacheEntries;
-
- private static object _lock;
+ private static readonly object _lock = new object();
+ private static bool _initialized;
- static JitCache()
+ public static void Initialize(IJitMemoryAllocator allocator)
{
- _jitRegion = new ReservedRegion(CacheSize);
-
- if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ if (_initialized) return;
+ lock (_lock)
{
- _jitRegion.ExpandIfNeeded(PageSize);
- JitUnwindWindows.InstallFunctionTableHandler(_basePointer, CacheSize);
-
- // The first page is used for the table based SEH structs.
- _offset = PageSize;
- }
+ if (_initialized) return;
+ _jitRegion = new ReservedRegion(allocator, CacheSize);
- _cacheEntries = new List<JitCacheEntry>();
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ _jitRegion.ExpandIfNeeded(PageSize);
+ JitUnwindWindows.InstallFunctionTableHandler(_jitRegion.Pointer, CacheSize);
- _lock = new object();
+ // The first page is used for the table based SEH structs.
+ _offset = PageSize;
+ }
+ _initialized = true;
+ }
}
public static IntPtr Map(CompiledFunction func)
@@ -49,9 +48,11 @@ namespace ARMeilleure.Translation
lock (_lock)
{
+ Debug.Assert(_initialized);
+
int funcOffset = Allocate(code.Length);
- IntPtr funcPtr = _basePointer + funcOffset;
+ IntPtr funcPtr = _jitRegion.Pointer + funcOffset;
Marshal.Copy(code, 0, funcPtr, code.Length);
@@ -77,18 +78,14 @@ namespace ARMeilleure.Translation
if (fullPagesSize != 0)
{
- IntPtr funcPtr = _basePointer + pageStart;
-
- MemoryManagement.Reprotect(funcPtr, (ulong)fullPagesSize, MemoryProtection.ReadAndExecute);
+ _jitRegion.Block.MapAsRx((ulong)pageStart, (ulong)fullPagesSize);
}
int remaining = endOffs - pageEnd;
if (remaining != 0)
{
- IntPtr funcPtr = _basePointer + pageEnd;
-
- MemoryManagement.Reprotect(funcPtr, (ulong)remaining, MemoryProtection.ReadWriteExecute);
+ _jitRegion.Block.MapAsRwx((ulong)pageEnd, (ulong)remaining);
}
}
@@ -132,7 +129,7 @@ namespace ARMeilleure.Translation
}
}
- entry = default(JitCacheEntry);
+ entry = default;
return false;
}