示例#1
0
        public void EnsureCapacity(int numBytes)
        {
            int required = position + numBytes;

            while (required >= Capacity)
            {
                blocks.Add(SegmentPool.Acquire());
            }
            if (required > back)
            {
                back = required;
            }
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the Buffer class.
        /// </summary>
        public Buffer()
        {
            if (sizeExponent < 0 || 31 < sizeExponent)
            {
                throw new ArgumentOutOfRangeException();
            }
            blocks = new List <Segment>();

            blocks.Add(SegmentPool.Acquire());

            currentIndex = 0;
            current      = blocks[currentIndex];
            position     = 0;
            back         = 0;
            front        = 0;

            marker = -1;
        }
示例#3
0
        public void ListAvailableSegments(IList <ArraySegment <byte> > blockList)
        {
            if (back < Capacity)
            {
                if (level > minLevel)
                {
                    --level;
                }
            }
            else
            {
                if (level < maxLevel)
                {
                    ++level;
                }
            }
            int roomFactor     = 1 << level;
            int numWholeBlocks = (Capacity - back) >> sizeExponent;

            if (numWholeBlocks < roomFactor)
            {
                int count = (roomFactor - numWholeBlocks);
                for (int i = 0; i < count; ++i)
                {
                    blocks.Add(SegmentPool.Acquire());
                }
            }

            int backIndex  = back >> sizeExponent;
            int backOffset = back & remainderMask;

            blockList.Add(new ArraySegment <byte>(
                              blocks[backIndex].Array,
                              blocks[backIndex].Offset + backOffset,
                              BlockSize - backOffset));
            for (int i = backIndex + 1, count = blocks.Count; i < count; ++i)
            {
                blockList.Add(new ArraySegment <byte>(
                                  blocks[i].Array,
                                  blocks[i].Offset,
                                  BlockSize));
            }
        }