MultiRegionHandle.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Ryujinx.Memory.Tracking
  4. {
  5. /// <summary>
  6. /// A region handle that tracks a large region using many smaller handles, to provide
  7. /// granular tracking that can be used to track partial updates.
  8. /// </summary>
  9. public class MultiRegionHandle : IMultiRegionHandle
  10. {
  11. /// <summary>
  12. /// A list of region handles for each granularity sized chunk of the whole region.
  13. /// </summary>
  14. private readonly RegionHandle[] _handles;
  15. private readonly ulong Address;
  16. private readonly ulong Granularity;
  17. private readonly ulong Size;
  18. public bool Dirty { get; private set; } = true;
  19. internal MultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, IEnumerable<IRegionHandle> handles, ulong granularity)
  20. {
  21. _handles = new RegionHandle[size / granularity];
  22. Granularity = granularity;
  23. int i = 0;
  24. if (handles != null)
  25. {
  26. // Inherit from the handles we were given. Any gaps must be filled with new handles,
  27. // and old handles larger than our granularity must copy their state onto new granular handles and dispose.
  28. // It is assumed that the provided handles do not overlap, in order, are on page boundaries,
  29. // and don't extend past the requested range.
  30. foreach (RegionHandle handle in handles)
  31. {
  32. int startIndex = (int)((handle.Address - address) / granularity);
  33. // Fill any gap left before this handle.
  34. while (i < startIndex)
  35. {
  36. RegionHandle fillHandle = tracking.BeginTracking(address + (ulong)i * granularity, granularity);
  37. fillHandle.Parent = this;
  38. _handles[i++] = fillHandle;
  39. }
  40. if (handle.Size == granularity)
  41. {
  42. handle.Parent = this;
  43. _handles[i++] = handle;
  44. }
  45. else
  46. {
  47. int endIndex = (int)((handle.EndAddress - address) / granularity);
  48. while (i < endIndex)
  49. {
  50. RegionHandle splitHandle = tracking.BeginTracking(address + (ulong)i * granularity, granularity);
  51. splitHandle.Parent = this;
  52. splitHandle.Reprotect(handle.Dirty);
  53. RegionSignal signal = handle.PreAction;
  54. if (signal != null)
  55. {
  56. splitHandle.RegisterAction(signal);
  57. }
  58. _handles[i++] = splitHandle;
  59. }
  60. handle.Dispose();
  61. }
  62. }
  63. }
  64. // Fill any remaining space with new handles.
  65. while (i < _handles.Length)
  66. {
  67. RegionHandle handle = tracking.BeginTracking(address + (ulong)i * granularity, granularity);
  68. handle.Parent = this;
  69. _handles[i++] = handle;
  70. }
  71. Address = address;
  72. Size = size;
  73. }
  74. public void ForceDirty(ulong address, ulong size)
  75. {
  76. Dirty = true;
  77. int startHandle = (int)((address - Address) / Granularity);
  78. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  79. for (int i = startHandle; i <= lastHandle; i++)
  80. {
  81. _handles[i].SequenceNumber--;
  82. _handles[i].ForceDirty();
  83. }
  84. }
  85. public IEnumerable<RegionHandle> GetHandles()
  86. {
  87. return _handles;
  88. }
  89. public void SignalWrite()
  90. {
  91. Dirty = true;
  92. }
  93. public void QueryModified(Action<ulong, ulong> modifiedAction)
  94. {
  95. if (!Dirty)
  96. {
  97. return;
  98. }
  99. Dirty = false;
  100. QueryModified(Address, Size, modifiedAction);
  101. }
  102. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction)
  103. {
  104. int startHandle = (int)((address - Address) / Granularity);
  105. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  106. ulong rgStart = _handles[startHandle].Address;
  107. ulong rgSize = 0;
  108. for (int i = startHandle; i <= lastHandle; i++)
  109. {
  110. RegionHandle handle = _handles[i];
  111. if (handle.Dirty)
  112. {
  113. rgSize += handle.Size;
  114. handle.Reprotect();
  115. }
  116. else
  117. {
  118. // Submit the region scanned so far as dirty
  119. if (rgSize != 0)
  120. {
  121. modifiedAction(rgStart, rgSize);
  122. rgSize = 0;
  123. }
  124. rgStart = handle.EndAddress;
  125. }
  126. }
  127. if (rgSize != 0)
  128. {
  129. modifiedAction(rgStart, rgSize);
  130. }
  131. }
  132. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction, int sequenceNumber)
  133. {
  134. int startHandle = (int)((address - Address) / Granularity);
  135. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  136. ulong rgStart = _handles[startHandle].Address;
  137. ulong rgSize = 0;
  138. for (int i = startHandle; i <= lastHandle; i++)
  139. {
  140. RegionHandle handle = _handles[i];
  141. if (sequenceNumber != handle.SequenceNumber && handle.DirtyOrVolatile())
  142. {
  143. rgSize += handle.Size;
  144. handle.Reprotect();
  145. }
  146. else
  147. {
  148. // Submit the region scanned so far as dirty
  149. if (rgSize != 0)
  150. {
  151. modifiedAction(rgStart, rgSize);
  152. rgSize = 0;
  153. }
  154. rgStart = handle.EndAddress;
  155. }
  156. handle.SequenceNumber = sequenceNumber;
  157. }
  158. if (rgSize != 0)
  159. {
  160. modifiedAction(rgStart, rgSize);
  161. }
  162. }
  163. public void RegisterAction(ulong address, ulong size, RegionSignal action)
  164. {
  165. int startHandle = (int)((address - Address) / Granularity);
  166. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  167. for (int i = startHandle; i <= lastHandle; i++)
  168. {
  169. _handles[i].RegisterAction(action);
  170. }
  171. }
  172. public void RegisterPreciseAction(ulong address, ulong size, PreciseRegionSignal action)
  173. {
  174. int startHandle = (int)((address - Address) / Granularity);
  175. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  176. for (int i = startHandle; i <= lastHandle; i++)
  177. {
  178. _handles[i].RegisterPreciseAction(action);
  179. }
  180. }
  181. public void Dispose()
  182. {
  183. foreach (var handle in _handles)
  184. {
  185. handle.Dispose();
  186. }
  187. }
  188. }
  189. }