RangeList.cs 11 KB

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