MemoryManagementWindows.cs 4.5 KB

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