Skip to content
This repository was archived by the owner on Aug 2, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DotnetCLIVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.2.0-preview1-008003
2.1.300-preview2-008018
2 changes: 1 addition & 1 deletion SharedRuntimeVersion.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.0-preview1-26122-01
2.1.0-preview2-26124-05
2 changes: 1 addition & 1 deletion corefxlab.sln
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27130.2020
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "MultiSegmentBytesReaderNumbers", "MultiSegmentBytesReaderNumbers", "{5E7EB061-B9BC-4DA2-B5E5-859AA7C67695}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5E7EB061-B9BC-4DA2-B5E5-859AA7C67695}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
NuGet.Config = NuGet.Config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Buffers.Native;
using System.Runtime.CompilerServices;

namespace System.Buffers.Native
{
Expand Down Expand Up @@ -41,10 +42,11 @@ protected override bool TryGetArray(out ArraySegment<byte> arraySegment)
return false;
}

public override MemoryHandle Pin()
public override MemoryHandle Pin(int byteOffset = 0)
{
Retain();
return new MemoryHandle(this, _pointer.ToPointer());
if (byteOffset < 0 || byteOffset > _length) throw new ArgumentOutOfRangeException(nameof(byteOffset));
return new MemoryHandle(this, Unsafe.Add<byte>(_pointer.ToPointer(), byteOffset));
}

private readonly NativeMemoryPool _pool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace System.Buffers.Native
Expand Down Expand Up @@ -36,12 +37,13 @@ protected override void Dispose(bool disposing)
_pointer = IntPtr.Zero;
}

public override MemoryHandle Pin()
public override MemoryHandle Pin(int byteOffset = 0)
{
Retain();
if (byteOffset < 0 || byteOffset > _length) throw new ArgumentOutOfRangeException(nameof(byteOffset));
unsafe
{
return new MemoryHandle(this, _pointer.ToPointer());
return new MemoryHandle(this, Unsafe.Add<byte>(_pointer.ToPointer(), byteOffset));
}
}
protected override bool TryGetArray(out ArraySegment<byte> arraySegment)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ protected override void Dispose(bool disposing)
base.Dispose(disposing);
}

public unsafe override MemoryHandle Pin()
public unsafe override MemoryHandle Pin(int byteOffset = 0)
{
return new MemoryHandle(this, _pointer.ToPointer());
if (byteOffset < 0 || (byteOffset / Unsafe.SizeOf<T>()) > _array.Length) throw new ArgumentOutOfRangeException(nameof(byteOffset));
return new MemoryHandle(this, Unsafe.Add<byte>(_pointer.ToPointer(), byteOffset));
}

protected override bool TryGetArray(out ArraySegment<T> arraySegment)
Expand Down
10 changes: 6 additions & 4 deletions src/System.Buffers.Primitives/System/Buffers/OwnedArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public OwnedArray(int length)

public OwnedArray(T[] array)
{
if (array == null) ThrowArgumentNullException(nameof(array));
if (array == null) ThrowArgumentNullException(nameof(array));
_array = array;
}

Expand All @@ -37,13 +37,15 @@ public override Span<T> Span
}
}

public override MemoryHandle Pin()
public override MemoryHandle Pin(int byteOffset = 0)
{
unsafe
{
Retain();
if (byteOffset < 0 || (byteOffset / Unsafe.SizeOf<T>()) > _array.Length) throw new ArgumentOutOfRangeException(nameof(byteOffset));
var handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
return new MemoryHandle(this, (void*)handle.AddrOfPinnedObject(), handle);
void* pointer = Unsafe.Add<byte>((void*)handle.AddrOfPinnedObject(), byteOffset);
return new MemoryHandle(this, pointer, handle);
}
}

