public static BufferWrapper GetBuffer(int RoundUpToPowerOfTwoBufferSize) { BufferWrapper bufferWrapper; if (RoundUpToPowerOfTwoBufferSize == 0) { return(BufferPool.EmptyByteBuffer); } lock (BufferPoolOriginalWeakReferenceLocking.buffers) { Stack <WeakReference> bufferStack = BufferPoolOriginalWeakReferenceLocking.GetBufferStack(RoundUpToPowerOfTwoBufferSize, BufferPoolOriginalWeakReferenceLocking.buffers); while (bufferStack.Count > 0) { byte[] target = (byte[])bufferStack.Pop().Target; if (target == null) { continue; } bufferWrapper = new BufferWrapper(target); return(bufferWrapper); } bufferWrapper = BufferPoolOriginalWeakReferenceLocking.AllocateNewBuffer(RoundUpToPowerOfTwoBufferSize); } return(bufferWrapper); }
internal static BufferWrapper GetExactBuffer(int powerOfTwoSize) { if (powerOfTwoSize == 0) { return(BufferPool.EmptyByteBuffer); } if (powerOfTwoSize < 0 || !BufferPool.IsPowerOfTwo(powerOfTwoSize)) { throw new ArgumentOutOfRangeException("powerOfTwoSize", "The buffer size must be a positive number that is a power of two"); } return(BufferPoolOriginalWeakReferenceLocking.GetBuffer(powerOfTwoSize)); }
public static BufferWrapper GetBuffer(int minBufferSize) { if (minBufferSize == 0) { return(BufferPool.EmptyByteBuffer); } if (minBufferSize < 0) { throw new ArgumentOutOfRangeException("bufferSize", (object)minBufferSize, "The buffer size must not be negative."); } return(BufferPoolOriginalWeakReferenceLocking.GetBuffer(BufferPool.RoundUpToPowerOfTwo(minBufferSize))); }
public static void ReleaseBuffer(BufferWrapper bufferWrapper) { if (object.ReferenceEquals(bufferWrapper, BufferPool.EmptyByteBuffer)) { return; } if (bufferWrapper.Buffer == null) { throw new InvalidOperationException("Buffer pointer is null which implies it is released twice to stack."); } if (((int)bufferWrapper.Buffer.Length & (int)bufferWrapper.Buffer.Length - 1) != 0) { return; } BufferPoolOriginalWeakReferenceLocking.ReleaseBuffer(bufferWrapper); }