Microsoft.AspNet.Server.Kestrel.Infrastructure.MemoryPool2.Return C# (CSharp) Method

Return() public method

Called to return a block to the pool. Once Return has been called the memory no longer belongs to the caller, and Very Bad Things will happen if the memory is read of modified subsequently. If a caller fails to call Return and the block tracking object is garbage collected, the block tracking object's finalizer will automatically re-create and return a new tracking object into the pool. This will only happen if there is a bug in the server, however it is necessary to avoid leaving "dead zones" in the slab due to lost block tracking objects.
public Return ( Microsoft.AspNet.Server.Kestrel.Infrastructure.MemoryPoolBlock2 block ) : void
block Microsoft.AspNet.Server.Kestrel.Infrastructure.MemoryPoolBlock2 The block to return. It must have been acquired by calling Lease on the same memory pool instance.
return void
        public void Return(MemoryPoolBlock2 block)
        {
            block.Reset();
            _blocks.Push(block);
        }

Usage Example

        public void GetLengthBetweenIteratorsWorks()
        {
            using (var pool = new MemoryPool2())
            {
                var block = pool.Lease(256);
                block.End += 256;
                TestAllLengths(block, 256);
                pool.Return(block);
                block = null;

                for (var fragment = 0; fragment < 256; fragment += 4)
                {
                    var next = block;
                    block = pool.Lease(4);
                    block.Next = next;
                    block.End += 4;
                }

                TestAllLengths(block, 256);

                while(block != null)
                {
                    var next = block.Next;
                    pool.Return(block);
                    block = next;
                }
            }
        }