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

@ -75,10 +75,10 @@ namespace System.Buffers
{
if (length < 0)
{
Debug.Assert(usingSequence, "usingSequence");
// Cast-away readonly to initialize lazy field
Volatile.Write(ref Unsafe.AsRef(length), sequence.Length);
}
return length;
}
}
@ -139,27 +139,25 @@ namespace System.Buffers
}
else
{
throw new ArgumentOutOfRangeException($"Rewind went past the start of the memory by {count}.", nameof(count));
throw new ArgumentOutOfRangeException(nameof(count), $"Rewind went past the start of the memory by {count}.");
}
}
[MethodImpl(MethodImplOptions.NoInlining)]
private void RetreatToPreviousSpan(long consumed)
{
Debug.Assert(usingSequence, "usingSequence");
ResetReader();
Advance(consumed);
}
private void ResetReader()
{
Debug.Assert(usingSequence, "usingSequence");
CurrentSpanIndex = 0;
Consumed = 0;
currentPosition = sequence.Start;
nextPosition = currentPosition;
if (sequence.TryGet(ref nextPosition, out ReadOnlyMemory<T> memory))
if (sequence.TryGet(ref nextPosition, out var memory))
{
moreData = true;
@ -184,11 +182,10 @@ namespace System.Buffers
private void GetNextSpan()
{
Debug.Assert(usingSequence, "usingSequence");
if (!sequence.IsSingleSegment)
{
SequencePosition previousNextPosition = nextPosition;
while (sequence.TryGet(ref nextPosition, out ReadOnlyMemory<T> memory))
var previousNextPosition = nextPosition;
while (sequence.TryGet(ref nextPosition, out var memory))
{
currentPosition = previousNextPosition;
if (memory.Length > 0)
@ -203,6 +200,7 @@ namespace System.Buffers
previousNextPosition = nextPosition;
}
}
moreData = false;
}
@ -234,13 +232,12 @@ namespace System.Buffers
private void AdvanceToNextSpan(long count)
{
Debug.Assert(usingSequence, "usingSequence");
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
Consumed += count;
while (moreData)
{
int remaining = CurrentSpan.Length - CurrentSpanIndex;
var remaining = CurrentSpan.Length - CurrentSpanIndex;
if (remaining > count)
{
@ -253,7 +250,6 @@ namespace System.Buffers
// push the current index to the end of the span.
CurrentSpanIndex += remaining;
count -= remaining;
Debug.Assert(count >= 0);
GetNextSpan();
@ -275,7 +271,7 @@ namespace System.Buffers
// We don't provide an advance option to allow easier utilizing of stack allocated destination spans.
// (Because we can make this method readonly we can guarantee that we won't capture the span.)
ReadOnlySpan<T> firstSpan = UnreadSpan;
var firstSpan = UnreadSpan;
if (firstSpan.Length >= destination.Length)
{
firstSpan.Slice(0, destination.Length).CopyTo(destination);
@ -292,17 +288,16 @@ namespace System.Buffers
if (Remaining < destination.Length)
return false;
ReadOnlySpan<T> firstSpan = UnreadSpan;
Debug.Assert(firstSpan.Length < destination.Length);
var firstSpan = UnreadSpan;
firstSpan.CopyTo(destination);
int copied = firstSpan.Length;
var copied = firstSpan.Length;
SequencePosition next = nextPosition;
while (sequence.TryGet(ref next, out ReadOnlyMemory<T> nextSegment))
var next = nextPosition;
while (sequence.TryGet(ref next, out var nextSegment))
if (nextSegment.Length > 0)
{
ReadOnlySpan<T> nextSpan = nextSegment.Span;
int toCopy = Math.Min(nextSpan.Length, destination.Length - copied);
var nextSpan = nextSegment.Span;
var toCopy = Math.Min(nextSpan.Length, destination.Length - copied);
nextSpan.Slice(0, toCopy).CopyTo(destination.Slice(copied));
copied += toCopy;
if (copied >= destination.Length) break;

View file

@ -14,7 +14,7 @@ namespace System.Buffers
private static unsafe bool TryRead<T>(ref this BufferReader<byte> reader, out T value)
where T : unmanaged
{
ReadOnlySpan<byte> span = reader.UnreadSpan;
var span = reader.UnreadSpan;
if (span.Length < sizeof(T)) return TryReadMultisegment(ref reader, out value);
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(span));
@ -25,11 +25,9 @@ namespace System.Buffers
private static unsafe bool TryReadMultisegment<T>(ref BufferReader<byte> reader, out T value)
where T : unmanaged
{
Debug.Assert(reader.UnreadSpan.Length < sizeof(T), "reader.UnreadSpan.Length < sizeof(T)");
// Not enough data in the current segment, try to peek for the data we need.
T buffer = default;
Span<byte> tempSpan = new Span<byte>(&buffer, sizeof(T));
var tempSpan = new Span<byte>(&buffer, sizeof(T));
if (!reader.TryCopyTo(tempSpan))
{
@ -153,7 +151,6 @@ namespace System.Buffers
return false;
}
public static bool TryReadLittleEndian(ref this BufferReader<byte> reader, out ulong value)
{
if (TryReadLittleEndian(ref reader, out long signedvalue))

View file

@ -16,6 +16,7 @@ namespace System.Buffers
/// relocated and enables any subsections of the array to be used as native memory pointers to P/Invoked API calls.
/// </summary>
private GCHandle _gcHandle;
private bool _isDisposed;
public MemoryPoolSlab(byte[] data)

View file

@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;
namespace System.Buffers
{
@ -16,7 +15,9 @@ namespace System.Buffers
[MethodImpl(MethodImplOptions.NoInlining)]
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException(int sourceLength, int offset) =>
(uint)offset > (uint)sourceLength ? new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.offset)) : new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.length));
(uint)offset > (uint)sourceLength
? new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.offset))
: new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.length));
public static void ThrowArgumentOutOfRangeException_BufferRequestTooLarge(int maxSize)
{
@ -29,17 +30,15 @@ namespace System.Buffers
}
[MethodImpl(MethodImplOptions.NoInlining)]
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException_BufferRequestTooLarge(int maxSize) => new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.size), $"Cannot allocate more than {maxSize} bytes in a single buffer");
private static ArgumentOutOfRangeException GetArgumentOutOfRangeException_BufferRequestTooLarge(int maxSize) =>
new ArgumentOutOfRangeException(GetArgumentName(ExceptionArgument.size),
$"Cannot allocate more than {maxSize} bytes in a single buffer");
[MethodImpl(MethodImplOptions.NoInlining)]
private static ObjectDisposedException GetObjectDisposedException(ExceptionArgument argument) => new ObjectDisposedException(GetArgumentName(argument));
private static ObjectDisposedException GetObjectDisposedException(ExceptionArgument argument) =>
new ObjectDisposedException(GetArgumentName(argument));
private static string GetArgumentName(ExceptionArgument argument)
{
Debug.Assert(Enum.IsDefined(typeof(ExceptionArgument), argument), "The enum value is not defined, please check the ExceptionArgument Enum.");
return argument.ToString();
}
private static string GetArgumentName(ExceptionArgument argument) => argument.ToString();
public enum ExceptionArgument
{

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);
}
}
}

View file

@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright (C) 2019 - ModernUO Development Team *
* Copyright (C) 2019-2020 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: SpanWriter.cs - Created: 2019/08/05 - Updated: 2019/12/24 *
* *
@ -176,7 +176,7 @@ namespace Server.Buffers
/// </summary>
public void Write(ReadOnlySpan<byte> input)
{
int size = Math.Min(input.Length, Length - Position);
var size = Math.Min(input.Length, Length - Position);
input.Slice(0, size).CopyTo(RawSpan.Slice(Position));
Position += size;
@ -197,7 +197,7 @@ namespace Server.Buffers
{
value ??= "";
int length = Math.Min(size, value.Length);
var length = Math.Min(size, value.Length);
Encoding.ASCII.GetBytes(value.AsSpan(0, length), RawSpan.Slice(Position));
@ -207,7 +207,9 @@ namespace Server.Buffers
Fill(size - length);
}
else
{
Position += size;
}
}
/// <summary>