Skip to content

Commit c0a99b8

Browse files
committed
Split pool and reader/writer into separate packages
1 parent c73826d commit c0a99b8

File tree

9 files changed

+55
-19
lines changed

9 files changed

+55
-19
lines changed

NuGetPackageVerifier.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"Microsoft.AspNetCore.Analyzer.Testing": {},
66
"Microsoft.AspNetCore.BenchmarkRunner.Sources": {},
77
"Microsoft.Extensions.Buffers.Sources": {},
8+
"Microsoft.Extensions.Buffers.MemoryPool.Sources": {},
89
"Microsoft.Extensions.Buffers.Testing.Sources": {},
910
"Microsoft.AspNetCore.Certificates.Generation.Sources": {},
1011
"Microsoft.Extensions.ClosedGenericMatcher.Sources": {},

shared/Microsoft.Extensions.Buffers.Sources/MemoryPoolBlock.Debug.cs renamed to shared/Microsoft.Extensions.Buffers.MemoryPool.Sources/MemoryPoolBlock.Debug.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public override Memory<byte> Memory
4545
{
4646
get
4747
{
48-
if (!Slab.IsActive) ThrowHelper.ThrowObjectDisposedException(ExceptionArgument.MemoryPoolBlock);
48+
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);
4949

5050
return CreateMemory(_length);
5151
}
@@ -74,11 +74,11 @@ public override Memory<byte> Memory
7474

7575
protected override void Dispose(bool disposing)
7676
{
77-
if (!Slab.IsActive) ThrowHelper.ThrowObjectDisposedException(ExceptionArgument.MemoryPoolBlock);
77+
if (!Slab.IsActive) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPoolBlock);
7878

7979
if (Volatile.Read(ref _pinCount) > 0)
8080
{
81-
ThrowHelper.ThrowInvalidOperationException_ReturningPinnedBlock();
81+
MemoryPoolThrowHelper.ThrowInvalidOperationException_ReturningPinnedBlock();
8282
}
8383

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

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

9494
Interlocked.Increment(ref _pinCount);
9595
unsafe
@@ -108,7 +108,7 @@ public override void Unpin()
108108
{
109109
if (Interlocked.Decrement(ref _pinCount) < 0)
110110
{
111-
ThrowHelper.ThrowInvalidOperationException_ReferenceCountZero();
111+
MemoryPoolThrowHelper.ThrowInvalidOperationException_ReferenceCountZero();
112112
}
113113
}
114114

shared/Microsoft.Extensions.Buffers.Sources/PipelinesThrowHelper.cs renamed to shared/Microsoft.Extensions.Buffers.MemoryPool.Sources/MemoryPoolThrowHelper.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace System.Buffers
88
{
9-
internal class ThrowHelper
9+
internal class MemoryPoolThrowHelper
1010
{
1111
public static void ThrowArgumentOutOfRangeException(int sourceLength, int offset)
1212
{
@@ -30,7 +30,7 @@ public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
3030
{
3131
throw GetArgumentOutOfRangeException(argument);
3232
}
33-
33+
3434
public static void ThrowInvalidOperationException_ReferenceCountZero()
3535
{
3636
throw new InvalidOperationException("Can't release when reference count is already zero");
@@ -75,13 +75,13 @@ private static string GetArgumentName(ExceptionArgument argument)
7575

7676
return argument.ToString();
7777
}
78-
}
7978

80-
internal enum ExceptionArgument
81-
{
82-
size,
83-
offset,
84-
length,
85-
MemoryPoolBlock
79+
internal enum ExceptionArgument
80+
{
81+
size,
82+
offset,
83+
length,
84+
MemoryPoolBlock
85+
}
8686
}
8787
}

shared/Microsoft.Extensions.Buffers.Sources/SlabMemoryPool.cs renamed to shared/Microsoft.Extensions.Buffers.MemoryPool.Sources/SlabMemoryPool.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public override IMemoryOwner<byte> Rent(int size = AnySize)
6161
if (size == AnySize) size = _blockSize;
6262
else if (size > _blockSize)
6363
{
64-
ThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockSize);
64+
MemoryPoolThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockSize);
6565
}
6666

6767
var block = Lease();

shared/Microsoft.Extensions.Buffers.Sources/BufferReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void Advance(int byteCount)
9494
{
9595
if (byteCount < 0)
9696
{
97-
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
97+
BuffersThrowHelper.ThrowArgumentOutOfRangeException(BuffersThrowHelper.ExceptionArgument.length);
9898
}
9999

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

119119
if (byteCount > 0)
120120
{
121-
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length);
121+
BuffersThrowHelper.ThrowArgumentOutOfRangeException(BuffersThrowHelper.ExceptionArgument.length);
122122
}
123123
}
124124
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System.Diagnostics;
5+
using System.Runtime.CompilerServices;
6+
7+
namespace System.Buffers
8+
{
9+
internal class BuffersThrowHelper
10+
{
11+
public static void ThrowArgumentOutOfRangeException(ExceptionArgument argument)
12+
{
13+
throw GetArgumentOutOfRangeException(argument);
14+
}
15+
16+
[MethodImpl(MethodImplOptions.NoInlining)]
17+
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(ExceptionArgument argument)
18+
{
19+
return new ArgumentOutOfRangeException(GetArgumentName(argument));
20+
}
21+
22+
private static string GetArgumentName(ExceptionArgument argument)
23+
{
24+
Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument), "The enum value is not defined, please check the ExceptionArgument Enum.");
25+
26+
return argument.ToString();
27+
}
28+
29+
internal enum ExceptionArgument
30+
{
31+
length,
32+
}
33+
}
34+
}

test/Microsoft.Extensions.Internal.Test/Microsoft.Extensions.Internal.Test.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFrameworks>$(StandardTestTfms)</TargetFrameworks>
@@ -10,6 +10,7 @@
1010
<Compile Include="..\..\shared\Microsoft.AspNetCore.Certificates.Generation.Sources\**\*.cs" />
1111
<Compile Include="..\..\shared\Microsoft.Extensions.ActivatorUtilities.Sources\**\*.cs" />
1212
<Compile Include="..\..\shared\Microsoft.Extensions.Buffers.Sources\**\*.cs" />
13+
<Compile Include="..\..\shared\Microsoft.Extensions.Buffers.MemoryPool.Sources\**\*.cs" />
1314
<Compile Include="..\..\shared\Microsoft.Extensions.Buffers.Testing.Sources\**\*.cs" />
1415
<Compile Include="..\..\shared\Microsoft.Extensions.ClosedGenericMatcher.Sources\**\*.cs" />
1516
<Compile Include="..\..\shared\Microsoft.Extensions.CopyOnWriteDictionary.Sources\**\*.cs" />

0 commit comments

Comments
 (0)