using System; using System.Buffers; using System.Runtime.CompilerServices; namespace DotRecast.Core.Buffers { public static class RcRentedArray { public static RcRentedArray Rent(int minimumLength) { var array = ArrayPool.Shared.Rent(minimumLength); return new RcRentedArray(ArrayPool.Shared, array, minimumLength); } } public class RcRentedArray : IDisposable { private ArrayPool _owner; private T[] _array; public int Length { get; } public bool IsDisposed => null == _owner || null == _array; internal RcRentedArray(ArrayPool owner, T[] array, int length) { _owner = owner; _array = array; Length = length; } public ref T this[int index] { [MethodImpl(MethodImplOptions.AggressiveInlining)] get { RcThrowHelper.ThrowExceptionIfIndexOutOfRange(index, Length); return ref _array[index]; } } public T[] AsArray() { return _array; } public Span AsSpan() { return new Span(_array, 0, Length); } public void Dispose() { if (null != _owner && null != _array) { _owner.Return(_array, true); _owner = null; _array = null; } } } }