internal void ReturnBlocks(ICollection<byte[]> blocks, string tag)
{
if (blocks == null)
{
throw new ArgumentNullException("blocks");
}
var bytesToReturn = blocks.Count * this.BlockSize;
Interlocked.Add(ref this.smallPoolInUseSize, -bytesToReturn);
foreach (var block in blocks)
{
if (block == null || block.Length != this.BlockSize)
{
throw new ArgumentException("blocks contains buffers that are not BlockSize in length");
}
}
foreach (var block in blocks)
{
if (this.MaximumFreeSmallPoolBytes == 0 || this.SmallPoolFreeSize < this.MaximumFreeSmallPoolBytes)
{
Interlocked.Add(ref this.smallPoolFreeSize, this.BlockSize);
this.smallPool.Push(block);
}
else
{
Events.Write.MemoryStreamDiscardBuffer(Events.MemoryStreamBufferType.Small, tag,
Events.MemoryStreamDiscardReason.EnoughFree);
if (this.BlockDiscarded != null)
{
this.BlockDiscarded();
}
break;
}
}
if (this.UsageReport != null)
{
this.UsageReport(this.smallPoolInUseSize, this.smallPoolFreeSize, this.LargePoolInUseSize,
this.LargePoolFreeSize);
}
}