internal static PooledArrayBuffer <T> NewInstance(int maxCapacity) { PooledArrayBuffer <T> buf = Recycler.Take(); buf.SetReferenceCount(1); buf.MaxCapacity = maxCapacity; buf.SetIndex(0, 0); buf.DiscardMarkers(); return(buf); }
internal void Reallocate(PooledArrayBuffer <T> buf, int newCapacity, bool freeOldMemory) { Contract.Requires(newCapacity >= 0 && newCapacity <= buf.MaxCapacity); int oldCapacity = buf.Length; if (oldCapacity == newCapacity) { return; } PoolChunk <T> oldChunk = buf.Chunk; long oldHandle = buf.Handle; T[] oldMemory = buf.Memory; int oldOffset = buf.Offset; int oldMaxLength = buf.MaxLength; int readerIndex = buf.ReaderIndex; int writerIndex = buf.WriterIndex; this.Allocate(this.Parent.ThreadCache(), buf, newCapacity); if (newCapacity > oldCapacity) { MemoryCopy( oldMemory, oldOffset, buf.Memory, buf.Offset, oldCapacity); } else if (newCapacity < oldCapacity) { if (readerIndex < newCapacity) { if (writerIndex > newCapacity) { writerIndex = newCapacity; } MemoryCopy( oldMemory, oldOffset + readerIndex, buf.Memory, buf.Offset + readerIndex, writerIndex - readerIndex); } else { readerIndex = writerIndex = newCapacity; } } buf.SetIndex(readerIndex, writerIndex); if (freeOldMemory) { this.Free(oldChunk, oldHandle, oldMaxLength, buf.Cache); } }