Skip to content

Commit 0c56654

Browse files
committed
update README.md, add document comments
1 parent aeedfc7 commit 0c56654

File tree

4 files changed

+183
-76
lines changed

4 files changed

+183
-76
lines changed

PooledStream.sln

+14-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.0
2-
# Visual Studio Version 16
3-
VisualStudioVersion = 16.0
1+
Microsoft Visual Studio Solution File, Format Version 12.00
2+
# Visual Studio Version 17
3+
VisualStudioVersion = 17.0.32002.185
44
MinimumVisualStudioVersion = 10.0.40219.1
55
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PooledStream.Benchmark", "PooledStream.Benchmark\PooledStream.Benchmark.csproj", "{41130469-5601-4C78-87DC-14574AD1EB0F}"
66
EndProject
77
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PooledStream.Test", "PooledStream.Test\PooledStream.Test.csproj", "{DA3F4EB2-D0F8-49C3-8A34-C86412EFC151}"
88
EndProject
99
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PooledStream", "PooledStream\PooledStream.csproj", "{3C446344-2FF5-45F9-9010-3227F715B8A9}"
1010
EndProject
11+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{180A5C83-F82B-40F0-BCAF-F240679667EC}"
12+
ProjectSection(SolutionItems) = preProject
13+
README.md = README.md
14+
EndProjectSection
15+
EndProject
1116
Global
1217
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1318
Debug|AnyCPU = Debug|AnyCPU
@@ -27,4 +32,10 @@ Global
2732
{3C446344-2FF5-45F9-9010-3227F715B8A9}.Release|AnyCPU.ActiveCfg = Debug|Any CPU
2833
{3C446344-2FF5-45F9-9010-3227F715B8A9}.Release|AnyCPU.Build.0 = Debug|Any CPU
2934
EndGlobalSection
35+
GlobalSection(SolutionProperties) = preSolution
36+
HideSolutionNode = FALSE
37+
EndGlobalSection
38+
GlobalSection(ExtensibilityGlobals) = postSolution
39+
SolutionGuid = {F11B0785-81AF-4F25-9079-8A1432151A79}
40+
EndGlobalSection
3041
EndGlobal

PooledStream/PooledBufferWriter.cs

+51-3
Original file line numberDiff line numberDiff line change
@@ -22,31 +22,55 @@ void Reallocate(int sizeHint)
2222
}
2323
_currentBuffer = nar;
2424
}
25+
/// <summary>
26+
/// use shared instance and preallocatedSize = 1024
27+
/// </summary>
2528
public PooledMemoryBufferWriter() : this(ArrayPool<T>.Shared)
2629
{
2730

2831
}
32+
/// <summary>
33+
/// use shared instance, use preallocateSize as reserved buffer length
34+
/// </summary>
35+
/// <param name="preallocateSize">initial reserved buffer size</param>
2936
public PooledMemoryBufferWriter(int preallocateSize) : this(ArrayPool<T>.Shared, preallocateSize)
3037
{
3138

3239
}
40+
/// <summary>
41+
/// use pool for memory pool
42+
/// </summary>
43+
/// <param name="pool">memory pool</param>
3344
public PooledMemoryBufferWriter(ArrayPool<T> pool) : this(pool, DefaultSize)
3445
{
3546
}
47+
/// <summary>
48+
/// </summary>
49+
/// <param name="pool">memory pool</param>
50+
/// <param name="preallocateSize">initial reserved buffer size</param>
3651
public PooledMemoryBufferWriter(ArrayPool<T> pool, int preallocateSize)
3752
{
53+
if(pool == null)
54+
{
55+
throw new ArgumentNullException(nameof(pool));
56+
}
57+
if(preallocateSize < 0)
58+
{
59+
throw new ArgumentOutOfRangeException(nameof(preallocateSize), "size must be greater than 0");
60+
}
3861
_Pool = pool;
3962
_currentBuffer = null;
4063
_Position = 0;
4164
_Length = 0;
4265
Reallocate(preallocateSize);
4366
}
67+
/// <inheritdoc/>
4468
[MethodImpl(MethodImplOptions.AggressiveInlining)]
4569
public void Advance(int count)
4670
{
4771
if (_Position + count > _currentBuffer.Length)
4872
{
49-
throw new IndexOutOfRangeException("advance too many(" + count.ToString() + ")");
73+
throw new ArgumentOutOfRangeException("advance too many(" + count.ToString() + ")");
5074
}
5175
_Position += count;
5276
if (_Length < _Position)
@@ -55,6 +79,7 @@ public void Advance(int count)
5579
}
5680
}
5781

