/// <summary> /// Retrieves a buffer that is at least the requested length. /// </summary> /// <param name="minimumLength">The minimum length of the array needed.</param> public override T[] Rent(int minimumLength) { if (minimumLength == 0) { return(EmptyArray <T> .Instance); } var index = MemoryUtility.GetBucketIndex(minimumLength); return(index < this.buckets.Length ? this.buckets[index].Rent() : new T[minimumLength]); }
/// <summary> /// Initializes a new instance of the <see cref="SharedArrayPool{T}"/> class. /// </summary> internal SharedArrayPool() { this.buckets = new Bucket[MemoryUtility.GetBucketIndex(SharedArrayPool <T> .MaxArrayLength) + 1]; for (var i = 0; i < this.buckets.Length; i++) { this.buckets[i] = new Bucket( bufferLength: MemoryUtility.GetBucketBufferSize(i), numberOfBuffers: SharedArrayPool <T> .MaxNumberOfArraysPerBucket); } }
/// <summary> /// Returns to the pool an array that was previously obtained. /// </summary> /// <param name="array">The buffer previously obtained to return to the pool.</param> public override void Return(T[] array) { if (array.Length == 0) { return; } // Determine with what bucket this array length is associated. var index = MemoryUtility.GetBucketIndex(array.Length); if (index < this.buckets.Length) { // Return the buffer to its bucket. In the future, we might consider having Return return false // instead of dropping a bucket, in which case we could try to return to a lower-sized bucket, // just as how in Rent we allow renting from a higher-sized bucket. this.buckets[index].Return(array); } }