RangeList.cs 14 KB

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