RangeList.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Runtime.CompilerServices;
  5. namespace Ryujinx.Memory.Range
  6. {
  7. /// <summary>
  8. /// Sorted list of ranges that supports binary search.
  9. /// </summary>
  10. /// <typeparam name="T">Type of the range.</typeparam>
  11. public class RangeList<T> : IEnumerable<T> where T : IRange
  12. {
  13. private readonly struct RangeItem<TValue> where TValue : IRange
  14. {
  15. public readonly ulong Address;
  16. public readonly ulong EndAddress;
  17. public readonly TValue Value;
  18. public RangeItem(TValue value)
  19. {
  20. Value = value;
  21. Address = value.Address;
  22. EndAddress = value.Address + value.Size;
  23. }
  24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  25. public bool OverlapsWith(ulong address, ulong endAddress)
  26. {
  27. return Address < endAddress && address < EndAddress;
  28. }
  29. }
  30. private const int ArrayGrowthSize = 32;
  31. private const int BackingGrowthSize = 1024;
  32. private RangeItem<T>[] _items;
  33. public int Count { get; protected set; }
  34. /// <summary>
  35. /// Creates a new range list.
  36. /// </summary>
  37. public RangeList()
  38. {
  39. _items = new RangeItem<T>[BackingGrowthSize];
  40. }
  41. /// <summary>
  42. /// Adds a new item to the list.
  43. /// </summary>
  44. /// <param name="item">The item to be added</param>
  45. public void Add(T item)
  46. {
  47. int index = BinarySearch(item.Address);
  48. if (index < 0)
  49. {
  50. index = ~index;
  51. }
  52. Insert(index, new RangeItem<T>(item));
  53. }
  54. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  55. private void Insert(int index, RangeItem<T> item)
  56. {
  57. if (Count + 1 > _items.Length)
  58. {
  59. Array.Resize(ref _items, _items.Length + ArrayGrowthSize);
  60. }
  61. if (index >= Count)
  62. {
  63. if (index == Count)
  64. {
  65. _items[Count++] = item;
  66. }
  67. }
  68. else
  69. {
  70. Array.Copy(_items, index, _items, index + 1, Count - index);
  71. _items[index] = item;
  72. Count++;
  73. }
  74. }
  75. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  76. private void RemoveAt(int index)
  77. {
  78. if (index < --Count)
  79. {
  80. Array.Copy(_items, index + 1, _items, index, Count - index);
  81. }
  82. }
  83. /// <summary>
  84. /// Removes an item from the list.
  85. /// </summary>
  86. /// <param name="item">The item to be removed</param>
  87. /// <returns>True if the item was removed, or false if it was not found</returns>
  88. public bool Remove(T item)
  89. {
  90. int index = BinarySearch(item.Address);
  91. if (index >= 0)
  92. {
  93. while (index > 0 && _items[index - 1].Address == item.Address)
  94. {
  95. index--;
  96. }
  97. while (index < Count)
  98. {
  99. if (_items[index].Value.Equals(item))
  100. {
  101. RemoveAt(index);
  102. return true;
  103. }
  104. if (_items[index].Address > item.Address)
  105. {
  106. break;
  107. }
  108. index++;
  109. }
  110. }
  111. return false;
  112. }
  113. /// <summary>
  114. /// Updates an item's end address.
  115. /// </summary>
  116. /// <param name="item">The item to be updated</param>
  117. public void UpdateEndAddress(T item)
  118. {
  119. int index = BinarySearch(item.Address);
  120. if (index >= 0)
  121. {
  122. while (index > 0 && _items[index - 1].Address == item.Address)
  123. {
  124. index--;
  125. }
  126. while (index < Count)
  127. {
  128. if (_items[index].Value.Equals(item))
  129. {
  130. _items[index] = new RangeItem<T>(item);
  131. return;
  132. }
  133. if (_items[index].Address > item.Address)
  134. {
  135. break;
  136. }
  137. index++;
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Gets the first item on the list overlapping in memory with the specified item.
  143. /// </summary>
  144. /// <remarks>
  145. /// Despite the name, this has no ordering guarantees of the returned item.
  146. /// It only ensures that the item returned overlaps the specified item.
  147. /// </remarks>
  148. /// <param name="item">Item to check for overlaps</param>
  149. /// <returns>The overlapping item, or the default value for the type if none found</returns>
  150. public T FindFirstOverlap(T item)
  151. {
  152. return FindFirstOverlap(item.Address, item.Size);
  153. }
  154. /// <summary>
  155. /// Gets the first item on the list overlapping the specified memory range.
  156. /// </summary>
  157. /// <remarks>
  158. /// Despite the name, this has no ordering guarantees of the returned item.
  159. /// It only ensures that the item returned overlaps the specified memory range.
  160. /// </remarks>
  161. /// <param name="address">Start address of the range</param>
  162. /// <param name="size">Size in bytes of the range</param>
  163. /// <returns>The overlapping item, or the default value for the type if none found</returns>
  164. public T FindFirstOverlap(ulong address, ulong size)
  165. {
  166. int index = BinarySearch(address, address + size);
  167. if (index < 0)
  168. {
  169. return default(T);
  170. }
  171. return _items[index].Value;
  172. }
  173. /// <summary>
  174. /// Gets all items overlapping with the specified item in memory.
  175. /// </summary>
  176. /// <param name="item">Item to check for overlaps</param>
  177. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  178. /// <returns>The number of overlapping items found</returns>
  179. public int FindOverlaps(T item, ref T[] output)
  180. {
  181. return FindOverlaps(item.Address, item.Size, ref output);
  182. }
  183. /// <summary>
  184. /// Gets all items on the list overlapping the specified memory range.
  185. /// </summary>
  186. /// <param name="address">Start address of the range</param>
  187. /// <param name="size">Size in bytes of the range</param>
  188. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  189. /// <returns>The number of overlapping items found</returns>
  190. public int FindOverlaps(ulong address, ulong size, ref T[] output)
  191. {
  192. int outputIndex = 0;
  193. ulong endAddress = address + size;
  194. for (int i = 0; i < Count; i++)
  195. {
  196. ref RangeItem<T> item = ref _items[i];
  197. if (item.Address >= endAddress)
  198. {
  199. break;
  200. }
  201. if (item.OverlapsWith(address, endAddress))
  202. {
  203. if (outputIndex == output.Length)
  204. {
  205. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  206. }
  207. output[outputIndex++] = item.Value;
  208. }
  209. }
  210. return outputIndex;
  211. }
  212. /// <summary>
  213. /// Gets all items overlapping with the specified item in memory.
  214. /// </summary>
  215. /// <remarks>
  216. /// This method only returns correct results if none of the items on the list overlaps with
  217. /// each other. If that is not the case, this method should not be used.
  218. /// This method is faster than the regular method to find all overlaps.
  219. /// </remarks>
  220. /// <param name="item">Item to check for overlaps</param>
  221. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  222. /// <returns>The number of overlapping items found</returns>
  223. public int FindOverlapsNonOverlapping(T item, ref T[] output)
  224. {
  225. return FindOverlapsNonOverlapping(item.Address, item.Size, ref output);
  226. }
  227. /// <summary>
  228. /// Gets all items on the list overlapping the specified memory range.
  229. /// </summary>
  230. /// <remarks>
  231. /// This method only returns correct results if none of the items on the list overlaps with
  232. /// each other. If that is not the case, this method should not be used.
  233. /// This method is faster than the regular method to find all overlaps.
  234. /// </remarks>
  235. /// <param name="address">Start address of the range</param>
  236. /// <param name="size">Size in bytes of the range</param>
  237. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  238. /// <returns>The number of overlapping items found</returns>
  239. public int FindOverlapsNonOverlapping(ulong address, ulong size, ref T[] output)
  240. {
  241. // This is a bit faster than FindOverlaps, but only works
  242. // when none of the items on the list overlaps with each other.
  243. int outputIndex = 0;
  244. ulong endAddress = address + size;
  245. int index = BinarySearch(address, endAddress);
  246. if (index >= 0)
  247. {
  248. while (index > 0 && _items[index - 1].OverlapsWith(address, endAddress))
  249. {
  250. index--;
  251. }
  252. do
  253. {
  254. if (outputIndex == output.Length)
  255. {
  256. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  257. }
  258. output[outputIndex++] = _items[index++].Value;
  259. }
  260. while (index < Count && _items[index].OverlapsWith(address, endAddress));
  261. }
  262. return outputIndex;
  263. }
  264. /// <summary>
  265. /// Gets all items on the list with the specified memory address.
  266. /// </summary>
  267. /// <param name="address">Address to find</param>
  268. /// <param name="output">Output array where matches will be written. It is automatically resized to fit the results</param>
  269. /// <returns>The number of matches found</returns>
  270. public int FindOverlaps(ulong address, ref T[] output)
  271. {
  272. int index = BinarySearch(address);
  273. int outputIndex = 0;
  274. if (index >= 0)
  275. {
  276. while (index > 0 && _items[index - 1].Address == address)
  277. {
  278. index--;
  279. }
  280. while (index < Count)
  281. {
  282. ref RangeItem<T> overlap = ref _items[index++];
  283. if (overlap.Address != address)
  284. {
  285. break;
  286. }
  287. if (outputIndex == output.Length)
  288. {
  289. Array.Resize(ref output, outputIndex + ArrayGrowthSize);
  290. }
  291. output[outputIndex++] = overlap.Value;
  292. }
  293. }
  294. return outputIndex;
  295. }
  296. /// <summary>
  297. /// Performs binary search on the internal list of items.
  298. /// </summary>
  299. /// <param name="address">Address to find</param>
  300. /// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
  301. private int BinarySearch(ulong address)
  302. {
  303. int left = 0;
  304. int right = Count - 1;
  305. while (left <= right)
  306. {
  307. int range = right - left;
  308. int middle = left + (range >> 1);
  309. ref RangeItem<T> item = ref _items[middle];
  310. if (item.Address == address)
  311. {
  312. return middle;
  313. }
  314. if (address < item.Address)
  315. {
  316. right = middle - 1;
  317. }
  318. else
  319. {
  320. left = middle + 1;
  321. }
  322. }
  323. return ~left;
  324. }
  325. /// <summary>
  326. /// Performs binary search for items overlapping a given memory range.
  327. /// </summary>
  328. /// <param name="address">Start address of the range</param>
  329. /// <param name="endAddress">End address of the range</param>
  330. /// <returns>List index of the item, or complement index of nearest item with lower value on the list</returns>
  331. private int BinarySearch(ulong address, ulong endAddress)
  332. {
  333. int left = 0;
  334. int right = Count - 1;
  335. while (left <= right)
  336. {
  337. int range = right - left;
  338. int middle = left + (range >> 1);
  339. ref RangeItem<T> item = ref _items[middle];
  340. if (item.OverlapsWith(address, endAddress))
  341. {
  342. return middle;
  343. }
  344. if (address < item.Address)
  345. {
  346. right = middle - 1;
  347. }
  348. else
  349. {
  350. left = middle + 1;
  351. }
  352. }
  353. return ~left;
  354. }
  355. public IEnumerator<T> GetEnumerator()
  356. {
  357. for (int i = 0; i < Count; i++)
  358. {
  359. yield return _items[i].Value;
  360. }
  361. }
  362. IEnumerator IEnumerable.GetEnumerator()
  363. {
  364. for (int i = 0; i < Count; i++)
  365. {
  366. yield return _items[i].Value;
  367. }
  368. }
  369. }
  370. }