// 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. /// public sealed class MemoryPoolBlock : IMemoryOwner { private readonly int _length; private readonly int _offset; /// /// 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; } public void Dispose() { Pool.Return(this); } ~MemoryPoolBlock() { Pool.RefreshBlock(Slab, _offset, _length); } public void Lease() { } } }