using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
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.
///
/// Type of the range.
public class NonOverlappingRangeList : RangeListBase where T : class, INonOverlappingRange
{
public readonly ReaderWriterLockSlim Lock = new();
///
/// Creates a new non-overlapping range list.
///
public NonOverlappingRangeList() { }
///
/// Creates a new non-overlapping range list.
///
/// The initial size of the backing array
public NonOverlappingRangeList(int backingInitialSize) : base(backingInitialSize) { }
///
/// Adds a new item to the list.
///
/// The item to be added
public override void Add(T item)
{
Debug.Assert(item.Address != item.EndAddress);
int index = BinarySearch(item.Address);
if (index < 0)
{
index = ~index;
}
if (Count + 1 > Items.Length)
{
Array.Resize(ref Items, (int)(Items.Length * 1.5));
}
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);
}
}
///
/// Removes an item from the list.
///
/// The item to be removed
/// True if the item was removed, or false if it was not found
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override bool Remove(T item)
{
int index = BinarySearch(item.Address);
if (index >= 0 && Items[index] == item)
{
RemoveAt(index);
return true;
}
return false;
}
///
/// Removes a range of items from the item list
///
/// The first item in the range of items to be removed
/// The last item in the range of items to be removed
public override void RemoveRange(T startItem, T endItem)
{
if (startItem is null)
{
return;
}
if (startItem == endItem)
{
Remove(startItem);
return;
}
(int startIndex, int endIndex) = BinarySearchEdges(startItem.Address, endItem.EndAddress);
if (endIndex < Count)
{
Items[endIndex].Previous = startIndex > 0 ? Items[startIndex - 1] : null;
}
if (startIndex > 0)
{
Items[startIndex - 1].Next = endIndex < Count ? Items[endIndex] : null;
}
if (endIndex < Count)
{
Array.Copy(Items, endIndex, Items, startIndex, Count - endIndex);
}
Count -= endIndex - startIndex;
}
///
/// Removes a range of items from the item list
///
/// Start address of the range
/// Size of the range
public void RemoveRange(ulong address, ulong size)
{
(int startIndex, int endIndex) = BinarySearchEdges(address, address + size);
if (startIndex < 0)
{
return;
}
if (startIndex == endIndex - 1)
{
RemoveAt(startIndex);
return;
}
RemoveRangeInternal(startIndex, endIndex);
}
///
/// Removes a range of items from the item list
///
/// Start index of the range
/// End index of the range (exclusive)
private void RemoveRangeInternal(int index, int endIndex)
{
if (endIndex < Count)
{
Items[endIndex].Previous = index > 0 ? Items[index - 1] : null;
}
if (index > 0)
{
Items[index - 1].Next = endIndex < Count ? Items[endIndex] : null;
}
if (endIndex < Count)
{
Array.Copy(Items, endIndex, Items, index, Count - endIndex);
}
Count -= endIndex - index;
}
///
/// Clear all ranges.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Clear()
{
Lock.EnterWriteLock();
Count = 0;
Lock.ExitWriteLock();
}
///
/// 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.
/// If there is no matching region, or there is a gap, then new regions are created with the factory.
/// Regions are added to the list in address ascending order.
///
/// List to add found regions to
/// Start address of the search region
/// Size of the search region
/// Factory for creating new ranges
public void GetOrAddRegions(out List list, ulong address, ulong size, Func 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.
Lock.EnterWriteLock();
(T first, T last) = FindOverlapsAsNodes(address, size);
list = [];
if (first is null)
{
// The region is fully unmapped. Create and add it to the range list.
T region = factory(address, size);
list.Add(region);
Add(region);
}
else
{
ulong lastAddress = address;
ulong endAddress = address + size;
T current = first;
while (last is not null && current is not null && current.Address < endAddress)
{
if (first == last && current.Address == address && current.Size == size)
{
// Exact match, no splitting required.
list.Add(current);
Lock.ExitWriteLock();
return;
}
if (lastAddress < current.Address)
{
// There is a gap between this region and the last. We need to fill it.
T fillRegion = factory(lastAddress, current.Address - lastAddress);
list.Add(fillRegion);
Add(fillRegion);
}
if (current.Address < address)
{
// Split the region around our base address and take the high half.
current = Split(current, address);
}
if (current.EndAddress > address + size)
{
// Split the region around our end address and take the low half.
Split(current, address + size);
}
list.Add(current);
lastAddress = current.EndAddress;
current = current.Next;
}
if (lastAddress < endAddress)
{
// There is a gap between this region and the end. We need to fill it.
T fillRegion = factory(lastAddress, endAddress - lastAddress);
list.Add(fillRegion);
Add(fillRegion);
}
}
Lock.ExitWriteLock();
}
///
/// Splits a region around a target point and updates the region list.
/// The original region's size is modified, but its address stays the same.
/// A new region starting from the split address is added to the region list and returned.
///
/// The region to split
/// The address to split with
/// The new region (high part)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private T Split(T region, ulong splitAddress)
{
T newRegion = (T)region.Split(splitAddress);
Add(newRegion);
return newRegion;
}
///
/// Gets an item on the list overlapping the specified memory range.
///
/// Start address of the range
/// Size in bytes of the range
/// The leftmost overlapping item, or null if none is found
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override T FindOverlap(ulong address, ulong size)
{
int index = BinarySearchLeftEdge(address, address + size);
return index < 0 ? null : Items[index];
}
///
/// Gets an item on the list overlapping the specified memory range.
///
/// Start address of the range
/// Size in bytes of the range
/// The overlapping item, or null if none is found
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override T FindOverlapFast(ulong address, ulong size)
{
int index = BinarySearch(address, address + size);
return index < 0 ? null : Items[index];
}
///
/// Gets all items on the list overlapping the specified memory range.
///
/// Start address of the range
/// Size in bytes of the range
/// The first and last overlapping items, or null if none are found
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public (T, T) FindOverlapsAsNodes(ulong address, ulong size)
{
(int index, int endIndex) = BinarySearchEdges(address, address + size);
return index < 0 ? (null, null) : (Items[index], Items[endIndex - 1]);
}
public T[] FindOverlapsAsArray(ulong address, ulong size, out int length)
{
(int index, int endIndex) = BinarySearchEdges(address, address + size);
T[] result;
if (index < 0)
{
result = null;
length = 0;
}
else
{
result = ArrayPool.Shared.Rent(endIndex - index);
length = endIndex - index;
Items.AsSpan(index, endIndex - index).CopyTo(result);
}
return result;
}
public ReadOnlySpan FindOverlapsAsSpan(ulong address, ulong size)
{
(int index, int endIndex) = BinarySearchEdges(address, address + size);
ReadOnlySpan result = index < 0 ? [] : Items.AsSpan(index, endIndex - index);
return result;
}
public override IEnumerator GetEnumerator()
{
for (int i = 0; i < Count; i++)
{
yield return Items[i];
}
}
}
}