@@ -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}
0 commit comments