in src/RecyclableMemoryStreamManager.cs [571:605]
internal void ReturnBlocks(List<byte[]> blocks, Guid id, string tag)
{
if (blocks == null)
{
throw new ArgumentNullException(nameof(blocks));
}
long bytesToReturn = (long)blocks.Count * (long)this.BlockSize;
Interlocked.Add(ref this.smallPoolInUseSize, -bytesToReturn);
foreach (var block in blocks)
{
if (block == null || block.Length != this.BlockSize)
{
throw new ArgumentException($"{nameof(blocks)} contains buffers that are not {nameof(BlockSize)} in length.", nameof(blocks));
}
}
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
{
ReportBufferDiscarded(id, tag, Events.MemoryStreamBufferType.Small, Events.MemoryStreamDiscardReason.EnoughFree);
break;
}
}
ReportUsageReport(this.smallPoolInUseSize, this.smallPoolFreeSize, this.LargePoolInUseSize,
this.LargePoolFreeSize);
}