using System; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading; namespace Ryujinx.Memory.Range { /// /// 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 /// /// /// startIndex is inclusive. /// endIndex is exclusive. /// public readonly struct OverlapResult where T : class, IRangeListRange { public readonly int StartIndex = -1; public readonly int EndIndex = -1; public readonly T QuickResult; public int Count => EndIndex - StartIndex; public OverlapResult(int startIndex, int endIndex, T quickResult = null) { this.StartIndex = startIndex; this.EndIndex = endIndex; this.QuickResult = quickResult; } } /// /// Sorted list of ranges that supports binary search. /// /// Type of the range. public class RangeList : RangeListBase where T : class, IRangeListRange { public readonly ReaderWriterLockSlim Lock = new(); /// /// Creates a new range list. /// public RangeList() { } /// /// Creates a new range list. /// /// The initial size of the backing array public RangeList(int backingInitialSize) : base(backingInitialSize) { } /// /// Adds a new item to the list. /// /// The item to be added public override void Add(T item) { int index = BinarySearch(item.Address); if (index < 0) { index = ~index; } 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); } } /// /// 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 [MethodImpl(MethodImplOptions.AggressiveInlining)] public override void RemoveRange(T startItem, T endItem) { if (startItem is null) { return; } if (startItem == endItem) { Remove(startItem); return; } (int index, int endIndex) = BinarySearchEdges(startItem.Address, endItem.EndAddress); 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; } /// /// Removes an item from the list. /// /// The item to be removed /// True if the item was removed, or false if it was not found public override bool Remove(T item) { int index = BinarySearch(item.Address); if (index >= 0) { while (index < Count) { if (Items[index] == item) { RemoveAt(index); return true; } if (Items[index].Address > item.Address) { break; } index++; } } return false; } /// /// Gets an item on the list overlapping the specified memory range. /// /// /// This has no ordering guarantees of the returned item. /// It only ensures that the item returned overlaps the specified memory range. /// /// Start address of the range /// Size in bytes of the range /// The overlapping item, or the default value for the type if none found [MethodImpl(MethodImplOptions.AggressiveInlining)] public override T FindOverlap(ulong address, ulong size) { int index = BinarySearchLeftEdge(address, address + size); if (index < 0) { return null; } return Items[index]; } /// /// Gets an item on the list overlapping the specified memory range. /// /// /// This has no ordering guarantees of the returned item. /// It only ensures that the item returned overlaps the specified memory range. /// /// Start address of the range /// Size in bytes of the range /// The overlapping item, or the default value for the type if none found [MethodImpl(MethodImplOptions.AggressiveInlining)] public override T FindOverlapFast(ulong address, ulong size) { int index = BinarySearch(address, address + size); if (index < 0) { return null; } return Items[index]; } /// /// Gets all items on the list overlapping the specified memory range. /// /// Start address of the range /// Size in bytes of the range /// Output array where matches will be written. It is automatically resized to fit the results /// Range information of overlapping items found private OverlapResult FindOverlaps(ulong address, ulong size, ref T[] output) { int outputCount = 0; ulong endAddress = address + size; int startIndex = BinarySearch(address, endAddress); if (startIndex < 0) startIndex = ~startIndex; int endIndex = -1; for (int i = startIndex; i < Count; i++) { T item = Items[i]; if (item.Address >= endAddress) { endIndex = i; break; } if (item.OverlapsWith(address, endAddress)) { outputCount++; } } if (endIndex == -1 && outputCount > 0) { endIndex = Count; } if (outputCount > 0 && outputCount == endIndex - startIndex) { Array.Resize(ref output, outputCount); Array.Copy(Items, endIndex - outputCount, output, 0, outputCount); return new OverlapResult(startIndex, endIndex); } else if (outputCount > 0) { Array.Resize(ref output, outputCount); int arrIndex = 0; for (int i = startIndex; i < endIndex; i++) { output[arrIndex++] = Items[i]; } return new OverlapResult(endIndex - outputCount, endIndex); } return new OverlapResult(); } public override IEnumerator GetEnumerator() { for (int i = 0; i < Count; i++) { yield return Items[i]; } } } }