public override MemoryHandle Pin(int elementIndex = 0)
 {
     return(_memory.Slice(elementIndex * Unsafe.SizeOf <TTo>() / Unsafe.SizeOf <TFrom>()).Pin());
 }
Exemplo n.º 2
0
        public bool TryGet(ref Position position, out Memory <byte> value, bool advance = true)
        {
            if (position == default)
            {
                value = _first;
                if (advance)
                {
                    position.SetItem(Rest);
                }
                return(!_first.IsEmpty || _all != null);
            }
            if (position.IsEnd)
            {
                value = default;
                return(false);
            }

            var(segment, index) = position.Get <IMemoryList <byte> >();

            if (segment == null)
            {
                if (_all == null) // single segment ROB
                {
                    value = _first.Slice(index - (int)_totalLengthOrVirtualIndex);
                    if (advance)
                    {
                        position = Position.End;
                    }
                    return(true);
                }
                else
                {
                    value = default;
                    return(false);
                }
            }

            var memory = segment.Memory;

            // We need to slice off the last segment based on length of this ROB.
            // This ROB is a potentially shorted view over a longer segment list.
            var virtualIndex  = segment.VirtualIndex;
            var lengthOfFirst = virtualIndex - VirtualIndex;
            var lengthOfEnd   = lengthOfFirst + memory.Length;

            if (lengthOfEnd > Length)
            {
                if (advance)
                {
                    position = Position.End;
                }
                value = memory.Slice(0, (int)(Length - lengthOfFirst));
                if (index < value.Length)
                {
                    if (index > 0)
                    {
                        value = value.Slice(index);
                    }
                    return(true);
                }
                else
                {
                    value = Memory <byte> .Empty;
                    return(false);
                }
            }
            else
            {
                value = memory.Slice(index);
                if (advance)
                {
                    position.SetItem(segment.Rest);
                }
                return(true);
            }
        }
Exemplo n.º 3
0
        internal bool TryGetBuffer(SequencePosition start, SequencePosition end, out ReadOnlyMemory <T> data, out SequencePosition next)
        {
            if (start.GetObject() == null)
            {
                data = default;
                next = default;
                return(false);
            }

            int          startIndex = start.GetInteger();
            int          endIndex   = end.GetInteger();
            SequenceType type       = GetSequenceType();

            startIndex = GetIndex(startIndex);
            endIndex   = GetIndex(endIndex);

            switch (type)
            {
            case SequenceType.MemoryList:
                var        segment             = (IMemoryList <T>)start.GetObject();
                Memory <T> bufferSegmentMemory = segment.Memory;
                int        currentEndIndex     = bufferSegmentMemory.Length;

                if (segment == end.GetObject())
                {
                    currentEndIndex = endIndex;
                    next            = default;
                }
                else
                {
                    IMemoryList <T> nextSegment = segment.Next;
                    if (nextSegment == null)
                    {
                        if (end.GetObject() != null)
                        {
                            ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached();
                        }

                        next = default;
                    }
                    else
                    {
                        next = new SequencePosition(nextSegment, 0);
                    }
                }

                data = bufferSegmentMemory.Slice(startIndex, currentEndIndex - startIndex);
                return(true);

            case SequenceType.OwnedMemory:
                var ownedMemory = (OwnedMemory <T>)start.GetObject();
                if (ownedMemory != end.GetObject())
                {
                    ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached();
                }

                data = ownedMemory.Memory.Slice(startIndex, endIndex - startIndex);
                next = default;
                return(true);

            case SequenceType.Array:
                var array = (T[])start.GetObject();

                if (array != end.GetObject())
                {
                    ThrowHelper.ThrowInvalidOperationException_EndPositionNotReached();
                }

                data = new Memory <T>(array, startIndex, endIndex - startIndex);
                next = default;
                return(true);

            default:
                ThrowHelper.ThrowInvalidOperationException_UnexpectedSegmentType();
                next = default;
                data = default;
                return(false);
            }
        }
Exemplo n.º 4
0
 internal void CorrectMemory(int offset) => Memory = Memory.Slice(offset);