1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
3
4
using System . Text ;
4
5
5
6
namespace QRCoder
@@ -21,15 +22,15 @@ private struct Polynom : IDisposable
21
22
public Polynom ( int count )
22
23
{
23
24
_length = 0 ;
24
- _polyItems = Allocate ( count ) ;
25
+ _polyItems = RentArray ( count ) ;
25
26
}
26
27
27
28
/// <summary>
28
29
/// Adds a polynomial term to the polynomial.
29
30
/// </summary>
30
31
public void Add ( PolynomItem item )
31
32
{
32
- EnsureCapacity ( _length + 1 ) ;
33
+ AssertCapacity ( _length + 1 ) ;
33
34
_polyItems [ _length ++ ] = item ;
34
35
}
35
36
@@ -53,17 +54,26 @@ public void RemoveAt(int index)
53
54
public PolynomItem this [ int index ]
54
55
{
55
56
get {
56
- if ( index < 0 || index >= _length )
57
- throw new IndexOutOfRangeException ( ) ;
57
+ if ( ( uint ) index >= _length )
58
+ ThrowIndexOutOfRangeException ( ) ;
58
59
return _polyItems [ index ] ;
59
60
}
60
61
set {
61
- if ( index < 0 || index >= _length )
62
- throw new IndexOutOfRangeException ( ) ;
62
+ if ( ( uint ) index >= _length )
63
+ ThrowIndexOutOfRangeException ( ) ;
63
64
_polyItems [ index ] = value ;
64
65
}
65
66
}
66
67
68
+ #if NET6_0_OR_GREATER
69
+ [ StackTraceHidden ]
70
+ #endif
71
+ private static void ThrowIndexOutOfRangeException ( )
72
+ {
73
+ throw new IndexOutOfRangeException ( ) ;
74
+ }
75
+
76
+
67
77
/// <summary>
68
78
/// Gets the number of polynomial terms in the polynomial.
69
79
/// </summary>
@@ -100,7 +110,9 @@ public void Sort(Func<PolynomItem, PolynomItem, int> comparer)
100
110
if ( comparer == null ) throw new ArgumentNullException ( nameof ( comparer ) ) ;
101
111
102
112
var items = _polyItems ;
103
- if ( items == null || _length <= 1 )
113
+ if ( items == null ) throw new ObjectDisposedException ( nameof ( Polynom ) ) ;
114
+
115
+ if ( _length <= 1 )
104
116
{
105
117
return ; // Nothing to sort if the list is empty or contains only one element
106
118
}
@@ -158,27 +170,30 @@ public override string ToString()
158
170
/// <inheritdoc/>
159
171
public void Dispose ( )
160
172
{
161
- Free ( _polyItems ) ;
173
+ ReturnArray ( _polyItems ) ;
162
174
_polyItems = null ;
163
175
}
164
176
165
177
/// <summary>
166
178
/// Ensures that the polynomial has enough capacity to store the specified number of polynomial terms.
167
179
/// </summary>
168
- private void EnsureCapacity ( int min )
180
+ private void AssertCapacity ( int min )
169
181
{
170
182
if ( _polyItems . Length < min )
171
183
{
172
184
// All math by QRCoder should be done with fixed polynomials, so we don't need to grow the capacity.
173
185
ThrowNotSupportedException ( ) ;
174
186
175
187
// Sample code for growing the capacity:
176
- //var newArray = Allocate (Math.Max(min - 1, 8) * 2); // Grow by 2x, but at least by 8
188
+ //var newArray = RentArray (Math.Max(min - 1, 8) * 2); // Grow by 2x, but at least by 8
177
189
//Array.Copy(_polyItems, newArray, _length);
178
- //Free (_polyItems);
190
+ //ReturnArray (_polyItems);
179
191
//_polyItems = newArray;
180
192
}
181
193
194
+ #if NET6_0_OR_GREATER
195
+ [ StackTraceHidden ]
196
+ #endif
182
197
void ThrowNotSupportedException ( )
183
198
{
184
199
throw new NotSupportedException ( "The polynomial capacity is fixed and cannot be increased." ) ;
@@ -187,29 +202,29 @@ void ThrowNotSupportedException()
187
202
188
203
#if NETCOREAPP
189
204
/// <summary>
190
- /// Allocates memory for the polynomial terms.
205
+ /// Rents memory for the polynomial terms from the shared memory pool .
191
206
/// </summary>
192
- private static PolynomItem [ ] Allocate ( int count )
207
+ private static PolynomItem [ ] RentArray ( int count )
193
208
{
194
209
return System . Buffers . ArrayPool < PolynomItem > . Shared . Rent ( count ) ;
195
210
}
196
211
197
212
/// <summary>
198
- /// Frees memory allocated for the polynomial terms.
213
+ /// Returns memory allocated for the polynomial terms back to the shared memory pool .
199
214
/// </summary>
200
- private static void Free ( PolynomItem [ ] array )
215
+ private static void ReturnArray ( PolynomItem [ ] array )
201
216
{
202
217
System . Buffers . ArrayPool < PolynomItem > . Shared . Return ( array ) ;
203
218
}
204
219
#else
205
220
// Implement a poor-man's array pool for .NET Framework
206
221
[ ThreadStatic ]
207
222
private static List < PolynomItem [ ] > _arrayPool ;
208
-
223
+
209
224
/// <summary>
210
- /// Allocates memory for the polynomial terms.
225
+ /// Rents memory for the polynomial terms from a shared memory pool .
211
226
/// </summary>
212
- private static PolynomItem [ ] Allocate ( int count )
227
+ private static PolynomItem [ ] RentArray ( int count )
213
228
{
214
229
if ( count <= 0 )
215
230
ThrowArgumentOutOfRangeException ( ) ;
@@ -236,11 +251,11 @@ void ThrowArgumentOutOfRangeException()
236
251
throw new ArgumentOutOfRangeException ( nameof ( count ) , "The count must be a positive number." ) ;
237
252
}
238
253
}
239
-
254
+
240
255
/// <summary>
241
- /// Frees memory allocated for the polynomial terms.
256
+ /// Returns memory allocated for the polynomial terms back to a shared memory pool .
242
257
/// </summary>
243
- private static void Free ( PolynomItem [ ] array )
258
+ private static void ReturnArray ( PolynomItem [ ] array )
244
259
{
245
260
if ( array == null )
246
261
ThrowArgumentNullException ( ) ;
0 commit comments