private void Cleanup() { if (blocks.Count == 0) { return; } for (int i = 0, count = blocks.Count; i < count; ++i) { SegmentPool.Release(blocks[i]); } blocks.Clear(); current = new Segment(); }
public void EnsureCapacity(int numBytes) { int required = position + numBytes; while (required >= Capacity) { blocks.Add(SegmentPool.Acquire()); } if (required > back) { back = required; } }
public void Trim() { int index, count; // position <- marker if (marker >= 0) { if (position != marker) { Position = (marker - front); } marker = -1; } if (position == back) { // all the bytes consumed; leave only the leading block index = 1; count = blocks.Count - 1; front = back = 0; } else { // clip out leading blocks consumed index = 0; count = position >> sizeExponent; if (count >= blocks.Count) { count = blocks.Count - 1; } front = position & remainderMask; back -= BlockSize * count; } if (count > 0) { List <Segment> blocksToRemove = blocks.GetRange(index, count); blocks.RemoveRange(index, count); for (int i = 0; i < blocksToRemove.Count; ++i) { SegmentPool.Release(blocksToRemove[i]); } } Position = 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; }
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)); } }