@@ -32,6 +32,11 @@ public sealed class MemoryOwner<T> : IMemoryOwner<T>
32
32
private readonly int length ;
33
33
#pragma warning restore IDE0032
34
34
35
+ /// <summary>
36
+ /// The <see cref="ArrayPool{T}"/> instance used to rent <see cref="array"/>.
37
+ /// </summary>
38
+ private readonly ArrayPool < T > pool ;
39
+
35
40
/// <summary>
36
41
/// The underlying <typeparamref name="T"/> array.
37
42
/// </summary>
@@ -41,12 +46,14 @@ public sealed class MemoryOwner<T> : IMemoryOwner<T>
41
46
/// Initializes a new instance of the <see cref="MemoryOwner{T}"/> class with the specified parameters.
42
47
/// </summary>
43
48
/// <param name="length">The length of the new memory buffer to use.</param>
49
+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance to use.</param>
44
50
/// <param name="mode">Indicates the allocation mode to use for the new buffer to rent.</param>
45
- private MemoryOwner ( int length , AllocationMode mode )
51
+ private MemoryOwner ( int length , ArrayPool < T > pool , AllocationMode mode )
46
52
{
47
53
this . start = 0 ;
48
54
this . length = length ;
49
- this . array = ArrayPool < T > . Shared . Rent ( length ) ;
55
+ this . pool = pool ;
56
+ this . array = pool . Rent ( length ) ;
50
57
51
58
if ( mode == AllocationMode . Clear )
52
59
{
@@ -57,20 +64,22 @@ private MemoryOwner(int length, AllocationMode mode)
57
64
/// <summary>
58
65
/// Initializes a new instance of the <see cref="MemoryOwner{T}"/> class with the specified parameters.
59
66
/// </summary>
60
- /// <param name="array">The input <typeparamref name="T"/> array to use.</param>
61
67
/// <param name="start">The starting offset within <paramref name="array"/>.</param>
62
68
/// <param name="length">The length of the array to use.</param>
63
- private MemoryOwner ( T [ ] array , int start , int length )
69
+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance currently in use.</param>
70
+ /// <param name="array">The input <typeparamref name="T"/> array to use.</param>
71
+ private MemoryOwner ( int start , int length , ArrayPool < T > pool , T [ ] array )
64
72
{
65
73
this . start = start ;
66
74
this . length = length ;
75
+ this . pool = pool ;
67
76
this . array = array ;
68
77
}
69
78
70
79
/// <summary>
71
80
/// Finalizes an instance of the <see cref="MemoryOwner{T}"/> class.
72
81
/// </summary>
73
- ~ MemoryOwner ( ) => this . Dispose ( ) ;
82
+ ~ MemoryOwner ( ) => Dispose ( ) ;
74
83
75
84
/// <summary>
76
85
/// Gets an empty <see cref="MemoryOwner{T}"/> instance.
@@ -79,7 +88,7 @@ private MemoryOwner(T[] array, int start, int length)
79
88
public static MemoryOwner < T > Empty
80
89
{
81
90
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
82
- get => new MemoryOwner < T > ( 0 , AllocationMode . Default ) ;
91
+ get => new MemoryOwner < T > ( 0 , ArrayPool < T > . Shared , AllocationMode . Default ) ;
83
92
}
84
93
85
94
/// <summary>
@@ -91,19 +100,44 @@ public static MemoryOwner<T> Empty
91
100
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
92
101
[ Pure ]
93
102
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
94
- public static MemoryOwner < T > Allocate ( int size ) => new MemoryOwner < T > ( size , AllocationMode . Default ) ;
103
+ public static MemoryOwner < T > Allocate ( int size ) => new MemoryOwner < T > ( size , ArrayPool < T > . Shared , AllocationMode . Default ) ;
104
+
105
+ /// <summary>
106
+ /// Creates a new <see cref="MemoryOwner{T}"/> instance with the specified parameters.
107
+ /// </summary>
108
+ /// <param name="size">The length of the new memory buffer to use.</param>
109
+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance currently in use.</param>
110
+ /// <returns>A <see cref="MemoryOwner{T}"/> instance of the requested length.</returns>
111
+ /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="size"/> is not valid.</exception>
112
+ /// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
113
+ [ Pure ]
114
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
115
+ public static MemoryOwner < T > Allocate ( int size , ArrayPool < T > pool ) => new MemoryOwner < T > ( size , pool , AllocationMode . Default ) ;
116
+
117
+ /// <summary>
118
+ /// Creates a new <see cref="MemoryOwner{T}"/> instance with the specified parameters.
119
+ /// </summary>
120
+ /// <param name="size">The length of the new memory buffer to use.</param>
121
+ /// <param name="mode">Indicates the allocation mode to use for the new buffer to rent.</param>
122
+ /// <returns>A <see cref="MemoryOwner{T}"/> instance of the requested length.</returns>
123
+ /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="size"/> is not valid.</exception>
124
+ /// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
125
+ [ Pure ]
126
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
127
+ public static MemoryOwner < T > Allocate ( int size , AllocationMode mode ) => new MemoryOwner < T > ( size , ArrayPool < T > . Shared , mode ) ;
95
128
96
129
/// <summary>
97
130
/// Creates a new <see cref="MemoryOwner{T}"/> instance with the specified parameters.
98
131
/// </summary>
99
132
/// <param name="size">The length of the new memory buffer to use.</param>
133
+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance currently in use.</param>
100
134
/// <param name="mode">Indicates the allocation mode to use for the new buffer to rent.</param>
101
135
/// <returns>A <see cref="MemoryOwner{T}"/> instance of the requested length.</returns>
102
136
/// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="size"/> is not valid.</exception>
103
137
/// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
104
138
[ Pure ]
105
139
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
106
- public static MemoryOwner < T > Allocate ( int size , AllocationMode mode ) => new MemoryOwner < T > ( size , mode ) ;
140
+ public static MemoryOwner < T > Allocate ( int size , ArrayPool < T > pool , AllocationMode mode ) => new MemoryOwner < T > ( size , pool , mode ) ;
107
141
108
142
/// <summary>
109
143
/// Gets the number of items in the current instance
@@ -210,7 +244,7 @@ public MemoryOwner<T> Slice(int start, int length)
210
244
ThrowInvalidLengthException ( ) ;
211
245
}
212
246
213
- return new MemoryOwner < T > ( array ! , start , length ) ;
247
+ return new MemoryOwner < T > ( start , length , this . pool , array ! ) ;
214
248
}
215
249
216
250
/// <inheritdoc/>
@@ -227,7 +261,7 @@ public void Dispose()
227
261
228
262
this . array = null ;
229
263
230
- ArrayPool < T > . Shared . Return ( array ) ;
264
+ this . pool . Return ( array ) ;
231
265
}
232
266
233
267
/// <inheritdoc/>
@@ -251,7 +285,6 @@ public override string ToString()
251
285
/// <summary>
252
286
/// Throws an <see cref="ObjectDisposedException"/> when <see cref="array"/> is <see langword="null"/>.
253
287
/// </summary>
254
- [ MethodImpl ( MethodImplOptions . NoInlining ) ]
255
288
private static void ThrowObjectDisposedException ( )
256
289
{
257
290
throw new ObjectDisposedException ( nameof ( MemoryOwner < T > ) , "The current buffer has already been disposed" ) ;
@@ -260,7 +293,6 @@ private static void ThrowObjectDisposedException()
260
293
/// <summary>
261
294
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the <see cref="start"/> is invalid.
262
295
/// </summary>
263
- [ MethodImpl ( MethodImplOptions . NoInlining ) ]
264
296
private static void ThrowInvalidOffsetException ( )
265
297
{
266
298
throw new ArgumentOutOfRangeException ( nameof ( start ) , "The input start parameter was not valid" ) ;
@@ -269,7 +301,6 @@ private static void ThrowInvalidOffsetException()
269
301
/// <summary>
270
302
/// Throws an <see cref="ArgumentOutOfRangeException"/> when the <see cref="length"/> is invalid.
271
303
/// </summary>
272
- [ MethodImpl ( MethodImplOptions . NoInlining ) ]
273
304
private static void ThrowInvalidLengthException ( )
274
305
{
275
306
throw new ArgumentOutOfRangeException ( nameof ( length ) , "The input length parameter was not valid" ) ;
0 commit comments