RangeList.cs 15 KB

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