public override byte[] TakeBuffer(int bufferSize)
 {
     InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.FindPool(bufferSize);
     if (bufferPool == null)
     {
         return(InternalBufferManager.AllocateByteArray(bufferSize));
     }
     byte[] numArray = bufferPool.Take();
     if (numArray != null)
     {
         bufferPool.DecrementCount();
         return(numArray);
     }
     if (bufferPool.Peak == bufferPool.Limit)
     {
         InternalBufferManager.PooledBufferManager.BufferPool misses = bufferPool;
         misses.Misses = misses.Misses + 1;
         InternalBufferManager.PooledBufferManager pooledBufferManager = this;
         int num  = pooledBufferManager.totalMisses + 1;
         int num1 = num;
         pooledBufferManager.totalMisses = num;
         if (num1 >= 8)
         {
             this.TuneQuotas();
         }
     }
     return(InternalBufferManager.AllocateByteArray(bufferPool.BufferSize));
 }
            public PooledBufferManager(long maxMemoryToPool, int maxBufferSize)
            {
                this.tuningLock      = new object();
                this.remainingMemory = maxMemoryToPool;
                List <InternalBufferManager.PooledBufferManager.BufferPool> bufferPools = new List <InternalBufferManager.PooledBufferManager.BufferPool>();
                int num = 128;

                while (true)
                {
                    long num1 = this.remainingMemory / (long)num;
                    int  num2 = (num1 > (long)2147483647 ? 2147483647 : (int)num1);
                    if (num2 > 1)
                    {
                        num2 = 1;
                    }
                    bufferPools.Add(InternalBufferManager.PooledBufferManager.BufferPool.CreatePool(num, num2));
                    InternalBufferManager.PooledBufferManager pooledBufferManager = this;
                    pooledBufferManager.remainingMemory = pooledBufferManager.remainingMemory - (long)num2 * (long)num;
                    if (num >= maxBufferSize)
                    {
                        break;
                    }
                    long num3 = (long)num * (long)2;
                    num = (num3 <= (long)maxBufferSize ? (int)num3 : maxBufferSize);
                }
                this.bufferPools = bufferPools.ToArray();
                this.bufferSizes = new int[(int)this.bufferPools.Length];
                for (int i = 0; i < (int)this.bufferPools.Length; i++)
                {
                    this.bufferSizes[i] = this.bufferPools[i].BufferSize;
                }
            }
            private void ChangeQuota(ref InternalBufferManager.PooledBufferManager.BufferPool bufferPool, int delta)
            {
                InternalBufferManager.PooledBufferManager.BufferPool bufferPool1 = bufferPool;
                int limit = bufferPool1.Limit + delta;

                InternalBufferManager.PooledBufferManager.BufferPool bufferPool2 = InternalBufferManager.PooledBufferManager.BufferPool.CreatePool(bufferPool1.BufferSize, limit);
                for (int i = 0; i < limit; i++)
                {
                    byte[] numArray = bufferPool1.Take();
                    if (numArray == null)
                    {
                        break;
                    }
                    bufferPool2.Return(numArray);
                    bufferPool2.IncrementCount();
                }
                InternalBufferManager.PooledBufferManager bufferSize = this;
                bufferSize.remainingMemory = bufferSize.remainingMemory - (long)(bufferPool1.BufferSize * delta);
                bufferPool = bufferPool2;
            }