Expand All @@ -68,7 +70,7 @@ public override void Retain()
public override bool Release()
{
int newRefCount = Interlocked.Decrement(ref _referenceCount);
if (newRefCount < 0) ThrowInvalidOperationException();
if (newRefCount < 0) ThrowInvalidOperationException();
if (newRefCount == 0)
{
OnNoReferences();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private sealed class ArrayPoolMemory : OwnedMemory<T>
T[] _array;
bool _disposed;
int _referenceCount;

public ArrayPoolMemory(int size)
{
_array = ArrayPool<T>.Shared.Rent(size);
Expand All @@ -57,7 +57,8 @@ public override Span<T> Span
protected override void Dispose(bool disposing)
{
var array = Interlocked.Exchange(ref _array, null);
if (array != null) {
if (array != null)
{
_disposed = true;
ArrayPool<T>.Shared.Return(array);
}
Expand All @@ -70,13 +71,15 @@ protected override bool TryGetArray(out ArraySegment<T> arraySegment)
return true;
}

public override MemoryHandle Pin()
public override MemoryHandle Pin(int byteOffset = 0)
{
unsafe
{
Retain(); // this checks IsDisposed
if (byteOffset < 0 || (byteOffset / Unsafe.SizeOf<T>()) > _array.Length) throw new ArgumentOutOfRangeException(nameof(byteOffset));
var handle = GCHandle.Alloc(_array, GCHandleType.Pinned);
return new MemoryHandle(this, (void*)handle.AddrOfPinnedObject(), handle);
void* pointer = Unsafe.Add<byte>((void*)handle.AddrOfPinnedObject(), byteOffset);
return new MemoryHandle(this, pointer, handle);
}
}

Expand All @@ -90,7 +93,7 @@ public override bool Release()
{
int newRefCount = Interlocked.Decrement(ref _referenceCount);
if (newRefCount < 0) throw new InvalidOperationException();
if (newRefCount == 0)
if (newRefCount == 0)
{
Dispose();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public override bool Release()
if (newRefCount < 0) RioPipelinesThrowHelper.ThrowInvalidOperationException(ExceptionResource.ReferenceCountZero);
if (newRefCount == 0)
{
OnZeroReferences();
return false;
OnZeroReferences();
return false;
}
return true;
}
Expand All @@ -134,13 +134,13 @@ protected override bool TryGetArray(out ArraySegment<byte> arraySegment)
return true;
}

public override MemoryHandle Pin()
public override MemoryHandle Pin(int byteOffset = 0)
{
if (IsDisposed) RioPipelinesThrowHelper.ThrowObjectDisposedException(nameof(RioMemoryPoolBlock));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note the removal of this check, since it was redundant with the check that Retain() already does (here and elsewhere).

Retain();
Retain(); // checks IsDisposed
if (byteOffset < 0 || byteOffset > _length) RioPipelinesThrowHelper.ThrowArgumentOutOfRangeException(_length, byteOffset);
unsafe
{
return new MemoryHandle(this, (Slab.NativePointer + _offset).ToPointer());
return new MemoryHandle(this, (Slab.NativePointer + _offset + byteOffset).ToPointer());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,30 @@
namespace System.IO.Pipelines
{
internal class RioPipelinesThrowHelper
{
{
public static void ThrowArgumentOutOfRangeException(int sourceLength, int offset)
{
throw GetArgumentOutOfRangeException(sourceLength, offset);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(int sourceLength, int offset)
{
if ((uint)offset > (uint)sourceLength)
{
// Offset is negative or less than array length
return new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.offset));
}

// The third parameter (not passed) length must be out of range
return new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.length));
}

public static void ThrowInvalidOperationException(ExceptionResource resource, string location = null)
{
throw GetInvalidOperationException(resource, location);
}

public static void ThrowArgumentOutOfRangeException_BufferRequestTooLarge(int maxSize)
{
throw GetArgumentOutOfRangeException_BufferRequestTooLarge(maxSize);
Expand All @@ -28,7 +46,7 @@ public static InvalidOperationException GetInvalidOperationException(ExceptionRe
{
return new InvalidOperationException(GetResourceString(resource, location));
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException_BufferRequestTooLarge(int maxSize)
{
Expand Down
12 changes: 6 additions & 6 deletions src/System.IO.Pipelines/System/Buffers/MemoryPoolBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ public override bool Release()
if (newRefCount < 0) PipelinesThrowHelper.ThrowInvalidOperationException(ExceptionResource.ReferenceCountZero);
if (newRefCount == 0)
{
OnZeroReferences();
return false;
OnZeroReferences();
return false;
}
return true;
}
Expand All @@ -134,13 +134,13 @@ protected override bool TryGetArray(out ArraySegment<byte> arraySegment)
return true;
}

public override MemoryHandle Pin()
public override MemoryHandle Pin(int byteOffset = 0)
{
if (IsDisposed) PipelinesThrowHelper.ThrowObjectDisposedException(nameof(MemoryPoolBlock));
Retain();
Retain(); // checks IsDisposed
if (byteOffset < 0 || byteOffset > _length) PipelinesThrowHelper.ThrowArgumentOutOfRangeException(_length, byteOffset);
unsafe
{
return new MemoryHandle(this, (Slab.NativePointer + _offset).ToPointer());
return new MemoryHandle(this, (Slab.NativePointer + _offset + byteOffset).ToPointer());
}
}
}
Expand Down
Loading