// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Runtime.InteropServices;
namespace System.Buffers
{
///
/// Block tracking object used by the byte buffer memory pool. A slab is a large allocation which is divided into smaller blocks. The
/// individual blocks are then treated as independent array segments.
///
internal sealed class MemoryPoolBlock : IMemoryOwner
{
private readonly int _offset;
private readonly int _length;
///
/// This object cannot be instantiated outside of the static Create method
///
internal MemoryPoolBlock(SlabMemoryPool pool, MemoryPoolSlab slab, int offset, int length)
{
_offset = offset;
_length = length;
Pool = pool;
Slab = slab;
Memory = MemoryMarshal.CreateFromPinnedArray(slab.Array, _offset, _length);
}
///
/// Back-reference to the memory pool which this block was allocated from. It may only be returned to this pool.
///
public SlabMemoryPool Pool { get; }
///
/// Back-reference to the slab from which this block was taken, or null if it is one-time-use memory.
///
public MemoryPoolSlab Slab { get; }
public Memory Memory { get; }
~MemoryPoolBlock()
{
if (Slab != null && Slab.IsActive)
{
// Need to make a new object because this one is being finalized
Pool.Return(new MemoryPoolBlock(Pool, Slab, _offset, _length));
}
}
public void Dispose()
{
Pool.Return(this);
}
public void Lease()
{
}
}
}