mirror of
https://git.ryujinx.app/ryubing/ryujinx.git
synced 2026-05-23 05:35:47 +00:00
Memory Changes (ryubing/ryujinx!46)
See merge request ryubing/ryujinx!46
This commit is contained in:
@@ -3,7 +3,7 @@ namespace Ryujinx.Memory.Range
|
||||
/// <summary>
|
||||
/// Range of memory that can be split in two.
|
||||
/// </summary>
|
||||
interface INonOverlappingRange : IRange
|
||||
public interface INonOverlappingRange : IRange
|
||||
{
|
||||
/// <summary>
|
||||
/// Split this region into two, around the specified address.
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Memory.Range
|
||||
{
|
||||
@@ -7,8 +10,284 @@ namespace Ryujinx.Memory.Range
|
||||
/// A range list that assumes ranges are non-overlapping, with list items that can be split in two to avoid overlaps.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the range.</typeparam>
|
||||
class NonOverlappingRangeList<T> : RangeList<T> where T : INonOverlappingRange
|
||||
public class NonOverlappingRangeList<T> : RangeListBase<T> where T : INonOverlappingRange
|
||||
{
|
||||
private readonly Dictionary<ulong, RangeItem<T>> _quickAccess = new(AddressEqualityComparer.Comparer);
|
||||
private readonly Dictionary<ulong, RangeItem<T>> _fastQuickAccess = new(AddressEqualityComparer.Comparer);
|
||||
|
||||
public readonly ReaderWriterLockSlim Lock = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new non-overlapping range list.
|
||||
/// </summary>
|
||||
public NonOverlappingRangeList() { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new non-overlapping range list.
|
||||
/// </summary>
|
||||
/// <param name="backingInitialSize">The initial size of the backing array</param>
|
||||
public NonOverlappingRangeList(int backingInitialSize) : base(backingInitialSize) { }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new item to the list.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be added</param>
|
||||
public override void Add(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
index = ~index;
|
||||
}
|
||||
|
||||
RangeItem<T> rangeItem = new(item);
|
||||
|
||||
Insert(index, rangeItem);
|
||||
|
||||
_quickAccess.Add(item.Address, rangeItem);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Updates an item's end address on the list. Address must be the same.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be updated</param>
|
||||
/// <returns>True if the item was located and updated, false otherwise</returns>
|
||||
protected override bool Update(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
if (index >= 0 && Items[index].Value.Equals(item))
|
||||
{
|
||||
RangeItem<T> rangeItem = new(item) { Previous = Items[index].Previous, Next = Items[index].Next };
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
Items[index - 1].Next = rangeItem;
|
||||
}
|
||||
|
||||
if (index < Count - 1)
|
||||
{
|
||||
Items[index + 1].Previous = rangeItem;
|
||||
}
|
||||
|
||||
foreach (ulong addr in Items[index].QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(addr);
|
||||
_fastQuickAccess.Remove(addr);
|
||||
}
|
||||
|
||||
Items[index] = rangeItem;
|
||||
|
||||
_quickAccess[item.Address] = rangeItem;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void Insert(int index, RangeItem<T> item)
|
||||
{
|
||||
Debug.Assert(item.Address != item.EndAddress);
|
||||
|
||||
if (Count + 1 > Items.Length)
|
||||
{
|
||||
Array.Resize(ref Items, Items.Length + BackingGrowthSize);
|
||||
}
|
||||
|
||||
if (index >= Count)
|
||||
{
|
||||
if (index == Count)
|
||||
{
|
||||
if (index != 0)
|
||||
{
|
||||
item.Previous = Items[index - 1];
|
||||
Items[index - 1].Next = item;
|
||||
}
|
||||
Items[index] = item;
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(Items, index, Items, index + 1, Count - index);
|
||||
|
||||
Items[index] = item;
|
||||
if (index != 0)
|
||||
{
|
||||
item.Previous = Items[index - 1];
|
||||
Items[index - 1].Next = item;
|
||||
}
|
||||
|
||||
item.Next = Items[index + 1];
|
||||
Items[index + 1].Previous = item;
|
||||
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void RemoveAt(int index)
|
||||
{
|
||||
if (index < Count - 1)
|
||||
{
|
||||
Items[index + 1].Previous = index > 0 ? Items[index - 1] : null;
|
||||
}
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
Items[index - 1].Next = index < Count - 1 ? Items[index + 1] : null;
|
||||
}
|
||||
|
||||
if (index < --Count)
|
||||
{
|
||||
Array.Copy(Items, index + 1, Items, index, Count - index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes an item from the list.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be removed</param>
|
||||
/// <returns>True if the item was removed, or false if it was not found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override bool Remove(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
if (index >= 0 && Items[index].Value.Equals(item))
|
||||
{
|
||||
_quickAccess.Remove(item.Address);
|
||||
|
||||
foreach (ulong addr in Items[index].QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(addr);
|
||||
_fastQuickAccess.Remove(addr);
|
||||
}
|
||||
|
||||
RemoveAt(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a range of items from the item list
|
||||
/// </summary>
|
||||
/// <param name="startItem">The first item in the range of items to be removed</param>
|
||||
/// <param name="endItem">The last item in the range of items to be removed</param>
|
||||
public override void RemoveRange(RangeItem<T> startItem, RangeItem<T> endItem)
|
||||
{
|
||||
if (startItem is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (startItem == endItem)
|
||||
{
|
||||
Remove(startItem.Value);
|
||||
return;
|
||||
}
|
||||
|
||||
int startIndex = BinarySearch(startItem.Address);
|
||||
int endIndex = BinarySearch(endItem.Address);
|
||||
|
||||
if (endIndex < Count - 1)
|
||||
{
|
||||
Items[endIndex + 1].Previous = startIndex > 0 ? Items[startIndex - 1] : null;
|
||||
}
|
||||
|
||||
if (startIndex > 0)
|
||||
{
|
||||
Items[startIndex - 1].Next = endIndex < Count - 1 ? Items[endIndex + 1] : null;
|
||||
}
|
||||
|
||||
|
||||
if (endIndex < Count - 1)
|
||||
{
|
||||
Array.Copy(Items, endIndex + 1, Items, startIndex, Count - endIndex - 1);
|
||||
}
|
||||
|
||||
Count -= endIndex - startIndex + 1;
|
||||
|
||||
while (startItem != endItem.Next)
|
||||
{
|
||||
_quickAccess.Remove(startItem.Address);
|
||||
foreach (ulong addr in startItem.QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(addr);
|
||||
_fastQuickAccess.Remove(addr);
|
||||
}
|
||||
startItem = startItem.Next;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a range of items from the item list
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size of the range</param>
|
||||
public void RemoveRange(ulong address, ulong size)
|
||||
{
|
||||
int startIndex = BinarySearchLeftEdge(address, address + size);
|
||||
|
||||
if (startIndex < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
RangeItem<T> startItem = Items[startIndex];
|
||||
|
||||
int endIndex = startIndex;
|
||||
|
||||
while (startItem is not null && startItem.Address < address + size)
|
||||
{
|
||||
_quickAccess.Remove(startItem.Address);
|
||||
foreach (ulong addr in startItem.QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(addr);
|
||||
_fastQuickAccess.Remove(addr);
|
||||
}
|
||||
startItem = startItem.Next;
|
||||
endIndex++;
|
||||
}
|
||||
|
||||
if (endIndex < Count - 1)
|
||||
{
|
||||
Items[endIndex + 1].Previous = startIndex > 0 ? Items[startIndex - 1] : null;
|
||||
}
|
||||
|
||||
if (startIndex > 0)
|
||||
{
|
||||
Items[startIndex - 1].Next = endIndex < Count - 1 ? Items[endIndex + 1] : null;
|
||||
}
|
||||
|
||||
|
||||
if (endIndex < Count - 1)
|
||||
{
|
||||
Array.Copy(Items, endIndex + 1, Items, startIndex, Count - endIndex - 1);
|
||||
}
|
||||
|
||||
Count -= endIndex - startIndex + 1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Clear all ranges.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Clear()
|
||||
{
|
||||
Lock.EnterWriteLock();
|
||||
Count = 0;
|
||||
_quickAccess.Clear();
|
||||
_fastQuickAccess.Clear();
|
||||
Lock.ExitWriteLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Finds a list of regions that cover the desired (address, size) range.
|
||||
/// If this range starts or ends in the middle of an existing region, it is split and only the relevant part is added.
|
||||
@@ -19,17 +298,18 @@ namespace Ryujinx.Memory.Range
|
||||
/// <param name="address">Start address of the search region</param>
|
||||
/// <param name="size">Size of the search region</param>
|
||||
/// <param name="factory">Factory for creating new ranges</param>
|
||||
public void GetOrAddRegions(List<T> list, ulong address, ulong size, Func<ulong, ulong, T> factory)
|
||||
public void GetOrAddRegions(out List<T> list, ulong address, ulong size, Func<ulong, ulong, T> factory)
|
||||
{
|
||||
// (regarding the specific case this generalized function is used for)
|
||||
// A new region may be split into multiple parts if multiple virtual regions have mapped to it.
|
||||
// For instance, while a virtual mapping could cover 0-2 in physical space, the space 0-1 may have already been reserved...
|
||||
// So we need to return both the split 0-1 and 1-2 ranges.
|
||||
|
||||
T[] results = new T[1];
|
||||
int count = FindOverlapsNonOverlapping(address, size, ref results);
|
||||
|
||||
if (count == 0)
|
||||
|
||||
Lock.EnterWriteLock();
|
||||
(RangeItem<T> first, RangeItem<T> last) = FindOverlaps(address, size);
|
||||
list = new List<T>();
|
||||
|
||||
if (first is null)
|
||||
{
|
||||
// The region is fully unmapped. Create and add it to the range list.
|
||||
T region = factory(address, size);
|
||||
@@ -41,13 +321,15 @@ namespace Ryujinx.Memory.Range
|
||||
ulong lastAddress = address;
|
||||
ulong endAddress = address + size;
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
RangeItem<T> current = first;
|
||||
while (last is not null && current is not null && current.Address < endAddress)
|
||||
{
|
||||
T region = results[i];
|
||||
if (count == 1 && region.Address == address && region.Size == size)
|
||||
T region = current.Value;
|
||||
if (first == last && region.Address == address && region.Size == size)
|
||||
{
|
||||
// Exact match, no splitting required.
|
||||
list.Add(region);
|
||||
Lock.ExitWriteLock();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -75,6 +357,7 @@ namespace Ryujinx.Memory.Range
|
||||
|
||||
list.Add(region);
|
||||
lastAddress = region.EndAddress;
|
||||
current = current.Next;
|
||||
}
|
||||
|
||||
if (lastAddress < endAddress)
|
||||
@@ -85,6 +368,8 @@ namespace Ryujinx.Memory.Range
|
||||
Add(fillRegion);
|
||||
}
|
||||
}
|
||||
|
||||
Lock.ExitWriteLock();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -95,6 +380,7 @@ namespace Ryujinx.Memory.Range
|
||||
/// <param name="region">The region to split</param>
|
||||
/// <param name="splitAddress">The address to split with</param>
|
||||
/// <returns>The new region (high part)</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private T Split(T region, ulong splitAddress)
|
||||
{
|
||||
T newRegion = (T)region.Split(splitAddress);
|
||||
@@ -102,5 +388,113 @@ namespace Ryujinx.Memory.Range
|
||||
Add(newRegion);
|
||||
return newRegion;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>The leftmost overlapping item, or null if none is found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override RangeItem<T> FindOverlap(ulong address, ulong size)
|
||||
{
|
||||
if (_quickAccess.TryGetValue(address, out RangeItem<T> overlap))
|
||||
{
|
||||
return overlap;
|
||||
}
|
||||
|
||||
int index = BinarySearchLeftEdge(address, address + size);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Items[index].Address < address)
|
||||
{
|
||||
_quickAccess.TryAdd(address, Items[index]);
|
||||
Items[index].QuickAccessAddresses.Add(address);
|
||||
}
|
||||
|
||||
return Items[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>The overlapping item, or null if none is found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override RangeItem<T> FindOverlapFast(ulong address, ulong size)
|
||||
{
|
||||
if (_quickAccess.TryGetValue(address, out RangeItem<T> overlap) || _fastQuickAccess.TryGetValue(address, out overlap))
|
||||
{
|
||||
return overlap;
|
||||
}
|
||||
|
||||
int index = BinarySearch(address, address + size);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (Items[index].Address < address)
|
||||
{
|
||||
_quickAccess.TryAdd(address, Items[index]);
|
||||
}
|
||||
else
|
||||
{
|
||||
_fastQuickAccess.TryAdd(address, Items[index]);
|
||||
}
|
||||
|
||||
Items[index].QuickAccessAddresses.Add(address);
|
||||
|
||||
return Items[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>The first and last overlapping items, or null if none are found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public (RangeItem<T>, RangeItem<T>) FindOverlaps(ulong address, ulong size)
|
||||
{
|
||||
if (_quickAccess.TryGetValue(address, out RangeItem<T> overlap))
|
||||
{
|
||||
if (overlap.Next is null || overlap.Next.Address >= address + size)
|
||||
{
|
||||
return (overlap, overlap);
|
||||
}
|
||||
|
||||
return (overlap, Items[BinarySearchRightEdge(address, address + size)]);
|
||||
}
|
||||
|
||||
(int index, int endIndex) = BinarySearchEdges(address, address + size);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return (null, null);
|
||||
}
|
||||
|
||||
if (Items[index].Address < address)
|
||||
{
|
||||
_quickAccess.TryAdd(address, Items[index]);
|
||||
Items[index].QuickAccessAddresses.Add(address);
|
||||
}
|
||||
|
||||
return (Items[index], Items[endIndex - 1]);
|
||||
}
|
||||
|
||||
public override IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
yield return Items[i].Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,61 +1,91 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Threading;
|
||||
|
||||
namespace Ryujinx.Memory.Range
|
||||
{
|
||||
public class RangeItem<TValue>(TValue value) where TValue : IRange
|
||||
{
|
||||
public RangeItem<TValue> Next;
|
||||
public RangeItem<TValue> Previous;
|
||||
|
||||
public readonly ulong Address = value.Address;
|
||||
public readonly ulong EndAddress = value.Address + value.Size;
|
||||
|
||||
public readonly TValue Value = value;
|
||||
|
||||
public readonly List<ulong> QuickAccessAddresses = [];
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool OverlapsWith(ulong address, ulong endAddress)
|
||||
{
|
||||
return Address < endAddress && address < EndAddress;
|
||||
}
|
||||
}
|
||||
|
||||
class AddressEqualityComparer : IEqualityComparer<ulong>
|
||||
{
|
||||
public bool Equals(ulong u1, ulong u2)
|
||||
{
|
||||
return u1 == u2;
|
||||
}
|
||||
|
||||
public int GetHashCode(ulong value) => (int)(value >> 5);
|
||||
|
||||
public static readonly AddressEqualityComparer Comparer = new();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Result of an Overlaps Finder function. WARNING: if the result is from the optimized
|
||||
/// Overlaps Finder, the StartIndex will be -1 even when the result isn't empty
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// startIndex is inclusive.
|
||||
/// endIndex is exclusive.
|
||||
/// </remarks>
|
||||
public readonly struct OverlapResult<T> where T : IRange
|
||||
{
|
||||
public readonly int StartIndex = -1;
|
||||
public readonly int EndIndex = -1;
|
||||
public readonly RangeItem<T> QuickResult;
|
||||
public int Count => EndIndex - StartIndex;
|
||||
|
||||
public OverlapResult(int startIndex, int endIndex, RangeItem<T> quickResult = null)
|
||||
{
|
||||
this.StartIndex = startIndex;
|
||||
this.EndIndex = endIndex;
|
||||
this.QuickResult = quickResult;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sorted list of ranges that supports binary search.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Type of the range.</typeparam>
|
||||
public class RangeList<T> : IEnumerable<T> where T : IRange
|
||||
public class RangeList<T> : RangeListBase<T> where T : IRange
|
||||
{
|
||||
private readonly struct RangeItem<TValue> where TValue : IRange
|
||||
{
|
||||
public readonly ulong Address;
|
||||
public readonly ulong EndAddress;
|
||||
|
||||
public readonly TValue Value;
|
||||
|
||||
public RangeItem(TValue value)
|
||||
{
|
||||
Value = value;
|
||||
|
||||
Address = value.Address;
|
||||
EndAddress = value.Address + value.Size;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool OverlapsWith(ulong address, ulong endAddress)
|
||||
{
|
||||
return Address < endAddress && address < EndAddress;
|
||||
}
|
||||
}
|
||||
|
||||
private const int BackingInitialSize = 1024;
|
||||
private const int ArrayGrowthSize = 32;
|
||||
|
||||
private RangeItem<T>[] _items;
|
||||
private readonly int _backingGrowthSize;
|
||||
|
||||
public int Count { get; protected set; }
|
||||
public readonly ReaderWriterLockSlim Lock = new();
|
||||
|
||||
private readonly Dictionary<ulong, RangeItem<T>> _quickAccess = new(AddressEqualityComparer.Comparer);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new range list.
|
||||
/// </summary>
|
||||
public RangeList() { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new range list.
|
||||
/// </summary>
|
||||
/// <param name="backingInitialSize">The initial size of the backing array</param>
|
||||
public RangeList(int backingInitialSize = BackingInitialSize)
|
||||
{
|
||||
_backingGrowthSize = backingInitialSize;
|
||||
_items = new RangeItem<T>[backingInitialSize];
|
||||
}
|
||||
public RangeList(int backingInitialSize) : base(backingInitialSize) { }
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new item to the list.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be added</param>
|
||||
public void Add(T item)
|
||||
public override void Add(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
@@ -72,27 +102,27 @@ namespace Ryujinx.Memory.Range
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be updated</param>
|
||||
/// <returns>True if the item was located and updated, false otherwise</returns>
|
||||
public bool Update(T item)
|
||||
protected override bool Update(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
while (index > 0 && _items[index - 1].Address == item.Address)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
|
||||
while (index < Count)
|
||||
{
|
||||
if (_items[index].Value.Equals(item))
|
||||
if (Items[index].Value.Equals(item))
|
||||
{
|
||||
_items[index] = new RangeItem<T>(item);
|
||||
foreach (ulong address in Items[index].QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(address);
|
||||
}
|
||||
|
||||
Items[index] = new RangeItem<T>(item);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_items[index].Address > item.Address)
|
||||
if (Items[index].Address > item.Address)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -107,23 +137,42 @@ namespace Ryujinx.Memory.Range
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void Insert(int index, RangeItem<T> item)
|
||||
{
|
||||
if (Count + 1 > _items.Length)
|
||||
Debug.Assert(item.Address != item.EndAddress);
|
||||
|
||||
Debug.Assert(item.Address % 32 == 0);
|
||||
|
||||
if (Count + 1 > Items.Length)
|
||||
{
|
||||
Array.Resize(ref _items, _items.Length + _backingGrowthSize);
|
||||
Array.Resize(ref Items, Items.Length + BackingGrowthSize);
|
||||
}
|
||||
|
||||
if (index >= Count)
|
||||
{
|
||||
if (index == Count)
|
||||
{
|
||||
_items[Count++] = item;
|
||||
if (index != 0)
|
||||
{
|
||||
item.Previous = Items[index - 1];
|
||||
Items[index - 1].Next = item;
|
||||
}
|
||||
Items[index] = item;
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Array.Copy(_items, index, _items, index + 1, Count - index);
|
||||
Array.Copy(Items, index, Items, index + 1, Count - index);
|
||||
|
||||
_items[index] = item;
|
||||
Items[index] = item;
|
||||
if (index != 0)
|
||||
{
|
||||
item.Previous = Items[index - 1];
|
||||
Items[index - 1].Next = item;
|
||||
}
|
||||
|
||||
item.Next = Items[index + 1];
|
||||
Items[index + 1].Previous = item;
|
||||
|
||||
Count++;
|
||||
}
|
||||
}
|
||||
@@ -131,9 +180,71 @@ namespace Ryujinx.Memory.Range
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void RemoveAt(int index)
|
||||
{
|
||||
foreach (ulong address in Items[index].QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(address);
|
||||
}
|
||||
|
||||
if (index < Count - 1)
|
||||
{
|
||||
Items[index + 1].Previous = index > 0 ? Items[index - 1] : null;
|
||||
}
|
||||
|
||||
if (index > 0)
|
||||
{
|
||||
Items[index - 1].Next = index < Count - 1 ? Items[index + 1] : null;
|
||||
}
|
||||
|
||||
if (index < --Count)
|
||||
{
|
||||
Array.Copy(_items, index + 1, _items, index, Count - index);
|
||||
Array.Copy(Items, index + 1, Items, index, Count - index);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes a range of items from the item list
|
||||
/// </summary>
|
||||
/// <param name="startItem">The first item in the range of items to be removed</param>
|
||||
/// <param name="endItem">The last item in the range of items to be removed</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void RemoveRange(RangeItem<T> startItem, RangeItem<T> endItem)
|
||||
{
|
||||
if (endItem.Next is not null)
|
||||
{
|
||||
endItem.Next.Previous = startItem.Previous;
|
||||
}
|
||||
|
||||
if (startItem.Previous is not null)
|
||||
{
|
||||
startItem.Previous.Next = endItem.Next;
|
||||
}
|
||||
|
||||
RangeItem<T> current = startItem;
|
||||
while (current != endItem.Next)
|
||||
{
|
||||
foreach (ulong address in current.QuickAccessAddresses)
|
||||
{
|
||||
_quickAccess.Remove(address);
|
||||
}
|
||||
|
||||
current = current.Next;
|
||||
}
|
||||
|
||||
RangeItem<T>[] array = [];
|
||||
OverlapResult<T> overlapResult = FindOverlaps(startItem.Address, endItem.EndAddress, ref array);
|
||||
|
||||
if (overlapResult.EndIndex < Count)
|
||||
{
|
||||
Array.Copy(Items, overlapResult.EndIndex, Items, overlapResult.StartIndex, Count - overlapResult.EndIndex);
|
||||
Count -= overlapResult.Count;
|
||||
}
|
||||
else if (overlapResult.EndIndex == Count)
|
||||
{
|
||||
Count = overlapResult.StartIndex;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,27 +253,22 @@ namespace Ryujinx.Memory.Range
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be removed</param>
|
||||
/// <returns>True if the item was removed, or false if it was not found</returns>
|
||||
public bool Remove(T item)
|
||||
public override bool Remove(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
while (index > 0 && _items[index - 1].Address == item.Address)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
|
||||
while (index < Count)
|
||||
{
|
||||
if (_items[index].Value.Equals(item))
|
||||
if (Items[index].Value.Equals(item))
|
||||
{
|
||||
RemoveAt(index);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (_items[index].Address > item.Address)
|
||||
if (Items[index].Address > item.Address)
|
||||
{
|
||||
break;
|
||||
}
|
||||
@@ -173,310 +279,130 @@ namespace Ryujinx.Memory.Range
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Updates an item's end address.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be updated</param>
|
||||
public void UpdateEndAddress(T item)
|
||||
{
|
||||
int index = BinarySearch(item.Address);
|
||||
|
||||
if (index >= 0)
|
||||
{
|
||||
while (index > 0 && _items[index - 1].Address == item.Address)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
|
||||
while (index < Count)
|
||||
{
|
||||
if (_items[index].Value.Equals(item))
|
||||
{
|
||||
_items[index] = new RangeItem<T>(item);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (_items[index].Address > item.Address)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first item on the list overlapping in memory with the specified item.
|
||||
/// Gets an item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Despite the name, this has no ordering guarantees of the returned item.
|
||||
/// It only ensures that the item returned overlaps the specified item.
|
||||
/// </remarks>
|
||||
/// <param name="item">Item to check for overlaps</param>
|
||||
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
||||
public T FindFirstOverlap(T item)
|
||||
{
|
||||
return FindFirstOverlap(item.Address, item.Size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the first item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Despite the name, this has no ordering guarantees of the returned item.
|
||||
/// This has no ordering guarantees of the returned item.
|
||||
/// It only ensures that the item returned overlaps the specified memory range.
|
||||
/// </remarks>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
||||
public T FindFirstOverlap(ulong address, ulong size)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override RangeItem<T> FindOverlap(ulong address, ulong size)
|
||||
{
|
||||
int index = BinarySearchLeftEdge(address, address + size);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Items[index];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an item on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This has no ordering guarantees of the returned item.
|
||||
/// It only ensures that the item returned overlaps the specified memory range.
|
||||
/// </remarks>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <returns>The overlapping item, or the default value for the type if none found</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override RangeItem<T> FindOverlapFast(ulong address, ulong size)
|
||||
{
|
||||
if (_quickAccess.TryGetValue(address, out RangeItem<T> quickResult))
|
||||
{
|
||||
return quickResult;
|
||||
}
|
||||
|
||||
int index = BinarySearch(address, address + size);
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
return default;
|
||||
return null;
|
||||
}
|
||||
|
||||
return _items[index].Value;
|
||||
}
|
||||
if (Items[index].OverlapsWith(address, address + 1))
|
||||
{
|
||||
_quickAccess.Add(address, Items[index]);
|
||||
Items[index].QuickAccessAddresses.Add(address);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items overlapping with the specified item in memory.
|
||||
/// </summary>
|
||||
/// <param name="item">Item to check for overlaps</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
public int FindOverlaps(T item, ref T[] output)
|
||||
{
|
||||
return FindOverlaps(item.Address, item.Size, ref output);
|
||||
return Items[index];
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
public int FindOverlaps(ulong address, ulong size, ref T[] output)
|
||||
/// <returns>Range information of overlapping items found</returns>
|
||||
private OverlapResult<T> FindOverlaps(ulong address, ulong size, ref RangeItem<T>[] output)
|
||||
{
|
||||
int outputIndex = 0;
|
||||
int outputCount = 0;
|
||||
|
||||
ulong endAddress = address + size;
|
||||
|
||||
int startIndex = BinarySearch(address, endAddress);
|
||||
if (startIndex < 0)
|
||||
startIndex = ~startIndex;
|
||||
int endIndex = -1;
|
||||
|
||||
for (int i = 0; i < Count; i++)
|
||||
for (int i = startIndex; i < Count; i++)
|
||||
{
|
||||
ref RangeItem<T> item = ref _items[i];
|
||||
ref RangeItem<T> item = ref Items[i];
|
||||
|
||||
if (item.Address >= endAddress)
|
||||
{
|
||||
endIndex = i;
|
||||
break;
|
||||
}
|
||||
|
||||
if (item.OverlapsWith(address, endAddress))
|
||||
{
|
||||
if (outputIndex == output.Length)
|
||||
{
|
||||
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
|
||||
}
|
||||
|
||||
output[outputIndex++] = item.Value;
|
||||
outputCount++;
|
||||
}
|
||||
}
|
||||
|
||||
return outputIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items overlapping with the specified item in memory.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method only returns correct results if none of the items on the list overlaps with
|
||||
/// each other. If that is not the case, this method should not be used.
|
||||
/// This method is faster than the regular method to find all overlaps.
|
||||
/// </remarks>
|
||||
/// <param name="item">Item to check for overlaps</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
public int FindOverlapsNonOverlapping(T item, ref T[] output)
|
||||
{
|
||||
return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items on the list overlapping the specified memory range.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// This method only returns correct results if none of the items on the list overlaps with
|
||||
/// each other. If that is not the case, this method should not be used.
|
||||
/// This method is faster than the regular method to find all overlaps.
|
||||
/// </remarks>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="size">Size in bytes of the range</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of overlapping items found</returns>
|
||||
public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
|
||||
{
|
||||
// This is a bit faster than FindOverlaps, but only works
|
||||
// when none of the items on the list overlaps with each other.
|
||||
int outputIndex = 0;
|
||||
|
||||
ulong endAddress = address + size;
|
||||
|
||||
int index = BinarySearch(address, endAddress);
|
||||
|
||||
if (index >= 0)
|
||||
if (endIndex == -1 && outputCount > 0)
|
||||
{
|
||||
while (index > 0 && _items[index - 1].OverlapsWith(address, endAddress))
|
||||
{
|
||||
index--;
|
||||
}
|
||||
|
||||
do
|
||||
{
|
||||
if (outputIndex == output.Length)
|
||||
{
|
||||
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
|
||||
}
|
||||
|
||||
output[outputIndex++] = _items[index++].Value;
|
||||
}
|
||||
while (index < Count && _items[index].OverlapsWith(address, endAddress));
|
||||
endIndex = Count;
|
||||
}
|
||||
|
||||
return outputIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all items on the list with the specified memory address.
|
||||
/// </summary>
|
||||
/// <param name="address">Address to find</param>
|
||||
/// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
|
||||
/// <returns>The number of matches found</returns>
|
||||
public int FindOverlaps(ulong address, ref T[] output)
|
||||
{
|
||||
int index = BinarySearch(address);
|
||||
|
||||
int outputIndex = 0;
|
||||
|
||||
if (index >= 0)
|
||||
if (outputCount > 0 && outputCount == endIndex - startIndex)
|
||||
{
|
||||
while (index > 0 && _items[index - 1].Address == address)
|
||||
{
|
||||
index--;
|
||||
}
|
||||
|
||||
while (index < Count)
|
||||
{
|
||||
ref RangeItem<T> overlap = ref _items[index++];
|
||||
|
||||
if (overlap.Address != address)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
if (outputIndex == output.Length)
|
||||
{
|
||||
Array.Resize(ref output, outputIndex + ArrayGrowthSize);
|
||||
}
|
||||
|
||||
output[outputIndex++] = overlap.Value;
|
||||
}
|
||||
Array.Resize(ref output, outputCount);
|
||||
Array.Copy(Items, endIndex - outputCount, output, 0, outputCount);
|
||||
|
||||
return new OverlapResult<T>(startIndex, endIndex);
|
||||
}
|
||||
|
||||
return outputIndex;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search on the internal list of items.
|
||||
/// </summary>
|
||||
/// <param name="address">Address to find</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
private int BinarySearch(ulong address)
|
||||
{
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
else if (outputCount > 0)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref _items[middle];
|
||||
|
||||
if (item.Address == address)
|
||||
Array.Resize(ref output, outputCount);
|
||||
int arrIndex = 0;
|
||||
for (int i = startIndex; i < endIndex; i++)
|
||||
{
|
||||
return middle;
|
||||
}
|
||||
|
||||
if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
output[arrIndex++] = Items[i];
|
||||
}
|
||||
|
||||
return new OverlapResult<T>(endIndex - outputCount, endIndex);
|
||||
}
|
||||
|
||||
return ~left;
|
||||
|
||||
return new OverlapResult<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search for items overlapping a given memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
private int BinarySearch(ulong address, ulong endAddress)
|
||||
{
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref _items[middle];
|
||||
|
||||
if (item.OverlapsWith(address, endAddress))
|
||||
{
|
||||
return middle;
|
||||
}
|
||||
|
||||
if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~left;
|
||||
}
|
||||
|
||||
public IEnumerator<T> GetEnumerator()
|
||||
public override IEnumerator<T> GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
yield return _items[i].Value;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
for (int i = 0; i < Count; i++)
|
||||
{
|
||||
yield return _items[i].Value;
|
||||
yield return Items[i].Value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
359
src/Ryujinx.Memory/Range/RangeListBase.cs
Normal file
359
src/Ryujinx.Memory/Range/RangeListBase.cs
Normal file
@@ -0,0 +1,359 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Ryujinx.Memory.Range
|
||||
{
|
||||
public abstract class RangeListBase<T> : IEnumerable<T> where T : IRange
|
||||
{
|
||||
protected const int BackingInitialSize = 1024;
|
||||
|
||||
protected RangeItem<T>[] Items;
|
||||
protected readonly int BackingGrowthSize;
|
||||
|
||||
public int Count { get; protected set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new range list.
|
||||
/// </summary>
|
||||
/// <param name="backingInitialSize">The initial size of the backing array</param>
|
||||
protected RangeListBase(int backingInitialSize = BackingInitialSize)
|
||||
{
|
||||
BackingGrowthSize = backingInitialSize;
|
||||
Items = new RangeItem<T>[backingInitialSize];
|
||||
}
|
||||
|
||||
public abstract void Add(T item);
|
||||
|
||||
/// <summary>
|
||||
/// Updates an item's end address on the list. Address must be the same.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to be updated</param>
|
||||
/// <returns>True if the item was located and updated, false otherwise</returns>
|
||||
protected abstract bool Update(T item);
|
||||
|
||||
public abstract bool Remove(T item);
|
||||
|
||||
public abstract void RemoveRange(RangeItem<T> startItem, RangeItem<T> endItem);
|
||||
|
||||
public abstract RangeItem<T> FindOverlap(ulong address, ulong size);
|
||||
|
||||
public abstract RangeItem<T> FindOverlapFast(ulong address, ulong size);
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search on the internal list of items.
|
||||
/// </summary>
|
||||
/// <param name="address">Address to find</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected int BinarySearch(ulong address)
|
||||
{
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref Items[middle];
|
||||
|
||||
if (item.Address == address)
|
||||
{
|
||||
return middle;
|
||||
}
|
||||
|
||||
if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search for items overlapping a given memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected int BinarySearch(ulong address, ulong endAddress)
|
||||
{
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref Items[middle];
|
||||
|
||||
if (item.OverlapsWith(address, endAddress))
|
||||
{
|
||||
return middle;
|
||||
}
|
||||
|
||||
if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search for items overlapping a given memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected int BinarySearchLeftEdge(ulong address, ulong endAddress)
|
||||
{
|
||||
if (Count == 0)
|
||||
return ~0;
|
||||
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref Items[middle];
|
||||
|
||||
bool match = item.OverlapsWith(address, endAddress);
|
||||
|
||||
if (range == 0)
|
||||
{
|
||||
if (match)
|
||||
return middle;
|
||||
else if (address < item.Address)
|
||||
return ~(right);
|
||||
else
|
||||
return ~(right + 1);
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
right = middle;
|
||||
}
|
||||
else if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search for items overlapping a given memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected int BinarySearchRightEdge(ulong address, ulong endAddress)
|
||||
{
|
||||
if (Count == 0)
|
||||
return ~0;
|
||||
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = right - (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref Items[middle];
|
||||
|
||||
bool match = item.OverlapsWith(address, endAddress);
|
||||
|
||||
if (range == 0)
|
||||
{
|
||||
if (match)
|
||||
return middle;
|
||||
else if (endAddress > item.EndAddress)
|
||||
return ~(left + 1);
|
||||
else
|
||||
return ~(left);
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
left = middle;
|
||||
}
|
||||
else if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return ~left;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Performs binary search for items overlapping a given memory range.
|
||||
/// </summary>
|
||||
/// <param name="address">Start address of the range</param>
|
||||
/// <param name="endAddress">End address of the range</param>
|
||||
/// <returns>Range information (inclusive, exclusive) of items that overlaps, or complement index of nearest item with lower value on the list</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
protected (int, int) BinarySearchEdges(ulong address, ulong endAddress)
|
||||
{
|
||||
if (Count == 0)
|
||||
return (~0, ~0);
|
||||
|
||||
if (Count == 1)
|
||||
{
|
||||
ref RangeItem<T> item = ref Items[0];
|
||||
|
||||
if (item.OverlapsWith(address, endAddress))
|
||||
{
|
||||
return (0, 1);
|
||||
}
|
||||
|
||||
if (address < item.Address)
|
||||
{
|
||||
return (~0, ~0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (~1, ~1);
|
||||
}
|
||||
}
|
||||
|
||||
int left = 0;
|
||||
int right = Count - 1;
|
||||
|
||||
int leftEdge = -1;
|
||||
int rightEdgeMatch = -1;
|
||||
int rightEdgeNoMatch = -1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = left + (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref Items[middle];
|
||||
|
||||
bool match = item.OverlapsWith(address, endAddress);
|
||||
|
||||
if (range == 0)
|
||||
{
|
||||
if (match)
|
||||
{
|
||||
leftEdge = middle;
|
||||
break;
|
||||
}
|
||||
else if (address < item.Address)
|
||||
{
|
||||
return (~right, ~right);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (~(right + 1), ~(right + 1));
|
||||
}
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
right = middle;
|
||||
if (rightEdgeMatch == -1)
|
||||
rightEdgeMatch = middle;
|
||||
}
|
||||
else if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
rightEdgeNoMatch = middle;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (left > right)
|
||||
{
|
||||
return (~left, ~left);
|
||||
}
|
||||
|
||||
if (rightEdgeMatch == -1)
|
||||
{
|
||||
return (leftEdge, leftEdge + 1);
|
||||
}
|
||||
|
||||
left = rightEdgeMatch;
|
||||
right = rightEdgeNoMatch > 0 ? rightEdgeNoMatch : Count - 1;
|
||||
|
||||
while (left <= right)
|
||||
{
|
||||
int range = right - left;
|
||||
|
||||
int middle = right - (range >> 1);
|
||||
|
||||
ref RangeItem<T> item = ref Items[middle];
|
||||
|
||||
bool match = item.OverlapsWith(address, endAddress);
|
||||
|
||||
if (range == 0)
|
||||
{
|
||||
if (match)
|
||||
return (leftEdge, middle + 1);
|
||||
else
|
||||
return (leftEdge, middle);
|
||||
}
|
||||
|
||||
if (match)
|
||||
{
|
||||
left = middle;
|
||||
}
|
||||
else if (address < item.Address)
|
||||
{
|
||||
right = middle - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
left = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return (leftEdge, right + 1);
|
||||
}
|
||||
|
||||
public abstract IEnumerator<T> GetEnumerator();
|
||||
|
||||
IEnumerator IEnumerable.GetEnumerator()
|
||||
{
|
||||
return GetEnumerator();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,3 @@
|
||||
using Ryujinx.Common.Pools;
|
||||
using Ryujinx.Memory.Range;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -76,17 +75,16 @@ namespace Ryujinx.Memory.Tracking
|
||||
|
||||
lock (TrackingLock)
|
||||
{
|
||||
ref VirtualRegion[] overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
||||
|
||||
for (int type = 0; type < 2; type++)
|
||||
{
|
||||
NonOverlappingRangeList<VirtualRegion> regions = type == 0 ? _virtualRegions : _guestVirtualRegions;
|
||||
|
||||
int count = regions.FindOverlapsNonOverlapping(va, size, ref overlaps);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
regions.Lock.EnterReadLock();
|
||||
(RangeItem<VirtualRegion> first, RangeItem<VirtualRegion> last) = regions.FindOverlaps(va, size);
|
||||
|
||||
RangeItem<VirtualRegion> current = first;
|
||||
while (last != null && current != last.Next)
|
||||
{
|
||||
VirtualRegion region = overlaps[i];
|
||||
VirtualRegion region = current.Value;
|
||||
|
||||
// If the region has been fully remapped, signal that it has been mapped again.
|
||||
bool remapped = _memoryManager.IsRangeMapped(region.Address, region.Size);
|
||||
@@ -96,7 +94,9 @@ namespace Ryujinx.Memory.Tracking
|
||||
}
|
||||
|
||||
region.UpdateProtection();
|
||||
current = current.Next;
|
||||
}
|
||||
regions.Lock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -114,20 +114,21 @@ namespace Ryujinx.Memory.Tracking
|
||||
|
||||
lock (TrackingLock)
|
||||
{
|
||||
ref VirtualRegion[] overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
||||
|
||||
for (int type = 0; type < 2; type++)
|
||||
{
|
||||
NonOverlappingRangeList<VirtualRegion> regions = type == 0 ? _virtualRegions : _guestVirtualRegions;
|
||||
|
||||
int count = regions.FindOverlapsNonOverlapping(va, size, ref overlaps);
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
regions.Lock.EnterReadLock();
|
||||
(RangeItem<VirtualRegion> first, RangeItem<VirtualRegion> last) = regions.FindOverlaps(va, size);
|
||||
|
||||
RangeItem<VirtualRegion> current = first;
|
||||
while (last != null && current != last.Next)
|
||||
{
|
||||
VirtualRegion region = overlaps[i];
|
||||
VirtualRegion region = current.Value;
|
||||
|
||||
region.SignalMappingChanged(false);
|
||||
current = current.Next;
|
||||
}
|
||||
regions.Lock.ExitReadLock();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -165,10 +166,11 @@ namespace Ryujinx.Memory.Tracking
|
||||
/// <returns>A list of virtual regions within the given range</returns>
|
||||
internal List<VirtualRegion> GetVirtualRegionsForHandle(ulong va, ulong size, bool guest)
|
||||
{
|
||||
List<VirtualRegion> result = [];
|
||||
NonOverlappingRangeList<VirtualRegion> regions = guest ? _guestVirtualRegions : _virtualRegions;
|
||||
regions.GetOrAddRegions(result, va, size, (va, size) => new VirtualRegion(this, va, size, guest));
|
||||
|
||||
regions.Lock.EnterUpgradeableReadLock();
|
||||
regions.GetOrAddRegions(out List<VirtualRegion> result, va, size, (va, size) => new VirtualRegion(this, va, size, guest));
|
||||
regions.Lock.ExitUpgradeableReadLock();
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -296,25 +298,33 @@ namespace Ryujinx.Memory.Tracking
|
||||
|
||||
lock (TrackingLock)
|
||||
{
|
||||
ref VirtualRegion[] overlaps = ref ThreadStaticArray<VirtualRegion>.Get();
|
||||
|
||||
NonOverlappingRangeList<VirtualRegion> regions = guest ? _guestVirtualRegions : _virtualRegions;
|
||||
List<RangeItem<VirtualRegion>> overlaps = [];
|
||||
|
||||
// We use the non-span method here because keeping the lock will cause a deadlock.
|
||||
regions.Lock.EnterReadLock();
|
||||
(RangeItem<VirtualRegion> first, RangeItem<VirtualRegion> last) = regions.FindOverlaps(address, size);
|
||||
|
||||
RangeItem<VirtualRegion> current = first;
|
||||
while (last != null && current != last.Next)
|
||||
{
|
||||
overlaps.Add(current);
|
||||
current = current.Next;
|
||||
}
|
||||
regions.Lock.ExitReadLock();
|
||||
|
||||
int count = regions.FindOverlapsNonOverlapping(address, size, ref overlaps);
|
||||
|
||||
if (count == 0 && !precise)
|
||||
if (first is null && !precise)
|
||||
{
|
||||
if (_memoryManager.IsRangeMapped(address, size))
|
||||
{
|
||||
// TODO: There is currently the possibility that a page can be protected after its virtual region is removed.
|
||||
// This code handles that case when it happens, but it would be better to find out how this happens.
|
||||
_memoryManager.TrackingReprotect(address & ~(ulong)(_pageSize - 1), (ulong)_pageSize, MemoryPermission.ReadAndWrite, guest);
|
||||
|
||||
return true; // This memory _should_ be mapped, so we need to try again.
|
||||
}
|
||||
else
|
||||
{
|
||||
shouldThrow = true;
|
||||
}
|
||||
|
||||
shouldThrow = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -324,9 +334,9 @@ namespace Ryujinx.Memory.Tracking
|
||||
size += (ulong)_pageSize;
|
||||
}
|
||||
|
||||
for (int i = 0; i < count; i++)
|
||||
for (int i = 0; i < overlaps.Count; i++)
|
||||
{
|
||||
VirtualRegion region = overlaps[i];
|
||||
VirtualRegion region = overlaps[i].Value;
|
||||
|
||||
if (precise)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user