Ejemplo n.º 1
0
 public override void Retain()
 {
     if (IsDisposed)
     {
         PipelinesThrowHelper.ThrowObjectDisposedException(nameof(MemoryPoolBlock));
     }
     Interlocked.Increment(ref _referenceCount);
 }
Ejemplo n.º 2
0
 public override Span <byte> AsSpan(int index, int length)
 {
     if (IsDisposed)
     {
         PipelinesThrowHelper.ThrowObjectDisposedException(nameof(DisposeTrackingBufferPool));
     }
     return(_array.Slice(index, length));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Moves to the next <see cref="Buffer{Byte}"/> in the <see cref="ReadableBuffer"/>
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            var segment = _segment;

            if (segment == null)
            {
                return(false);
            }

            if (segment is BufferSegment bufferSegment)
            {
                var start = _startIndex;
                var end   = bufferSegment.End;

                if (segment == _endSegment)
                {
                    end      = _endIndex;
                    _segment = null;
                }
                else
                {
                    _segment = bufferSegment.Next;
                    if (_segment == null)
                    {
                        if (_endSegment != null)
                        {
                            ThrowEndNotSeen();
                        }
                    }
                    else
                    {
                        _startIndex = bufferSegment.Next.Start;
                    }
                }

                Current        = bufferSegment.Memory.Slice(start, end - start);
                _cursorSegment = bufferSegment;
                _cursorStart   = start;
                return(true);
            }

            if (segment is byte[] array)
            {
                Current        = new Memory <byte>(array, _startIndex, _endIndex - _startIndex);
                _cursorSegment = segment;
                _cursorStart   = _startIndex;

                if (_segment != _endSegment)
                {
                    ThrowEndNotSeen();
                }

                _segment = null;
                return(true);
            }

            PipelinesThrowHelper.ThrowNotSupportedException();
            return(default);
Ejemplo n.º 4
0
 protected override bool TryGetArray(out ArraySegment <byte> arraySegment)
 {
     if (IsDisposed)
     {
         PipelinesThrowHelper.ThrowObjectDisposedException(nameof(DisposeTrackingBufferPool));
     }
     arraySegment = new ArraySegment <byte>(_array);
     return(true);
 }
Ejemplo n.º 5
0
 // In kestrel both MemoryPoolBlock and OwnedMemory end up in the same assembly so
 // this method access modifiers need to be `protected internal`
 protected override bool TryGetArray(out ArraySegment <byte> arraySegment)
 {
     if (IsDisposed)
     {
         PipelinesThrowHelper.ThrowObjectDisposedException(nameof(MemoryPoolBlock));
     }
     arraySegment = new ArraySegment <byte>(Slab.Array, _offset, _length);
     return(true);
 }
Ejemplo n.º 6
0
        public override OwnedMemory <byte> Rent(int size)
        {
            if (size > _blockLength)
            {
                PipelinesThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockLength);
            }

            var block = Lease();

            return(block);
        }
Ejemplo n.º 7
0
 public override MemoryHandle Pin()
 {
     if (IsDisposed)
     {
         PipelinesThrowHelper.ThrowObjectDisposedException(nameof(MemoryPoolBlock));
     }
     Retain();
     unsafe
     {
         return(new MemoryHandle(this, (Slab.NativePointer + _offset).ToPointer()));
     }
 }
Ejemplo n.º 8
0
 public override MemoryHandle Pin(int byteOffset = 0)
 {
     Retain();   // checks IsDisposed
     if (byteOffset < 0 || byteOffset > _length)
     {
         PipelinesThrowHelper.ThrowArgumentOutOfRangeException(_length, byteOffset);
     }
     unsafe
     {
         return(new MemoryHandle(this, (Slab.NativePointer + _offset + byteOffset).ToPointer()));
     }
 }
Ejemplo n.º 9
0
        public override bool Release()
        {
            int newRefCount = Interlocked.Decrement(ref _referenceCount);

            if (newRefCount < 0)
            {
                PipelinesThrowHelper.ThrowInvalidOperationException(ExceptionResource.ReferenceCountZero);
            }
            if (newRefCount == 0)
            {
                OnZeroReferences();
                return(false);
            }
            return(true);
        }
Ejemplo n.º 10
0
 /// <summary>
 ///
 /// </summary>
 public void Reset()
 {
     PipelinesThrowHelper.ThrowNotSupportedException();
 }