public override byte[] TakeBuffer(int bufferSize)
 {
     InternalBufferManager.PooledBufferManager.BufferPool pool = this.FindPool(bufferSize);
     byte[] numArray1;
     if (pool != null)
     {
         byte[] numArray2 = pool.Take();
         if (numArray2 != null)
         {
             pool.DecrementCount();
             numArray1 = numArray2;
         }
         else
         {
             if (pool.Peak == pool.Limit)
             {
                 ++pool.Misses;
                 if (++this.totalMisses >= 8)
                 {
                     this.TuneQuotas();
                 }
             }
             numArray1 = Fx.AllocateByteArray(pool.BufferSize);
         }
     }
     else
     {
         numArray1 = Fx.AllocateByteArray(bufferSize);
     }
     return(numArray1);
 }
            private void TuneQuotas()
            {
                if (this.areQuotasBeingTuned)
                {
                    return;
                }
                bool lockTaken = false;

                try
                {
                    Monitor.TryEnter(this.tuningLock, ref lockTaken);
                    if (!lockTaken || this.areQuotasBeingTuned)
                    {
                        return;
                    }
                    this.areQuotasBeingTuned = true;
                }
                finally
                {
                    if (lockTaken)
                    {
                        Monitor.Exit(this.tuningLock);
                    }
                }
                int mostStarvedPool = this.FindMostStarvedPool();

                if (mostStarvedPool >= 0)
                {
                    InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.bufferPools[mostStarvedPool];
                    if (this.remainingMemory < (long)bufferPool.BufferSize)
                    {
                        int mostExcessivePool = this.FindMostExcessivePool();
                        if (mostExcessivePool >= 0)
                        {
                            this.DecreaseQuota(ref this.bufferPools[mostExcessivePool]);
                        }
                    }
                    if (this.remainingMemory >= (long)bufferPool.BufferSize)
                    {
                        this.IncreaseQuota(ref this.bufferPools[mostStarvedPool]);
                    }
                }
                for (int index = 0; index < this.bufferPools.Length; ++index)
                {
                    this.bufferPools[index].Misses = 0;
                }
                this.totalMisses         = 0;
                this.areQuotasBeingTuned = false;
            }
 public override void ReturnBuffer(byte[] buffer)
 {
     InternalBufferManager.PooledBufferManager.BufferPool pool = this.FindPool(buffer.Length);
     if (pool == null)
     {
         return;
     }
     if (buffer.Length != pool.BufferSize)
     {
         throw Fx.Exception.Argument(nameof(buffer), InternalSR.BufferIsNotRightSizeForBufferManager);
     }
     if (!pool.Return(buffer))
     {
         return;
     }
     pool.IncrementCount();
 }
            private int FindMostStarvedPool()
            {
                long num1 = 0;
                int  num2 = -1;

                for (int index = 0; index < this.bufferPools.Length; ++index)
                {
                    InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.bufferPools[index];
                    if (bufferPool.Peak == bufferPool.Limit)
                    {
                        long num3 = (long)bufferPool.Misses * (long)bufferPool.BufferSize;
                        if (num3 > num1)
                        {
                            num2 = index;
                            num1 = num3;
                        }
                    }
                }
                return(num2);
            }
            private void ChangeQuota(ref InternalBufferManager.PooledBufferManager.BufferPool bufferPool, int delta)
            {
                InternalBufferManager.PooledBufferManager.BufferPool bufferPool1 = bufferPool;
                int limit = bufferPool1.Limit + delta;

                InternalBufferManager.PooledBufferManager.BufferPool pool = InternalBufferManager.PooledBufferManager.BufferPool.CreatePool(bufferPool1.BufferSize, limit);
                for (int index = 0; index < limit; ++index)
                {
                    byte[] buffer = bufferPool1.Take();
                    if (buffer != null)
                    {
                        pool.Return(buffer);
                        pool.IncrementCount();
                    }
                    else
                    {
                        break;
                    }
                }
                this.remainingMemory -= (long)(bufferPool1.BufferSize * delta);
                bufferPool            = pool;
            }
 private void IncreaseQuota(ref InternalBufferManager.PooledBufferManager.BufferPool bufferPool)
 {
     this.ChangeQuota(ref bufferPool, 1);
 }