using Ryujinx.Common.Collections; using System.Collections; using System.Collections.Generic; namespace Ryujinx.Memory.Range { public class MultiRangeList : IEnumerable where T : IMultiRangeItem { private readonly IntervalTree _items; public int Count { get; private set; } /// /// Creates a new range list. /// public MultiRangeList() { _items = new IntervalTree(); } /// /// Adds a new item to the list. /// /// The item to be added public void Add(T item) { MultiRange range = item.Range; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); if (IsInvalid(ref subrange)) { continue; } _items.Add(subrange.Address, subrange.EndAddress, item); } Count++; } /// /// 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 bool Remove(T item) { MultiRange range = item.Range; int removed = 0; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); if (IsInvalid(ref subrange)) { continue; } removed += _items.Remove(subrange.Address, item); } if (removed > 0) { // All deleted intervals are for the same item - the one we removed. Count--; } return removed > 0; } /// /// 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 /// The number of overlapping items found public int FindOverlaps(ulong address, ulong size, ref T[] output) { return FindOverlaps(new MultiRange(address, size), ref output); } /// /// Gets all items on the list overlapping the specified memory ranges. /// /// Ranges of memory being searched /// Output array where matches will be written. It is automatically resized to fit the results /// The number of overlapping items found public int FindOverlaps(MultiRange range, ref T[] output) { int overlapCount = 0; for (int i = 0; i < range.Count; i++) { var subrange = range.GetSubRange(i); if (IsInvalid(ref subrange)) { continue; } overlapCount = _items.Get(subrange.Address, subrange.EndAddress, ref output, overlapCount); } // Remove any duplicates, caused by items having multiple sub range nodes in the tree. if (overlapCount > 1) { int insertPtr = 0; for (int i = 0; i < overlapCount; i++) { T item = output[i]; bool duplicate = false; for (int j = insertPtr - 1; j >= 0; j--) { if (item.Equals(output[j])) { duplicate = true; break; } } if (!duplicate) { if (insertPtr != i) { output[insertPtr] = item; } insertPtr++; } } overlapCount = insertPtr; } return overlapCount; } /// /// Checks if a given sub-range of memory is invalid. /// Those are used to represent unmapped memory regions (holes in the region mapping). /// /// Memory range to checl /// True if the memory range is considered invalid, false otherwise private static bool IsInvalid(ref MemoryRange subRange) { return subRange.Address == ulong.MaxValue; } /// /// Gets all items on the list starting at the specified memory address. /// /// Base address to find /// Output array where matches will be written. It is automatically resized to fit the results /// The number of matches found public int FindOverlaps(ulong baseAddress, ref T[] output) { int count = _items.Get(baseAddress, ref output); // Only output items with matching base address int insertPtr = 0; for (int i = 0; i < count; i++) { if (output[i].BaseAddress == baseAddress) { if (i != insertPtr) { output[insertPtr] = output[i]; } insertPtr++; } } return insertPtr; } private List GetList() { var items = _items.AsList(); var result = new List(); foreach (RangeNode item in items) { if (item.Start == item.Value.BaseAddress) { result.Add(item.Value); } } return result; } public IEnumerator GetEnumerator() { return GetList().GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetList().GetEnumerator(); } } }