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 : IRange { public readonly int StartIndex = -1; public readonly int EndIndex = -1; public readonly RangeItem QuickResult; public int Count => EndIndex - StartIndex; public OverlapResult(int startIndex, int endIndex, RangeItem 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 : IRange { public readonly ReaderWriterLockSlim Lock = new(); private readonly Dictionary> _quickAccess = new(AddressEqualityComparer.Comparer); /// /// 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; } Insert(index, new RangeItem(item)); } /// /// Updates an item's end address on the list. Address must be the same. /// /// The item to be updated /// True if the item was located and updated, false otherwise protected override bool Update(T item) { int index = BinarySearch(item.Address); if (index >= 0) { while (index < Count) { if (Items[index].Value.Equals(item)) { RangeItem 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 address in Items[index].QuickAccessAddresses) { _quickAccess.Remove(address); } Items[index] = rangeItem; return true; } if (Items[index].Address > item.Address) { break; } index++; } } return false; } /// /// Updates an item's end address on the list. Address must be the same. /// /// The RangeItem to be updated /// True if the item was located and updated, false otherwise protected override bool Update(RangeItem item) { int index = BinarySearch(item.Address); if (index >= 0) { while (index < Count) { if (Items[index].Equals(item)) { RangeItem rangeItem = new(item.Value) { Previous = item.Previous, Next = item.Next }; if (index > 0) { Items[index - 1].Next = rangeItem; } if (index < Count - 1) { Items[index + 1].Previous = rangeItem; } foreach (ulong address in item.QuickAccessAddresses) { _quickAccess.Remove(address); } Items[index] = rangeItem; return true; } if (Items[index].Address > item.Address) { break; } index++; } } return false; } [MethodImpl(MethodImplOptions.AggressiveInlining)] private void Insert(int index, RangeItem item) { Debug.Assert(item.Address != item.EndAddress); Debug.Assert(item.Address % 32 == 0); 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) { 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); } } /// /// 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(RangeItem startItem, RangeItem endItem) { if (startItem is null) { return; } if (startItem == endItem) { Remove(startItem.Value); return; } int startIndex = BinarySearch(startItem.Address); int endIndex = BinarySearch(endItem.Address); for (int i = startIndex; i <= endIndex; i++) { _quickAccess.Remove(Items[i].Address); foreach (ulong addr in Items[i].QuickAccessAddresses) { _quickAccess.Remove(addr); } } 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; } /// /// 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].Value.Equals(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 RangeItem 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 RangeItem FindOverlapFast(ulong address, ulong size) { if (_quickAccess.TryGetValue(address, out RangeItem quickResult)) { return quickResult; } int index = BinarySearch(address, address + size); if (index < 0) { return null; } if (Items[index].OverlapsWith(address, address + 1)) { _quickAccess.Add(address, Items[index]); Items[index].QuickAccessAddresses.Add(address); } 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 RangeItem[] 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++) { ref RangeItem item = ref 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].Value; } } } }