initial commit

This commit is contained in:
LotP1 2025-06-01 07:30:35 +02:00
parent d688fed7d2
commit be8f4897a2
10 changed files with 935 additions and 407 deletions

View file

@ -1,45 +1,68 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Threading;
namespace Ryujinx.Memory.Range
{
public 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;
}
}
/// <summary>
/// Result of an Overlaps Finder function.
/// </summary>
/// <remarks>
/// startIndex is inclusive.
/// endIndex is exclusive.
/// </remarks>
public readonly struct OverlapResult
{
public readonly int StartIndex = -1;
public readonly int EndIndex = -1;
public int Count => EndIndex - StartIndex;
public OverlapResult(int startIndex, int endIndex)
{
this.StartIndex = startIndex;
this.EndIndex = endIndex;
}
}
/// <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
{
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 ReaderWriterLockSlim();
/// <summary>
/// Creates a new range list.
@ -74,15 +97,10 @@ namespace Ryujinx.Memory.Range
/// <returns>True if the item was located and updated, false otherwise</returns>
public bool Update(T item)
{
int index = BinarySearch(item.Address);
int index = BinarySearchLeftEdge(item.Address);
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == item.Address)
{
index--;
}
while (index < Count)
{
if (_items[index].Value.Equals(item))
@ -129,7 +147,7 @@ namespace Ryujinx.Memory.Range
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RemoveAt(int index)
public void RemoveAt(int index)
{
if (index < --Count)
{
@ -137,6 +155,32 @@ namespace Ryujinx.Memory.Range
}
}
public void RemoveRange(int start, int end)
{
if (end <= Count)
{
Array.Copy(_items, end, _items, start, Count - end);
Count -= end - start;
}
else if (end == Count)
{
Count = start;
}
}
public void RemoveRange(OverlapResult overlapResult)
{
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;
}
}
/// <summary>
/// Removes an item from the list.
/// </summary>
@ -144,15 +188,10 @@ namespace Ryujinx.Memory.Range
/// <returns>True if the item was removed, or false if it was not found</returns>
public bool Remove(T item)
{
int index = BinarySearch(item.Address);
int index = BinarySearchLeftEdge(item.Address);
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == item.Address)
{
index--;
}
while (index < Count)
{
if (_items[index].Value.Equals(item))
@ -180,15 +219,10 @@ namespace Ryujinx.Memory.Range
/// <param name="item">The item to be updated</param>
public void UpdateEndAddress(T item)
{
int index = BinarySearch(item.Address);
int index = BinarySearchLeftEdge(item.Address);
if (index >= 0)
{
while (index > 0 && _items[index - 1].Address == item.Address)
{
index--;
}
while (index < Count)
{
if (_items[index].Value.Equals(item))
@ -250,7 +284,7 @@ namespace Ryujinx.Memory.Range
/// <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)
public OverlapResult FindOverlaps(T item, ref RangeItem<T>[] output)
{
return FindOverlaps(item.Address, item.Size, ref output);
}
@ -262,33 +296,42 @@ namespace Ryujinx.Memory.Range
/// <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)
public OverlapResult FindOverlaps(ulong address, ulong size, ref RangeItem<T>[] output)
{
int outputIndex = 0;
ulong endAddress = address + size;
int endIndex = -1;
for (int i = 0; i < Count; 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;
outputIndex++;
}
}
return outputIndex;
if (endIndex == -1 && outputIndex > 0)
{
endIndex = Count;
}
if (endIndex - outputIndex >= 0)
{
Array.Resize(ref output, outputIndex);
Array.Copy(_items, endIndex - outputIndex, output, 0, outputIndex);
}
return new OverlapResult(endIndex - outputIndex, endIndex);
}
/// <summary>
@ -302,7 +345,7 @@ namespace Ryujinx.Memory.Range
/// <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)
public OverlapResult FindOverlapsNonOverlapping(T item, ref RangeItem<T>[] output)
{
return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
}
@ -319,36 +362,64 @@ namespace Ryujinx.Memory.Range
/// <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)
public OverlapResult FindOverlapsNonOverlapping(ulong address, ulong size, ref RangeItem<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);
(int index, int endIndex) = BinarySearchEdges(address, endAddress);
if (index >= 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));
Array.Resize(ref output, endIndex - index);
Array.Copy(_items, index, output, 0, endIndex - index);
}
return outputIndex;
return new OverlapResult(index, endIndex);
}
public OverlapResult FindOverlapsNonOverlappingAsSpan(ulong address, ulong size, out ReadOnlySpan<RangeItem<T>> span)
{
// This is a bit faster than FindOverlaps, but only works
// when none of the items on the list overlaps with each other.
ulong endAddress = address + size;
(int index, int endIndex) = BinarySearchEdges(address, endAddress);
if (index >= 0)
{
span = new ReadOnlySpan<RangeItem<T>>(_items, index, endIndex - index);
return new OverlapResult(index, endIndex);
}
span = ReadOnlySpan<RangeItem<T>>.Empty;
return new OverlapResult(index, endIndex);
}
/// <summary>
/// Gets range of 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>
/// <returns>Range information of overlapping items found</returns>
public OverlapResult FindOverlapsNonOverlapping(ulong address, ulong size)
{
// This is a bit faster than FindOverlaps, but only works
// when none of the items on the list overlaps with each other.
ulong endAddress = address + size;
(int index, int endIndex) = BinarySearchEdges(address, endAddress);
return new OverlapResult(index, endIndex);
}
/// <summary>
@ -357,38 +428,17 @@ namespace Ryujinx.Memory.Range
/// <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)
public OverlapResult FindOverlaps(ulong address, ref RangeItem<T>[] output)
{
int index = BinarySearch(address);
int outputIndex = 0;
(int index, int endIndex) = BinarySearchEdges(address);
if (index >= 0)
{
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, endIndex - index);
Array.Copy(_items, index, output, 0, endIndex - index);
}
return outputIndex;
return new OverlapResult(index, endIndex);
}
/// <summary>
@ -427,6 +477,202 @@ namespace Ryujinx.Memory.Range
return ~left;
}
/// <summary>
/// Performs binary search on the internal list of items.
/// </summary>
/// <param name="address">Address to find</param>
/// <returns>List index of the left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
private int BinarySearchLeftEdge(ulong address)
{
if (Count == 0)
return ~0;
if (Count == 1)
{
ref RangeItem<T> item = ref _items[0];
if (address == item.Address)
{
return 0;
}
if (address < item.Address)
{
return ~0;
}
else
{
return ~1;
}
}
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.Address == address;
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 on the internal list of items.
/// </summary>
/// <param name="address">Start address of the range</param>
/// <returns>List index of the left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
private (int, int) BinarySearchEdges(ulong address)
{
if (Count == 0)
return (~0, ~0);
if (Count == 1)
{
ref RangeItem<T> item = ref _items[0];
if (item.Address == address)
{
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.Address == address;
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.Address == address;
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);
}
/// <summary>
/// Performs binary search for items overlapping a given memory range.
/// </summary>
@ -464,6 +710,205 @@ namespace Ryujinx.Memory.Range
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 left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
private int BinarySearchLeftEdge(ulong address, ulong endAddress)
{
if (Count == 0)
return ~0;
if (Count == 1)
{
ref RangeItem<T> item = ref _items[0];
if (item.OverlapsWith(address, endAddress))
{
return 0;
}
if (address < item.Address)
{
return ~0;
}
else
{
return ~1;
}
}
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 left-most item that overlaps, or complement index of nearest item with lower value on the list</returns>
private (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 IEnumerator<T> GetEnumerator()
{
for (int i = 0; i < Count; i++)