@@ -42,6 +42,11 @@ public readonly ref struct SpanOwner<T>
4242 private readonly int length ;
4343#pragma warning restore IDE0032
4444
45+ /// <summary>
46+ /// The <see cref="ArrayPool{T}"/> instance used to rent <see cref="array"/>.
47+ /// </summary>
48+ private readonly ArrayPool < T > pool ;
49+
4550 /// <summary>
4651 /// The underlying <typeparamref name="T"/> array.
4752 /// </summary>
@@ -51,11 +56,13 @@ public readonly ref struct SpanOwner<T>
5156 /// Initializes a new instance of the <see cref="SpanOwner{T}"/> struct with the specified parameters.
5257 /// </summary>
5358 /// <param name="length">The length of the new memory buffer to use.</param>
59+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance to use.</param>
5460 /// <param name="mode">Indicates the allocation mode to use for the new buffer to rent.</param>
55- private SpanOwner ( int length , AllocationMode mode )
61+ private SpanOwner ( int length , ArrayPool < T > pool , AllocationMode mode )
5662 {
5763 this . length = length ;
58- this . array = ArrayPool < T > . Shared . Rent ( length ) ;
64+ this . pool = pool ;
65+ this . array = pool . Rent ( length ) ;
5966
6067 if ( mode == AllocationMode . Clear )
6168 {
@@ -70,7 +77,7 @@ private SpanOwner(int length, AllocationMode mode)
7077 public static SpanOwner < T > Empty
7178 {
7279 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
73- get => new SpanOwner < T > ( 0 , AllocationMode . Default ) ;
80+ get => new SpanOwner < T > ( 0 , ArrayPool < T > . Shared , AllocationMode . Default ) ;
7481 }
7582
7683 /// <summary>
@@ -82,19 +89,44 @@ public static SpanOwner<T> Empty
8289 /// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
8390 [ Pure ]
8491 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
85- public static SpanOwner < T > Allocate ( int size ) => new SpanOwner < T > ( size , AllocationMode . Default ) ;
92+ public static SpanOwner < T > Allocate ( int size ) => new SpanOwner < T > ( size , ArrayPool < T > . Shared , AllocationMode . Default ) ;
93+
94+ /// <summary>
95+ /// Creates a new <see cref="SpanOwner{T}"/> instance with the specified parameters.
96+ /// </summary>
97+ /// <param name="size">The length of the new memory buffer to use.</param>
98+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance to use.</param>
99+ /// <returns>A <see cref="SpanOwner{T}"/> instance of the requested length.</returns>
100+ /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="size"/> is not valid.</exception>
101+ /// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
102+ [ Pure ]
103+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
104+ public static SpanOwner < T > Allocate ( int size , ArrayPool < T > pool ) => new SpanOwner < T > ( size , pool , AllocationMode . Default ) ;
105+
106+ /// <summary>
107+ /// Creates a new <see cref="SpanOwner{T}"/> instance with the specified parameters.
108+ /// </summary>
109+ /// <param name="size">The length of the new memory buffer to use.</param>
110+ /// <param name="mode">Indicates the allocation mode to use for the new buffer to rent.</param>
111+ /// <returns>A <see cref="SpanOwner{T}"/> instance of the requested length.</returns>
112+ /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="size"/> is not valid.</exception>
113+ /// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
114+ [ Pure ]
115+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
116+ public static SpanOwner < T > Allocate ( int size , AllocationMode mode ) => new SpanOwner < T > ( size , ArrayPool < T > . Shared , mode ) ;
86117
87118 /// <summary>
88119 /// Creates a new <see cref="SpanOwner{T}"/> instance with the specified parameters.
89120 /// </summary>
90121 /// <param name="size">The length of the new memory buffer to use.</param>
122+ /// <param name="pool">The <see cref="ArrayPool{T}"/> instance to use.</param>
91123 /// <param name="mode">Indicates the allocation mode to use for the new buffer to rent.</param>
92124 /// <returns>A <see cref="SpanOwner{T}"/> instance of the requested length.</returns>
93125 /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="size"/> is not valid.</exception>
94126 /// <remarks>This method is just a proxy for the <see langword="private"/> constructor, for clarity.</remarks>
95127 [ Pure ]
96128 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
97- public static SpanOwner < T > Allocate ( int size , AllocationMode mode ) => new SpanOwner < T > ( size , mode ) ;
129+ public static SpanOwner < T > Allocate ( int size , ArrayPool < T > pool , AllocationMode mode ) => new SpanOwner < T > ( size , pool , mode ) ;
98130
99131 /// <summary>
100132 /// Gets the number of items in the current instance
@@ -111,7 +143,7 @@ public int Length
111143 public Span < T > Span
112144 {
113145 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
114- get => new Span < T > ( array , 0 , this . length ) ;
146+ get => new Span < T > ( this . array , 0 , this . length ) ;
115147 }
116148
117149 /// <summary>
@@ -122,7 +154,7 @@ public Span<T> Span
122154 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
123155 public ref T DangerousGetReference ( )
124156 {
125- return ref array . DangerousGetReference ( ) ;
157+ return ref this . array . DangerousGetReference ( ) ;
126158 }
127159
128160 /// <summary>
@@ -131,7 +163,7 @@ public ref T DangerousGetReference()
131163 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
132164 public void Dispose ( )
133165 {
134- ArrayPool < T > . Shared . Return ( array ) ;
166+ this . pool . Return ( this . array ) ;
135167 }
136168
137169 /// <inheritdoc/>
0 commit comments