0
0
Fork 0
mirror of https://github.com/GreemDev/Ryujinx.git synced 2024-12-22 21:35:48 +00:00

Add missing "yield return" (#424)

This commit is contained in:
Marco Carvalho 2024-12-22 02:28:31 -03:00 committed by GitHub
parent 67ec10feea
commit decd37ce6d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 54 additions and 95 deletions

View file

@ -230,25 +230,20 @@ namespace Ryujinx.Cpu.AppleHv
{ {
if (size == 0) if (size == 0)
{ {
return Enumerable.Empty<HostMemoryRange>(); yield break;
} }
var guestRegions = GetPhysicalRegionsImpl(va, size); var guestRegions = GetPhysicalRegionsImpl(va, size);
if (guestRegions == null) if (guestRegions == null)
{ {
return null; yield break;
} }
var regions = new HostMemoryRange[guestRegions.Count]; foreach (var guestRegion in guestRegions)
for (int i = 0; i < regions.Length; i++)
{ {
var guestRegion = guestRegions[i];
nint pointer = _backingMemory.GetPointer(guestRegion.Address, guestRegion.Size); 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;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -256,23 +251,24 @@ namespace Ryujinx.Cpu.AppleHv
{ {
if (size == 0) if (size == 0)
{ {
return Enumerable.Empty<MemoryRange>(); yield break;
} }
return GetPhysicalRegionsImpl(va, size); foreach (var physicalRegion in GetPhysicalRegionsImpl(va, size))
{
yield return physicalRegion;
}
} }
private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size) private IEnumerable<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
{ {
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
{ {
return null; yield break;
} }
int pages = GetPagesCount(va, (uint)size, out va); int pages = GetPagesCount(va, (uint)size, out va);
var regions = new List<MemoryRange>();
ulong regionStart = GetPhysicalAddressInternal(va); ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize; ulong regionSize = PageSize;
@ -280,14 +276,14 @@ namespace Ryujinx.Cpu.AppleHv
{ {
if (!ValidateAddress(va + PageSize)) if (!ValidateAddress(va + PageSize))
{ {
return null; yield break;
} }
ulong newPa = GetPhysicalAddressInternal(va + PageSize); ulong newPa = GetPhysicalAddressInternal(va + PageSize);
if (GetPhysicalAddressInternal(va) + PageSize != newPa) if (GetPhysicalAddressInternal(va) + PageSize != newPa)
{ {
regions.Add(new MemoryRange(regionStart, regionSize)); yield return new MemoryRange(regionStart, regionSize);
regionStart = newPa; regionStart = newPa;
regionSize = 0; regionSize = 0;
} }
@ -296,9 +292,7 @@ namespace Ryujinx.Cpu.AppleHv
regionSize += PageSize; regionSize += PageSize;
} }
regions.Add(new MemoryRange(regionStart, regionSize)); yield return new MemoryRange(regionStart, regionSize);
return regions;
} }
/// <remarks> /// <remarks>

View file

@ -250,25 +250,20 @@ namespace Ryujinx.Cpu.Jit
{ {
if (size == 0) if (size == 0)
{ {
return Enumerable.Empty<HostMemoryRange>(); yield break;
} }
var guestRegions = GetPhysicalRegionsImpl(va, size); var guestRegions = GetPhysicalRegionsImpl(va, size);
if (guestRegions == null) if (guestRegions == null)
{ {
return null; yield break;
} }
var regions = new HostMemoryRange[guestRegions.Count]; foreach (var guestRegion in guestRegions)
for (int i = 0; i < regions.Length; i++)
{ {
var guestRegion = guestRegions[i];
nint pointer = _backingMemory.GetPointer(guestRegion.Address, guestRegion.Size); 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;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -276,23 +271,24 @@ namespace Ryujinx.Cpu.Jit
{ {
if (size == 0) if (size == 0)
{ {
return Enumerable.Empty<MemoryRange>(); yield break;
} }
return GetPhysicalRegionsImpl(va, size); foreach (var physicalRegion in GetPhysicalRegionsImpl(va, size))
{
yield return physicalRegion;
}
} }
private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size) private IEnumerable<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
{ {
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
{ {
return null; yield break;
} }
int pages = GetPagesCount(va, (uint)size, out va); int pages = GetPagesCount(va, (uint)size, out va);
var regions = new List<MemoryRange>();
ulong regionStart = GetPhysicalAddressInternal(va); ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize; ulong regionSize = PageSize;
@ -300,14 +296,14 @@ namespace Ryujinx.Cpu.Jit
{ {
if (!ValidateAddress(va + PageSize)) if (!ValidateAddress(va + PageSize))
{ {
return null; yield break;
} }
ulong newPa = GetPhysicalAddressInternal(va + PageSize); ulong newPa = GetPhysicalAddressInternal(va + PageSize);
if (GetPhysicalAddressInternal(va) + PageSize != newPa) if (GetPhysicalAddressInternal(va) + PageSize != newPa)
{ {
regions.Add(new MemoryRange(regionStart, regionSize)); yield return new MemoryRange(regionStart, regionSize);
regionStart = newPa; regionStart = newPa;
regionSize = 0; regionSize = 0;
} }
@ -316,9 +312,7 @@ namespace Ryujinx.Cpu.Jit
regionSize += PageSize; regionSize += PageSize;
} }
regions.Add(new MemoryRange(regionStart, regionSize)); yield return new MemoryRange(regionStart, regionSize);
return regions;
} }
/// <inheritdoc/> /// <inheritdoc/>

View file

@ -475,17 +475,15 @@ namespace Ryujinx.Cpu.Jit
return GetPhysicalRegionsImpl(va, size); return GetPhysicalRegionsImpl(va, size);
} }
private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size) private IEnumerable<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
{ {
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
{ {
return null; yield break;
} }
int pages = GetPagesCount(va, (uint)size, out va); int pages = GetPagesCount(va, (uint)size, out va);
var regions = new List<MemoryRange>();
ulong regionStart = GetPhysicalAddressInternal(va); ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize; ulong regionSize = PageSize;
@ -493,14 +491,14 @@ namespace Ryujinx.Cpu.Jit
{ {
if (!ValidateAddress(va + PageSize)) if (!ValidateAddress(va + PageSize))
{ {
return null; yield break;
} }
ulong newPa = GetPhysicalAddressInternal(va + PageSize); ulong newPa = GetPhysicalAddressInternal(va + PageSize);
if (GetPhysicalAddressInternal(va) + PageSize != newPa) if (GetPhysicalAddressInternal(va) + PageSize != newPa)
{ {
regions.Add(new MemoryRange(regionStart, regionSize)); yield return new MemoryRange(regionStart, regionSize);
regionStart = newPa; regionStart = newPa;
regionSize = 0; regionSize = 0;
} }
@ -509,9 +507,7 @@ namespace Ryujinx.Cpu.Jit
regionSize += PageSize; regionSize += PageSize;
} }
regions.Add(new MemoryRange(regionStart, regionSize)); yield return new MemoryRange(regionStart, regionSize);
return regions;
} }
/// <inheritdoc/> /// <inheritdoc/>

View file

@ -8,8 +8,6 @@ namespace Ryujinx.Cpu.LightningJit.CodeGen.Arm64
{ {
public IEnumerable<ulong> GetCallStack(nint framePointer, nint codeRegionStart, int codeRegionSize, nint codeRegion2Start, int codeRegion2Size) public IEnumerable<ulong> GetCallStack(nint framePointer, nint codeRegionStart, int codeRegionSize, nint codeRegion2Start, int codeRegion2Size)
{ {
List<ulong> functionPointers = new();
while (true) while (true)
{ {
nint functionPointer = Marshal.ReadIntPtr(framePointer, nint.Size); nint functionPointer = Marshal.ReadIntPtr(framePointer, nint.Size);
@ -20,11 +18,9 @@ namespace Ryujinx.Cpu.LightningJit.CodeGen.Arm64
break; break;
} }
functionPointers.Add((ulong)functionPointer - 4); yield return (ulong)functionPointer - 4;
framePointer = Marshal.ReadIntPtr(framePointer); framePointer = Marshal.ReadIntPtr(framePointer);
} }
return functionPointers;
} }
} }
} }

