all that stylecopp shizz and it doesn't have a line length limit =/

This commit is contained in:
nattthebear 2020-05-25 08:06:01 -04:00
parent f37a51ef27
commit 4ec0753f52
4 changed files with 53 additions and 29 deletions

View File

@ -10,14 +10,14 @@ namespace BizHawk.BizInvoke
/// <summary> /// <summary>
/// Map in the memory area at the predetermined address /// Map in the memory area at the predetermined address
/// </summary> /// </summary>
void PalActivate(); void Activate();
/// <summary> /// <summary>
/// Unmap the memory area /// Unmap the memory area
/// </summary> /// </summary>
void PalDeactivate(); void Deactivate();
/// <summary> /// <summary>
/// Change protection on some addresses, guaranteed to be page aligned and in the memory area /// Change protection on some addresses, guaranteed to be page aligned and in the memory area
/// </summary> /// </summary>
void PalProtect(ulong start, ulong size, MemoryBlock.Protection prot); void Protect(ulong start, ulong size, MemoryBlock.Protection prot);
} }
} }

View File

@ -57,8 +57,13 @@ namespace BizHawk.BizInvoke
/// <summary>get a start address for a page index within the block</summary> /// <summary>get a start address for a page index within the block</summary>
protected ulong GetStartAddr(int page) => ((ulong) page << WaterboxUtils.PageShift) + Start; protected ulong GetStartAddr(int page) => ((ulong) page << WaterboxUtils.PageShift) + Start;
/// <summary>Get a stream that can be used to read or write from part of the block. Does not check for or change <see cref="Protect"/>!</summary> /// <summary>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="start"/> or end (= <paramref name="start"/> + <paramref name="length"/> - <c>1</c>) are outside [<see cref="Start"/>, <see cref="EndExclusive"/>), the range of the block</exception> /// Get a stream that can be used to read or write from part of the block. Does not check for or change <see cref="Protect"/>!
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> or end (= <paramref name="start"/> + <paramref name="length"/> - <c>1</c>)
/// are outside [<see cref="Start"/>, <see cref="EndExclusive"/>), the range of the block
/// </exception>
public Stream GetStream(ulong start, ulong length, bool writer) public Stream GetStream(ulong start, ulong length, bool writer)
{ {
if (start < Start) if (start < Start)
@ -68,14 +73,23 @@ namespace BizHawk.BizInvoke
return new MemoryViewStream(!writer, writer, (long) start, (long) length, this); return new MemoryViewStream(!writer, writer, (long) start, (long) length, this);
} }
/// <summary>get a stream that can be used to read or write from part of the block. both reads and writes will be XORed against an earlier recorded snapshot</summary> /// <summary>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="start"/> or end (= <paramref name="start"/> + <paramref name="length"/> - <c>1</c>) are outside [<see cref="Start"/>, <see cref="EndExclusive"/>), the range of the block</exception> /// get a stream that can be used to read or write from part of the block.
/// both reads and writes will be XORed against an earlier recorded snapshot
/// </summary>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="start"/> or end (= <paramref name="start"/> + <paramref name="length"/> - <c>1</c>) are outside
/// [<see cref="Start"/>, <see cref="EndExclusive"/>), the range of the block
/// </exception>
/// <exception cref="InvalidOperationException">no snapshot taken (haven't called <see cref="SaveXorSnapshot"/>)</exception> /// <exception cref="InvalidOperationException">no snapshot taken (haven't called <see cref="SaveXorSnapshot"/>)</exception>
public Stream GetXorStream(ulong start, ulong length, bool writer) public Stream GetXorStream(ulong start, ulong length, bool writer)
{ {
if (start < Start) throw new ArgumentOutOfRangeException(nameof(start), start, "invalid address"); if (start < Start)
if (EndExclusive < start + length) throw new ArgumentOutOfRangeException(nameof(length), length, "requested length implies invalid end address"); throw new ArgumentOutOfRangeException(nameof(start), start, "invalid address");
if (_snapshot == null) throw new InvalidOperationException("No snapshot taken!"); if (EndExclusive < start + length)
throw new ArgumentOutOfRangeException(nameof(length), length, "requested length implies invalid end address");
if (_snapshot == null)
throw new InvalidOperationException("No snapshot taken!");
return new MemoryViewXorStream(!writer, writer, (long) start, (long) length, this, _snapshot, (long) (start - Start)); return new MemoryViewXorStream(!writer, writer, (long) start, (long) length, this, _snapshot, (long) (start - Start));
} }
@ -85,29 +99,33 @@ namespace BizHawk.BizInvoke
{ {
if (Active) if (Active)
throw new InvalidOperationException("Already active"); throw new InvalidOperationException("Already active");
_pal.PalActivate(); _pal.Activate();
ProtectAll(); ProtectAll();
Active = true; Active = true;
} }
/// <summary>deactivate the memory block, removing it from RAM but leaving it immediately available to swap back in</summary> /// <summary>deactivate the memory block, removing it from RAM but leaving it immediately available to swap back in</summary>
/// <exception cref="InvalidOperationException"><see cref="MemoryBlock.Active"/> is <see langword="false"/> or failed to unmap file view</exception> /// <exception cref="InvalidOperationException">
/// <see cref="MemoryBlock.Active"/> is <see langword="false"/> or failed to unmap file view
/// </exception>
public void Deactivate() public void Deactivate()
{ {
if (!Active) if (!Active)
throw new InvalidOperationException("Not active"); throw new InvalidOperationException("Not active");
_pal.PalDeactivate(); _pal.Deactivate();
Active = false; Active = false;
} }
/// <summary>take a hash of the current full contents of the block, including unreadable areas</summary> /// <summary>take a hash of the current full contents of the block, including unreadable areas</summary>
/// <exception cref="InvalidOperationException"><see cref="MemoryBlock.Active"/> is <see langword="false"/> or failed to make memory read-only</exception> /// <exception cref="InvalidOperationException">
/// <see cref="MemoryBlock.Active"/> is <see langword="false"/> or failed to make memory read-only
/// </exception>
public byte[] FullHash() public byte[] FullHash()
{ {
if (!Active) if (!Active)
throw new InvalidOperationException("Not active"); throw new InvalidOperationException("Not active");
// temporarily switch the entire block to `R` // temporarily switch the entire block to `R`
_pal.PalProtect(Start, Size, Protection.R); _pal.Protect(Start, Size, Protection.R);
var ret = WaterboxUtils.Hash(GetStream(Start, Size, false)); var ret = WaterboxUtils.Hash(GetStream(Start, Size, false));
ProtectAll(); ProtectAll();
return ret; return ret;
@ -132,7 +150,7 @@ namespace BizHawk.BizInvoke
var computedEnd = WaterboxUtils.AlignUp(start + length); var computedEnd = WaterboxUtils.AlignUp(start + length);
var computedLength = computedEnd - computedStart; var computedLength = computedEnd - computedStart;
_pal.PalProtect(computedStart, computedLength, prot); _pal.Protect(computedStart, computedLength, prot);
} }
} }
@ -146,14 +164,16 @@ namespace BizHawk.BizInvoke
{ {
ulong zstart = GetStartAddr(ps); ulong zstart = GetStartAddr(ps);
ulong zend = GetStartAddr(i + 1); ulong zend = GetStartAddr(i + 1);
_pal.PalProtect(zstart, zend - zstart, _pageData[i]); _pal.Protect(zstart, zend - zstart, _pageData[i]);
ps = i + 1; ps = i + 1;
} }
} }
} }
/// <summary>take a snapshot of the entire memory block's contents, for use in <see cref="GetXorStream"/></summary> /// <summary>take a snapshot of the entire memory block's contents, for use in <see cref="GetXorStream"/></summary>
/// <exception cref="InvalidOperationException">snapshot already taken, <see cref="MemoryBlock.Active"/> is <see langword="false"/>, or failed to make memory read-only</exception> /// <exception cref="InvalidOperationException">
/// snapshot already taken, <see cref="MemoryBlock.Active"/> is <see langword="false"/>, or failed to make memory read-only
/// </exception>
public void SaveXorSnapshot() public void SaveXorSnapshot()
{ {
if (_snapshot != null) if (_snapshot != null)
@ -163,7 +183,7 @@ namespace BizHawk.BizInvoke
// temporarily switch the entire block to `R`: in case some areas are unreadable, we don't want // temporarily switch the entire block to `R`: in case some areas are unreadable, we don't want
// that to complicate things // that to complicate things
_pal.PalProtect(Start, Size, Protection.R); _pal.Protect(Start, Size, Protection.R);
_snapshot = new byte[Size]; _snapshot = new byte[Size];
var ds = new MemoryStream(_snapshot, true); var ds = new MemoryStream(_snapshot, true);
@ -236,8 +256,10 @@ namespace BizHawk.BizInvoke
public override int Read(byte[] buffer, int offset, int count) public override int Read(byte[] buffer, int offset, int count)
{ {
if (!_readable) throw new InvalidOperationException(); if (!_readable)
if (count < 0 || buffer.Length < count + offset) throw new ArgumentOutOfRangeException(); throw new InvalidOperationException();
if (count < 0 || buffer.Length < count + offset)
throw new ArgumentOutOfRangeException();
EnsureNotDisposed(); EnsureNotDisposed();
count = (int) Math.Min(count, _length - _pos); count = (int) Math.Min(count, _length - _pos);
@ -273,8 +295,10 @@ namespace BizHawk.BizInvoke
public override void Write(byte[] buffer, int offset, int count) public override void Write(byte[] buffer, int offset, int count)
{ {
if (!_writable) throw new InvalidOperationException(); if (!_writable)
if (count < 0 || _length - _pos < count || buffer.Length < count + offset) throw new ArgumentOutOfRangeException(); throw new InvalidOperationException();
if (count < 0 || _length - _pos < count || buffer.Length < count + offset)
throw new ArgumentOutOfRangeException();
EnsureNotDisposed(); EnsureNotDisposed();
Marshal.Copy(buffer, offset, Z.SS(_ptr + _pos), count); Marshal.Copy(buffer, offset, Z.SS(_ptr + _pos), count);

View File

@ -44,21 +44,21 @@ namespace BizHawk.BizInvoke
Dispose(); Dispose();
} }
public void PalActivate() public void Activate()
{ {
var ptr = mmap(Z.US(_start), Z.UU(_size), MemoryProtection.Read | MemoryProtection.Write | MemoryProtection.Execute, 16, _fd, IntPtr.Zero); var ptr = mmap(Z.US(_start), Z.UU(_size), MemoryProtection.Read | MemoryProtection.Write | MemoryProtection.Execute, 16, _fd, IntPtr.Zero);
if (ptr != Z.US(_start)) if (ptr != Z.US(_start))
throw new InvalidOperationException($"{nameof(mmap)}() returned NULL or the wrong pointer"); throw new InvalidOperationException($"{nameof(mmap)}() returned NULL or the wrong pointer");
} }
public void PalDeactivate() public void Deactivate()
{ {
var exitCode = munmap(Z.US(_start), Z.UU(_size)); var exitCode = munmap(Z.US(_start), Z.UU(_size));
if (exitCode != 0) if (exitCode != 0)
throw new InvalidOperationException($"{nameof(munmap)}() returned {exitCode}"); throw new InvalidOperationException($"{nameof(munmap)}() returned {exitCode}");
} }
public void PalProtect(ulong start, ulong size, Protection prot) public void Protect(ulong start, ulong size, Protection prot)
{ {
var exitCode = mprotect( var exitCode = mprotect(
Z.US(start), Z.US(start),

View File

@ -36,7 +36,7 @@ namespace BizHawk.BizInvoke
} }
} }
public void PalActivate() public void Activate()
{ {
if (Kernel32.MapViewOfFileEx( if (Kernel32.MapViewOfFileEx(
_handle, _handle,
@ -51,13 +51,13 @@ namespace BizHawk.BizInvoke
} }
} }
public void PalDeactivate() public void Deactivate()
{ {
if (!Kernel32.UnmapViewOfFile(Z.US(_start))) if (!Kernel32.UnmapViewOfFile(Z.US(_start)))
throw new InvalidOperationException($"{nameof(Kernel32.UnmapViewOfFile)}() returned NULL"); throw new InvalidOperationException($"{nameof(Kernel32.UnmapViewOfFile)}() returned NULL");
} }
public void PalProtect(ulong start, ulong size, Protection prot) public void Protect(ulong start, ulong size, Protection prot)
{ {
if (!Kernel32.VirtualProtect(Z.UU(start), Z.UU(size), GetKernelMemoryProtectionValue(prot), out var old)) if (!Kernel32.VirtualProtect(Z.UU(start), Z.UU(size), GetKernelMemoryProtectionValue(prot), out var old))
throw new InvalidOperationException($"{nameof(Kernel32.VirtualProtect)}() returned FALSE!"); throw new InvalidOperationException($"{nameof(Kernel32.VirtualProtect)}() returned FALSE!");