ConcurrentRangeList.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System.Collections.Generic;
  2. namespace Ryujinx.Graphics.Gpu.Memory
  3. {
  4. class ConcurrentRangeList<T> where T : IRange<T>
  5. {
  6. private List<T> _items;
  7. public int Count => _items.Count;
  8. public ConcurrentRangeList()
  9. {
  10. _items = new List<T>();
  11. }
  12. public void Add(T item)
  13. {
  14. lock (_items)
  15. {
  16. int index = BinarySearch(item.Address);
  17. if (index < 0)
  18. {
  19. index = ~index;
  20. }
  21. _items.Insert(index, item);
  22. }
  23. }
  24. public bool Remove(T item)
  25. {
  26. lock (_items)
  27. {
  28. int index = BinarySearch(item.Address);
  29. if (index >= 0)
  30. {
  31. while (index > 0 && _items[index - 1].Address == item.Address)
  32. {
  33. index--;
  34. }
  35. while (index < _items.Count)
  36. {
  37. if (_items[index].Equals(item))
  38. {
  39. _items.RemoveAt(index);
  40. return true;
  41. }
  42. if (_items[index].Address > item.Address)
  43. {
  44. break;
  45. }
  46. index++;
  47. }
  48. }
  49. }
  50. return false;
  51. }
  52. public T FindFirstOverlap(T item)
  53. {
  54. return FindFirstOverlap(item.Address, item.Size);
  55. }
  56. public T FindFirstOverlap(ulong address, ulong size)
  57. {
  58. lock (_items)
  59. {
  60. int index = BinarySearch(address, size);
  61. if (index < 0)
  62. {
  63. return default(T);
  64. }
  65. return _items[index];
  66. }
  67. }
  68. public T[] FindOverlaps(T item)
  69. {
  70. return FindOverlaps(item.Address, item.Size);
  71. }
  72. public T[] FindOverlaps(ulong address, ulong size)
  73. {
  74. List<T> overlapsList = new List<T>();
  75. ulong endAddress = address + size;
  76. lock (_items)
  77. {
  78. foreach (T item in _items)
  79. {
  80. if (item.Address >= endAddress)
  81. {
  82. break;
  83. }
  84. if (item.OverlapsWith(address, size))
  85. {
  86. overlapsList.Add(item);
  87. }
  88. }
  89. }
  90. return overlapsList.ToArray();
  91. }
  92. public T[] FindOverlaps(ulong address)
  93. {
  94. List<T> overlapsList = new List<T>();
  95. lock (_items)
  96. {
  97. int index = BinarySearch(address);
  98. if (index >= 0)
  99. {
  100. while (index > 0 && _items[index - 1].Address == address)
  101. {
  102. index--;
  103. }
  104. while (index < _items.Count)
  105. {
  106. T overlap = _items[index++];
  107. if (overlap.Address != address)
  108. {
  109. break;
  110. }
  111. overlapsList.Add(overlap);
  112. }
  113. }
  114. }
  115. return overlapsList.ToArray();
  116. }
  117. private int BinarySearch(ulong address)
  118. {
  119. int left = 0;
  120. int right = _items.Count - 1;
  121. while (left <= right)
  122. {
  123. int range = right - left;
  124. int middle = left + (range >> 1);
  125. T item = _items[middle];
  126. if (item.Address == address)
  127. {
  128. return middle;
  129. }
  130. if (address < item.Address)
  131. {
  132. right = middle - 1;
  133. }
  134. else
  135. {
  136. left = middle + 1;
  137. }
  138. }
  139. return ~left;
  140. }
  141. private int BinarySearch(ulong address, ulong size)
  142. {
  143. int left = 0;
  144. int right = _items.Count - 1;
  145. while (left <= right)
  146. {
  147. int range = right - left;
  148. int middle = left + (range >> 1);
  149. T item = _items[middle];
  150. if (item.OverlapsWith(address, size))
  151. {
  152. return middle;
  153. }
  154. if (address < item.Address)
  155. {
  156. right = middle - 1;
  157. }
  158. else
  159. {
  160. left = middle + 1;
  161. }
  162. }
  163. return ~left;
  164. }
  165. }
  166. }