Move WriteBytes to AMemory, implement it with a Marshal copy like ReadBytes, fix regression on address range checking

This commit is contained in:
gdkchan 2018-06-09 13:05:41 -03:00
parent 91420a402c
commit 7f5a8effbb
15 changed files with 73 additions and 77 deletions

View file

@ -353,22 +353,6 @@ namespace ChocolArm64.Memory
return *((ulong*)(RamPtr + (uint)Position));
}
public byte[] ReadBytes(long Position, long Size)
{
if ((uint)Size > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(Size));
}
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
byte[] Data = new byte[Size];
Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
return Data;
}
public Vector128<float> ReadVector8Unchecked(long Position)
{
if (Sse2.IsSupported)
@ -433,6 +417,22 @@ namespace ChocolArm64.Memory
}
}
public byte[] ReadBytes(long Position, long Size)
{
if ((uint)Size > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(Size));
}
EnsureRangeIsValid(Position, Size, AMemoryPerm.Read);
byte[] Data = new byte[Size];
Marshal.Copy((IntPtr)(RamPtr + (uint)Position), Data, 0, (int)Size);
return Data;
}
public void WriteSByte(long Position, sbyte Value)
{
WriteByte(Position, (byte)Value);
@ -666,6 +666,27 @@ namespace ChocolArm64.Memory
}
}
public void WriteBytes(long Position, byte[] Data)
{
EnsureRangeIsValid(Position, (uint)Data.Length, AMemoryPerm.Write);
Marshal.Copy(Data, 0, (IntPtr)(RamPtr + (uint)Position), Data.Length);
}
private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
{
long EndPos = Position + Size;
Position &= ~AMemoryMgr.PageMask;
while ((ulong)Position < (ulong)EndPos)
{
EnsureAccessIsValid(Position, Perm);
Position += AMemoryMgr.PageSize;
}
}
private void EnsureAccessIsValid(long Position, AMemoryPerm Perm)
{
if (!Manager.IsMapped(Position))
@ -679,18 +700,6 @@ namespace ChocolArm64.Memory
}
}
private void EnsureRangeIsValid(long Position, long Size, AMemoryPerm Perm)
{
long EndPos = Position + Size;
while ((ulong)Position < (ulong)EndPos)
{
EnsureAccessIsValid(Position, Perm);
Position += AMemoryMgr.PageSize;
}
}
public void Dispose()
{
Dispose(true);