RangeList.cs 11 KB

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