/// <summary> /// Internal method called when a block is requested and the pool is empty. It allocates one additional slab, creates all of the /// block tracking objects, and adds them all to the pool. /// </summary> private MemoryPoolBlock2 AllocateSlab() { var slab = MemoryPoolSlab2.Create(_slabLength); _slabs.Push(slab); var basePtr = slab.ArrayPtr; var firstOffset = (int)((_blockStride - 1) - ((ulong)(basePtr + _blockStride - 1) % _blockStride)); var poolAllocationLength = _slabLength - _blockStride; var offset = firstOffset; for (; offset + _blockLength < poolAllocationLength; offset += _blockStride) { var block = MemoryPoolBlock2.Create( new ArraySegment <byte>(slab.Array, offset, _blockLength), basePtr, this, slab); Return(block); } // return last block rather than adding to pool var newBlock = MemoryPoolBlock2.Create( new ArraySegment <byte>(slab.Array, offset, _blockLength), basePtr, this, slab); return(newBlock); }
public static MemoryPoolBlock2 Create( ArraySegment <byte> data, IntPtr dataPtr, MemoryPool2 pool, MemoryPoolSlab2 slab) { return(new MemoryPoolBlock2 { Data = data, _dataArrayPtr = dataPtr, Pool = pool, Slab = slab, Start = data.Offset, End = data.Offset, }); }
/// <summary> /// Internal method called when a block is requested and the pool is empty. It allocates one additional slab, creates all of the /// block tracking objects, and adds them all to the pool. /// </summary> private void AllocateSlab() { var slab = MemoryPoolSlab2.Create(_slabLength); _slabs.Push(slab); var basePtr = slab.ArrayPtr; var firstOffset = (int)((_blockStride - 1) - ((ulong)(basePtr + _blockStride - 1) % _blockStride)); for (var offset = firstOffset; offset + _blockLength <= _slabLength; offset += _blockStride) { var block = MemoryPoolBlock2.Create( new ArraySegment <byte>(slab.Array, offset, _blockLength), basePtr, this, slab); Return(block); } }
public static MemoryPoolBlock2 Create( ArraySegment<byte> data, IntPtr dataPtr, MemoryPool2 pool, MemoryPoolSlab2 slab) { return new MemoryPoolBlock2 { Data = data, _dataArrayPtr = dataPtr, Pool = pool, Slab = slab, Start = data.Offset, End = data.Offset, }; }