private BufferPool.BufferSubPool FindPool(int size) { int index1 = 0; int num = this._pools.Length; while (index1 < num) { int index2 = (num + index1) / 2; BufferPool.BufferSubPool bufferSubPool = this._pools[index2]; if (bufferSubPool.Size == size) { return(bufferSubPool); } if (bufferSubPool.Size < size) { index1 = index2 + 1; } else { num = index2; } } if (num >= this._pools.Length) { return((BufferPool.BufferSubPool)null); } return(this._pools[index1]); }
public BufferInstance Allocate(int minSize) { Interlocked.Increment(ref this._allocationCount); Interlocked.Add(ref this._requestedAllocationBytes, minSize); BufferPool.BufferSubPool pool = this.FindPool(minSize); PoolBufferInstance poolBufferInstance; if (null != pool) { poolBufferInstance = pool.Allocate(minSize); } else { poolBufferInstance = new PoolBufferInstance(minSize); Interlocked.Increment(ref this._nonPoolAllocationCount); } Interlocked.Add(ref this._actualAllocationBytes, poolBufferInstance.Buffer.Length); poolBufferInstance.Reference(); return(poolBufferInstance); }
public void Free(BufferInstance bufferInstance) { if (!bufferInstance.Dereference()) { return; } for (int index = 0; index < bufferInstance.Buffer.Length; ++index) { bufferInstance.Buffer[index] = byte.MaxValue; } Interlocked.Increment(ref this._freeCount); Interlocked.Add(ref this._actualFreeBytes, bufferInstance.Buffer.Length); BufferPool.BufferSubPool pool = this.FindPool(bufferInstance.Buffer.Length); if (null == pool) { return; } if (pool.Size != bufferInstance.Buffer.Length) { throw new ArgumentException("Invalid buffer size", "bufferInstance"); } pool.Free((PoolBufferInstance)bufferInstance); }