PlaceholderManager.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. using System;
  2. using System.Diagnostics;
  3. using System.Runtime.Versioning;
  4. using System.Threading;
  5. namespace Ryujinx.Memory.WindowsShared
  6. {
  7. /// <summary>
  8. /// Windows memory placeholder manager.
  9. /// </summary>
  10. [SupportedOSPlatform("windows")]
  11. class PlaceholderManager
  12. {
  13. private const ulong MinimumPageSize = 0x1000;
  14. [ThreadStatic]
  15. private static int _threadLocalPartialUnmapsCount;
  16. private readonly IntervalTree<ulong, ulong> _mappings;
  17. private readonly IntervalTree<ulong, MemoryPermission> _protections;
  18. private readonly ReaderWriterLock _partialUnmapLock;
  19. private int _partialUnmapsCount;
  20. /// <summary>
  21. /// Creates a new instance of the Windows memory placeholder manager.
  22. /// </summary>
  23. public PlaceholderManager()
  24. {
  25. _mappings = new IntervalTree<ulong, ulong>();
  26. _protections = new IntervalTree<ulong, MemoryPermission>();
  27. _partialUnmapLock = new ReaderWriterLock();
  28. }
  29. /// <summary>
  30. /// Reserves a range of the address space to be later mapped as shared memory views.
  31. /// </summary>
  32. /// <param name="address">Start address of the region to reserve</param>
  33. /// <param name="size">Size in bytes of the region to reserve</param>
  34. public void ReserveRange(ulong address, ulong size)
  35. {
  36. lock (_mappings)
  37. {
  38. _mappings.Add(address, address + size, ulong.MaxValue);
  39. }
  40. }
  41. /// <summary>
  42. /// Unreserves a range of memory that has been previously reserved with <see cref="ReserveRange"/>.
  43. /// </summary>
  44. /// <param name="address">Start address of the region to unreserve</param>
  45. /// <param name="size">Size in bytes of the region to unreserve</param>
  46. /// <exception cref="WindowsApiException">Thrown when the Windows API returns an error unreserving the memory</exception>
  47. public void UnreserveRange(ulong address, ulong size)
  48. {
  49. ulong endAddress = address + size;
  50. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  51. int count;
  52. lock (_mappings)
  53. {
  54. count = _mappings.Get(address, endAddress, ref overlaps);
  55. for (int index = 0; index < count; index++)
  56. {
  57. var overlap = overlaps[index];
  58. if (IsMapped(overlap.Value))
  59. {
  60. if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
  61. {
  62. throw new WindowsApiException("UnmapViewOfFile2");
  63. }
  64. }
  65. _mappings.Remove(overlap);
  66. }
  67. }
  68. if (count > 1)
  69. {
  70. CheckFreeResult(WindowsApi.VirtualFree(
  71. (IntPtr)address,
  72. (IntPtr)size,
  73. AllocationType.Release | AllocationType.CoalescePlaceholders));
  74. }
  75. RemoveProtection(address, size);
  76. }
  77. /// <summary>
  78. /// Maps a shared memory view on a previously reserved memory region.
  79. /// </summary>
  80. /// <param name="sharedMemory">Shared memory that will be the backing storage for the view</param>
  81. /// <param name="srcOffset">Offset in the shared memory to map</param>
  82. /// <param name="location">Address to map the view into</param>
  83. /// <param name="size">Size of the view in bytes</param>
  84. /// <param name="owner">Memory block that owns the mapping</param>
  85. public void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, MemoryBlock owner)
  86. {
  87. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  88. try
  89. {
  90. UnmapViewInternal(sharedMemory, location, size, owner);
  91. MapViewInternal(sharedMemory, srcOffset, location, size);
  92. }
  93. finally
  94. {
  95. _partialUnmapLock.ReleaseReaderLock();
  96. }
  97. }
  98. /// <summary>
  99. /// Maps a shared memory view on a previously reserved memory region.
  100. /// </summary>
  101. /// <param name="sharedMemory">Shared memory that will be the backing storage for the view</param>
  102. /// <param name="srcOffset">Offset in the shared memory to map</param>
  103. /// <param name="location">Address to map the view into</param>
  104. /// <param name="size">Size of the view in bytes</param>
  105. /// <exception cref="WindowsApiException">Thrown when the Windows API returns an error mapping the memory</exception>
  106. private void MapViewInternal(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size)
  107. {
  108. SplitForMap((ulong)location, (ulong)size, srcOffset);
  109. var ptr = WindowsApi.MapViewOfFile3(
  110. sharedMemory,
  111. WindowsApi.CurrentProcessHandle,
  112. location,
  113. srcOffset,
  114. size,
  115. 0x4000,
  116. MemoryProtection.ReadWrite,
  117. IntPtr.Zero,
  118. 0);
  119. if (ptr == IntPtr.Zero)
  120. {
  121. throw new WindowsApiException("MapViewOfFile3");
  122. }
  123. }
  124. /// <summary>
  125. /// Splits a larger placeholder, slicing at the start and end address, for a new memory mapping.
  126. /// </summary>
  127. /// <param name="address">Address to split</param>
  128. /// <param name="size">Size of the new region</param>
  129. /// <param name="backingOffset">Offset in the shared memory that will be mapped</param>
  130. private void SplitForMap(ulong address, ulong size, ulong backingOffset)
  131. {
  132. ulong endAddress = address + size;
  133. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  134. lock (_mappings)
  135. {
  136. int count = _mappings.Get(address, endAddress, ref overlaps);
  137. Debug.Assert(count == 1);
  138. Debug.Assert(!IsMapped(overlaps[0].Value));
  139. var overlap = overlaps[0];
  140. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  141. ulong overlapStart = overlap.Start;
  142. ulong overlapEnd = overlap.End;
  143. ulong overlapValue = overlap.Value;
  144. _mappings.Remove(overlap);
  145. bool overlapStartsBefore = overlapStart < address;
  146. bool overlapEndsAfter = overlapEnd > endAddress;
  147. if (overlapStartsBefore && overlapEndsAfter)
  148. {
  149. CheckFreeResult(WindowsApi.VirtualFree(
  150. (IntPtr)address,
  151. (IntPtr)size,
  152. AllocationType.Release | AllocationType.PreservePlaceholder));
  153. _mappings.Add(overlapStart, address, overlapValue);
  154. _mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, endAddress - overlapStart));
  155. }
  156. else if (overlapStartsBefore)
  157. {
  158. ulong overlappedSize = overlapEnd - address;
  159. CheckFreeResult(WindowsApi.VirtualFree(
  160. (IntPtr)address,
  161. (IntPtr)overlappedSize,
  162. AllocationType.Release | AllocationType.PreservePlaceholder));
  163. _mappings.Add(overlapStart, address, overlapValue);
  164. }
  165. else if (overlapEndsAfter)
  166. {
  167. ulong overlappedSize = endAddress - overlapStart;
  168. CheckFreeResult(WindowsApi.VirtualFree(
  169. (IntPtr)overlapStart,
  170. (IntPtr)overlappedSize,
  171. AllocationType.Release | AllocationType.PreservePlaceholder));
  172. _mappings.Add(endAddress, overlapEnd, AddBackingOffset(overlapValue, overlappedSize));
  173. }
  174. _mappings.Add(address, endAddress, backingOffset);
  175. }
  176. }
  177. /// <summary>
  178. /// Unmaps a view that has been previously mapped with <see cref="MapView"/>.
  179. /// </summary>
  180. /// <remarks>
  181. /// For "partial unmaps" (when not the entire mapped range is being unmapped), it might be
  182. /// necessary to unmap the whole range and then remap the sub-ranges that should remain mapped.
  183. /// </remarks>
  184. /// <param name="sharedMemory">Shared memory that the view being unmapped belongs to</param>
  185. /// <param name="location">Address to unmap</param>
  186. /// <param name="size">Size of the region to unmap in bytes</param>
  187. /// <param name="owner">Memory block that owns the mapping</param>
  188. public void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner)
  189. {
  190. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  191. try
  192. {
  193. UnmapViewInternal(sharedMemory, location, size, owner);
  194. }
  195. finally
  196. {
  197. _partialUnmapLock.ReleaseReaderLock();
  198. }
  199. }
  200. /// <summary>
  201. /// Unmaps a view that has been previously mapped with <see cref="MapView"/>.
  202. /// </summary>
  203. /// <remarks>
  204. /// For "partial unmaps" (when not the entire mapped range is being unmapped), it might be
  205. /// necessary to unmap the whole range and then remap the sub-ranges that should remain mapped.
  206. /// </remarks>
  207. /// <param name="sharedMemory">Shared memory that the view being unmapped belongs to</param>
  208. /// <param name="location">Address to unmap</param>
  209. /// <param name="size">Size of the region to unmap in bytes</param>
  210. /// <param name="owner">Memory block that owns the mapping</param>
  211. /// <exception cref="WindowsApiException">Thrown when the Windows API returns an error unmapping or remapping the memory</exception>
  212. private void UnmapViewInternal(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner)
  213. {
  214. ulong startAddress = (ulong)location;
  215. ulong unmapSize = (ulong)size;
  216. ulong endAddress = startAddress + unmapSize;
  217. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  218. int count;
  219. lock (_mappings)
  220. {
  221. count = _mappings.Get(startAddress, endAddress, ref overlaps);
  222. }
  223. for (int index = 0; index < count; index++)
  224. {
  225. var overlap = overlaps[index];
  226. if (IsMapped(overlap.Value))
  227. {
  228. if (!WindowsApi.UnmapViewOfFile2(WindowsApi.CurrentProcessHandle, (IntPtr)overlap.Start, 2))
  229. {
  230. throw new WindowsApiException("UnmapViewOfFile2");
  231. }
  232. // Tree operations might modify the node start/end values, so save a copy before we modify the tree.
  233. ulong overlapStart = overlap.Start;
  234. ulong overlapEnd = overlap.End;
  235. ulong overlapValue = overlap.Value;
  236. lock (_mappings)
  237. {
  238. _mappings.Remove(overlap);
  239. _mappings.Add(overlapStart, overlapEnd, ulong.MaxValue);
  240. }
  241. bool overlapStartsBefore = overlapStart < startAddress;
  242. bool overlapEndsAfter = overlapEnd > endAddress;
  243. if (overlapStartsBefore || overlapEndsAfter)
  244. {
  245. // If the overlap extends beyond the region we are unmapping,
  246. // then we need to re-map the regions that are supposed to remain mapped.
  247. // This is necessary because Windows does not support partial view unmaps.
  248. // That is, you can only fully unmap a view that was previously mapped, you can't just unmap a chunck of it.
  249. LockCookie lockCookie = _partialUnmapLock.UpgradeToWriterLock(Timeout.Infinite);
  250. _partialUnmapsCount++;
  251. if (overlapStartsBefore)
  252. {
  253. ulong remapSize = startAddress - overlapStart;
  254. MapViewInternal(sharedMemory, overlapValue, (IntPtr)overlapStart, (IntPtr)remapSize);
  255. RestoreRangeProtection(overlapStart, remapSize);
  256. }
  257. if (overlapEndsAfter)
  258. {
  259. ulong overlappedSize = endAddress - overlapStart;
  260. ulong remapBackingOffset = overlapValue + overlappedSize;
  261. ulong remapAddress = overlapStart + overlappedSize;
  262. ulong remapSize = overlapEnd - endAddress;
  263. MapViewInternal(sharedMemory, remapBackingOffset, (IntPtr)remapAddress, (IntPtr)remapSize);
  264. RestoreRangeProtection(remapAddress, remapSize);
  265. }
  266. _partialUnmapLock.DowngradeFromWriterLock(ref lockCookie);
  267. }
  268. }
  269. }
  270. CoalesceForUnmap(startAddress, unmapSize, owner);
  271. RemoveProtection(startAddress, unmapSize);
  272. }
  273. /// <summary>
  274. /// Coalesces adjacent placeholders after unmap.
  275. /// </summary>
  276. /// <param name="address">Address of the region that was unmapped</param>
  277. /// <param name="size">Size of the region that was unmapped in bytes</param>
  278. /// <param name="owner">Memory block that owns the mapping</param>
  279. private void CoalesceForUnmap(ulong address, ulong size, MemoryBlock owner)
  280. {
  281. ulong endAddress = address + size;
  282. ulong blockAddress = (ulong)owner.Pointer;
  283. ulong blockEnd = blockAddress + owner.Size;
  284. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  285. int unmappedCount = 0;
  286. lock (_mappings)
  287. {
  288. int count = _mappings.Get(
  289. Math.Max(address - MinimumPageSize, blockAddress),
  290. Math.Min(endAddress + MinimumPageSize, blockEnd), ref overlaps);
  291. if (count < 2)
  292. {
  293. // Nothing to coalesce if we only have 1 or no overlaps.
  294. return;
  295. }
  296. for (int index = 0; index < count; index++)
  297. {
  298. var overlap = overlaps[index];
  299. if (!IsMapped(overlap.Value))
  300. {
  301. if (address > overlap.Start)
  302. {
  303. address = overlap.Start;
  304. }
  305. if (endAddress < overlap.End)
  306. {
  307. endAddress = overlap.End;
  308. }
  309. _mappings.Remove(overlap);
  310. unmappedCount++;
  311. }
  312. }
  313. _mappings.Add(address, endAddress, ulong.MaxValue);
  314. }
  315. if (unmappedCount > 1)
  316. {
  317. size = endAddress - address;
  318. CheckFreeResult(WindowsApi.VirtualFree(
  319. (IntPtr)address,
  320. (IntPtr)size,
  321. AllocationType.Release | AllocationType.CoalescePlaceholders));
  322. }
  323. }
  324. /// <summary>
  325. /// Reprotects a region of memory that has been mapped.
  326. /// </summary>
  327. /// <param name="address">Address of the region to reprotect</param>
  328. /// <param name="size">Size of the region to reprotect in bytes</param>
  329. /// <param name="permission">New permissions</param>
  330. /// <returns>True if the reprotection was successful, false otherwise</returns>
  331. public bool ReprotectView(IntPtr address, IntPtr size, MemoryPermission permission)
  332. {
  333. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  334. try
  335. {
  336. return ReprotectViewInternal(address, size, permission, false);
  337. }
  338. finally
  339. {
  340. _partialUnmapLock.ReleaseReaderLock();
  341. }
  342. }
  343. /// <summary>
  344. /// Reprotects a region of memory that has been mapped.
  345. /// </summary>
  346. /// <param name="address">Address of the region to reprotect</param>
  347. /// <param name="size">Size of the region to reprotect in bytes</param>
  348. /// <param name="permission">New permissions</param>
  349. /// <param name="throwOnError">Throw an exception instead of returning an error if the operation fails</param>
  350. /// <returns>True if the reprotection was successful or if <paramref name="throwOnError"/> is true, false otherwise</returns>
  351. /// <exception cref="WindowsApiException">If <paramref name="throwOnError"/> is true, it is thrown when the Windows API returns an error reprotecting the memory</exception>
  352. private bool ReprotectViewInternal(IntPtr address, IntPtr size, MemoryPermission permission, bool throwOnError)
  353. {
  354. ulong reprotectAddress = (ulong)address;
  355. ulong reprotectSize = (ulong)size;
  356. ulong endAddress = reprotectAddress + reprotectSize;
  357. var overlaps = Array.Empty<IntervalTreeNode<ulong, ulong>>();
  358. int count;
  359. lock (_mappings)
  360. {
  361. count = _mappings.Get(reprotectAddress, endAddress, ref overlaps);
  362. }
  363. bool success = true;
  364. for (int index = 0; index < count; index++)
  365. {
  366. var overlap = overlaps[index];
  367. ulong mappedAddress = overlap.Start;
  368. ulong mappedSize = overlap.End - overlap.Start;
  369. if (mappedAddress < reprotectAddress)
  370. {
  371. ulong delta = reprotectAddress - mappedAddress;
  372. mappedAddress = reprotectAddress;
  373. mappedSize -= delta;
  374. }
  375. ulong mappedEndAddress = mappedAddress + mappedSize;
  376. if (mappedEndAddress > endAddress)
  377. {
  378. ulong delta = mappedEndAddress - endAddress;
  379. mappedSize -= delta;
  380. }
  381. if (!WindowsApi.VirtualProtect((IntPtr)mappedAddress, (IntPtr)mappedSize, WindowsApi.GetProtection(permission), out _))
  382. {
  383. if (throwOnError)
  384. {
  385. throw new WindowsApiException("VirtualProtect");
  386. }
  387. success = false;
  388. }
  389. // We only keep track of "non-standard" protections,
  390. // that is, everything that is not just RW (which is the default when views are mapped).
  391. if (permission == MemoryPermission.ReadAndWrite)
  392. {
  393. RemoveProtection(mappedAddress, mappedSize);
  394. }
  395. else
  396. {
  397. AddProtection(mappedAddress, mappedSize, permission);
  398. }
  399. }
  400. return success;
  401. }
  402. /// <summary>
  403. /// Checks the result of a VirtualFree operation, throwing if needed.
  404. /// </summary>
  405. /// <param name="success">Operation result</param>
  406. /// <exception cref="WindowsApiException">Thrown if <paramref name="success"/> is false</exception>
  407. private static void CheckFreeResult(bool success)
  408. {
  409. if (!success)
  410. {
  411. throw new WindowsApiException("VirtualFree");
  412. }
  413. }
  414. /// <summary>
  415. /// Adds an offset to a backing offset. This will do nothing if the backing offset is the special "unmapped" value.
  416. /// </summary>
  417. /// <param name="backingOffset">Backing offset</param>
  418. /// <param name="offset">Offset to be added</param>
  419. /// <returns>Added offset or just <paramref name="backingOffset"/> if the region is unmapped</returns>
  420. private static ulong AddBackingOffset(ulong backingOffset, ulong offset)
  421. {
  422. if (backingOffset == ulong.MaxValue)
  423. {
  424. return backingOffset;
  425. }
  426. return backingOffset + offset;
  427. }
  428. /// <summary>
  429. /// Checks if a region is unmapped.
  430. /// </summary>
  431. /// <param name="backingOffset">Backing offset to check</param>
  432. /// <returns>True if the backing offset is the special "unmapped" value, false otherwise</returns>
  433. private static bool IsMapped(ulong backingOffset)
  434. {
  435. return backingOffset != ulong.MaxValue;
  436. }
  437. /// <summary>
  438. /// Adds a protection to the list of protections.
  439. /// </summary>
  440. /// <param name="address">Address of the protected region</param>
  441. /// <param name="size">Size of the protected region in bytes</param>
  442. /// <param name="permission">Memory permissions of the region</param>
  443. private void AddProtection(ulong address, ulong size, MemoryPermission permission)
  444. {
  445. ulong endAddress = address + size;
  446. var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
  447. int count;
  448. lock (_protections)
  449. {
  450. count = _protections.Get(address, endAddress, ref overlaps);
  451. if (count == 1 &&
  452. overlaps[0].Start <= address &&
  453. overlaps[0].End >= endAddress &&
  454. overlaps[0].Value == permission)
  455. {
  456. return;
  457. }
  458. ulong startAddress = address;
  459. for (int index = 0; index < count; index++)
  460. {
  461. var protection = overlaps[index];
  462. ulong protAddress = protection.Start;
  463. ulong protEndAddress = protection.End;
  464. MemoryPermission protPermission = protection.Value;
  465. _protections.Remove(protection);
  466. if (protection.Value == permission)
  467. {
  468. if (startAddress > protAddress)
  469. {
  470. startAddress = protAddress;
  471. }
  472. if (endAddress < protEndAddress)
  473. {
  474. endAddress = protEndAddress;
  475. }
  476. }
  477. else
  478. {
  479. if (startAddress > protAddress)
  480. {
  481. _protections.Add(protAddress, startAddress, protPermission);
  482. }
  483. if (endAddress < protEndAddress)
  484. {
  485. _protections.Add(endAddress, protEndAddress, protPermission);
  486. }
  487. }
  488. }
  489. _protections.Add(startAddress, endAddress, permission);
  490. }
  491. }
  492. /// <summary>
  493. /// Removes protection from the list of protections.
  494. /// </summary>
  495. /// <param name="address">Address of the protected region</param>
  496. /// <param name="size">Size of the protected region in bytes</param>
  497. private void RemoveProtection(ulong address, ulong size)
  498. {
  499. ulong endAddress = address + size;
  500. var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
  501. int count;
  502. lock (_protections)
  503. {
  504. count = _protections.Get(address, endAddress, ref overlaps);
  505. for (int index = 0; index < count; index++)
  506. {
  507. var protection = overlaps[index];
  508. ulong protAddress = protection.Start;
  509. ulong protEndAddress = protection.End;
  510. MemoryPermission protPermission = protection.Value;
  511. _protections.Remove(protection);
  512. if (address > protAddress)
  513. {
  514. _protections.Add(protAddress, address, protPermission);
  515. }
  516. if (endAddress < protEndAddress)
  517. {
  518. _protections.Add(endAddress, protEndAddress, protPermission);
  519. }
  520. }
  521. }
  522. }
  523. /// <summary>
  524. /// Restores the protection of a given memory region that was remapped, using the protections list.
  525. /// </summary>
  526. /// <param name="address">Address of the remapped region</param>
  527. /// <param name="size">Size of the remapped region in bytes</param>
  528. private void RestoreRangeProtection(ulong address, ulong size)
  529. {
  530. ulong endAddress = address + size;
  531. var overlaps = Array.Empty<IntervalTreeNode<ulong, MemoryPermission>>();
  532. int count;
  533. lock (_protections)
  534. {
  535. count = _protections.Get(address, endAddress, ref overlaps);
  536. }
  537. ulong startAddress = address;
  538. for (int index = 0; index < count; index++)
  539. {
  540. var protection = overlaps[index];
  541. ulong protAddress = protection.Start;
  542. ulong protEndAddress = protection.End;
  543. if (protAddress < address)
  544. {
  545. protAddress = address;
  546. }
  547. if (protEndAddress > endAddress)
  548. {
  549. protEndAddress = endAddress;
  550. }
  551. ReprotectViewInternal((IntPtr)protAddress, (IntPtr)(protEndAddress - protAddress), protection.Value, true);
  552. }
  553. }
  554. /// <summary>
  555. /// Checks if an access violation handler should retry execution due to a fault caused by partial unmap.
  556. /// </summary>
  557. /// <remarks>
  558. /// Due to Windows limitations, <see cref="UnmapView"/> might need to unmap more memory than requested.
  559. /// The additional memory that was unmapped is later remapped, however this leaves a time gap where the
  560. /// memory might be accessed but is unmapped. Users of the API must compensate for that by catching the
  561. /// access violation and retrying if it happened between the unmap and remap operation.
  562. /// This method can be used to decide if retrying in such cases is necessary or not.
  563. /// </remarks>
  564. /// <returns>True if execution should be retried, false otherwise</returns>
  565. public bool RetryFromAccessViolation()
  566. {
  567. _partialUnmapLock.AcquireReaderLock(Timeout.Infinite);
  568. bool retry = _threadLocalPartialUnmapsCount != _partialUnmapsCount;
  569. if (retry)
  570. {
  571. _threadLocalPartialUnmapsCount = _partialUnmapsCount;
  572. }
  573. _partialUnmapLock.ReleaseReaderLock();
  574. return retry;
  575. }
  576. }
  577. }