@@ -22,31 +22,55 @@ void Reallocate(int sizeHint)
22
22
}
23
23
_currentBuffer = nar ;
24
24
}
25
+ /// <summary>
26
+ /// use shared instance and preallocatedSize = 1024
27
+ /// </summary>
25
28
public PooledMemoryBufferWriter ( ) : this ( ArrayPool < T > . Shared )
26
29
{
27
30
28
31
}
32
+ /// <summary>
33
+ /// use shared instance, use preallocateSize as reserved buffer length
34
+ /// </summary>
35
+ /// <param name="preallocateSize">initial reserved buffer size</param>
29
36
public PooledMemoryBufferWriter ( int preallocateSize ) : this ( ArrayPool < T > . Shared , preallocateSize )
30
37
{
31
38
32
39
}
40
+ /// <summary>
41
+ /// use pool for memory pool
42
+ /// </summary>
43
+ /// <param name="pool">memory pool</param>
33
44
public PooledMemoryBufferWriter ( ArrayPool < T > pool ) : this ( pool , DefaultSize )
34
45
{
35
46
}
47
+ /// <summary>
48
+ /// </summary>
49
+ /// <param name="pool">memory pool</param>
50
+ /// <param name="preallocateSize">initial reserved buffer size</param>
36
51
public PooledMemoryBufferWriter ( ArrayPool < T > pool , int preallocateSize )
37
52
{
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
+ }
38
61
_Pool = pool ;
39
62
_currentBuffer = null ;
40
63
_Position = 0 ;
41
64
_Length = 0 ;
42
65
Reallocate ( preallocateSize ) ;
43
66
}
67
+ /// <inheritdoc/>
44
68
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
45
69
public void Advance ( int count )
46
70
{
47
71
if ( _Position + count > _currentBuffer . Length )
48
72
{
49
- throw new IndexOutOfRangeException ( "advance too many(" + count . ToString ( ) + ")" ) ;
73
+ throw new ArgumentOutOfRangeException ( "advance too many(" + count . ToString ( ) + ")" ) ;
50
74
}
51
75
_Position += count ;
52
76
if ( _Length < _Position )
@@ -55,6 +79,7 @@ public void Advance(int count)
55
79
}
56
80
}
57
81
82
+ /// <summary>return buffer to pool and reset buffer status</summary>
58
83
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
59
84
public void Dispose ( )
60
85
{
@@ -67,9 +92,14 @@ public void Dispose()
67
92
}
68
93
}
69
94
95
+ /// <inheritdoc/>
70
96
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
71
97
public Memory < T > GetMemory ( int sizeHint = 0 )
72
98
{
99
+ if ( sizeHint < 0 )
100
+ {
101
+ throw new ArgumentOutOfRangeException ( "sizeHint" , "size must be greater than 0" ) ;
102
+ }
73
103
if ( sizeHint == 0 )
74
104
{
75
105
sizeHint = DefaultSize ;
@@ -80,10 +110,14 @@ public Memory<T> GetMemory(int sizeHint = 0)
80
110
}
81
111
return _currentBuffer . AsMemory ( _Position , sizeHint ) ;
82
112
}
83
-
113
+ /// <inheritdoc/>
84
114
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
85
115
public Span < T > GetSpan ( int sizeHint = 0 )
86
116
{
117
+ if ( sizeHint < 0 )
118
+ {
119
+ throw new ArgumentOutOfRangeException ( "sizeHint" , "size must be greater than 0" ) ;
120
+ }
87
121
if ( sizeHint == 0 )
88
122
{
89
123
sizeHint = DefaultSize ;
@@ -94,23 +128,37 @@ public Span<T> GetSpan(int sizeHint = 0)
94
128
}
95
129
return _currentBuffer . AsSpan ( _Position , sizeHint ) ;
96
130
}
131
+ /// <summary>expose current buffer as Span</summary>
97
132
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
98
133
public ReadOnlySpan < T > ToSpanUnsafe ( )
99
134
{
100
135
return _currentBuffer . AsSpan ( 0 , _Length ) ;
101
136
}
137
+ /// <summary>expose current buffer as Memory</summary>
102
138
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
103
139
public ReadOnlyMemory < T > ToMemoryUnsafe ( )
104
140
{
105
141
return _currentBuffer . AsMemory ( 0 , _Length ) ;
106
142
}
143
+ /// <summary>reset buffer status, buffer will be reallocated</summary>
107
144
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
108
- public void Reset ( int preallocateSize = DefaultSize )
145
+ public void Reset ( int preallocateSize )
109
146
{
147
+ if ( preallocateSize < 0 )
148
+ {
149
+ throw new ArgumentOutOfRangeException ( "preallocateSize" , "size must be greater than 0" ) ;
150
+ }
110
151
_Pool . Return ( _currentBuffer ) ;
111
152
_currentBuffer = _Pool . Rent ( preallocateSize ) ;
112
153
_Length = 0 ;
113
154
_Position = 0 ;
114
155
}
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
+ }
115
163
}
116
164
}
0 commit comments