Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missing "yield return" #424

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions src/Ryujinx.Cpu/AppleHv/HvMemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,64 +230,60 @@ public IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size)
{
if (size == 0)
{
return Enumerable.Empty<HostMemoryRange>();
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;
}

/// <inheritdoc/>
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
{
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))
{
return null;
yield break;
}

int pages = GetPagesCount(va, (uint)size, out va);

var regions = new List<MemoryRange>();

ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize;

for (int page = 0; page < pages - 1; page++)
{
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;
}
Expand All @@ -296,9 +292,7 @@ private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
regionSize += PageSize;
}

regions.Add(new MemoryRange(regionStart, regionSize));

return regions;
yield return new MemoryRange(regionStart, regionSize);
}

/// <remarks>
Expand Down
34 changes: 14 additions & 20 deletions src/Ryujinx.Cpu/Jit/MemoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,64 +250,60 @@ public IEnumerable<HostMemoryRange> GetHostRegions(ulong va, ulong size)
{
if (size == 0)
{
return Enumerable.Empty<HostMemoryRange>();
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;
}

/// <inheritdoc/>
public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong size)
{
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))
{
return null;
yield break;
}

int pages = GetPagesCount(va, (uint)size, out va);

var regions = new List<MemoryRange>();

ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize;

for (int page = 0; page < pages - 1; page++)
{
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;
}
Expand All @@ -316,9 +312,7 @@ private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
regionSize += PageSize;
}

regions.Add(new MemoryRange(regionStart, regionSize));

return regions;
yield return new MemoryRange(regionStart, regionSize);
}

/// <inheritdoc/>
Expand Down
14 changes: 5 additions & 9 deletions src/Ryujinx.Cpu/Jit/MemoryManagerHostTracked.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,32 +475,30 @@ public IEnumerable<MemoryRange> GetPhysicalRegions(ulong va, ulong 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))
{
return null;
yield break;
}

int pages = GetPagesCount(va, (uint)size, out va);

var regions = new List<MemoryRange>();

ulong regionStart = GetPhysicalAddressInternal(va);
ulong regionSize = PageSize;

for (int page = 0; page < pages - 1; page++)
{
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;
}
Expand All @@ -509,9 +507,7 @@ private List<MemoryRange> GetPhysicalRegionsImpl(ulong va, ulong size)
regionSize += PageSize;
}

regions.Add(new MemoryRange(regionStart, regionSize));

return regions;
yield return new MemoryRange(regionStart, regionSize);
}

/// <inheritdoc/>
Expand Down
6 changes: 1 addition & 5 deletions src/Ryujinx.Cpu/LightningJit/CodeGen/Arm64/StackWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class StackWalker : IStackWalker
{
public IEnumerable<ulong> GetCallStack(nint framePointer, nint codeRegionStart, int codeRegionSize, nint codeRegion2Start, int codeRegion2Size)
{
List<ulong> functionPointers = new();

while (true)
{
nint functionPointer = Marshal.ReadIntPtr(framePointer, nint.Size);
Expand All @@ -20,11 +18,9 @@ public IEnumerable<ulong> GetCallStack(nint framePointer, nint codeRegionStart,
break;
}

functionPointers.Add((ulong)functionPointer - 4);
yield return (ulong)functionPointer - 4;
framePointer = Marshal.ReadIntPtr(framePointer);
}

return functionPointers;
}
}
}
10 changes: 3 additions & 7 deletions src/Ryujinx.Graphics.Vulkan/BufferMirrorRangeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,14 @@ public readonly bool OverlapsWith(int offset, int size)
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;
if (list == null)
{
return null;
yield break;
}

List<Range> result = null;

int index = BinarySearch(list, offset, size);

if (index >= 0)
Expand All @@ -189,12 +187,10 @@ public readonly List<Range> FindOverlaps(int offset, int size)

do
{
(result ??= new List<Range>()).Add(list[index++]);
yield return list[index++];
}
while (index < list.Count && list[index].OverlapsWith(offset, size));
}

return result;
}

private static int BinarySearch(List<Range> list, int offset, int size)
Expand Down
9 changes: 3 additions & 6 deletions src/Ryujinx.HLE/HOS/ModLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,6 @@ private static IEnumerable<Cheat> GetCheatsInFile(FileInfo cheatFile)
{
string cheatName = DefaultCheatName;
List<string> instructions = new();
List<Cheat> cheats = new();

using StreamReader cheatData = cheatFile.OpenText();
while (cheatData.ReadLine() is { } line)
Expand All @@ -373,13 +372,13 @@ private static IEnumerable<Cheat> GetCheatsInFile(FileInfo cheatFile)

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.
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.
Expand All @@ -396,10 +395,8 @@ private static IEnumerable<Cheat> GetCheatsInFile(FileInfo cheatFile)
// 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
Expand Down
Loading
Loading