Skip to content
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
1 change: 1 addition & 0 deletions NuGetPackageVerifier.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"Microsoft.AspNetCore.Analyzer.Testing": {},
"Microsoft.AspNetCore.BenchmarkRunner.Sources": {},
"Microsoft.Extensions.Buffers.Sources": {},
"Microsoft.Extensions.Buffers.MemoryPool.Sources": {},
"Microsoft.Extensions.Buffers.Testing.Sources": {},
"Microsoft.AspNetCore.Certificates.Generation.Sources": {},
"Microsoft.Extensions.ClosedGenericMatcher.Sources": {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public override Memory<byte> Memory
{
get
{
if (!Slab.IsActive) ThrowHelper.ThrowObjectDisposedException(ExceptionArgument.MemoryPoolBlock);
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);

return CreateMemory(_length);
}
Expand Down Expand Up @@ -74,11 +74,11 @@ public override Memory<byte> Memory

protected override void Dispose(bool disposing)
{
if (!Slab.IsActive) ThrowHelper.ThrowObjectDisposedException(ExceptionArgument.MemoryPoolBlock);
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);

if (Volatile.Read(ref _pinCount) > 0)
{
ThrowHelper.ThrowInvalidOperationException_ReturningPinnedBlock();
MemoryPoolThrowHelper.ThrowInvalidOperationException_ReturningPinnedBlock();
}

Pool.Return(this);
Expand All @@ -88,8 +88,8 @@ protected override void Dispose(bool disposing)

public override MemoryHandle Pin(int byteOffset = 0)
{
if (!Slab.IsActive) ThrowHelper.ThrowObjectDisposedException(ExceptionArgument.MemoryPoolBlock);
if (byteOffset < 0 || byteOffset > _length) ThrowHelper.ThrowArgumentOutOfRangeException(_length, byteOffset);
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);
if (byteOffset < 0 || byteOffset > _length) MemoryPoolThrowHelper.ThrowArgumentOutOfRangeException(_length, byteOffset);

Interlocked.Increment(ref _pinCount);
unsafe
Expand All @@ -108,7 +108,7 @@ public override void Unpin()
{
if (Interlocked.Decrement(ref _pinCount) < 0)
{
ThrowHelper.ThrowInvalidOperationException_ReferenceCountZero();
MemoryPoolThrowHelper.ThrowInvalidOperationException_ReferenceCountZero();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal sealed class MemoryPoolBlock : IMemoryOwner<byte>
{
private readonly int _offset;
private readonly int _length;

/// <summary>
/// This object cannot be instantiated outside of the static Create method
/// </summary>
Expand All @@ -26,7 +26,7 @@ internal MemoryPoolBlock(SlabMemoryPool pool, MemoryPoolSlab slab, int offset, i

Pool = pool;
Slab = slab;

Memory = MemoryMarshal.CreateFromPinnedArray(slab.Array, _offset, _length);
}

Expand All @@ -53,7 +53,7 @@ internal MemoryPoolBlock(SlabMemoryPool pool, MemoryPoolSlab slab, int offset, i

public void Dispose()
{
if (!Slab.IsActive) ThrowHelper.ThrowObjectDisposedException(ExceptionArgument.MemoryPoolBlock);
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);
Pool.Return(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace System.Buffers
{
internal class ThrowHelper
internal class MemoryPoolThrowHelper
{
public static void ThrowArgumentOutOfRangeException(int sourceLength, int offset)
{
Expand All @@ -30,7 +30,7 @@ public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
{
throw GetArgumentOutOfRangeException(argument);
}

public static void ThrowInvalidOperationException_ReferenceCountZero()
{
throw new InvalidOperationException("Can't release when reference count is already zero");
Expand Down Expand Up @@ -75,13 +75,13 @@ private static string GetArgumentName(ExceptionArgument argument)

return argument.ToString();
}
}

internal enum ExceptionArgument
{
size,
offset,
length,
MemoryPoolBlock
internal enum ExceptionArgument
{
size,
offset,
length,
MemoryPoolBlock
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override IMemoryOwner<byte> Rent(int size = AnySize)
if (size == AnySize) size = _blockSize;
else if (size > _blockSize)
{
ThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockSize);
MemoryPoolThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockSize);
}

var block = Lease();
Expand Down
4 changes: 2 additions & 2 deletions shared/Microsoft.Extensions.Buffers.Sources/BufferReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void Advance(int byteCount)
{
if (byteCount < 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
BuffersThrowHelper.ThrowArgumentOutOfRangeException(BuffersThrowHelper.ExceptionArgument.length);
}

_consumedBytes += byteCount;
Expand All @@ -118,7 +118,7 @@ public void Advance(int byteCount)

if (byteCount > 0)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
BuffersThrowHelper.ThrowArgumentOutOfRangeException(BuffersThrowHelper.ExceptionArgument.length);
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions shared/Microsoft.Extensions.Buffers.Sources/BuffersThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace System.Buffers
{
internal class BuffersThrowHelper
{
public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
{
throw GetArgumentOutOfRangeException(argument);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument)
{
return new ArgumentOutOfRangeException(GetArgumentName(argument));
}

private static string GetArgumentName(ExceptionArgument argument)
{
Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument), "The enum value is not defined, please check the ExceptionArgument Enum.");

return argument.ToString();
}

internal enum ExceptionArgument
{
length,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
Expand All @@ -10,6 +10,7 @@
<Compile Include="..\..\shared\Microsoft.AspNetCore.Certificates.Generation.Sources\**\*.cs" />
<Compile Include="..\..\shared\Microsoft.Extensions.ActivatorUtilities.Sources\**\*.cs" />
<Compile Include="..\..\shared\Microsoft.Extensions.Buffers.Sources\**\*.cs" />
<Compile Include="..\..\shared\Microsoft.Extensions.Buffers.MemoryPool.Sources\**\*.cs" />
<Compile Include="..\..\shared\Microsoft.Extensions.Buffers.Testing.Sources\**\*.cs" />
<Compile Include="..\..\shared\Microsoft.Extensions.ClosedGenericMatcher.Sources\**\*.cs" />
<Compile Include="..\..\shared\Microsoft.Extensions.CopyOnWriteDictionary.Sources\**\*.cs" />
Expand Down