public void EnsureCapacity(int numBytes) { int required = position + numBytes; while (required >= Capacity) { blocks.Add(SegmentPool.Acquire()); } if (required > back) { back = required; } }
/// <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; }
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)); } }