82+
/// <summary>return buffer to pool and reset buffer status</summary>
5883
[MethodImpl(MethodImplOptions.AggressiveInlining)]
5984
public void Dispose()
6085
{
@@ -67,9 +92,14 @@ public void Dispose()
6792
}
6893
}
6994

95+
/// <inheritdoc/>
7096
[MethodImpl(MethodImplOptions.AggressiveInlining)]
7197
public Memory<T> GetMemory(int sizeHint = 0)
7298
{
99+
if (sizeHint < 0)
100+
{
101+
throw new ArgumentOutOfRangeException("sizeHint", "size must be greater than 0");
102+
}
73103
if (sizeHint == 0)
74104
{
75105
sizeHint = DefaultSize;
@@ -80,10 +110,14 @@ public Memory<T> GetMemory(int sizeHint = 0)
80110
}
81111
return _currentBuffer.AsMemory(_Position, sizeHint);
82112
}
83-
113+
/// <inheritdoc/>
84114
[MethodImpl(MethodImplOptions.AggressiveInlining)]
85115
public Span<T> GetSpan(int sizeHint = 0)
86116
{
117+
if (sizeHint < 0)
118+
{
119+
throw new ArgumentOutOfRangeException("sizeHint", "size must be greater than 0");
120+
}
87121
if (sizeHint == 0)
88122
{
89123
sizeHint = DefaultSize;
@@ -94,23 +128,37 @@ public Span<T> GetSpan(int sizeHint = 0)
94128
}
95129
return _currentBuffer.AsSpan(_Position, sizeHint);
96130
}
131+
/// <summary>expose current buffer as Span</summary>
97132
[MethodImpl(MethodImplOptions.AggressiveInlining)]
98133
public ReadOnlySpan<T> ToSpanUnsafe()
99134
{
100135
return _currentBuffer.AsSpan(0, _Length);
101136
}
137+
/// <summary>expose current buffer as Memory</summary>
102138
[MethodImpl(MethodImplOptions.AggressiveInlining)]
103139
public ReadOnlyMemory<T> ToMemoryUnsafe()
104140
{
105141
return _currentBuffer.AsMemory(0, _Length);
106142
}
143+
/// <summary>reset buffer status, buffer will be reallocated</summary>
107144
[MethodImpl(MethodImplOptions.AggressiveInlining)]
108-
public void Reset(int preallocateSize = DefaultSize)
145+
public void Reset(int preallocateSize)
109146
{
147+
if(preallocateSize < 0)
148+
{
149+
throw new ArgumentOutOfRangeException("preallocateSize", "size must be greater than 0");
150+
}
110151
_Pool.Return(_currentBuffer);
111152
_currentBuffer = _Pool.Rent(preallocateSize);
112153
_Length = 0;
113154
_Position = 0;
114155
}
156+
/// <summary>reset buffer status, buffer will be reused</summary>
157+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
158+
public void Reset()
159+
{
160+
_Length = 0;
161+
_Position = 0;
162+
}
115163
}
116164
}

PooledStream/PooledStream.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
<TargetFrameworks>netstandard2.1;netstandard1.1</TargetFrameworks>
55
<!-- <TargetFramework>netstandard2.1</TargetFramework> -->
66
<PackageId>PooledStream</PackageId>
7-
<PackageVersion>0.2.1.2</PackageVersion>
7+
<PackageVersion>0.3.0</PackageVersion>
88
<Authors>itn3000</Authors>
99
<Description>Efficient MemoryStream powered by System.Buffers.ArrayPool</Description>
1010
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
11-
<PackageReleaseNotes>add netstandard2.1 to TargetFramework</PackageReleaseNotes>
11+
<PackageReleaseNotes>add PooledMemoryBufferWriter</PackageReleaseNotes>
1212
<Copyright>Copyright(c) 2017 itn3000. All rights reserved</Copyright>
13-
<PackageTags>MemoryStream</PackageTags>
13+
<PackageTags>MemoryStream;ArrayPool</PackageTags>
1414
<PackageProjectUrl>https://github.com/itn3000/PooledStream/</PackageProjectUrl>
1515
<RepositoryUrl>https://github.com/itn3000/PooledStream/</RepositoryUrl>
1616
<PackageLicenseFile>LICENSE.md</PackageLicenseFile>

0 commit comments

Comments
 (0)