private void AddToPushback(ReadOnlySpan <char> c)
        {
            if (!PushBackOwner.HasValue)
            {
                PushBackOwner.Value = MemoryPool.Rent(BufferSizeHint);
            }

            var pushBackOwnerValue = PushBackOwner.Value;

            if (PushBackLength + c.Length > pushBackOwnerValue.Memory.Length)
            {
                var oldSize = pushBackOwnerValue.Memory.Length;

                var newSize  = (PushBackLength + c.Length) * 2;   // double size, because we're sharing the buffer
                var newOwner = Utils.RentMustIncrease(MemoryPool, newSize, oldSize);
                pushBackOwnerValue.Memory.CopyTo(newOwner.Memory);

                pushBackOwnerValue.Dispose();
                PushBackOwner.Value = pushBackOwnerValue = newOwner;
            }

            if (PushBackLength + c.Length > pushBackOwnerValue.Memory.Length)
            {
                Throw.InvalidOperationException($"Could not allocate large enough buffer to read headers");
            }

            c.CopyTo(PushBack.Span.Slice(PushBackLength));
            PushBackLength += c.Length;
        }
        private void ResizeCopy(int newDesiredSize)
        {
            var oldSize = CopyOwner != null ? CopyOwner.Memory.Length : 0;
            var newCopy = Utils.RentMustIncrease(MemoryPool, newDesiredSize, oldSize);

            if (CopyOwner != null)
            {
                Copy.CopyTo(newCopy.Memory.Span);

                CopyOwner.Dispose();
            }

            CopyOwner = newCopy;
        }
        private void ResizeCopy(int newDesiredSize)
        {
            newDesiredSize = Math.Max(1, newDesiredSize);

            var oldSize = CopyOwner.HasValue ? CopyOwner.Value.Memory.Length : 0;
            var newCopy = Utils.RentMustIncrease(MemoryPool, newDesiredSize, oldSize);

            if (CopyOwner.HasValue)
            {
                Copy.CopyTo(newCopy.Memory.Span);

                CopyOwner.Value.Dispose();
            }

            CopyOwner.Value = newCopy;
        }
        internal void PushBackFromOutsideBuffer(Memory <char> pushback)
        {
            if (pushback.Length > PushBack.Length)
            {
                // blow away the current buffer, we
                //   don't need any of the data in there
                var oldSize = BackingOwner.Memory.Length;

                var newSize = pushback.Length * 2;
                BackingOwner.Dispose();
                BackingOwner = Utils.RentMustIncrease(MemoryPool, newSize, oldSize);

                UpdateBufferAndPushBack();
            }

            var pushbackSpan = PushBack.Span;

            pushback.Span.CopyTo(pushbackSpan);

            InPushBack = pushback.Length;
        }