using System;
using System.Buffers;
using System.Threading;
namespace Ryujinx.Common.Memory
{
public partial class ByteMemoryPool
{
///
/// Represents a that wraps an array rented from
/// and exposes it as
/// with a length of the requested size.
///
private sealed class ByteMemoryPoolBuffer : IMemoryOwner
{
private byte[] _array;
private readonly int _length;
public ByteMemoryPoolBuffer(int length)
{
_array = ArrayPool.Shared.Rent(length);
_length = length;
}
///
/// Returns a belonging to this owner.
///
public Memory Memory
{
get
{
byte[] array = _array;
ObjectDisposedException.ThrowIf(array is null, this);
return new Memory(array, 0, _length);
}
}
public void Dispose()
{
var array = Interlocked.Exchange(ref _array, null);
if (array != null)
{
ArrayPool.Shared.Return(array);
}
}
}
}
}