From c3251a51a9a43b79c2809354e4ab97d93728fb8a Mon Sep 17 00:00:00 2001 From: sakno Date: Fri, 12 Jan 2024 08:07:09 +0200 Subject: [PATCH] Fixed regression --- src/DotNext.Tests/IO/StreamSourceTests.cs | 2 +- src/DotNext.Unsafe/Buffers/UnmanagedMemory.cs | 10 +++++++--- .../Runtime/InteropServices/Pointer.cs | 17 +++-------------- 3 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/DotNext.Tests/IO/StreamSourceTests.cs b/src/DotNext.Tests/IO/StreamSourceTests.cs index 126aa56f4..488a12f72 100644 --- a/src/DotNext.Tests/IO/StreamSourceTests.cs +++ b/src/DotNext.Tests/IO/StreamSourceTests.cs @@ -289,7 +289,7 @@ public static void ReadApm() var buffer = new byte[4]; src.Position = 1; var ar = src.BeginRead(buffer, 0, 2, null, "state"); - False(ar.CompletedSynchronously); + True(ar.CompletedSynchronously); Equal("state", ar.AsyncState); True(ar.AsyncWaitHandle.WaitOne(DefaultTimeout)); Equal(2, src.EndRead(ar)); diff --git a/src/DotNext.Unsafe/Buffers/UnmanagedMemory.cs b/src/DotNext.Unsafe/Buffers/UnmanagedMemory.cs index bd6039759..6d5c5efc8 100644 --- a/src/DotNext.Unsafe/Buffers/UnmanagedMemory.cs +++ b/src/DotNext.Unsafe/Buffers/UnmanagedMemory.cs @@ -70,6 +70,10 @@ internal unsafe class UnmanagedMemory : MemoryManager private readonly bool owner; private void* address; + private UnmanagedMemory() + { + } + internal UnmanagedMemory(nint address, int length) { Debug.Assert(address is not 0); @@ -78,6 +82,8 @@ internal UnmanagedMemory(nint address, int length) Length = length; } + internal static UnmanagedMemory CreateEmpty() => new(); + private protected UnmanagedMemory(int length, delegate* allocator) { Debug.Assert(length > 0); @@ -105,7 +111,6 @@ public ref T this[int index] internal void Reallocate(int length) { ArgumentOutOfRangeException.ThrowIfNegativeOrZero(length); - ObjectDisposedException.ThrowIf(address is null, this); var size = (nuint)length * (nuint)sizeof(T); address = NativeMemory.Realloc(address, size); @@ -117,8 +122,7 @@ public sealed override Span GetSpan() public sealed override MemoryHandle Pin(int elementIndex = 0) { - ObjectDisposedException.ThrowIf(address is null, this); - ArgumentOutOfRangeException.ThrowIfNegative(elementIndex); + ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual((uint)elementIndex, (uint)Length, nameof(elementIndex)); return new(Unsafe.Add(address, elementIndex)); } diff --git a/src/DotNext.Unsafe/Runtime/InteropServices/Pointer.cs b/src/DotNext.Unsafe/Runtime/InteropServices/Pointer.cs index 9d0eb33f4..b16cf9f1b 100644 --- a/src/DotNext.Unsafe/Runtime/InteropServices/Pointer.cs +++ b/src/DotNext.Unsafe/Runtime/InteropServices/Pointer.cs @@ -1047,20 +1047,9 @@ public unsafe int BitwiseCompare(Pointer other, nuint count) /// The number of elements in the memory. /// The instance of memory owner. public unsafe IMemoryOwner ToMemoryOwner(int length) - { - nint address; - if (IsNull) - { - length = 0; - address = 0; - } - else - { - address = (nint)value; - } - - return new Buffers.UnmanagedMemory(address, length); - } + => IsNull + ? Buffers.UnmanagedMemory.CreateEmpty() + : new Buffers.UnmanagedMemory(Address, length); /// /// Obtains pointer to the memory represented by given memory handle.