View file

@ -168,16 +168,14 @@ namespace Ryujinx.Graphics.Vulkan
return BinarySearch(list, offset, size) >= 0; return BinarySearch(list, offset, size) >= 0;
} }
public readonly List<Range> FindOverlaps(int offset, int size) public readonly IEnumerable<Range> FindOverlaps(int offset, int size)
{ {
var list = _ranges; var list = _ranges;
if (list == null) if (list == null)
{ {
return null; yield break;
} }
List<Range> result = null;
int index = BinarySearch(list, offset, size); int index = BinarySearch(list, offset, size);
if (index >= 0) if (index >= 0)
@ -189,12 +187,10 @@ namespace Ryujinx.Graphics.Vulkan
do do
{ {
(result ??= new List<Range>()).Add(list[index++]); yield return list[index++];
} }
while (index < list.Count && list[index].OverlapsWith(offset, size)); while (index < list.Count && list[index].OverlapsWith(offset, size));
} }
return result;
} }
private static int BinarySearch(List<Range> list, int offset, int size) private static int BinarySearch(List<Range> list, int offset, int size)

View file

@ -357,7 +357,6 @@ namespace Ryujinx.HLE.HOS
{ {
string cheatName = DefaultCheatName; string cheatName = DefaultCheatName;
List<string> instructions = new(); List<string> instructions = new();
List<Cheat> cheats = new();
using StreamReader cheatData = cheatFile.OpenText(); using StreamReader cheatData = cheatFile.OpenText();
while (cheatData.ReadLine() is { } line) 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"); Logger.Warning?.Print(LogClass.ModLoader, $"Ignoring cheat '{cheatFile.FullName}' because it is malformed");
return Array.Empty<Cheat>(); yield break;
} }
// Add the previous section to the list. // Add the previous section to the list.
if (instructions.Count > 0) 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. // Start a new cheat section.
@ -396,10 +395,8 @@ namespace Ryujinx.HLE.HOS
// Add the last section being processed. // Add the last section being processed.
if (instructions.Count > 0) 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 // Assumes searchDirPaths don't overlap

