From decd37ce6d670d6a1136409292d253dabd69475d Mon Sep 17 00:00:00 2001 From: Marco Carvalho Date: Sun, 22 Dec 2024 02:28:31 -0300 Subject: [PATCH] Add missing "yield return" (#424) --- src/Ryujinx.Cpu/AppleHv/HvMemoryManager.cs | 34 +++++++-------- src/Ryujinx.Cpu/Jit/MemoryManager.cs | 34 +++++++-------- .../Jit/MemoryManagerHostTracked.cs | 14 +++---- .../LightningJit/CodeGen/Arm64/StackWalker.cs | 6 +-- .../BufferMirrorRangeList.cs | 10 ++--- src/Ryujinx.HLE/HOS/ModLoader.cs | 9 ++-- src/Ryujinx.Memory/AddressSpaceManager.cs | 42 +++++++------------ 7 files changed, 54 insertions(+), 95 deletions(-) diff --git a/src/Ryujinx.Cpu/AppleHv/HvMemoryManager.cs b/src/Ryujinx.Cpu/AppleHv/HvMemoryManager.cs index bb56a4344..74c39d6a8 100644 --- a/src/Ryujinx.Cpu/AppleHv/HvMemoryManager.cs +++ b/src/Ryujinx.Cpu/AppleHv/HvMemoryManager.cs @@ -230,25 +230,20 @@ namespace Ryujinx.Cpu.AppleHv { if (size == 0) { - return Enumerable.Empty(); + yield break; } var guestRegions = GetPhysicalRegionsImpl(va, size); if (guestRegions == null) { - return null; + yield break; } - var regions = new HostMemoryRange[guestRegions.Count]; - - for (int i = 0; i < regions.Length; i++) + foreach (var guestRegion in guestRegions) { - var guestRegion = guestRegions[i]; nint pointer = _backingMemory.GetPointer(guestRegion.Address, guestRegion.Size); - regions[i] = new HostMemoryRange((nuint)(ulong)pointer, guestRegion.Size); + yield return new HostMemoryRange((nuint)(ulong)pointer, guestRegion.Size); } - - return regions; } /// @@ -256,23 +251,24 @@ namespace Ryujinx.Cpu.AppleHv { if (size == 0) { - return Enumerable.Empty(); + yield break; } - return GetPhysicalRegionsImpl(va, size); + foreach (var physicalRegion in GetPhysicalRegionsImpl(va, size)) + { + yield return physicalRegion; + } } - private List GetPhysicalRegionsImpl(ulong va, ulong size) + private IEnumerable GetPhysicalRegionsImpl(ulong va, ulong size) { if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) { - return null; + yield break; } int pages = GetPagesCount(va, (uint)size, out va); - var regions = new List(); - ulong regionStart = GetPhysicalAddressInternal(va); ulong regionSize = PageSize; @@ -280,14 +276,14 @@ namespace Ryujinx.Cpu.AppleHv { if (!ValidateAddress(va + PageSize)) { - return null; + yield break; } ulong newPa = GetPhysicalAddressInternal(va + PageSize); if (GetPhysicalAddressInternal(va) + PageSize != newPa) { - regions.Add(new MemoryRange(regionStart, regionSize)); + yield return new MemoryRange(regionStart, regionSize); regionStart = newPa; regionSize = 0; } @@ -296,9 +292,7 @@ namespace Ryujinx.Cpu.AppleHv regionSize += PageSize; } - regions.Add(new MemoryRange(regionStart, regionSize)); - - return regions; + yield return new MemoryRange(regionStart, regionSize); } /// diff --git a/src/Ryujinx.Cpu/Jit/MemoryManager.cs b/src/Ryujinx.Cpu/Jit/MemoryManager.cs index 049e508d0..076fb6ad8 100644 --- a/src/Ryujinx.Cpu/Jit/MemoryManager.cs +++ b/src/Ryujinx.Cpu/Jit/MemoryManager.cs @@ -250,25 +250,20 @@ namespace Ryujinx.Cpu.Jit { if (size == 0) { - return Enumerable.Empty(); + yield break; } var guestRegions = GetPhysicalRegionsImpl(va, size); if (guestRegions == null) { - return null; + yield break; } - var regions = new HostMemoryRange[guestRegions.Count]; - - for (int i = 0; i < regions.Length; i++) + foreach (var guestRegion in guestRegions) { - var guestRegion = guestRegions[i]; nint pointer = _backingMemory.GetPointer(guestRegion.Address, guestRegion.Size); - regions[i] = new HostMemoryRange((nuint)(ulong)pointer, guestRegion.Size); + yield return new HostMemoryRange((nuint)(ulong)pointer, guestRegion.Size); } - - return regions; } /// @@ -276,23 +271,24 @@ namespace Ryujinx.Cpu.Jit { if (size == 0) { - return Enumerable.Empty(); + yield break; } - return GetPhysicalRegionsImpl(va, size); + foreach (var physicalRegion in GetPhysicalRegionsImpl(va, size)) + { + yield return physicalRegion; + } } - private List GetPhysicalRegionsImpl(ulong va, ulong size) + private IEnumerable GetPhysicalRegionsImpl(ulong va, ulong size) { if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) { - return null; + yield break; } int pages = GetPagesCount(va, (uint)size, out va); - var regions = new List(); - ulong regionStart = GetPhysicalAddressInternal(va); ulong regionSize = PageSize; @@ -300,14 +296,14 @@ namespace Ryujinx.Cpu.Jit { if (!ValidateAddress(va + PageSize)) { - return null; + yield break; } ulong newPa = GetPhysicalAddressInternal(va + PageSize); if (GetPhysicalAddressInternal(va) + PageSize != newPa) { - regions.Add(new MemoryRange(regionStart, regionSize)); + yield return new MemoryRange(regionStart, regionSize); regionStart = newPa; regionSize = 0; } @@ -316,9 +312,7 @@ namespace Ryujinx.Cpu.Jit regionSize += PageSize; } - regions.Add(new MemoryRange(regionStart, regionSize)); - - return regions; + yield return new MemoryRange(regionStart, regionSize); } /// diff --git a/src/Ryujinx.Cpu/Jit/MemoryManagerHostTracked.cs b/src/Ryujinx.Cpu/Jit/MemoryManagerHostTracked.cs index 4dab212a7..499f991f2 100644 --- a/src/Ryujinx.Cpu/Jit/MemoryManagerHostTracked.cs +++ b/src/Ryujinx.Cpu/Jit/MemoryManagerHostTracked.cs @@ -475,17 +475,15 @@ namespace Ryujinx.Cpu.Jit return GetPhysicalRegionsImpl(va, size); } - private List GetPhysicalRegionsImpl(ulong va, ulong size) + private IEnumerable GetPhysicalRegionsImpl(ulong va, ulong size) { if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) { - return null; + yield break; } int pages = GetPagesCount(va, (uint)size, out va); - var regions = new List(); - ulong regionStart = GetPhysicalAddressInternal(va); ulong regionSize = PageSize; @@ -493,14 +491,14 @@ namespace Ryujinx.Cpu.Jit { if (!ValidateAddress(va + PageSize)) { - return null; + yield break; } ulong newPa = GetPhysicalAddressInternal(va + PageSize); if (GetPhysicalAddressInternal(va) + PageSize != newPa) { - regions.Add(new MemoryRange(regionStart, regionSize)); + yield return new MemoryRange(regionStart, regionSize); regionStart = newPa; regionSize = 0; } @@ -509,9 +507,7 @@ namespace Ryujinx.Cpu.Jit regionSize += PageSize; } - regions.Add(new MemoryRange(regionStart, regionSize)); - - return regions; + yield return new MemoryRange(regionStart, regionSize); } /// diff --git a/src/Ryujinx.Cpu/LightningJit/CodeGen/Arm64/StackWalker.cs b/src/Ryujinx.Cpu/LightningJit/CodeGen/Arm64/StackWalker.cs index 3ce7c4f9c..1432c4598 100644 --- a/src/Ryujinx.Cpu/LightningJit/CodeGen/Arm64/StackWalker.cs +++ b/src/Ryujinx.Cpu/LightningJit/CodeGen/Arm64/StackWalker.cs @@ -8,8 +8,6 @@ namespace Ryujinx.Cpu.LightningJit.CodeGen.Arm64 { public IEnumerable GetCallStack(nint framePointer, nint codeRegionStart, int codeRegionSize, nint codeRegion2Start, int codeRegion2Size) { - List functionPointers = new(); - while (true) { nint functionPointer = Marshal.ReadIntPtr(framePointer, nint.Size); @@ -20,11 +18,9 @@ namespace Ryujinx.Cpu.LightningJit.CodeGen.Arm64 break; } - functionPointers.Add((ulong)functionPointer - 4); + yield return (ulong)functionPointer - 4; framePointer = Marshal.ReadIntPtr(framePointer); } - - return functionPointers; } } } diff --git a/src/Ryujinx.Graphics.Vulkan/BufferMirrorRangeList.cs b/src/Ryujinx.Graphics.Vulkan/BufferMirrorRangeList.cs index 5722ca1ac..e79248a47 100644 --- a/src/Ryujinx.Graphics.Vulkan/BufferMirrorRangeList.cs +++ b/src/Ryujinx.Graphics.Vulkan/BufferMirrorRangeList.cs @@ -168,16 +168,14 @@ namespace Ryujinx.Graphics.Vulkan return BinarySearch(list, offset, size) >= 0; } - public readonly List FindOverlaps(int offset, int size) + public readonly IEnumerable FindOverlaps(int offset, int size) { var list = _ranges; if (list == null) { - return null; + yield break; } - List result = null; - int index = BinarySearch(list, offset, size); if (index >= 0) @@ -189,12 +187,10 @@ namespace Ryujinx.Graphics.Vulkan do { - (result ??= new List()).Add(list[index++]); + yield return list[index++]; } while (index < list.Count && list[index].OverlapsWith(offset, size)); } - - return result; } private static int BinarySearch(List list, int offset, int size) diff --git a/src/Ryujinx.HLE/HOS/ModLoader.cs b/src/Ryujinx.HLE/HOS/ModLoader.cs index d8c62fc66..4bd695ae5 100644 --- a/src/Ryujinx.HLE/HOS/ModLoader.cs +++ b/src/Ryujinx.HLE/HOS/ModLoader.cs @@ -357,7 +357,6 @@ namespace Ryujinx.HLE.HOS { string cheatName = DefaultCheatName; List instructions = new(); - List cheats = new(); using StreamReader cheatData = cheatFile.OpenText(); while (cheatData.ReadLine() is { } line) @@ -373,13 +372,13 @@ namespace Ryujinx.HLE.HOS Logger.Warning?.Print(LogClass.ModLoader, $"Ignoring cheat '{cheatFile.FullName}' because it is malformed"); - return Array.Empty(); + yield break; } // Add the previous section to the list. if (instructions.Count > 0) { - cheats.Add(new Cheat($"<{cheatName} Cheat>", cheatFile, instructions)); + yield return new Cheat($"<{cheatName} Cheat>", cheatFile, instructions); } // Start a new cheat section. @@ -396,10 +395,8 @@ namespace Ryujinx.HLE.HOS // Add the last section being processed. if (instructions.Count > 0) { - cheats.Add(new Cheat($"<{cheatName} Cheat>", cheatFile, instructions)); + yield return new Cheat($"<{cheatName} Cheat>", cheatFile, instructions); } - - return cheats; } // Assumes searchDirPaths don't overlap diff --git a/src/Ryujinx.Memory/AddressSpaceManager.cs b/src/Ryujinx.Memory/AddressSpaceManager.cs index 807c5c0f4..7bd572d7a 100644 --- a/src/Ryujinx.Memory/AddressSpaceManager.cs +++ b/src/Ryujinx.Memory/AddressSpaceManager.cs @@ -106,10 +106,13 @@ namespace Ryujinx.Memory { if (size == 0) { - return Enumerable.Empty(); + yield break; } - return GetHostRegionsImpl(va, size); + foreach (var hostRegion in GetHostRegionsImpl(va, size)) + { + yield return hostRegion; + } } /// @@ -117,51 +120,36 @@ namespace Ryujinx.Memory { if (size == 0) { - return Enumerable.Empty(); + yield break; } var hostRegions = GetHostRegionsImpl(va, size); if (hostRegions == null) { - return null; + yield break; } - var regions = new MemoryRange[hostRegions.Count]; - ulong backingStart = (ulong)_backingMemory.Pointer; ulong backingEnd = backingStart + _backingMemory.Size; - int count = 0; - - for (int i = 0; i < regions.Length; i++) + foreach (var hostRegion in hostRegions) { - var hostRegion = hostRegions[i]; - if (hostRegion.Address >= backingStart && hostRegion.Address < backingEnd) { - regions[count++] = new MemoryRange(hostRegion.Address - backingStart, hostRegion.Size); + yield return new MemoryRange(hostRegion.Address - backingStart, hostRegion.Size); } } - - if (count != regions.Length) - { - return new ArraySegment(regions, 0, count); - } - - return regions; } - private List GetHostRegionsImpl(ulong va, ulong size) + private IEnumerable GetHostRegionsImpl(ulong va, ulong size) { if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) { - return null; + yield break; } int pages = GetPagesCount(va, size, out va); - var regions = new List(); - nuint regionStart = GetHostAddress(va); ulong regionSize = PageSize; @@ -169,14 +157,14 @@ namespace Ryujinx.Memory { if (!ValidateAddress(va + PageSize)) { - return null; + yield break; } nuint newHostAddress = GetHostAddress(va + PageSize); if (GetHostAddress(va) + PageSize != newHostAddress) { - regions.Add(new HostMemoryRange(regionStart, regionSize)); + yield return new HostMemoryRange(regionStart, regionSize); regionStart = newHostAddress; regionSize = 0; } @@ -185,9 +173,7 @@ namespace Ryujinx.Memory regionSize += PageSize; } - regions.Add(new HostMemoryRange(regionStart, regionSize)); - - return regions; + yield return new HostMemoryRange(regionStart, regionSize); } [MethodImpl(MethodImplOptions.AggressiveInlining)]