PlaceholderList.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. using Ryujinx.Memory.Range;
  2. using System;
  3. using System.Diagnostics;
  4. namespace Ryujinx.Memory.WindowsShared
  5. {
  6. /// <summary>
  7. /// A specialized list used for keeping track of Windows 10's memory placeholders.
  8. /// This is used to make splitting a large placeholder into equally small
  9. /// granular chunks much easier, while avoiding slowdown due to a large number of
  10. /// placeholders by coalescing adjacent granular placeholders after they are unused.
  11. /// </summary>
  12. class PlaceholderList
  13. {
  14. private class PlaceholderBlock : IRange
  15. {
  16. public ulong Address { get; }
  17. public ulong Size { get; private set; }
  18. public ulong EndAddress { get; private set; }
  19. public bool IsGranular { get; set; }
  20. public PlaceholderBlock(ulong id, ulong size, bool isGranular)
  21. {
  22. Address = id;
  23. Size = size;
  24. EndAddress = id + size;
  25. IsGranular = isGranular;
  26. }
  27. public bool OverlapsWith(ulong address, ulong size)
  28. {
  29. return Address < address + size && address < EndAddress;
  30. }
  31. public void ExtendTo(ulong end)
  32. {
  33. EndAddress = end;
  34. Size = end - Address;
  35. }
  36. }
  37. private RangeList<PlaceholderBlock> _placeholders;
  38. private PlaceholderBlock[] _foundBlocks = new PlaceholderBlock[32];
  39. /// <summary>
  40. /// Create a new list to manage placeholders.
  41. /// Note that a size is measured in granular placeholders.
  42. /// If the placeholder granularity is 65536 bytes, then a 65536 region will be covered by 1 placeholder granularity.
  43. /// </summary>
  44. /// <param name="size">Size measured in granular placeholders</param>
  45. public PlaceholderList(ulong size)
  46. {
  47. _placeholders = new RangeList<PlaceholderBlock>();
  48. _placeholders.Add(new PlaceholderBlock(0, size, false));
  49. }
  50. /// <summary>
  51. /// Ensure that the given range of placeholders is granular.
  52. /// </summary>
  53. /// <param name="id">Start of the range, measured in granular placeholders</param>
  54. /// <param name="size">Size of the range, measured in granular placeholders</param>
  55. /// <param name="splitPlaceholderCallback">Callback function to run when splitting placeholders, calls with (start, middle)</param>
  56. public void EnsurePlaceholders(ulong id, ulong size, Action<ulong, ulong> splitPlaceholderCallback)
  57. {
  58. // Search 1 before and after the placeholders, as we may need to expand/join granular regions surrounding the requested area.
  59. ulong endId = id + size;
  60. ulong searchStartId = id == 0 ? 0 : (id - 1);
  61. int blockCount = _placeholders.FindOverlapsNonOverlapping(searchStartId, (endId - searchStartId) + 1, ref _foundBlocks);
  62. PlaceholderBlock first = _foundBlocks[0];
  63. PlaceholderBlock last = _foundBlocks[blockCount - 1];
  64. bool overlapStart = first.EndAddress >= id && id != 0;
  65. bool overlapEnd = last.Address <= endId;
  66. for (int i = 0; i < blockCount; i++)
  67. {
  68. // Go through all non-granular blocks in the range and create placeholders.
  69. PlaceholderBlock block = _foundBlocks[i];
  70. if (block.Address <= id && block.EndAddress >= endId && block.IsGranular)
  71. {
  72. return; // The region we're searching for is already granular.
  73. }
  74. if (!block.IsGranular)
  75. {
  76. ulong placeholderStart = Math.Max(block.Address, id);
  77. ulong placeholderEnd = Math.Min(block.EndAddress - 1, endId);
  78. if (placeholderStart != block.Address && placeholderStart != block.EndAddress)
  79. {
  80. splitPlaceholderCallback(block.Address, placeholderStart - block.Address);
  81. }
  82. for (ulong j = placeholderStart; j < placeholderEnd; j++)
  83. {
  84. splitPlaceholderCallback(j, 1);
  85. }
  86. }
  87. if (!((block == first && overlapStart) || (block == last && overlapEnd)))
  88. {
  89. // Remove blocks that will be replaced
  90. _placeholders.Remove(block);
  91. }
  92. }
  93. if (overlapEnd)
  94. {
  95. if (!(first == last && overlapStart))
  96. {
  97. _placeholders.Remove(last);
  98. }
  99. if (last.IsGranular)
  100. {
  101. endId = last.EndAddress;
  102. }
  103. else if (last.EndAddress != endId)
  104. {
  105. _placeholders.Add(new PlaceholderBlock(endId, last.EndAddress - endId, false));
  106. }
  107. }
  108. if (overlapStart && first.IsGranular)
  109. {
  110. first.ExtendTo(endId);
  111. }
  112. else
  113. {
  114. if (overlapStart)
  115. {
  116. first.ExtendTo(id);
  117. }
  118. _placeholders.Add(new PlaceholderBlock(id, endId - id, true));
  119. }
  120. ValidateList();
  121. }
  122. /// <summary>
  123. /// Coalesces placeholders in a given region, as they are not being used.
  124. /// This assumes that the region only contains placeholders - all views and allocations must have been replaced with placeholders.
  125. /// </summary>
  126. /// <param name="id">Start of the range, measured in granular placeholders</param>
  127. /// <param name="size">Size of the range, measured in granular placeholders</param>
  128. /// <param name="coalescePlaceholderCallback">Callback function to run when coalescing two placeholders, calls with (start, end)</param>
  129. public void RemovePlaceholders(ulong id, ulong size, Action<ulong, ulong> coalescePlaceholderCallback)
  130. {
  131. ulong endId = id + size;
  132. int blockCount = _placeholders.FindOverlapsNonOverlapping(id, size, ref _foundBlocks);
  133. PlaceholderBlock first = _foundBlocks[0];
  134. PlaceholderBlock last = _foundBlocks[blockCount - 1];
  135. // All granular blocks must have non-granular blocks surrounding them, unless they start at 0.
  136. // We must extend the non-granular blocks into the granular ones. This does mean that we need to search twice.
  137. if (first.IsGranular || last.IsGranular)
  138. {
  139. ulong surroundStart = Math.Max(0, (first.IsGranular && first.Address != 0) ? first.Address - 1 : id);
  140. blockCount = _placeholders.FindOverlapsNonOverlapping(
  141. surroundStart,
  142. (last.IsGranular ? last.EndAddress + 1 : endId) - surroundStart,
  143. ref _foundBlocks);
  144. first = _foundBlocks[0];
  145. last = _foundBlocks[blockCount - 1];
  146. }
  147. if (first == last)
  148. {
  149. return; // Already coalesced.
  150. }
  151. PlaceholderBlock extendBlock = id == 0 ? null : first;
  152. bool newBlock = false;
  153. for (int i = extendBlock == null ? 0 : 1; i < blockCount; i++)
  154. {
  155. // Go through all granular blocks in the range and extend placeholders.
  156. PlaceholderBlock block = _foundBlocks[i];
  157. ulong blockEnd = block.EndAddress;
  158. ulong extendFrom;
  159. ulong extent = Math.Min(blockEnd, endId);
  160. if (block.Address < id && blockEnd > id)
  161. {
  162. block.ExtendTo(id);
  163. extendBlock = null;
  164. }
  165. else
  166. {
  167. _placeholders.Remove(block);
  168. }
  169. if (extendBlock == null)
  170. {
  171. extendFrom = id;
  172. extendBlock = new PlaceholderBlock(id, extent - id, false);
  173. _placeholders.Add(extendBlock);
  174. if (blockEnd > extent)
  175. {
  176. _placeholders.Add(new PlaceholderBlock(extent, blockEnd - extent, true));
  177. // Skip the next non-granular block, and extend from that into the granular block afterwards.
  178. // (assuming that one is still in the requested range)
  179. if (i + 1 < blockCount)
  180. {
  181. extendBlock = _foundBlocks[i + 1];
  182. }
  183. i++;
  184. }
  185. newBlock = true;
  186. }
  187. else
  188. {
  189. extendFrom = extendBlock.Address;
  190. extendBlock.ExtendTo(block.IsGranular ? extent : block.EndAddress);
  191. }
  192. if (block.IsGranular)
  193. {
  194. ulong placeholderStart = Math.Max(block.Address, id);
  195. ulong placeholderEnd = extent;
  196. if (newBlock)
  197. {
  198. placeholderStart++;
  199. newBlock = false;
  200. }
  201. for (ulong j = placeholderStart; j < placeholderEnd; j++)
  202. {
  203. coalescePlaceholderCallback(extendFrom, (j + 1) - extendFrom);
  204. }
  205. if (extent < block.EndAddress)
  206. {
  207. _placeholders.Add(new PlaceholderBlock(placeholderEnd, block.EndAddress - placeholderEnd, true));
  208. ValidateList();
  209. return;
  210. }
  211. }
  212. else
  213. {
  214. coalescePlaceholderCallback(extendFrom, block.EndAddress - extendFrom);
  215. }
  216. }
  217. ValidateList();
  218. }
  219. /// <summary>
  220. /// Ensure that the placeholder list is valid.
  221. /// A valid list should not have any gaps between the placeholders,
  222. /// and there may be no placehonders with the same IsGranular value next to each other.
  223. /// </summary>
  224. [Conditional("DEBUG")]
  225. private void ValidateList()
  226. {
  227. bool isGranular = false;
  228. bool first = true;
  229. ulong lastAddress = 0;
  230. foreach (var placeholder in _placeholders)
  231. {
  232. if (placeholder.Address != lastAddress)
  233. {
  234. throw new InvalidOperationException("Gap in placeholder list.");
  235. }
  236. if (isGranular == placeholder.IsGranular && !first)
  237. {
  238. throw new InvalidOperationException("Placeholder list not alternating.");
  239. }
  240. first = false;
  241. isGranular = placeholder.IsGranular;
  242. lastAddress = placeholder.EndAddress;
  243. }
  244. }
  245. }
  246. }