PlaceholderList.cs 11 KB

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