ConcurrentRangeList.cs 4.8 KB

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