MultiRegionHandle.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. using System;
  2. namespace Ryujinx.Memory.Tracking
  3. {
  4. /// <summary>
  5. /// A region handle that tracks a large region using many smaller handles, to provide
  6. /// granular tracking that can be used to track partial updates.
  7. /// </summary>
  8. public class MultiRegionHandle : IMultiRegionHandle
  9. {
  10. /// <summary>
  11. /// A list of region handles for each granularity sized chunk of the whole region.
  12. /// </summary>
  13. private readonly RegionHandle[] _handles;
  14. private readonly ulong Address;
  15. private readonly ulong Granularity;
  16. private readonly ulong Size;
  17. public bool Dirty { get; private set; } = true;
  18. internal MultiRegionHandle(MemoryTracking tracking, ulong address, ulong size, ulong granularity)
  19. {
  20. _handles = new RegionHandle[size / granularity];
  21. Granularity = granularity;
  22. for (int i = 0; i < _handles.Length; i++)
  23. {
  24. RegionHandle handle = tracking.BeginTracking(address + (ulong)i * granularity, granularity);
  25. handle.Parent = this;
  26. _handles[i] = handle;
  27. }
  28. Address = address;
  29. Size = size;
  30. }
  31. public void ForceDirty(ulong address, ulong size)
  32. {
  33. Dirty = true;
  34. int startHandle = (int)((address - Address) / Granularity);
  35. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  36. for (int i = startHandle; i <= lastHandle; i++)
  37. {
  38. _handles[i].SequenceNumber--;
  39. _handles[i].ForceDirty();
  40. }
  41. }
  42. public void SignalWrite()
  43. {
  44. Dirty = true;
  45. }
  46. public void QueryModified(Action<ulong, ulong> modifiedAction)
  47. {
  48. if (!Dirty)
  49. {
  50. return;
  51. }
  52. Dirty = false;
  53. QueryModified(Address, Size, modifiedAction);
  54. }
  55. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction)
  56. {
  57. int startHandle = (int)((address - Address) / Granularity);
  58. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  59. ulong rgStart = _handles[startHandle].Address;
  60. ulong rgSize = 0;
  61. for (int i = startHandle; i <= lastHandle; i++)
  62. {
  63. RegionHandle handle = _handles[i];
  64. if (handle.Dirty)
  65. {
  66. rgSize += handle.Size;
  67. handle.Reprotect();
  68. }
  69. else
  70. {
  71. // Submit the region scanned so far as dirty
  72. if (rgSize != 0)
  73. {
  74. modifiedAction(rgStart, rgSize);
  75. rgSize = 0;
  76. }
  77. rgStart = handle.EndAddress;
  78. }
  79. }
  80. if (rgSize != 0)
  81. {
  82. modifiedAction(rgStart, rgSize);
  83. }
  84. }
  85. public void QueryModified(ulong address, ulong size, Action<ulong, ulong> modifiedAction, int sequenceNumber)
  86. {
  87. int startHandle = (int)((address - Address) / Granularity);
  88. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  89. ulong rgStart = _handles[startHandle].Address;
  90. ulong rgSize = 0;
  91. for (int i = startHandle; i <= lastHandle; i++)
  92. {
  93. RegionHandle handle = _handles[i];
  94. if (sequenceNumber != handle.SequenceNumber && handle.DirtyOrVolatile())
  95. {
  96. rgSize += handle.Size;
  97. handle.Reprotect();
  98. }
  99. else
  100. {
  101. // Submit the region scanned so far as dirty
  102. if (rgSize != 0)
  103. {
  104. modifiedAction(rgStart, rgSize);
  105. rgSize = 0;
  106. }
  107. rgStart = handle.EndAddress;
  108. }
  109. handle.SequenceNumber = sequenceNumber;
  110. }
  111. if (rgSize != 0)
  112. {
  113. modifiedAction(rgStart, rgSize);
  114. }
  115. }
  116. public void RegisterAction(ulong address, ulong size, RegionSignal action)
  117. {
  118. int startHandle = (int)((address - Address) / Granularity);
  119. int lastHandle = (int)((address + (size - 1) - Address) / Granularity);
  120. for (int i = startHandle; i <= lastHandle; i++)
  121. {
  122. _handles[i].RegisterAction(action);
  123. }
  124. }
  125. public void Dispose()
  126. {
  127. foreach (var handle in _handles)
  128. {
  129. handle.Dispose();
  130. }
  131. }
  132. }
  133. }