public override byte[] TakeBuffer(int bufferSize) { InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.FindPool(bufferSize); if (bufferPool == null) { if (TraceCore.BufferPoolAllocationIsEnabled(Fx.Trace)) { TraceCore.BufferPoolAllocation(Fx.Trace, bufferSize); } return(Fx.AllocateByteArray(bufferSize)); } byte[] array = bufferPool.Take(); if (array != null) { bufferPool.DecrementCount(); return(array); } if (bufferPool.Peak == bufferPool.Limit) { bufferPool.Misses++; if (++this.totalMisses >= 8) { this.TuneQuotas(); } } if (TraceCore.BufferPoolAllocationIsEnabled(Fx.Trace)) { TraceCore.BufferPoolAllocation(Fx.Trace, bufferPool.BufferSize); } return(Fx.AllocateByteArray(bufferPool.BufferSize)); }
public override void Clear() { for (int i = 0; i < this.bufferPools.Length; i++) { InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.bufferPools[i]; bufferPool.Clear(); } }
private void TuneQuotas() { if (this.areQuotasBeingTuned) { return; } bool flag = false; try { Monitor.TryEnter(this.tuningLock, ref flag); if (!flag || this.areQuotasBeingTuned) { return; } this.areQuotasBeingTuned = true; } finally { if (flag) { Monitor.Exit(this.tuningLock); } } int num = this.FindMostStarvedPool(); if (num >= 0) { InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.bufferPools[num]; if (this.remainingMemory < (long)bufferPool.BufferSize) { int num2 = this.FindMostExcessivePool(); if (num2 >= 0) { this.DecreaseQuota(ref this.bufferPools[num2]); } } if (this.remainingMemory >= (long)bufferPool.BufferSize) { this.IncreaseQuota(ref this.bufferPools[num]); } } for (int i = 0; i < this.bufferPools.Length; i++) { InternalBufferManager.PooledBufferManager.BufferPool bufferPool2 = this.bufferPools[i]; bufferPool2.Misses = 0; } this.totalMisses = 0; this.areQuotasBeingTuned = false; }
public override void ReturnBuffer(byte[] buffer) { InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.FindPool(buffer.Length); if (bufferPool != null) { if (buffer.Length != bufferPool.BufferSize) { throw Fx.Exception.Argument("buffer", InternalSR.BufferIsNotRightSizeForBufferManager); } if (bufferPool.Return(buffer)) { bufferPool.IncrementCount(); } } }
private int FindMostStarvedPool() { long num = 0L; int result = -1; for (int i = 0; i < this.bufferPools.Length; i++) { InternalBufferManager.PooledBufferManager.BufferPool bufferPool = this.bufferPools[i]; if (bufferPool.Peak == bufferPool.Limit) { long num2 = (long)bufferPool.Misses * (long)bufferPool.BufferSize; if (num2 > num) { result = i; num = num2; } } } return(result); }
private void ChangeQuota(ref InternalBufferManager.PooledBufferManager.BufferPool bufferPool, int delta) { if (TraceCore.BufferPoolChangeQuotaIsEnabled(Fx.Trace)) { TraceCore.BufferPoolChangeQuota(Fx.Trace, bufferPool.BufferSize, delta); } InternalBufferManager.PooledBufferManager.BufferPool bufferPool2 = bufferPool; int num = bufferPool2.Limit + delta; InternalBufferManager.PooledBufferManager.BufferPool bufferPool3 = InternalBufferManager.PooledBufferManager.BufferPool.CreatePool(bufferPool2.BufferSize, num); for (int i = 0; i < num; i++) { byte[] array = bufferPool2.Take(); if (array == null) { break; } bufferPool3.Return(array); bufferPool3.IncrementCount(); } this.remainingMemory -= (long)(bufferPool2.BufferSize * delta); bufferPool = bufferPool3; }
private void IncreaseQuota(ref InternalBufferManager.PooledBufferManager.BufferPool bufferPool) { this.ChangeQuota(ref bufferPool, 1); }