2021-09-19 14:55:07 +02:00
|
|
|
|
using Ryujinx.Common.Collections;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Ryujinx.Memory.Range
|
|
|
|
|
{
|
|
|
|
|
public class MultiRangeList<T> : IEnumerable<T> where T : IMultiRangeItem
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
private readonly IntervalTree<ulong, T> _items;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
public int Count { get; private set; }
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Creates a new range list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public MultiRangeList()
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
_items = new IntervalTree<ulong, T>();
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Adds a new item to the list.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="item">The item to be added</param>
|
|
|
|
|
public void Add(T item)
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
MultiRange range = item.Range;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
var subrange = range.GetSubRange(i);
|
|
|
|
|
_items.Add(subrange.Address, subrange.EndAddress, item);
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
Count++;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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>
|
|
|
|
|
public bool Remove(T item)
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
MultiRange range = item.Range;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
int removed = 0;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var subrange = range.GetSubRange(i);
|
|
|
|
|
removed += _items.Remove(subrange.Address, item);
|
|
|
|
|
}
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
if (removed > 0)
|
|
|
|
|
{
|
|
|
|
|
// All deleted intervals are for the same item - the one we removed.
|
|
|
|
|
Count--;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
return removed > 0;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
return FindOverlaps(new MultiRange(address, size), ref output);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all items on the list overlapping the specified memory ranges.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="range">Ranges of memory being searched</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(MultiRange range, ref T[] output)
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
int overlapCount = 0;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < range.Count; i++)
|
|
|
|
|
{
|
|
|
|
|
var subrange = range.GetSubRange(i);
|
|
|
|
|
overlapCount = _items.Get(subrange.Address, subrange.EndAddress, ref output, overlapCount);
|
|
|
|
|
}
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
// Remove any duplicates, caused by items having multiple sub range nodes in the tree.
|
|
|
|
|
if (overlapCount > 1)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
int insertPtr = 0;
|
|
|
|
|
for (int i = 0; i < overlapCount; i++)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
T item = output[i];
|
|
|
|
|
bool duplicate = false;
|
|
|
|
|
|
|
|
|
|
for (int j = insertPtr - 1; j >= 0; j--)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
if (item.Equals(output[j]))
|
|
|
|
|
{
|
|
|
|
|
duplicate = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
if (!duplicate)
|
|
|
|
|
{
|
|
|
|
|
if (insertPtr != i)
|
|
|
|
|
{
|
|
|
|
|
output[insertPtr] = item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
insertPtr++;
|
|
|
|
|
}
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
2021-09-19 14:55:07 +02:00
|
|
|
|
|
|
|
|
|
overlapCount = insertPtr;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
return overlapCount;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets all items on the list starting at the specified memory address.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="baseAddress">Base 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 baseAddress, ref T[] output)
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
int count = _items.Get(baseAddress, ref output);
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
// Only output items with matching base address
|
|
|
|
|
int insertPtr = 0;
|
|
|
|
|
for (int i = 0; i < count; i++)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
if (output[i].BaseAddress == baseAddress)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
if (i != insertPtr)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
output[insertPtr] = output[i];
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
insertPtr++;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
return insertPtr;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
private List<T> GetList()
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
var items = _items.AsList();
|
|
|
|
|
var result = new List<T>();
|
2021-01-17 19:44:34 +01:00
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
foreach (RangeNode<ulong, T> item in items)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
if (item.Start == item.Value.BaseAddress)
|
2021-01-17 19:44:34 +01:00
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
result.Add(item.Value);
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-19 14:55:07 +02:00
|
|
|
|
return result;
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IEnumerator<T> GetEnumerator()
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
return GetList().GetEnumerator();
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
IEnumerator IEnumerable.GetEnumerator()
|
|
|
|
|
{
|
2021-09-19 14:55:07 +02:00
|
|
|
|
return GetList().GetEnumerator();
|
2021-01-17 19:44:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-19 14:55:07 +02:00
|
|
|
|
}
|