From 7a73362bf9b391d988c2d634841e6c2b0e9e429a Mon Sep 17 00:00:00 2001 From: Jacobwasbeast Date: Tue, 11 Mar 2025 15:13:53 -0500 Subject: [PATCH] temp-fix: revert JitCache changes Reverting this basically prevents it from resetting the JitCache which causes the crash when loading a second game/app process. --- src/ARMeilleure/Translation/Cache/JitCache.cs | 61 +++++++++---------- 1 file changed, 28 insertions(+), 33 deletions(-) diff --git a/src/ARMeilleure/Translation/Cache/JitCache.cs b/src/ARMeilleure/Translation/Cache/JitCache.cs index e480985b1..7b5f2ca81 100644 --- a/src/ARMeilleure/Translation/Cache/JitCache.cs +++ b/src/ARMeilleure/Translation/Cache/JitCache.cs @@ -24,7 +24,7 @@ namespace ARMeilleure.Translation.Cache private static JitCacheInvalidation _jitCacheInvalidator; - private static List _cacheAllocators = []; + private static CacheMemoryAllocator _cacheAllocator; private static readonly List _cacheEntries = []; @@ -40,48 +40,37 @@ namespace ARMeilleure.Translation.Cache public static void Initialize(IJitMemoryAllocator allocator) { + if (_initialized) + { + return; + } + lock (_lock) { if (_initialized) { - if (OperatingSystem.IsWindows()) - { - JitUnwindWindows.RemoveFunctionTableHandler( - _jitRegions[0].Pointer); - } - - for (int i = 0; i < _jitRegions.Count; i++) - { - _jitRegions[i].Dispose(); - } - - _jitRegions.Clear(); - _cacheAllocators.Clear(); + return; } - else - { - _initialized = true; - } - - _activeRegionIndex = 0; ReservedRegion firstRegion = new(allocator, CacheSize); _jitRegions.Add(firstRegion); - - CacheMemoryAllocator firstCacheAllocator = new(CacheSize); - _cacheAllocators.Add(firstCacheAllocator); + _activeRegionIndex = 0; if (!OperatingSystem.IsWindows() && !OperatingSystem.IsMacOS()) { _jitCacheInvalidator = new JitCacheInvalidation(allocator); } + _cacheAllocator = new CacheMemoryAllocator(CacheSize); + if (OperatingSystem.IsWindows()) { JitUnwindWindows.InstallFunctionTableHandler( firstRegion.Pointer, CacheSize, firstRegion.Pointer + Allocate(_pageSize) ); } + + _initialized = true; } } @@ -147,7 +136,7 @@ namespace ARMeilleure.Translation.Cache if (TryFind(funcOffset, out CacheEntry entry, out int entryIndex) && entry.Offset == funcOffset) { - _cacheAllocators[_activeRegionIndex].Free(funcOffset, AlignCodeSize(entry.Size)); + _cacheAllocator.Free(funcOffset, AlignCodeSize(entry.Size)); _cacheEntries.RemoveAt(entryIndex); } @@ -178,24 +167,30 @@ namespace ARMeilleure.Translation.Cache { codeSize = AlignCodeSize(codeSize); - int allocOffset = _cacheAllocators[_activeRegionIndex].Allocate(codeSize); - - if (allocOffset >= 0) + for (int i = _activeRegionIndex; i < _jitRegions.Count; i++) { - _jitRegions[_activeRegionIndex].ExpandIfNeeded((ulong)allocOffset + (ulong)codeSize); - return allocOffset; + int allocOffset = _cacheAllocator.Allocate(codeSize); + + if (allocOffset >= 0) + { + _jitRegions[i].ExpandIfNeeded((ulong)allocOffset + (ulong)codeSize); + _activeRegionIndex = i; + return allocOffset; + } } int exhaustedRegion = _activeRegionIndex; ReservedRegion newRegion = new(_jitRegions[0].Allocator, CacheSize); _jitRegions.Add(newRegion); _activeRegionIndex = _jitRegions.Count - 1; + + int newRegionNumber = _activeRegionIndex; - Logger.Warning?.Print(LogClass.Cpu, $"JIT Cache Region {exhaustedRegion} exhausted, creating new Cache Region {_activeRegionIndex} ({((long)(_activeRegionIndex + 1) * CacheSize).Bytes()} Total Allocation)."); + Logger.Warning?.Print(LogClass.Cpu, $"JIT Cache Region {exhaustedRegion} exhausted, creating new Cache Region {newRegionNumber} ({((long)(newRegionNumber + 1) * CacheSize).Bytes()} Total Allocation)."); + + _cacheAllocator = new CacheMemoryAllocator(CacheSize); - _cacheAllocators.Add(new CacheMemoryAllocator(CacheSize)); - - int allocOffsetNew = _cacheAllocators[_activeRegionIndex].Allocate(codeSize); + int allocOffsetNew = _cacheAllocator.Allocate(codeSize); if (allocOffsetNew < 0) { throw new OutOfMemoryException("Failed to allocate in new Cache Region!");