Releaser Rent(int capacity, out ByteBuffer buffer) { if (capacity <= SmallMessageBufferSize) { return(Rent(SmallMessageBuffers, SmallMessageBufferSize, out buffer)); } if (capacity <= LargeMessageBufferSize) { return(Rent(LargeMessageBuffers, LargeMessageBufferSize, out buffer)); } lock (MassiveBuffers) { for (int i = 0; i < MassiveBuffers.Count; i++) { if ((buffer = MassiveBuffers.Dequeue()).Span.Length >= capacity) { return(new Releaser(MassiveBuffers, buffer)); } else { MassiveBuffers.Enqueue(buffer); } } buffer = new ByteBuffer(new Memory <byte> (new byte[capacity]), CreateSocketAsyncArgs); return(new Releaser(MassiveBuffers, buffer)); } }
internal Releaser Rent(int minCapacity, out ByteBuffer buffer) { if (minCapacity <= SmallMessageBufferSize) { return(Rent(SmallMessageBuffers, SmallMessageBufferSize, out buffer)); } if (minCapacity <= MediumMessageBufferSize) { return(Rent(MediumMessageBuffers, MediumMessageBufferSize, out buffer)); } if (minCapacity <= LargeMessageBufferSize) { return(Rent(LargeMessageBuffers, LargeMessageBufferSize, out buffer)); } lock (MassiveBuffers) { for (int i = 0; i < MassiveBuffers.Count; i++) { if ((buffer = MassiveBuffers.Dequeue()).Data.Length >= minCapacity) { return(new Releaser(MassiveBuffers, buffer)); } else { MassiveBuffers.Enqueue(buffer); } } buffer = new ByteBuffer(minCapacity); return(new Releaser(MassiveBuffers, buffer)); } }
void Return(byte[] buffer) { if (buffer == null) { return; } // All buffers should be allocated in this class, so if something else is passed in that isn't the right size // We just throw an exception as someone has done something wrong. if (!Owns(buffer)) { throw new TorrentException("That buffer wasn't created by this manager"); } if (buffer.Length == SmallMessageBufferSize) { lock (SmallMessageBuffers) SmallMessageBuffers.Enqueue(buffer); } else if (buffer.Length == MediumMessageBufferSize) { lock (MediumMessageBuffers) MediumMessageBuffers.Enqueue(buffer); } else if (buffer.Length == LargeMessageBufferSize) { lock (LargeMessageBuffers) LargeMessageBuffers.Enqueue(buffer); } else if (buffer.Length > LargeMessageBufferSize) { lock (MassiveBuffers) MassiveBuffers.Enqueue(buffer); } }
public byte[] Rent(int minCapacity) { if (minCapacity <= SmallMessageBufferSize) { return(Rent(SmallMessageBuffers, SmallMessageBufferSize)); } if (minCapacity <= MediumMessageBufferSize) { return(Rent(MediumMessageBuffers, MediumMessageBufferSize)); } if (minCapacity <= LargeMessageBufferSize) { return(Rent(LargeMessageBuffers, LargeMessageBufferSize)); } lock (MassiveBuffers) { byte[] buffer; for (int i = 0; i < MassiveBuffers.Count; i++) { if ((buffer = MassiveBuffers.Dequeue()).Length >= minCapacity) { return(buffer); } else { MassiveBuffers.Enqueue(buffer); } } buffer = new byte[minCapacity]; lock (AllocatedBuffers) AllocatedBuffers.Add(buffer); return(buffer); } }