diff --git a/src/IKVM.ByteCode/Buffers/MappedFileMemoryManager.cs b/src/IKVM.ByteCode/Buffers/MappedFileMemoryManager.cs index 20627e9..aa1973e 100644 --- a/src/IKVM.ByteCode/Buffers/MappedFileMemoryManager.cs +++ b/src/IKVM.ByteCode/Buffers/MappedFileMemoryManager.cs @@ -15,6 +15,7 @@ sealed unsafe class MappedFileMemoryManager : MemoryManager readonly MemoryMappedFile _file; readonly MemoryMappedViewAccessor _view; + readonly byte* _addr; /// /// Initializes a new instance. @@ -31,15 +32,15 @@ public MappedFileMemoryManager(MemoryMappedFile file, MemoryMappedViewAccessor v throw new InvalidOperationException(); if (_view.SafeMemoryMappedViewHandle.IsClosed || _view.SafeMemoryMappedViewHandle.IsInvalid) throw new InvalidOperationException(); + + _view.SafeMemoryMappedViewHandle.AcquirePointer(ref _addr); } /// public override Span GetSpan() { - byte* ptr = null; - _view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); var len = _view.SafeMemoryMappedViewHandle.ByteLength; - return new Span(ptr, checked((int)len)); + return new Span(_addr, checked((int)len)); } /// @@ -48,9 +49,7 @@ public override MemoryHandle Pin(int elementIndex = 0) if (elementIndex < 0 || elementIndex >= checked((int)_view.SafeMemoryMappedViewHandle.ByteLength)) throw new ArgumentOutOfRangeException(nameof(elementIndex)); - byte* ptr = null; - _view.SafeMemoryMappedViewHandle.AcquirePointer(ref ptr); - return new MemoryHandle(Unsafe.Add(ptr, elementIndex)); + return new MemoryHandle(Unsafe.Add(_addr, elementIndex)); } /// @@ -62,11 +61,9 @@ public override void Unpin() /// protected override void Dispose(bool disposing) { - if (true) - { - _view.Dispose(); - _file.Dispose(); - } + _view.SafeMemoryMappedViewHandle.ReleasePointer(); + _view.Dispose(); + _file.Dispose(); } } diff --git a/src/IKVM.ByteCode/Decoding/ClassFile.cs b/src/IKVM.ByteCode/Decoding/ClassFile.cs index cc3919a..be39ced 100644 --- a/src/IKVM.ByteCode/Decoding/ClassFile.cs +++ b/src/IKVM.ByteCode/Decoding/ClassFile.cs @@ -19,7 +19,6 @@ public sealed class ClassFile : IDisposable public const uint MAGIC = 0xCAFEBABE; public const uint MIN_CLASS_SIZE = 30; - /// /// Attempts to measure a class starting at the current position. /// @@ -178,20 +177,19 @@ public static bool TryRead(in ReadOnlySequence buffer, out ClassFile? claz /// public static bool TryMeasure(in ReadOnlySequence buffer, ref int size, out SequencePosition consumed, out SequencePosition examined) { - consumed = buffer.Start; - var reader = new ClassFormatReader(buffer); if (TryMeasure(ref reader, ref size) == false) { - // examined up to the position of the reader, but consumed nothing - examined = reader.Position; + // examined up to the end of the buffer, but consumed nothing + examined = buffer.End; + consumed = buffer.Start; return false; } else { - // examined up to the point of the reader, consumed the same + // examined up to the end of thebuffer, but consumed up until the position of the reader + examined = buffer.End; consumed = reader.Position; - examined = reader.Position; return true; } } diff --git a/src/IKVM.ByteCode/Text/EncodingExtensions.cs b/src/IKVM.ByteCode/Text/EncodingExtensions.cs index 83d801a..1c925c1 100644 --- a/src/IKVM.ByteCode/Text/EncodingExtensions.cs +++ b/src/IKVM.ByteCode/Text/EncodingExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Text; namespace IKVM.ByteCode.Text {