MemoryManagementWindows.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Ryujinx.Memory.WindowsShared;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Versioning;
  5. namespace Ryujinx.Memory
  6. {
  7. [SupportedOSPlatform("windows")]
  8. static class MemoryManagementWindows
  9. {
  10. public const int PageSize = 0x1000;
  11. private static readonly PlaceholderManager _placeholders = new();
  12. public static IntPtr Allocate(IntPtr size)
  13. {
  14. return AllocateInternal(size, AllocationType.Reserve | AllocationType.Commit);
  15. }
  16. public static IntPtr Reserve(IntPtr size, bool viewCompatible)
  17. {
  18. if (viewCompatible)
  19. {
  20. IntPtr baseAddress = AllocateInternal2(size, AllocationType.Reserve | AllocationType.ReservePlaceholder);
  21. _placeholders.ReserveRange((ulong)baseAddress, (ulong)size);
  22. return baseAddress;
  23. }
  24. return AllocateInternal(size, AllocationType.Reserve);
  25. }
  26. private static IntPtr AllocateInternal(IntPtr size, AllocationType flags = 0)
  27. {
  28. IntPtr ptr = WindowsApi.VirtualAlloc(IntPtr.Zero, size, flags, MemoryProtection.ReadWrite);
  29. if (ptr == IntPtr.Zero)
  30. {
  31. throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
  32. }
  33. return ptr;
  34. }
  35. private static IntPtr AllocateInternal2(IntPtr size, AllocationType flags = 0)
  36. {
  37. IntPtr ptr = WindowsApi.VirtualAlloc2(WindowsApi.CurrentProcessHandle, IntPtr.Zero, size, flags, MemoryProtection.NoAccess, IntPtr.Zero, 0);
  38. if (ptr == IntPtr.Zero)
  39. {
  40. throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
  41. }
  42. return ptr;
  43. }
  44. public static void Commit(IntPtr location, IntPtr size)
  45. {
  46. if (WindowsApi.VirtualAlloc(location, size, AllocationType.Commit, MemoryProtection.ReadWrite) == IntPtr.Zero)
  47. {
  48. throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
  49. }
  50. }
  51. public static void Decommit(IntPtr location, IntPtr size)
  52. {
  53. if (!WindowsApi.VirtualFree(location, size, AllocationType.Decommit))
  54. {
  55. throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
  56. }
  57. }
  58. public static void MapView(IntPtr sharedMemory, ulong srcOffset, IntPtr location, IntPtr size, MemoryBlock owner)
  59. {
  60. _placeholders.MapView(sharedMemory, srcOffset, location, size, owner);
  61. }
  62. public static void UnmapView(IntPtr sharedMemory, IntPtr location, IntPtr size, MemoryBlock owner)
  63. {
  64. _placeholders.UnmapView(sharedMemory, location, size, owner);
  65. }
  66. public static bool Reprotect(IntPtr address, IntPtr size, MemoryPermission permission, bool forView)
  67. {
  68. if (forView)
  69. {
  70. return _placeholders.ReprotectView(address, size, permission);
  71. }
  72. else
  73. {
  74. return WindowsApi.VirtualProtect(address, size, WindowsApi.GetProtection(permission), out _);
  75. }
  76. }
  77. public static bool Free(IntPtr address, IntPtr size)
  78. {
  79. _placeholders.UnreserveRange((ulong)address, (ulong)size);
  80. return WindowsApi.VirtualFree(address, IntPtr.Zero, AllocationType.Release);
  81. }
  82. public static IntPtr CreateSharedMemory(IntPtr size, bool reserve)
  83. {
  84. var prot = reserve ? FileMapProtection.SectionReserve : FileMapProtection.SectionCommit;
  85. IntPtr handle = WindowsApi.CreateFileMapping(
  86. WindowsApi.InvalidHandleValue,
  87. IntPtr.Zero,
  88. FileMapProtection.PageReadWrite | prot,
  89. (uint)(size.ToInt64() >> 32),
  90. (uint)size.ToInt64(),
  91. null);
  92. if (handle == IntPtr.Zero)
  93. {
  94. throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
  95. }
  96. return handle;
  97. }
  98. public static void DestroySharedMemory(IntPtr handle)
  99. {
  100. if (!WindowsApi.CloseHandle(handle))
  101. {
  102. throw new ArgumentException("Invalid handle.", nameof(handle));
  103. }
  104. }
  105. public static IntPtr MapSharedMemory(IntPtr handle)
  106. {
  107. IntPtr ptr = WindowsApi.MapViewOfFile(handle, 4 | 2, 0, 0, IntPtr.Zero);
  108. if (ptr == IntPtr.Zero)
  109. {
  110. throw new SystemException(Marshal.GetLastPInvokeErrorMessage());
  111. }
  112. return ptr;
  113. }
  114. public static void UnmapSharedMemory(IntPtr address)
  115. {
  116. if (!WindowsApi.UnmapViewOfFile(address))
  117. {
  118. throw new ArgumentException("Invalid address.", nameof(address));
  119. }
  120. }
  121. }
  122. }