MultiRangeList.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace Ryujinx.Memory.Range
  5. {
  6. /// <summary>
  7. /// Sorted list of ranges that supports binary search.
  8. /// </summary>
  9. /// <typeparam name="T">Type of the range.</typeparam>
  10. public class MultiRangeList<T> : IEnumerable<T> where T : IMultiRangeItem
  11. {
  12. private const int ArrayGrowthSize = 32;
  13. private readonly List<T> _items;
  14. public int Count => _items.Count;
  15. /// <summary>
  16. /// Creates a new range list.
  17. /// </summary>
  18. public MultiRangeList()
  19. {
  20. _items = new List<T>();
  21. }
  22. /// <summary>
  23. /// Adds a new item to the list.
  24. /// </summary>
  25. /// <param name="item">The item to be added</param>
  26. public void Add(T item)
  27. {
  28. int index = BinarySearch(item.BaseAddress);
  29. if (index < 0)
  30. {
  31. index = ~index;
  32. }
  33. _items.Insert(index, item);
  34. }
  35. /// <summary>
  36. /// Removes an item from the list.
  37. /// </summary>
  38. /// <param name="item">The item to be removed</param>
  39. /// <returns>True if the item was removed, or false if it was not found</returns>
  40. public bool Remove(T item)
  41. {
  42. int index = BinarySearch(item.BaseAddress);
  43. if (index >= 0)
  44. {
  45. while (index > 0 && _items[index - 1].BaseAddress == item.BaseAddress)
  46. {
  47. index--;
  48. }
  49. while (index < _items.Count)
  50. {
  51. if (_items[index].Equals(item))
  52. {
  53. _items.RemoveAt(index);
  54. return true;
  55. }
  56. if (_items[index].BaseAddress > item.BaseAddress)
  57. {
  58. break;
  59. }
  60. index++;
  61. }
  62. }
  63. return false;
  64. }
  65. /// <summary>
  66. /// Gets all items on the list overlapping the specified memory range.
  67. /// </summary>
  68. /// <param name="address">Start address of the range</param>
  69. /// <param name="size">Size in bytes of the range</param>
  70. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  71. /// <returns>The number of overlapping items found</returns>
  72. public int FindOverlaps(ulong address, ulong size, ref T[] output)
  73. {
  74. return FindOverlaps(new MultiRange(address, size), ref output);
  75. }
  76. /// <summary>
  77. /// Gets all items on the list overlapping the specified memory ranges.
  78. /// </summary>
  79. /// <param name="range">Ranges of memory being searched</param>
  80. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  81. /// <returns>The number of overlapping items found</returns>
  82. public int FindOverlaps(MultiRange range, ref T[] output)
  83. {
  84. int outputIndex = 0;
  85. foreach (T item in _items)
  86. {
  87. if (item.Range.OverlapsWith(range))
  88. {
  89. if (outputIndex == output.Length)
  90. {
  91. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  92. }
  93. output[outputIndex++] = item;
  94. }
  95. }
  96. return outputIndex;
  97. }
  98. /// <summary>
  99. /// Gets all items on the list starting at the specified memory address.
  100. /// </summary>
  101. /// <param name="baseAddress">Base address to find</param>
  102. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  103. /// <returns>The number of matches found</returns>
  104. public int FindOverlaps(ulong baseAddress, ref T[] output)
  105. {
  106. int index = BinarySearch(baseAddress);
  107. int outputIndex = 0;
  108. if (index >= 0)
  109. {
  110. while (index > 0 && _items[index - 1].BaseAddress == baseAddress)
  111. {
  112. index--;
  113. }
  114. while (index < _items.Count)
  115. {
  116. T overlap = _items[index++];
  117. if (overlap.BaseAddress != baseAddress)
  118. {
  119. break;
  120. }
  121. if (outputIndex == output.Length)
  122. {
  123. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  124. }
  125. output[outputIndex++] = overlap;
  126. }
  127. }
  128. return outputIndex;
  129. }
  130. /// <summary>
  131. /// Performs binary search on the internal list of items.
  132. /// </summary>
  133. /// <param name="address">Address to find</param>
  134. /// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
  135. private int BinarySearch(ulong address)
  136. {
  137. int left = 0;
  138. int right = _items.Count - 1;
  139. while (left <= right)
  140. {
  141. int range = right - left;
  142. int middle = left + (range >> 1);
  143. T item = _items[middle];
  144. if (item.BaseAddress == address)
  145. {
  146. return middle;
  147. }
  148. if (address < item.BaseAddress)
  149. {
  150. right = middle - 1;
  151. }
  152. else
  153. {
  154. left = middle + 1;
  155. }
  156. }
  157. return ~left;
  158. }
  159. public IEnumerator<T> GetEnumerator()
  160. {
  161. return _items.GetEnumerator();
  162. }
  163. IEnumerator IEnumerable.GetEnumerator()
  164. {
  165. return _items.GetEnumerator();
  166. }
  167. }
  168. }