View file

@ -106,10 +106,13 @@ namespace Ryujinx.Memory
{ {
if (size == 0) if (size == 0)
{ {
return Enumerable.Empty<HostMemoryRange>(); yield break;
} }
return GetHostRegionsImpl(va, size); foreach (var hostRegion in GetHostRegionsImpl(va, size))
{
yield return hostRegion;
}
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -117,51 +120,36 @@ namespace Ryujinx.Memory
{ {
if (size == 0) if (size == 0)
{ {
return Enumerable.Empty<MemoryRange>(); yield break;
} }
var hostRegions = GetHostRegionsImpl(va, size); var hostRegions = GetHostRegionsImpl(va, size);
if (hostRegions == null) if (hostRegions == null)
{ {
return null; yield break;
} }
var regions = new MemoryRange[hostRegions.Count];
ulong backingStart = (ulong)_backingMemory.Pointer; ulong backingStart = (ulong)_backingMemory.Pointer;
ulong backingEnd = backingStart + _backingMemory.Size; ulong backingEnd = backingStart + _backingMemory.Size;
int count = 0; foreach (var hostRegion in hostRegions)
for (int i = 0; i < regions.Length; i++)
{ {
var hostRegion = hostRegions[i];
if (hostRegion.Address >= backingStart && hostRegion.Address < backingEnd) 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<MemoryRange>(regions, 0, count);
}
return regions;
} }
private List<HostMemoryRange> GetHostRegionsImpl(ulong va, ulong size) private IEnumerable<HostMemoryRange> GetHostRegionsImpl(ulong va, ulong size)
{ {
if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size)) if (!ValidateAddress(va) || !ValidateAddressAndSize(va, size))
{ {
return null; yield break;
} }
int pages = GetPagesCount(va, size, out va); int pages = GetPagesCount(va, size, out va);
var regions = new List<HostMemoryRange>();
nuint regionStart = GetHostAddress(va); nuint regionStart = GetHostAddress(va);
ulong regionSize = PageSize; ulong regionSize = PageSize;
@ -169,14 +157,14 @@ namespace Ryujinx.Memory
{ {
if (!ValidateAddress(va + PageSize)) if (!ValidateAddress(va + PageSize))
{ {
return null; yield break;
} }
nuint newHostAddress = GetHostAddress(va + PageSize); nuint newHostAddress = GetHostAddress(va + PageSize);
if (GetHostAddress(va) + PageSize != newHostAddress) if (GetHostAddress(va) + PageSize != newHostAddress)
{ {
regions.Add(new HostMemoryRange(regionStart, regionSize)); yield return new HostMemoryRange(regionStart, regionSize);
regionStart = newHostAddress; regionStart = newHostAddress;
regionSize = 0; regionSize = 0;
} }
@ -185,9 +173,7 @@ namespace Ryujinx.Memory
regionSize += PageSize; regionSize += PageSize;
} }
regions.Add(new HostMemoryRange(regionStart, regionSize)); yield return new HostMemoryRange(regionStart, regionSize);
return regions;
} }
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]