diff options
Diffstat (limited to 'ChocolArm64/Memory/AMemory.cs')
| -rw-r--r-- | ChocolArm64/Memory/AMemory.cs | 63 |
1 files changed, 61 insertions, 2 deletions
diff --git a/ChocolArm64/Memory/AMemory.cs b/ChocolArm64/Memory/AMemory.cs index e7d46565..7e9a358a 100644 --- a/ChocolArm64/Memory/AMemory.cs +++ b/ChocolArm64/Memory/AMemory.cs @@ -54,7 +54,14 @@ namespace ChocolArm64.Memory ExAddrs = new HashSet<long>(); - Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + Ram = AMemoryWin32.Allocate((IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize); + } + else + { + Ram = Marshal.AllocHGlobal((IntPtr)AMemoryMgr.RamSize + AMemoryMgr.PageSize); + } RamPtr = (byte*)Ram; } @@ -141,6 +148,51 @@ namespace ChocolArm64.Memory } } + public long GetHostPageSize() + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return AMemoryMgr.PageSize; + } + + IntPtr MemAddress = new IntPtr(RamPtr); + IntPtr MemSize = new IntPtr(AMemoryMgr.RamSize); + + long PageSize = AMemoryWin32.IsRegionModified(MemAddress, MemSize, Reset: false); + + if (PageSize < 1) + { + throw new InvalidOperationException(); + } + + return PageSize; + } + + public bool IsRegionModified(long Position, long Size) + { + if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + return true; + } + + long EndPos = Position + Size; + + if ((ulong)EndPos < (ulong)Position) + { + return false; + } + + if ((ulong)EndPos > AMemoryMgr.RamSize) + { + return false; + } + + IntPtr MemAddress = new IntPtr(RamPtr + Position); + IntPtr MemSize = new IntPtr(Size); + + return AMemoryWin32.IsRegionModified(MemAddress, MemSize, Reset: true) != 0; + } + public sbyte ReadSByte(long Position) { return (sbyte)ReadByte(Position); @@ -640,7 +692,14 @@ namespace ChocolArm64.Memory { if (Ram != IntPtr.Zero) { - Marshal.FreeHGlobal(Ram); + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) + { + AMemoryWin32.Free(Ram); + } + else + { + Marshal.FreeHGlobal(Ram); + } Ram = IntPtr.Zero; } |
