Cleanup/Housekeeping (#242)
This commit is contained in:
parent
90ede0659f
commit
741e8d8300
228 changed files with 6292 additions and 2690 deletions
|
|
@ -74,7 +74,9 @@ namespace System.Buffers
|
|||
{
|
||||
if (length < 0)
|
||||
// Cast-away readonly to initialize lazy field
|
||||
{
|
||||
Volatile.Write(ref Unsafe.AsRef(length), sequence.Length);
|
||||
}
|
||||
|
||||
return length;
|
||||
}
|
||||
|
|
@ -109,9 +111,13 @@ namespace System.Buffers
|
|||
if (CurrentSpanIndex >= CurrentSpan.Length)
|
||||
{
|
||||
if (usingSequence)
|
||||
{
|
||||
GetNextSpan();
|
||||
}
|
||||
else
|
||||
{
|
||||
moreData = false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
|
@ -120,7 +126,10 @@ namespace System.Buffers
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Rewind(long count)
|
||||
{
|
||||
if ((ulong)count > (ulong)Consumed) throw new ArgumentOutOfRangeException(nameof(count));
|
||||
if ((ulong)count > (ulong)Consumed)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
}
|
||||
|
||||
Consumed -= count;
|
||||
|
||||
|
|
@ -232,7 +241,10 @@ namespace System.Buffers
|
|||
|
||||
private void AdvanceToNextSpan(long count)
|
||||
{
|
||||
if (count < 0) throw new ArgumentOutOfRangeException(nameof(count));
|
||||
if (count < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException(nameof(count));
|
||||
}
|
||||
|
||||
Consumed += count;
|
||||
while (moreData)
|
||||
|
|
@ -253,7 +265,10 @@ namespace System.Buffers
|
|||
|
||||
GetNextSpan();
|
||||
|
||||
if (count == 0) break;
|
||||
if (count == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (count != 0)
|
||||
|
|
@ -286,7 +301,9 @@ namespace System.Buffers
|
|||
{
|
||||
// If we don't have enough to fill the requested buffer, return false
|
||||
if (Remaining < destination.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var firstSpan = UnreadSpan;
|
||||
firstSpan.CopyTo(destination);
|
||||
|
|
@ -294,14 +311,19 @@ namespace System.Buffers
|
|||
|
||||
var next = nextPosition;
|
||||
while (sequence.TryGet(ref next, out var nextSegment))
|
||||
{
|
||||
if (nextSegment.Length > 0)
|
||||
{
|
||||
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;
|
||||
if (copied >= destination.Length)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,7 +14,10 @@ namespace System.Buffers
|
|||
where T : unmanaged
|
||||
{
|
||||
var span = reader.UnreadSpan;
|
||||
if (span.Length < sizeof(T)) return TryReadMultisegment(ref reader, out value);
|
||||
if (span.Length < sizeof(T))
|
||||
{
|
||||
return TryReadMultisegment(ref reader, out value);
|
||||
}
|
||||
|
||||
value = Unsafe.ReadUnaligned<T>(ref MemoryMarshal.GetReference(span));
|
||||
reader.Advance(sizeof(T));
|
||||
|
|
|
|||
|
|
@ -110,7 +110,10 @@ namespace System.Buffers
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Ensure(int count = 1)
|
||||
{
|
||||
if (_span.Length < count) EnsureMore(count);
|
||||
if (_span.Length < count)
|
||||
{
|
||||
EnsureMore(count);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -120,7 +123,10 @@ namespace System.Buffers
|
|||
[MethodImpl(MethodImplOptions.NoInlining)]
|
||||
private void EnsureMore(int count = 0)
|
||||
{
|
||||
if (_buffered > 0) Commit();
|
||||
if (_buffered > 0)
|
||||
{
|
||||
Commit();
|
||||
}
|
||||
|
||||
_span = _output.GetSpan(count);
|
||||
}
|
||||
|
|
@ -133,7 +139,10 @@ namespace System.Buffers
|
|||
{
|
||||
while (source.Length > 0)
|
||||
{
|
||||
if (_span.Length == 0) EnsureMore();
|
||||
if (_span.Length == 0)
|
||||
{
|
||||
EnsureMore();
|
||||
}
|
||||
|
||||
var writable = Math.Min(source.Length, _span.Length);
|
||||
source.Slice(0, writable).CopyTo(_span);
|
||||
|
|
|
|||
|
|
@ -58,14 +58,20 @@ namespace System.Buffers
|
|||
|
||||
protected void Dispose(bool disposing)
|
||||
{
|
||||
if (_isDisposed) return;
|
||||
if (_isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_isDisposed = true;
|
||||
|
||||
Array = null;
|
||||
NativePointer = IntPtr.Zero;
|
||||
|
||||
if (_gcHandle.IsAllocated) _gcHandle.Free();
|
||||
if (_gcHandle.IsAllocated)
|
||||
{
|
||||
_gcHandle.Free();
|
||||
}
|
||||
}
|
||||
|
||||
~MemoryPoolSlab()
|
||||
|
|
|
|||
|
|
@ -72,7 +72,10 @@ namespace System.Buffers
|
|||
|
||||
public override IMemoryOwner<byte> Rent(int size = AnySize)
|
||||
{
|
||||
if (size > _blockSize) MemoryPoolThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockSize);
|
||||
if (size > _blockSize)
|
||||
{
|
||||
MemoryPoolThrowHelper.ThrowArgumentOutOfRangeException_BufferRequestTooLarge(_blockSize);
|
||||
}
|
||||
|
||||
var block = Lease();
|
||||
return block;
|
||||
|
|
@ -85,7 +88,9 @@ namespace System.Buffers
|
|||
private MemoryPoolBlock Lease()
|
||||
{
|
||||
if (_isDisposed)
|
||||
{
|
||||
MemoryPoolThrowHelper.ThrowObjectDisposedException(MemoryPoolThrowHelper.ExceptionArgument.MemoryPool);
|
||||
}
|
||||
|
||||
if (_blocks.TryDequeue(out var block))
|
||||
{
|
||||
|
|
@ -127,7 +132,9 @@ namespace System.Buffers
|
|||
block = new MemoryPoolBlock(this, slab, offset, _blockSize);
|
||||
|
||||
if (i != blockCount - 1) // last block
|
||||
{
|
||||
Return(block);
|
||||
}
|
||||
|
||||
offset += _blockSize;
|
||||
}
|
||||
|
|
@ -148,9 +155,13 @@ namespace System.Buffers
|
|||
internal void Return(MemoryPoolBlock block)
|
||||
{
|
||||
if (!_isDisposed)
|
||||
{
|
||||
_blocks.Enqueue(block);
|
||||
}
|
||||
else
|
||||
{
|
||||
GC.SuppressFinalize(block);
|
||||
}
|
||||
}
|
||||
|
||||
// This method can ONLY be called from the finalizer of MemoryPoolBlock
|
||||
|
|
@ -162,25 +173,37 @@ namespace System.Buffers
|
|||
// Need to make a new object because this one is being finalized
|
||||
// Note, this must be called within the _disposeSync lock because the block
|
||||
// could be disposed at the same time as the finalizer.
|
||||
{
|
||||
Return(new MemoryPoolBlock(this, slab, offset, length));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Dispose(bool disposing)
|
||||
{
|
||||
if (_isDisposed) return;
|
||||
if (_isDisposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
lock (_disposeSync)
|
||||
{
|
||||
_isDisposed = true;
|
||||
|
||||
if (disposing)
|
||||
{
|
||||
while (_slabs.TryPop(out var slab))
|
||||
// dispose managed state (managed objects).
|
||||
{
|
||||
slab.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
// Discard blocks in pool
|
||||
while (_blocks.TryDequeue(out var block)) GC.SuppressFinalize(block);
|
||||
while (_blocks.TryDequeue(out var block))
|
||||
{
|
||||
GC.SuppressFinalize(block);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue