RangeList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 RangeList<T> : IEnumerable<T> where T : IRange
  11. {
  12. private const int ArrayGrowthSize = 32;
  13. protected readonly List<T> Items;
  14. public int Count => Items.Count;
  15. /// <summary>
  16. /// Creates a new range list.
  17. /// </summary>
  18. public RangeList()
  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.Address);
  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.Address);
  43. if (index >= 0)
  44. {
  45. while (index > 0 && Items[index - 1].Address == item.Address)
  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].Address > item.Address)
  57. {
  58. break;
  59. }
  60. index++;
  61. }
  62. }
  63. return false;
  64. }
  65. /// <summary>
  66. /// Gets the first item on the list overlapping in memory with the specified item.
  67. /// </summary>
  68. /// <remarks>
  69. /// Despite the name, this has no ordering guarantees of the returned item.
  70. /// It only ensures that the item returned overlaps the specified item.
  71. /// </remarks>
  72. /// <param name="item">Item to check for overlaps</param>
  73. /// <returns>The overlapping item, or the default value for the type if none found</returns>
  74. public T FindFirstOverlap(T item)
  75. {
  76. return FindFirstOverlap(item.Address, item.Size);
  77. }
  78. /// <summary>
  79. /// Gets the first item on the list overlapping the specified memory range.
  80. /// </summary>
  81. /// <remarks>
  82. /// Despite the name, this has no ordering guarantees of the returned item.
  83. /// It only ensures that the item returned overlaps the specified memory range.
  84. /// </remarks>
  85. /// <param name="address">Start address of the range</param>
  86. /// <param name="size">Size in bytes of the range</param>
  87. /// <returns>The overlapping item, or the default value for the type if none found</returns>
  88. public T FindFirstOverlap(ulong address, ulong size)
  89. {
  90. int index = BinarySearch(address, size);
  91. if (index < 0)
  92. {
  93. return default(T);
  94. }
  95. return Items[index];
  96. }
  97. /// <summary>
  98. /// Gets all items overlapping with the specified item in memory.
  99. /// </summary>
  100. /// <param name="item">Item to check for overlaps</param>
  101. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  102. /// <returns>The number of overlapping items found</returns>
  103. public int FindOverlaps(T item, ref T[] output)
  104. {
  105. return FindOverlaps(item.Address, item.Size, ref output);
  106. }
  107. /// <summary>
  108. /// Gets all items on the list overlapping the specified memory range.
  109. /// </summary>
  110. /// <param name="address">Start address of the range</param>
  111. /// <param name="size">Size in bytes of the range</param>
  112. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  113. /// <returns>The number of overlapping items found</returns>
  114. public int FindOverlaps(ulong address, ulong size, ref T[] output)
  115. {
  116. int outputIndex = 0;
  117. ulong endAddress = address + size;
  118. foreach (T item in Items)
  119. {
  120. if (item.Address >= endAddress)
  121. {
  122. break;
  123. }
  124. if (item.OverlapsWith(address, size))
  125. {
  126. if (outputIndex == output.Length)
  127. {
  128. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  129. }
  130. output[outputIndex++] = item;
  131. }
  132. }
  133. return outputIndex;
  134. }
  135. /// <summary>
  136. /// Gets all items overlapping with the specified item in memory.
  137. /// </summary>
  138. /// <remarks>
  139. /// This method only returns correct results if none of the items on the list overlaps with
  140. /// each other. If that is not the case, this method should not be used.
  141. /// This method is faster than the regular method to find all overlaps.
  142. /// </remarks>
  143. /// <param name="item">Item to check for overlaps</param>
  144. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  145. /// <returns>The number of overlapping items found</returns>
  146. public int FindOverlapsNonOverlapping(T item, ref T[] output)
  147. {
  148. return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
  149. }
  150. /// <summary>
  151. /// Gets all items on the list overlapping the specified memory range.
  152. /// </summary>
  153. /// <remarks>
  154. /// This method only returns correct results if none of the items on the list overlaps with
  155. /// each other. If that is not the case, this method should not be used.
  156. /// This method is faster than the regular method to find all overlaps.
  157. /// </remarks>
  158. /// <param name="address">Start address of the range</param>
  159. /// <param name="size">Size in bytes of the range</param>
  160. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  161. /// <returns>The number of overlapping items found</returns>
  162. public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
  163. {
  164. // This is a bit faster than FindOverlaps, but only works
  165. // when none of the items on the list overlaps with each other.
  166. int outputIndex = 0;
  167. int index = BinarySearch(address, size);
  168. if (index >= 0)
  169. {
  170. while (index > 0 && Items[index - 1].OverlapsWith(address, size))
  171. {
  172. index--;
  173. }
  174. do
  175. {
  176. if (outputIndex == output.Length)
  177. {
  178. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  179. }
  180. output[outputIndex++] = Items[index++];
  181. }
  182. while (index < Items.Count && Items[index].OverlapsWith(address, size));
  183. }
  184. return outputIndex;
  185. }
  186. /// <summary>
  187. /// Gets all items on the list with the specified memory address.
  188. /// </summary>
  189. /// <param name="address">Address to find</param>
  190. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  191. /// <returns>The number of matches found</returns>
  192. public int FindOverlaps(ulong address, ref T[] output)
  193. {
  194. int index = BinarySearch(address);
  195. int outputIndex = 0;
  196. if (index >= 0)
  197. {
  198. while (index > 0 && Items[index - 1].Address == address)
  199. {
  200. index--;
  201. }
  202. while (index < Items.Count)
  203. {
  204. T overlap = Items[index++];
  205. if (overlap.Address != address)
  206. {
  207. break;
  208. }
  209. if (outputIndex == output.Length)
  210. {
  211. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  212. }
  213. output[outputIndex++] = overlap;
  214. }
  215. }
  216. return outputIndex;
  217. }
  218. /// <summary>
  219. /// Performs binary search on the internal list of items.
  220. /// </summary>
  221. /// <param name="address">Address to find</param>
  222. /// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
  223. private int BinarySearch(ulong address)
  224. {
  225. int left = 0;
  226. int right = Items.Count - 1;
  227. while (left <= right)
  228. {
  229. int range = right - left;
  230. int middle = left + (range >> 1);
  231. T item = Items[middle];
  232. if (item.Address == address)
  233. {
  234. return middle;
  235. }
  236. if (address < item.Address)
  237. {
  238. right = middle - 1;
  239. }
  240. else
  241. {
  242. left = middle + 1;
  243. }
  244. }
  245. return ~left;
  246. }
  247. /// <summary>
  248. /// Performs binary search for items overlapping a given memory range.
  249. /// </summary>
  250. /// <param name="address">Start address of the range</param>
  251. /// <param name="size">Size in bytes of the range</param>
  252. /// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
  253. private int BinarySearch(ulong address, ulong size)
  254. {
  255. int left = 0;
  256. int right = Items.Count - 1;
  257. while (left <= right)
  258. {
  259. int range = right - left;
  260. int middle = left + (range >> 1);
  261. T item = Items[middle];
  262. if (item.OverlapsWith(address, size))
  263. {
  264. return middle;
  265. }
  266. if (address < item.Address)
  267. {
  268. right = middle - 1;
  269. }
  270. else
  271. {
  272. left = middle + 1;
  273. }
  274. }
  275. return ~left;
  276. }
  277. public IEnumerator<T> GetEnumerator()
  278. {
  279. return Items.GetEnumerator();
  280. }
  281. IEnumerator IEnumerable.GetEnumerator()
  282. {
  283. return Items.GetEnumerator();
  284. }
  285. }
  286. }