Adds style cop (#109)

This commit is contained in:
Kamron Batman 2020-04-26 00:16:02 -07:00 committed by GitHub
parent 3e715bcb60
commit 556a17aba8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
1725 changed files with 33831 additions and 38046 deletions

View file

@ -80,15 +80,17 @@ namespace System.Buffers
/// <returns>The block that is reserved for the called. It must be passed to Return when it is no longer being used.</returns>
private MemoryPoolBlock Lease()
{
if (_isDisposed) MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPool);
if (_isDisposed)
MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPool);
if (_blocks.TryDequeue(out MemoryPoolBlock block))
if (_blocks.TryDequeue(out var block))
{
// block successfully taken from the stack - return it
block.Lease();
return block;
}
// no blocks available - grow the pool
block = AllocateSlab();
block.Lease();
@ -101,21 +103,21 @@ namespace System.Buffers
/// </summary>
private MemoryPoolBlock AllocateSlab()
{
#pragma warning disable CA2000 // Dispose objects before losing scope
var slab = MemoryPoolSlab.Create(_slabLength);
#pragma warning restore CA2000 // Dispose objects before losing scope
_slabs.Push(slab);
var basePtr = slab.NativePointer;
// Page align the blocks
var offset = (int)((((ulong)basePtr + _blockSize - 1) & ~((uint)_blockSize - 1)) - (ulong)basePtr);
// Ensure page aligned
Debug.Assert(((ulong)basePtr + (uint)offset) % _blockSize == 0);
var blockCount = (_slabLength - offset) / _blockSize;
Interlocked.Add(ref _totalAllocatedBlocks, blockCount);
MemoryPoolBlock block = null;
for (int i = 0; i < blockCount; i++)
for (var i = 0; i < blockCount; i++)
{
block = new MemoryPoolBlock(this, slab, offset, _blockSize);
@ -143,12 +145,6 @@ namespace System.Buffers
/// <param name="block">The block to return. It must have been acquired by calling Lease on the same memory pool instance.</param>
internal void Return(MemoryPoolBlock block)
{
#if BLOCK_LEASE_TRACKING
Debug.Assert(block.Pool == this, "Returned block was not leased from this pool");
Debug.Assert(block.IsLeased, $"Block being returned to pool twice: {block.Leaser}{Environment.NewLine}");
block.IsLeased = false;
#endif
if (!_isDisposed)
_blocks.Enqueue(block);
else
@ -177,12 +173,12 @@ namespace System.Buffers
_isDisposed = true;
if (disposing)
while (_slabs.TryPop(out MemoryPoolSlab slab))
while (_slabs.TryPop(out var slab))
// dispose managed state (managed objects).
slab.Dispose();
// Discard blocks in pool
while (_blocks.TryDequeue(out MemoryPoolBlock block)) GC.SuppressFinalize(block);
while (_blocks.TryDequeue(out var block)) GC.SuppressFinalize(block);
}
}
}