From 3bd0ffdef64b4510b408200cf4281d24b5742abd Mon Sep 17 00:00:00 2001 From: Krzysztof Cwalina Date: Mon, 20 Mar 2017 13:45:39 -0700 Subject: [PATCH] renamed buffer related types (#1343) --- .../Program.cs | 4 +- .../OwnedBuffer.cs | 36 +++++----- .../ReadWriteBytes.cs | 66 +++++++++---------- .../TcpServer.cs | 4 +- .../System/Buffers/BufferExtensions.cs | 20 +++--- .../System/Buffers/BytesReader.cs | 14 ++-- .../System/Buffers/IReadOnlyMemoryList.cs | 6 +- .../System/Buffers/NativeBufferPool.cs | 4 +- .../System/Buffers/OwnedNativeMemory.cs | 10 +-- .../System/Buffers/OwnedPinnedArray.cs | 18 ++--- .../System/Buffers/ReadOnlyBytes.cs | 60 ++++++++--------- .../System/Buffers/BufferPool.cs | 8 ++- .../System/Buffers/ManagedBufferPool.cs | 8 +-- .../System/Buffers/Memory.cs | 52 +++++++-------- .../System/Buffers/MemoryHandle.cs | 10 +-- .../System/Buffers/OwnedArray.cs | 6 +- .../System/Buffers/OwnedMemory.cs | 25 ++++--- .../System/Buffers/ReadOnlyMemory.cs | 54 +++++++-------- .../System/Runtime/DebuggerViews.cs | 21 +++--- .../CompressionPipelineExtensions.cs | 34 +++++----- .../ReadWriteExtensions.cs | 4 +- .../StreamExtensions.cs | 6 +- .../UnownedBufferReader.cs | 4 +- src/System.IO.Pipelines.File/FileReader.cs | 4 +- .../Interop/UvWriteReq.cs | 2 +- .../UvTcpConnection.cs | 6 +- .../SocketConnection.cs | 4 +- .../RioTcpConnection.cs | 8 +-- .../PipelineTextOutput.cs | 2 +- src/System.IO.Pipelines/BufferSegment.cs | 42 ++++++------ src/System.IO.Pipelines/MemoryEnumerator.cs | 20 +++--- src/System.IO.Pipelines/MemoryPool.cs | 2 +- src/System.IO.Pipelines/MemoryPoolBlock.cs | 4 +- src/System.IO.Pipelines/Pipe.cs | 6 +- src/System.IO.Pipelines/PipeFactory.cs | 2 +- src/System.IO.Pipelines/ReadCursor.cs | 17 ++--- .../ReadCursorOperations.cs | 6 +- src/System.IO.Pipelines/ReadableBuffer.cs | 34 +++++----- .../ReadableBufferReader.cs | 2 +- src/System.IO.Pipelines/SegmentEnumerator.cs | 4 +- .../Testing/BufferUtilities.cs | 6 +- src/System.IO.Pipelines/UnownedBuffer.cs | 8 +-- src/System.IO.Pipelines/WritableBuffer.cs | 10 +-- .../WritableBufferExtensions.cs | 10 +-- .../System/Net/Libuv/Stream.cs | 14 ++-- .../Formatters/SequenceFormatter.cs | 16 ++--- .../System/Text/Parsing/SequenceParser.cs | 32 ++++----- .../System/Text/Http/HttpRequest.cs | 8 +-- .../System/Text/Json/JsonParseObject.cs | 4 +- .../System/Text/Json/JsonParser.cs | 20 +++--- .../BytesReader.cs | 2 +- .../MemoryTests.cs | 20 +++--- .../ReadOnlyBytesTests.cs | 14 ++-- .../MemoryTests.cs | 63 +++++++++--------- .../Enumerators.cs | 2 +- .../PipelineReaderWriterFacts.cs | 8 +-- .../PipelineWriterFacts.cs | 4 +- .../ReadableBufferFacts.cs | 12 ++-- .../UnownedBufferReaderFacts.cs | 7 +- .../WritableBufferFacts.cs | 6 +- .../SequenceFormatterTests.cs | 3 +- .../SequenceTests.cs | 6 +- 62 files changed, 466 insertions(+), 448 deletions(-) diff --git a/samples/LibuvWithNonAllocatingFormatters/Program.cs b/samples/LibuvWithNonAllocatingFormatters/Program.cs index 45662533b48..cffdbf09585 100644 --- a/samples/LibuvWithNonAllocatingFormatters/Program.cs +++ b/samples/LibuvWithNonAllocatingFormatters/Program.cs @@ -95,8 +95,8 @@ static void RunLoop(bool log) } var segment = formatter.Formatted; - using (var memory = new OwnedPinnedArray(segment.Array)) { - connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count)); + using (var memory = new OwnedPinnedBuffer(segment.Array)) { + connection.TryWrite(memory.Buffer.Slice(segment.Offset, segment.Count)); connection.Dispose(); } }; diff --git a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/OwnedBuffer.cs b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/OwnedBuffer.cs index e6278d3179a..ee234e22e5b 100644 --- a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/OwnedBuffer.cs +++ b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/OwnedBuffer.cs @@ -8,7 +8,7 @@ namespace Microsoft.Net.Http { - class OwnedBuffer : OwnedMemory, IMemoryList, IReadOnlyMemoryList + class OwnedBuffer : OwnedBuffer, IBufferList, IReadOnlyBufferList { public const int DefaultBufferSize = 1024; internal OwnedBuffer _next; @@ -25,42 +25,42 @@ static void Free(byte[] array) { } - public Memory First => Memory; + public Buffer First => Buffer; - public IMemoryList Rest => _next; + public IBufferList Rest => _next; public int WrittenByteCount => _written; - ReadOnlyMemory IReadOnlyMemoryList.First => Memory; + ReadOnlyBuffer IReadOnlyBufferList.First => Buffer; - IReadOnlyMemoryList IReadOnlyMemoryList.Rest => _next; + IReadOnlyBufferList IReadOnlyBufferList.Rest => _next; - // TODO: maybe these should be renamed to no conflict with OwnedMemory.Length? - int? ISequence>.Length => null; - int? ISequence>.Length => null; + // TODO: maybe these should be renamed to no conflict with OwnedBuffer.Length? + int? ISequence>.Length => null; + int? ISequence>.Length => null; public int CopyTo(Span buffer) { if (buffer.Length > _written) { - Memory.Slice(0, _written).CopyTo(buffer); + Buffer.Slice(0, _written).CopyTo(buffer); return _next.CopyTo(buffer.Slice(_written)); } - Memory.Slice(0, buffer.Length).CopyTo(buffer); + Buffer.Slice(0, buffer.Length).CopyTo(buffer); return buffer.Length; } - public bool TryGet(ref Position position, out Memory item, bool advance = true) + public bool TryGet(ref Position position, out Buffer item, bool advance = true) { if (position == Position.First) { - item = Memory.Slice(0, _written); + item = Buffer.Slice(0, _written); if (advance) { position.IntegerPosition++; position.ObjectPosition = _next; } return true; } - else if (position.ObjectPosition == null) { item = default(Memory); return false; } + else if (position.ObjectPosition == null) { item = default(Buffer); return false; } var sequence = (OwnedBuffer)position.ObjectPosition; - item = sequence.Memory.Slice(0, _written); + item = sequence.Buffer.Slice(0, _written); if (advance) { if (position == Position.First) { position.ObjectPosition = _next; @@ -73,17 +73,17 @@ public bool TryGet(ref Position position, out Memory item, bool advance = return true; } - public bool TryGet(ref Position position, out ReadOnlyMemory item, bool advance = true) + public bool TryGet(ref Position position, out ReadOnlyBuffer item, bool advance = true) { if (position == Position.First) { - item = Memory.Slice(0, _written); + item = Buffer.Slice(0, _written); if (advance) { position.IntegerPosition++; position.ObjectPosition = _next; } return true; } - else if (position.ObjectPosition == null) { item = default(ReadOnlyMemory); return false; } + else if (position.ObjectPosition == null) { item = default(ReadOnlyBuffer); return false; } var sequence = (OwnedBuffer)position.ObjectPosition; - item = sequence.Memory.Slice(0, _written); + item = sequence.Buffer.Slice(0, _written); if (advance) { if (position == Position.First) { position.ObjectPosition = _next; diff --git a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/ReadWriteBytes.cs b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/ReadWriteBytes.cs index 5a80a67fa3e..d1ac6cca0c6 100644 --- a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/ReadWriteBytes.cs +++ b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/ReadWriteBytes.cs @@ -7,48 +7,48 @@ namespace System.Buffers { - public interface IMemoryList : ISequence> + public interface IBufferList : ISequence> { int CopyTo(Span buffer); - Memory First { get; } - IMemoryList Rest { get; } + Buffer First { get; } + IBufferList Rest { get; } } /// /// Multi-segment buffer /// - public struct ReadWriteBytes : IMemoryList + public struct ReadWriteBytes : IBufferList { - Memory _first; - IMemoryList _rest; + Buffer _first; + IBufferList _rest; int _length; - static readonly ReadWriteBytes s_empty = new ReadWriteBytes(Memory.Empty); + static readonly ReadWriteBytes s_empty = new ReadWriteBytes(Buffer.Empty); - public ReadWriteBytes(Memory first, IMemoryList rest, int length) + public ReadWriteBytes(Buffer first, IBufferList rest, int length) { _rest = rest; _first = first; _length = length; } - public ReadWriteBytes(Memory first, IMemoryList rest) : + public ReadWriteBytes(Buffer first, IBufferList rest) : this(first, rest, Unspecified) { } - public ReadWriteBytes(Memory memory) : - this(memory, null, memory.Length) + public ReadWriteBytes(Buffer buffer) : + this(buffer, null, buffer.Length) { } - public ReadWriteBytes(IMemoryList segments, int length) : + public ReadWriteBytes(IBufferList segments, int length) : this(segments.First, segments.Rest, length) { } - public ReadWriteBytes(IMemoryList segments) : + public ReadWriteBytes(IBufferList segments) : this(segments.First, segments.Rest, Unspecified) { } - public bool TryGet(ref Position position, out Memory value, bool advance = true) + public bool TryGet(ref Position position, out Buffer value, bool advance = true) { if (position == Position.First) { @@ -62,10 +62,10 @@ public bool TryGet(ref Position position, out Memory value, bool advance = return true; } - var rest = position.ObjectPosition as IMemoryList; + var rest = position.ObjectPosition as IBufferList; if (rest == null) { - value = default(Memory); + value = default(Buffer); return false; } @@ -85,9 +85,9 @@ public bool TryGet(ref Position position, out Memory value, bool advance = return true; } - public Memory First => _first; + public Buffer First => _first; - public IMemoryList Rest => _rest; + public IBufferList Rest => _rest; public int? Length { @@ -147,7 +147,7 @@ ReadWriteBytes SliceRest(int index, int length) { if (First.Length == index && length == 0) { - return new ReadWriteBytes(Memory.Empty); + return new ReadWriteBytes(Buffer.Empty); } else { @@ -171,7 +171,7 @@ public int ComputeLength() if (_rest != null) { Position position = new Position(); - Memory segment; + Buffer segment; while (_rest.TryGet(ref position, out segment)) { length += segment.Length; @@ -184,11 +184,11 @@ public int ComputeLength() // and knowing the length is not needed in amy operations, e.g. slicing small section of the front. const int Unspecified = -1; - class MemoryListNode : IMemoryList + class BufferListNode : IBufferList { - internal Memory _first; - internal MemoryListNode _rest; - public Memory First => _first; + internal Buffer _first; + internal BufferListNode _rest; + public Buffer First => _first; public int? Length { @@ -197,13 +197,13 @@ public int? Length } } - public IMemoryList Rest => _rest; + public IBufferList Rest => _rest; public int CopyTo(Span buffer) { int copied = 0; var position = Position.First; - Memory segment; + Buffer segment; var free = buffer; while (TryGet(ref position, out segment, true)) { @@ -223,7 +223,7 @@ public int CopyTo(Span buffer) return copied; } - public bool TryGet(ref Position position, out Memory item, bool advance = true) + public bool TryGet(ref Position position, out Buffer item, bool advance = true) { if (position == Position.First) { @@ -231,9 +231,9 @@ public bool TryGet(ref Position position, out Memory item, bool advance = if (advance) { position.IntegerPosition++; position.ObjectPosition = _rest; } return true; } - else if (position.ObjectPosition == null) { item = default(Memory); return false; } + else if (position.ObjectPosition == null) { item = default(Buffer); return false; } - var sequence = (MemoryListNode)position.ObjectPosition; + var sequence = (BufferListNode)position.ObjectPosition; item = sequence._first; if (advance) { @@ -253,18 +253,18 @@ public bool TryGet(ref Position position, out Memory item, bool advance = public static ReadWriteBytes Create(params byte[][] buffers) { - MemoryListNode first = null; - MemoryListNode current = null; + BufferListNode first = null; + BufferListNode current = null; foreach (var buffer in buffers) { if (first == null) { - current = new MemoryListNode(); + current = new BufferListNode(); first = current; } else { - current._rest = new MemoryListNode(); + current._rest = new BufferListNode(); current = current._rest; } current._first = buffer; diff --git a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpServer.cs b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpServer.cs index fc9c27835e2..a620fbe9838 100644 --- a/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpServer.cs +++ b/samples/LowAllocationWebServer/LowAllocationWebServerLibrary/TcpServer.cs @@ -172,7 +172,7 @@ public unsafe int Send(ReadOnlySpan buffer) } } - public int Send(ReadOnlyMemory buffer) + public int Send(ReadOnlyBuffer buffer) { return Send(buffer.Span); } @@ -202,7 +202,7 @@ public unsafe int Receive(Span buffer) } } - public int Receive(Memory buffer) + public int Receive(Buffer buffer) { return Receive(buffer.Span); } diff --git a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs index f2ebbce240f..d4aa1419de8 100644 --- a/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs +++ b/src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs @@ -9,21 +9,21 @@ namespace System.Buffers { public static class BufferExtensions { - public static ReadOnlySpan ToSpan(this T memorySequence) where T : ISequence> + public static ReadOnlySpan ToSpan(this T bufferSequence) where T : ISequence> { Position position = Position.First; - ReadOnlyMemory memory; - ResizableArray array = new ResizableArray(memorySequence.Length.GetValueOrDefault(1024)); - while (memorySequence.TryGet(ref position, out memory)) + ReadOnlyBuffer buffer; + ResizableArray array = new ResizableArray(bufferSequence.Length.GetValueOrDefault(1024)); + while (bufferSequence.TryGet(ref position, out buffer)) { - array.AddAll(memory.Span); + array.AddAll(buffer.Span); } array.Resize(array.Count); return array.Items.Slice(0, array.Count); } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int IndexOf(this IReadOnlyMemoryList sequence, ReadOnlySpan value) + public static int IndexOf(this IReadOnlyBufferList sequence, ReadOnlySpan value) { var first = sequence.First.Span; var index = first.IndexOf(value); @@ -35,7 +35,7 @@ public static int IndexOf(this IReadOnlyMemoryList sequence, ReadOnlySpan< return IndexOfStraddling(first, sequence.Rest, value); } - public static int IndexOf(this IReadOnlyMemoryList sequence, byte value) + public static int IndexOf(this IReadOnlyBufferList sequence, byte value) { var first = sequence.First.Span; var index = first.IndexOf(value); @@ -53,7 +53,7 @@ public static int IndexOf(this IReadOnlyMemoryList sequence, byte value) // TODO (pri 3): I am pretty sure this whole routine can be written much better // searches values that potentially straddle between first and rest - internal static int IndexOfStraddling(this ReadOnlySpan first, IReadOnlyMemoryList rest, ReadOnlySpan value) + internal static int IndexOfStraddling(this ReadOnlySpan first, IReadOnlyBufferList rest, ReadOnlySpan value) { Debug.Assert(rest != null); @@ -119,9 +119,9 @@ internal static int IndexOfStraddling(this ReadOnlySpan first, IReadOnlyMe } [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static int IndexOf(this ReadOnlyMemory memory, ReadOnlySpan values) + public static int IndexOf(this ReadOnlyBuffer buffer, ReadOnlySpan values) { - return SpanExtensions.IndexOf(memory.Span, values); + return SpanExtensions.IndexOf(buffer.Span, values); } } } \ No newline at end of file diff --git a/src/System.Buffers.Experimental/System/Buffers/BytesReader.cs b/src/System.Buffers.Experimental/System/Buffers/BytesReader.cs index 100e10b3edb..69d84e711d3 100644 --- a/src/System.Buffers.Experimental/System/Buffers/BytesReader.cs +++ b/src/System.Buffers.Experimental/System/Buffers/BytesReader.cs @@ -15,7 +15,7 @@ public struct BytesReader ReadOnlyBytes _unreadSegments; int _index; // index relative to the begining of bytes passed to the constructor - ReadOnlyMemory _currentSegment; + ReadOnlyBuffer _currentSegment; int _currentSegmentIndex; public BytesReader(ReadOnlyBytes bytes, TextEncoder encoder) @@ -27,7 +27,7 @@ public BytesReader(ReadOnlyBytes bytes, TextEncoder encoder) _index = 0; } - public BytesReader(ReadOnlyMemory bytes, TextEncoder encoder) + public BytesReader(ReadOnlyBuffer bytes, TextEncoder encoder) { _unreadSegments = new ReadOnlyBytes(bytes); _currentSegment = bytes; @@ -36,16 +36,16 @@ public BytesReader(ReadOnlyMemory bytes, TextEncoder encoder) _index = 0; } - public BytesReader(ReadOnlyMemory bytes) : this(bytes, TextEncoder.Utf8) + public BytesReader(ReadOnlyBuffer bytes) : this(bytes, TextEncoder.Utf8) { } public BytesReader(ReadOnlyBytes bytes) : this(bytes, TextEncoder.Utf8) { } - public BytesReader(IReadOnlyMemoryList bytes) : this(bytes, TextEncoder.Utf8) + public BytesReader(IReadOnlyBufferList bytes) : this(bytes, TextEncoder.Utf8) { } - public BytesReader(IReadOnlyMemoryList bytes, TextEncoder encoder) : this(new ReadOnlyBytes(bytes)) + public BytesReader(IReadOnlyBufferList bytes, TextEncoder encoder) : this(new ReadOnlyBytes(bytes)) { } public byte Peek() => _currentSegment.Span[_currentSegmentIndex]; @@ -182,7 +182,7 @@ void AdvanceNextSegment(int count, int advancedInCurrent) if (newSegments == null && toAdvance == 0) { _unreadSegments = ReadOnlyBytes.Empty; - _currentSegment = ReadOnlyMemory.Empty; + _currentSegment = ReadOnlyBuffer.Empty; _currentSegmentIndex = 0; return; } @@ -210,7 +210,7 @@ void AdvanceNextSegment(int count, int advancedInCurrent) } } - int IndexOf(IReadOnlyMemoryList sequence, byte value) + int IndexOf(IReadOnlyBufferList sequence, byte value) { if (sequence == null) return -1; var first = sequence.First; diff --git a/src/System.Buffers.Experimental/System/Buffers/IReadOnlyMemoryList.cs b/src/System.Buffers.Experimental/System/Buffers/IReadOnlyMemoryList.cs index 1a7009db482..d262447fd9a 100644 --- a/src/System.Buffers.Experimental/System/Buffers/IReadOnlyMemoryList.cs +++ b/src/System.Buffers.Experimental/System/Buffers/IReadOnlyMemoryList.cs @@ -5,11 +5,11 @@ using System.Collections.Sequences; namespace System.Buffers { - public interface IReadOnlyMemoryList : ISequence> + public interface IReadOnlyBufferList : ISequence> { int CopyTo(Span buffer); - ReadOnlyMemory First { get; } + ReadOnlyBuffer First { get; } - IReadOnlyMemoryList Rest { get; } + IReadOnlyBufferList Rest { get; } } } \ No newline at end of file diff --git a/src/System.Buffers.Experimental/System/Buffers/NativeBufferPool.cs b/src/System.Buffers.Experimental/System/Buffers/NativeBufferPool.cs index ca4ae07719d..e5115340a10 100644 --- a/src/System.Buffers.Experimental/System/Buffers/NativeBufferPool.cs +++ b/src/System.Buffers.Experimental/System/Buffers/NativeBufferPool.cs @@ -39,7 +39,7 @@ protected override void Dispose(bool disposing) Marshal.FreeHGlobal(_memory); } - public override OwnedMemory Rent(int numberOfBytes) + public override OwnedBuffer Rent(int numberOfBytes) { if (numberOfBytes < 1) throw new ArgumentOutOfRangeException(nameof(numberOfBytes)); if (numberOfBytes > _bufferSize) new NotSupportedException(); @@ -77,7 +77,7 @@ internal void Return(BufferManager pooledBuffer) } } - internal sealed class BufferManager : OwnedMemory + internal sealed class BufferManager : OwnedBuffer { private readonly NativeBufferPool _pool; diff --git a/src/System.Buffers.Experimental/System/Buffers/OwnedNativeMemory.cs b/src/System.Buffers.Experimental/System/Buffers/OwnedNativeMemory.cs index 3809d4d4040..c164c946792 100644 --- a/src/System.Buffers.Experimental/System/Buffers/OwnedNativeMemory.cs +++ b/src/System.Buffers.Experimental/System/Buffers/OwnedNativeMemory.cs @@ -5,14 +5,14 @@ namespace System.Buffers { - public class OwnedNativeMemory : OwnedMemory + public class OwnedNativeBuffer : OwnedBuffer { - public OwnedNativeMemory(int length) : this(length, Marshal.AllocHGlobal(length)) + public OwnedNativeBuffer(int length) : this(length, Marshal.AllocHGlobal(length)) { } - public OwnedNativeMemory(int length, IntPtr address) : base(null, 0, length, address) { } + public OwnedNativeBuffer(int length, IntPtr address) : base(null, 0, length, address) { } - public static implicit operator IntPtr(OwnedNativeMemory owner) + public static implicit operator IntPtr(OwnedNativeBuffer owner) { unsafe { @@ -20,7 +20,7 @@ public static implicit operator IntPtr(OwnedNativeMemory owner) } } - ~OwnedNativeMemory() + ~OwnedNativeBuffer() { Dispose(false); } diff --git a/src/System.Buffers.Experimental/System/Buffers/OwnedPinnedArray.cs b/src/System.Buffers.Experimental/System/Buffers/OwnedPinnedArray.cs index 7e8fa14faa6..fe3035b7f80 100644 --- a/src/System.Buffers.Experimental/System/Buffers/OwnedPinnedArray.cs +++ b/src/System.Buffers.Experimental/System/Buffers/OwnedPinnedArray.cs @@ -6,12 +6,12 @@ namespace System.Buffers { - // This is to support secnarios today covered by Memory in corefxlab - public class OwnedPinnedArray : OwnedMemory + // This is to support secnarios today covered by Buffer in corefxlab + public class OwnedPinnedBuffer : OwnedBuffer { private GCHandle _handle; - public unsafe OwnedPinnedArray(T[] array, void* pointer, GCHandle handle = default(GCHandle)) : + public unsafe OwnedPinnedBuffer(T[] array, void* pointer, GCHandle handle = default(GCHandle)) : base(array, 0, array.Length, new IntPtr(pointer)) { var computedPointer = new IntPtr(Unsafe.AsPointer(ref Array[0])); @@ -22,28 +22,28 @@ public class OwnedPinnedArray : OwnedMemory _handle = handle; } - public unsafe OwnedPinnedArray(T[] array) : this(array, GCHandle.Alloc(array, GCHandleType.Pinned)) + public unsafe OwnedPinnedBuffer(T[] array) : this(array, GCHandle.Alloc(array, GCHandleType.Pinned)) { } - private OwnedPinnedArray(T[] array, GCHandle handle) : base(array, 0, array.Length, handle.AddrOfPinnedObject()) + private OwnedPinnedBuffer(T[] array, GCHandle handle) : base(array, 0, array.Length, handle.AddrOfPinnedObject()) { _handle = handle; } - public static implicit operator OwnedPinnedArray(T[] array) + public static implicit operator OwnedPinnedBuffer(T[] array) { - return new OwnedPinnedArray(array); + return new OwnedPinnedBuffer(array); } public new unsafe byte* Pointer => (byte*)base.Pointer.ToPointer(); public new T[] Array => base.Array; - public unsafe static implicit operator IntPtr(OwnedPinnedArray owner) + public unsafe static implicit operator IntPtr(OwnedPinnedBuffer owner) { return new IntPtr(owner.Pointer); } - public static implicit operator T[] (OwnedPinnedArray owner) + public static implicit operator T[] (OwnedPinnedBuffer owner) { return owner.Array; } diff --git a/src/System.Buffers.Experimental/System/Buffers/ReadOnlyBytes.cs b/src/System.Buffers.Experimental/System/Buffers/ReadOnlyBytes.cs index 1f4d7bddb57..94f25c6d871 100644 --- a/src/System.Buffers.Experimental/System/Buffers/ReadOnlyBytes.cs +++ b/src/System.Buffers.Experimental/System/Buffers/ReadOnlyBytes.cs @@ -11,15 +11,15 @@ namespace System.Buffers /// /// Multi-segment buffer /// - public struct ReadOnlyBytes : IReadOnlyMemoryList + public struct ReadOnlyBytes : IReadOnlyBufferList { - ReadOnlyMemory _first; + ReadOnlyBuffer _first; int _totalLength; - IReadOnlyMemoryList _rest; + IReadOnlyBufferList _rest; - static readonly ReadOnlyBytes s_empty = new ReadOnlyBytes(ReadOnlyMemory.Empty); + static readonly ReadOnlyBytes s_empty = new ReadOnlyBytes(ReadOnlyBuffer.Empty); - public ReadOnlyBytes(ReadOnlyMemory first, IReadOnlyMemoryList rest, int length) + public ReadOnlyBytes(ReadOnlyBuffer first, IReadOnlyBufferList rest, int length) { Debug.Assert(rest != null || length <= first.Length); _rest = rest; @@ -27,24 +27,24 @@ public ReadOnlyBytes(ReadOnlyMemory first, IReadOnlyMemoryList rest, _totalLength = length; } - public ReadOnlyBytes(ReadOnlyMemory first, IReadOnlyMemoryList rest) : + public ReadOnlyBytes(ReadOnlyBuffer first, IReadOnlyBufferList rest) : this(first, rest, rest==null?first.Length:Unspecified) { } - public ReadOnlyBytes(ReadOnlyMemory memory) : - this(memory, null, memory.Length) + public ReadOnlyBytes(ReadOnlyBuffer buffer) : + this(buffer, null, buffer.Length) { } - public ReadOnlyBytes(IReadOnlyMemoryList segments, int length) : + public ReadOnlyBytes(IReadOnlyBufferList segments, int length) : this(segments.First, segments.Rest, length) { } - public ReadOnlyBytes(IReadOnlyMemoryList segments) : + public ReadOnlyBytes(IReadOnlyBufferList segments) : this(segments.First, segments.Rest, Unspecified) { } - public bool TryGet(ref Position position, out ReadOnlyMemory value, bool advance = true) + public bool TryGet(ref Position position, out ReadOnlyBuffer value, bool advance = true) { if (position == Position.First) { @@ -58,10 +58,10 @@ public bool TryGet(ref Position position, out ReadOnlyMemory value, bool a return true; } - var rest = position.ObjectPosition as IReadOnlyMemoryList; + var rest = position.ObjectPosition as IReadOnlyBufferList; if (rest == null) { - value = default(ReadOnlyMemory); + value = default(ReadOnlyBuffer); return false; } @@ -81,9 +81,9 @@ public bool TryGet(ref Position position, out ReadOnlyMemory value, bool a return true; } - public ReadOnlyMemory First => _first; + public ReadOnlyBuffer First => _first; - public IReadOnlyMemoryList Rest => _rest; + public IReadOnlyBufferList Rest => _rest; public int? Length { @@ -198,7 +198,7 @@ ReadOnlyBytes SliceRest(int index, int length) { if (First.Length == index && length == 0) { - return new ReadOnlyBytes(ReadOnlyMemory.Empty); + return new ReadOnlyBytes(ReadOnlyBuffer.Empty); } else { @@ -222,7 +222,7 @@ public int ComputeLength() if (_rest != null) { Position position = new Position(); - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (_rest.TryGet(ref position, out segment)) { length += segment.Length; @@ -236,11 +236,11 @@ public int ComputeLength() // and knowing the length is not needed in amy operations, e.g. slicing small section of the front. const int Unspecified = -1; - class MemoryListNode : IReadOnlyMemoryList + class BufferListNode : IReadOnlyBufferList { - internal ReadOnlyMemory _first; - internal MemoryListNode _rest; - public ReadOnlyMemory First => _first; + internal ReadOnlyBuffer _first; + internal BufferListNode _rest; + public ReadOnlyBuffer First => _first; public int? Length { get { @@ -248,13 +248,13 @@ public int? Length { } } - public IReadOnlyMemoryList Rest => _rest; + public IReadOnlyBufferList Rest => _rest; public int CopyTo(Span buffer) { int copied = 0; var position = Position.First; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; var free = buffer; while (TryGet(ref position, out segment, true)) { @@ -274,7 +274,7 @@ public int CopyTo(Span buffer) return copied; } - public bool TryGet(ref Position position, out ReadOnlyMemory item, bool advance = true) + public bool TryGet(ref Position position, out ReadOnlyBuffer item, bool advance = true) { if (position == Position.First) { @@ -282,9 +282,9 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad if (advance) { position.IntegerPosition++; position.ObjectPosition = _rest; } return true; } - else if (position.ObjectPosition == null) { item = default(ReadOnlyMemory); return false; } + else if (position.ObjectPosition == null) { item = default(ReadOnlyBuffer); return false; } - var sequence = (MemoryListNode)position.ObjectPosition; + var sequence = (BufferListNode)position.ObjectPosition; item = sequence._first; if (advance) { @@ -304,18 +304,18 @@ public bool TryGet(ref Position position, out ReadOnlyMemory item, bool ad public static ReadOnlyBytes Create(params byte[][] buffers) { - MemoryListNode first = null; - MemoryListNode current = null; + BufferListNode first = null; + BufferListNode current = null; foreach (var buffer in buffers) { if (first == null) { - current = new MemoryListNode(); + current = new BufferListNode(); first = current; } else { - current._rest = new MemoryListNode(); + current._rest = new BufferListNode(); current = current._rest; } current._first = buffer; diff --git a/src/System.Buffers.Primitives/System/Buffers/BufferPool.cs b/src/System.Buffers.Primitives/System/Buffers/BufferPool.cs index 370da562e45..1404a92649e 100644 --- a/src/System.Buffers.Primitives/System/Buffers/BufferPool.cs +++ b/src/System.Buffers.Primitives/System/Buffers/BufferPool.cs @@ -1,11 +1,15 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -namespace System.Buffers.Pools +using System.Buffers.Pools; + +namespace System.Buffers { public abstract class BufferPool : IDisposable { - public abstract OwnedMemory Rent(int minimumBufferSize); + public static BufferPool Default => ManagedBufferPool.Shared; + + public abstract OwnedBuffer Rent(int minimumBufferSize); public void Dispose() { diff --git a/src/System.Buffers.Primitives/System/Buffers/ManagedBufferPool.cs b/src/System.Buffers.Primitives/System/Buffers/ManagedBufferPool.cs index c6718a8a20a..176fccc19ac 100644 --- a/src/System.Buffers.Primitives/System/Buffers/ManagedBufferPool.cs +++ b/src/System.Buffers.Primitives/System/Buffers/ManagedBufferPool.cs @@ -3,9 +3,9 @@ namespace System.Buffers.Pools { - public sealed class ManagedBufferPool : BufferPool + internal sealed class ManagedBufferPool : BufferPool { - static ManagedBufferPool s_shared = new ManagedBufferPool(); + readonly static ManagedBufferPool s_shared = new ManagedBufferPool(); public static ManagedBufferPool Shared { @@ -15,7 +15,7 @@ public static ManagedBufferPool Shared } } - public override OwnedMemory Rent(int minimumBufferSize) + public override OwnedBuffer Rent(int minimumBufferSize) { return new ArrayPoolMemory(minimumBufferSize); } @@ -24,7 +24,7 @@ protected override void Dispose(bool disposing) { } - private class ArrayPoolMemory : OwnedMemory + private class ArrayPoolMemory : OwnedBuffer { public ArrayPoolMemory(int size) : base(ArrayPool.Shared.Rent(size)) { diff --git a/src/System.Buffers.Primitives/System/Buffers/Memory.cs b/src/System.Buffers.Primitives/System/Buffers/Memory.cs index f236c37849b..62794584a6d 100644 --- a/src/System.Buffers.Primitives/System/Buffers/Memory.cs +++ b/src/System.Buffers.Primitives/System/Buffers/Memory.cs @@ -9,60 +9,60 @@ using System.Runtime.CompilerServices; using System.Text; -namespace System +namespace System.Buffers { - [DebuggerTypeProxy(typeof(MemoryDebuggerView<>))] - public struct Memory : IEquatable>, IEquatable> + [DebuggerTypeProxy(typeof(BufferDebuggerView<>))] + public struct Buffer : IEquatable>, IEquatable> { - readonly OwnedMemory _owner; + readonly OwnedBuffer _owner; readonly int _index; readonly int _length; - internal Memory(OwnedMemory owner, int length) + internal Buffer(OwnedBuffer owner, int length) { _owner = owner; _index = 0; _length = length; } - private Memory(OwnedMemory owner, int index, int length) + private Buffer(OwnedBuffer owner, int index, int length) { _owner = owner; _index = index; _length = length; } - public static implicit operator ReadOnlyMemory(Memory memory) + public static implicit operator ReadOnlyBuffer(Buffer buffer) { - return new ReadOnlyMemory(memory._owner, memory._index, memory._length); + return new ReadOnlyBuffer(buffer._owner, buffer._index, buffer._length); } - public static implicit operator Memory(T[] array) + public static implicit operator Buffer(T[] array) { var owner = new OwnedArray(array); - return owner.Memory; + return owner.Buffer; } - public static Memory Empty { get; } = OwnerEmptyMemory.Shared.Memory; + public static Buffer Empty { get; } = OwnerEmptyMemory.Shared.Buffer; public int Length => _length; public bool IsEmpty => Length == 0; - public Memory Slice(int index) + public Buffer Slice(int index) { - return new Memory(_owner, _index + index, _length - index); + return new Buffer(_owner, _index + index, _length - index); } - public Memory Slice(int index, int length) + public Buffer Slice(int index, int length) { - return new Memory(_owner, _index + index, length); + return new Buffer(_owner, _index + index, length); } public Span Span => _owner.GetSpanInternal(_index, _length); public DisposableReservation Reserve() => new DisposableReservation(_owner); - public unsafe MemoryHandle Pin() => MemoryHandle.Create(_owner, _index); + public unsafe BufferHandle Pin() => BufferHandle.Create(_owner, _index); public unsafe bool TryGetPointer(out void* pointer) { @@ -97,45 +97,45 @@ public void CopyTo(Span span) Span.CopyTo(span); } - public void CopyTo(Memory memory) + public void CopyTo(Buffer buffer) { - Span.CopyTo(memory.Span); + Span.CopyTo(buffer.Span); } [EditorBrowsable(EditorBrowsableState.Never)] public override bool Equals(object obj) { - if(!(obj is Memory)) { + if(!(obj is Buffer)) { return false; } - var other = (Memory)obj; + var other = (Buffer)obj; return Equals(other); } - public bool Equals(Memory other) + public bool Equals(Buffer other) { return _owner == other._owner && _index == other._index && _length == other._length; } - public bool Equals(ReadOnlyMemory other) + public bool Equals(ReadOnlyBuffer other) { return other.Equals(this); } - public static bool operator==(Memory left, Memory right) + public static bool operator==(Buffer left, Buffer right) { return left.Equals(right); } - public static bool operator!=(Memory left, Memory right) + public static bool operator!=(Buffer left, Buffer right) { return !left.Equals(right); } - public static bool operator ==(Memory left, ReadOnlyMemory right) + public static bool operator ==(Buffer left, ReadOnlyBuffer right) { return left.Equals(right); } - public static bool operator !=(Memory left, ReadOnlyMemory right) + public static bool operator !=(Buffer left, ReadOnlyBuffer right) { return !left.Equals(right); } diff --git a/src/System.Buffers.Primitives/System/Buffers/MemoryHandle.cs b/src/System.Buffers.Primitives/System/Buffers/MemoryHandle.cs index 5bce7b3723a..28903f11dae 100644 --- a/src/System.Buffers.Primitives/System/Buffers/MemoryHandle.cs +++ b/src/System.Buffers.Primitives/System/Buffers/MemoryHandle.cs @@ -6,19 +6,19 @@ namespace System.Buffers { - public unsafe struct MemoryHandle + public unsafe struct BufferHandle { IKnown _owner; GCHandle _handle; void* _pointer; - internal static MemoryHandle Create(OwnedMemory owner, int index) + internal static BufferHandle Create(OwnedBuffer owner, int index) { void* pointer; GCHandle handle; if (owner.TryGetPointerInternal(out pointer)) { - pointer = Memory.Add(pointer, index); + pointer = Buffer.Add(pointer, index); } else { @@ -26,7 +26,7 @@ internal static MemoryHandle Create(OwnedMemory owner, int index) if (owner.TryGetArrayInternal(out buffer)) { handle = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned); - pointer = Memory.Add((void*)handle.AddrOfPinnedObject(), buffer.Offset + index); + pointer = Buffer.Add((void*)handle.AddrOfPinnedObject(), buffer.Offset + index); } else { @@ -36,7 +36,7 @@ internal static MemoryHandle Create(OwnedMemory owner, int index) owner.AddReference(); - return new MemoryHandle { + return new BufferHandle { _owner = owner, _handle = handle, _pointer = pointer diff --git a/src/System.Buffers.Primitives/System/Buffers/OwnedArray.cs b/src/System.Buffers.Primitives/System/Buffers/OwnedArray.cs index 1a756718bd3..6cdce56fdc4 100644 --- a/src/System.Buffers.Primitives/System/Buffers/OwnedArray.cs +++ b/src/System.Buffers.Primitives/System/Buffers/OwnedArray.cs @@ -3,7 +3,7 @@ namespace System.Buffers { - public sealed class OwnedArray : OwnedMemory + internal sealed class OwnedArray : OwnedBuffer { public new T[] Array => base.Array; @@ -30,10 +30,10 @@ public OwnedArray(ArraySegment segment) : base(segment.Array, segment.Offset, { } } - internal class OwnerEmptyMemory : OwnedMemory + internal class OwnerEmptyMemory : OwnedBuffer { readonly static T[] s_empty = new T[0]; - public readonly static OwnedMemory Shared = new OwnerEmptyMemory(); + public readonly static OwnedBuffer Shared = new OwnerEmptyMemory(); public OwnerEmptyMemory() : base(s_empty, 0, 0) { } diff --git a/src/System.Buffers.Primitives/System/Buffers/OwnedMemory.cs b/src/System.Buffers.Primitives/System/Buffers/OwnedMemory.cs index 12984a3463b..01cce50579b 100644 --- a/src/System.Buffers.Primitives/System/Buffers/OwnedMemory.cs +++ b/src/System.Buffers.Primitives/System/Buffers/OwnedMemory.cs @@ -9,7 +9,7 @@ namespace System.Buffers { - public abstract class OwnedMemory : IDisposable, IKnown + public abstract class OwnedBuffer : IDisposable, IKnown { static long _nextId = InitializedId + 1; const int InitializedId = int.MinValue; @@ -39,18 +39,18 @@ public bool HasOutstandingReferences { } } - private OwnedMemory() { } + private OwnedBuffer() { } - protected OwnedMemory(T[] array) : this(array, 0, array.Length) { } + protected OwnedBuffer(T[] array) : this(array, 0, array.Length) { } - protected OwnedMemory(T[] array, int arrayOffset, int length, IntPtr pointer = default(IntPtr)) + protected OwnedBuffer(T[] array, int arrayOffset, int length, IntPtr pointer = default(IntPtr)) { _id = InitializedId; Initialize(array, arrayOffset, length, pointer); } - public Memory Memory => new Memory(this, Length); - public ReadOnlyMemory ReadOnlyMemory => new ReadOnlyMemory(this, Length); + public Buffer Buffer => new Buffer(this, Length); + public ReadOnlyBuffer ReadOnlyMemory => new ReadOnlyBuffer(this, Length); public Span Span { @@ -65,7 +65,7 @@ public Span Span } } - public static implicit operator OwnedMemory(T[] array) => new OwnedArray(array); + public static implicit operator OwnedBuffer(T[] array) => new OwnedArray(array); protected bool TryGetArrayCore(out ArraySegment buffer) { @@ -207,13 +207,18 @@ void VerifyId(long id) { } void ThrowIdHelper() { - throw new ObjectDisposedException(nameof(Memory)); + throw new ObjectDisposedException(nameof(Buffer)); } void ThrowArgHelper() { throw new ArgumentOutOfRangeException(); } #endregion + + public static OwnedBuffer Create(ArraySegment segment) + { + return new OwnedArray(segment); + } } interface IKnown @@ -224,9 +229,9 @@ interface IKnown public struct DisposableReservation : IDisposable { - OwnedMemory _owner; + OwnedBuffer _owner; - internal DisposableReservation(OwnedMemory owner) + internal DisposableReservation(OwnedBuffer owner) { _owner = owner; switch(ReferenceCountingSettings.OwnedMemory) { diff --git a/src/System.Buffers.Primitives/System/Buffers/ReadOnlyMemory.cs b/src/System.Buffers.Primitives/System/Buffers/ReadOnlyMemory.cs index be172e79f83..8e754663562 100644 --- a/src/System.Buffers.Primitives/System/Buffers/ReadOnlyMemory.cs +++ b/src/System.Buffers.Primitives/System/Buffers/ReadOnlyMemory.cs @@ -8,65 +8,65 @@ using System.Runtime; using System.Text; -namespace System +namespace System.Buffers { - [DebuggerTypeProxy(typeof(ReadOnlyMemoryDebuggerView<>))] - public struct ReadOnlyMemory : IEquatable>, IEquatable> + [DebuggerTypeProxy(typeof(ReadOnlyBufferDebuggerView<>))] + public struct ReadOnlyBuffer : IEquatable>, IEquatable> { - readonly OwnedMemory _owner; + readonly OwnedBuffer _owner; readonly int _index; readonly int _length; - internal ReadOnlyMemory(OwnedMemory owner, int length) + internal ReadOnlyBuffer(OwnedBuffer owner, int length) { _owner = owner; _index = 0; _length = length; } - internal ReadOnlyMemory(OwnedMemory owner,int index, int length) + internal ReadOnlyBuffer(OwnedBuffer owner,int index, int length) { _owner = owner; _index = index; _length = length; } - public static implicit operator ReadOnlyMemory(T[] array) + public static implicit operator ReadOnlyBuffer(T[] array) { var owner = new OwnedArray(array); - return owner.Memory; + return owner.Buffer; } - public static ReadOnlyMemory Empty { get; } = OwnerEmptyMemory.Shared.Memory; + public static ReadOnlyBuffer Empty { get; } = OwnerEmptyMemory.Shared.Buffer; public int Length => _length; public bool IsEmpty => Length == 0; - public ReadOnlyMemory Slice(int index) + public ReadOnlyBuffer Slice(int index) { - return new ReadOnlyMemory(_owner, _index + index, _length - index); + return new ReadOnlyBuffer(_owner, _index + index, _length - index); } - public ReadOnlyMemory Slice(int index, int length) + public ReadOnlyBuffer Slice(int index, int length) { - return new ReadOnlyMemory(_owner, _index + index, length); + return new ReadOnlyBuffer(_owner, _index + index, length); } public ReadOnlySpan Span => _owner.GetSpanInternal(_index, _length); public DisposableReservation Reserve() { - return _owner.Memory.Reserve(); + return _owner.Buffer.Reserve(); } - public unsafe MemoryHandle Pin() => MemoryHandle.Create(_owner, _index); + public unsafe BufferHandle Pin() => BufferHandle.Create(_owner, _index); public unsafe bool TryGetPointer(out void* pointer) { if (!_owner.TryGetPointerInternal(out pointer)) { return false; } - pointer = Memory.Add(pointer, _index); + pointer = Buffer.Add(pointer, _index); return true; } @@ -89,27 +89,27 @@ public void CopyTo(Span span) Span.CopyTo(span); } - public void CopyTo(Memory memory) + public void CopyTo(Buffer buffer) { - Span.CopyTo(memory.Span); + Span.CopyTo(buffer.Span); } public override bool Equals(object obj) { - if (!(obj is Memory)) { + if (!(obj is Buffer)) { return false; } - var other = (Memory)obj; + var other = (Buffer)obj; return Equals(other); } - public bool Equals(Memory other) + public bool Equals(Buffer other) { - return Equals((ReadOnlyMemory)other); + return Equals((ReadOnlyBuffer)other); } - public bool Equals(ReadOnlyMemory other) + public bool Equals(ReadOnlyBuffer other) { return _owner == other._owner && @@ -117,20 +117,20 @@ public bool Equals(ReadOnlyMemory other) _length == other._length; } - public static bool operator ==(ReadOnlyMemory left, Memory right) + public static bool operator ==(ReadOnlyBuffer left, Buffer right) { return left.Equals(right); } - public static bool operator !=(ReadOnlyMemory left, Memory right) + public static bool operator !=(ReadOnlyBuffer left, Buffer right) { return left.Equals(right); } - public static bool operator ==(ReadOnlyMemory left, ReadOnlyMemory right) + public static bool operator ==(ReadOnlyBuffer left, ReadOnlyBuffer right) { return left.Equals(right); } - public static bool operator !=(ReadOnlyMemory left, ReadOnlyMemory right) + public static bool operator !=(ReadOnlyBuffer left, ReadOnlyBuffer right) { return left.Equals(right); } diff --git a/src/System.Buffers.Primitives/System/Runtime/DebuggerViews.cs b/src/System.Buffers.Primitives/System/Runtime/DebuggerViews.cs index d665d057668..1eb6a0b62b4 100644 --- a/src/System.Buffers.Primitives/System/Runtime/DebuggerViews.cs +++ b/src/System.Buffers.Primitives/System/Runtime/DebuggerViews.cs @@ -1,42 +1,43 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Buffers; using System.Diagnostics; namespace System.Runtime { - internal class MemoryDebuggerView + internal class BufferDebuggerView { - private ReadOnlyMemory _memory; + private ReadOnlyBuffer _buffer; - public MemoryDebuggerView(Memory memory) + public BufferDebuggerView(Buffer buffer) { - _memory = memory; + _buffer = buffer; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public T[] Items { get { - return _memory.ToArray(); + return _buffer.ToArray(); } } } - internal class ReadOnlyMemoryDebuggerView + internal class ReadOnlyBufferDebuggerView { - private ReadOnlyMemory _memory; + private ReadOnlyBuffer _buffer; - public ReadOnlyMemoryDebuggerView(ReadOnlyMemory memory) + public ReadOnlyBufferDebuggerView(ReadOnlyBuffer buffer) { - _memory = memory; + _buffer = buffer; } [DebuggerBrowsable(DebuggerBrowsableState.RootHidden)] public T[] Items { get { - return _memory.ToArray(); + return _buffer.ToArray(); } } } diff --git a/src/System.IO.Pipelines.Compression/CompressionPipelineExtensions.cs b/src/System.IO.Pipelines.Compression/CompressionPipelineExtensions.cs index e03a22881ca..bbf8e1f7846 100644 --- a/src/System.IO.Pipelines.Compression/CompressionPipelineExtensions.cs +++ b/src/System.IO.Pipelines.Compression/CompressionPipelineExtensions.cs @@ -102,7 +102,7 @@ public WritableDeflateTransform(CompressionLevel compressionLevel, int bits) public async Task Execute(IPipeReader reader, IPipeWriter writer) { - List handles = new List(); + List handles = new List(); while (true) { @@ -121,13 +121,13 @@ public async Task Execute(IPipeReader reader, IPipeWriter writer) } var writerBuffer = writer.Alloc(); - var memory = inputBuffer.First; + var buffer = inputBuffer.First; unsafe { - var handle = memory.Pin(); + var handle = buffer.Pin(); handles.Add(handle); - _deflater.SetInput((IntPtr)handle.PinnedPointer, memory.Length); + _deflater.SetInput((IntPtr)handle.PinnedPointer, buffer.Length); } while (!_deflater.NeedsInput()) @@ -135,14 +135,14 @@ public async Task Execute(IPipeReader reader, IPipeWriter writer) unsafe { writerBuffer.Ensure(); - var handle = writerBuffer.Memory.Pin(); + var handle = writerBuffer.Buffer.Pin(); handles.Add(handle); - int written = _deflater.ReadDeflateOutput((IntPtr)handle.PinnedPointer, writerBuffer.Memory.Length); + int written = _deflater.ReadDeflateOutput((IntPtr)handle.PinnedPointer, writerBuffer.Buffer.Length); writerBuffer.Advance(written); } } - var consumed = memory.Length - _deflater.AvailableInput; + var consumed = buffer.Length - _deflater.AvailableInput; inputBuffer = inputBuffer.Slice(0, consumed); @@ -160,7 +160,7 @@ public async Task Execute(IPipeReader reader, IPipeWriter writer) unsafe { writerBuffer.Ensure(); - var memory = writerBuffer.Memory; + var memory = writerBuffer.Buffer; var handle = memory.Pin(); handles.Add(handle); int compressedBytes; @@ -181,7 +181,7 @@ public async Task Execute(IPipeReader reader, IPipeWriter writer) unsafe { writerBuffer.Ensure(); - var memory = writerBuffer.Memory; + var memory = writerBuffer.Buffer; var handle = memory.Pin(); handles.Add(handle); int compressedBytes; @@ -217,7 +217,7 @@ public ReadableDeflateTransform(int bits) public async Task Execute(IPipeReader reader, IPipeWriter writer) { - List handles = new List(); + List handles = new List(); while (true) { @@ -236,22 +236,22 @@ public async Task Execute(IPipeReader reader, IPipeWriter writer) } var writerBuffer = writer.Alloc(); - var memory = inputBuffer.First; - if (memory.Length > 0) + var buffer = inputBuffer.First; + if (buffer.Length > 0) { unsafe { - var handle = memory.Pin(); + var handle = buffer.Pin(); handles.Add(handle); - _inflater.SetInput((IntPtr)handle.PinnedPointer, memory.Length); + _inflater.SetInput((IntPtr)handle.PinnedPointer, buffer.Length); writerBuffer.Ensure(); - handle = writerBuffer.Memory.Pin(); + handle = writerBuffer.Buffer.Pin(); handles.Add(handle); - int written = _inflater.Inflate((IntPtr)handle.PinnedPointer, writerBuffer.Memory.Length); + int written = _inflater.Inflate((IntPtr)handle.PinnedPointer, writerBuffer.Buffer.Length); writerBuffer.Advance(written); - var consumed = memory.Length - _inflater.AvailableInput; + var consumed = buffer.Length - _inflater.AvailableInput; inputBuffer = inputBuffer.Slice(0, consumed); } diff --git a/src/System.IO.Pipelines.Extensions/ReadWriteExtensions.cs b/src/System.IO.Pipelines.Extensions/ReadWriteExtensions.cs index cc7298352cb..95f571c2420 100644 --- a/src/System.IO.Pipelines.Extensions/ReadWriteExtensions.cs +++ b/src/System.IO.Pipelines.Extensions/ReadWriteExtensions.cs @@ -78,7 +78,7 @@ public static void WriteBigEndian<[Primitive]T>(this WritableBuffer buffer, T va { int len = Unsafe.SizeOf(); buffer.Ensure(len); - buffer.Memory.Span.WriteBigEndian(value); + buffer.Buffer.Span.WriteBigEndian(value); buffer.Advance(len); } @@ -90,7 +90,7 @@ public static void WriteLittleEndian<[Primitive]T>(this WritableBuffer buffer, T { int len = Unsafe.SizeOf(); buffer.Ensure(len); - buffer.Memory.Span.WriteLittleEndian(value); + buffer.Buffer.Span.WriteLittleEndian(value); buffer.Advance(len); } } diff --git a/src/System.IO.Pipelines.Extensions/StreamExtensions.cs b/src/System.IO.Pipelines.Extensions/StreamExtensions.cs index 64b3ffe3cc8..a39caf5d2e0 100644 --- a/src/System.IO.Pipelines.Extensions/StreamExtensions.cs +++ b/src/System.IO.Pipelines.Extensions/StreamExtensions.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Buffers.Pools; +using System.Buffers; using System.Threading; using System.Threading.Tasks; @@ -16,7 +16,7 @@ public static class StreamExtensions /// public static IPipeWriter AsPipelineWriter(this Stream stream) { - return (stream as IPipeWriter) ?? stream.AsPipelineWriter(ManagedBufferPool.Shared); + return (stream as IPipeWriter) ?? stream.AsPipelineWriter(BufferPool.Default); } /// @@ -106,7 +106,7 @@ private static async Task CopyMultipleToStreamAsync(this ReadableBuffer buffer, } } - private static async Task WriteToStream(Stream stream, Memory memory) + private static async Task WriteToStream(Stream stream, Buffer memory) { ArraySegment data; if (memory.TryGetArray(out data)) diff --git a/src/System.IO.Pipelines.Extensions/UnownedBufferReader.cs b/src/System.IO.Pipelines.Extensions/UnownedBufferReader.cs index 277b906bb40..bf10fd596ff 100644 --- a/src/System.IO.Pipelines.Extensions/UnownedBufferReader.cs +++ b/src/System.IO.Pipelines.Extensions/UnownedBufferReader.cs @@ -93,7 +93,7 @@ public async Task WriteAsync(ArraySegment buffer, CancellationToken cancel /// /// // Called by the WRITER - public async Task WriteAsync(OwnedMemory buffer, CancellationToken cancellationToken) + public async Task WriteAsync(OwnedBuffer buffer, CancellationToken cancellationToken) { // If Writing has stopped, why is the caller writing?? if (Writing.Status != TaskStatus.WaitingForActivation) @@ -119,7 +119,7 @@ public async Task WriteAsync(OwnedMemory buffer, CancellationToken cancell // Allocate a new segment to hold the buffer being written. using (var segment = new BufferSegment(buffer)) { - segment.End = buffer.Memory.Length; + segment.End = buffer.Buffer.Length; if (_head == null || _head.ReadableBytes == 0) { diff --git a/src/System.IO.Pipelines.File/FileReader.cs b/src/System.IO.Pipelines.File/FileReader.cs index 6e8e59d46e0..e6c8ebe192d 100644 --- a/src/System.IO.Pipelines.File/FileReader.cs +++ b/src/System.IO.Pipelines.File/FileReader.cs @@ -112,9 +112,9 @@ private class ReadOperation public unsafe void Read() { var buffer = Writer.Alloc(2048); - fixed (byte* source = &buffer.Memory.Span.DangerousGetPinnableReference()) + fixed (byte* source = &buffer.Buffer.Span.DangerousGetPinnableReference()) { - var count = buffer.Memory.Length; + var count = buffer.Buffer.Length; var overlapped = ThreadPoolBoundHandle.AllocateNativeOverlapped(PreAllocatedOverlapped); overlapped->OffsetLow = Offset; diff --git a/src/System.IO.Pipelines.Networking.Libuv/Interop/UvWriteReq.cs b/src/System.IO.Pipelines.Networking.Libuv/Interop/UvWriteReq.cs index 423795a669f..160656f0042 100644 --- a/src/System.IO.Pipelines.Networking.Libuv/Interop/UvWriteReq.cs +++ b/src/System.IO.Pipelines.Networking.Libuv/Interop/UvWriteReq.cs @@ -20,7 +20,7 @@ public class UvWriteReq : UvRequest private object _state; private const int BUFFER_COUNT = 4; - private List _handles = new List(); + private List _handles = new List(); private List _pins = new List(BUFFER_COUNT + 1); private LibuvAwaitable _awaitable = new LibuvAwaitable(); diff --git a/src/System.IO.Pipelines.Networking.Libuv/UvTcpConnection.cs b/src/System.IO.Pipelines.Networking.Libuv/UvTcpConnection.cs index eebc9745310..036bec35360 100644 --- a/src/System.IO.Pipelines.Networking.Libuv/UvTcpConnection.cs +++ b/src/System.IO.Pipelines.Networking.Libuv/UvTcpConnection.cs @@ -22,7 +22,7 @@ public class UvTcpConnection : IPipeConnection private Task _sendingTask; private WritableBuffer? _inputBuffer; - private MemoryHandle _inputBufferPin; + private BufferHandle _inputBufferPin; public UvTcpConnection(UvThread thread, UvTcpHandle handle) { @@ -242,10 +242,10 @@ private unsafe Uv.uv_buf_t OnAlloc(UvStreamHandle handle, int status) var inputBuffer = _input.Writer.Alloc(2048); _inputBuffer = inputBuffer; - var pinnedHandle = inputBuffer.Memory.Pin(); + var pinnedHandle = inputBuffer.Buffer.Pin(); _inputBufferPin = pinnedHandle; - return handle.Libuv.buf_init((IntPtr)pinnedHandle.PinnedPointer, inputBuffer.Memory.Length); + return handle.Libuv.buf_init((IntPtr)pinnedHandle.PinnedPointer, inputBuffer.Buffer.Length); } } } diff --git a/src/System.IO.Pipelines.Networking.Sockets/SocketConnection.cs b/src/System.IO.Pipelines.Networking.Sockets/SocketConnection.cs index 2163a1c5874..fdc1c3e88eb 100644 --- a/src/System.IO.Pipelines.Networking.Sockets/SocketConnection.cs +++ b/src/System.IO.Pipelines.Networking.Sockets/SocketConnection.cs @@ -380,7 +380,7 @@ private async Task ReceiveFromSocketAndPushToWriterAsync() while (Socket.Available != 0 && buffer.BytesWritten < FlushInputEveryBytes) { buffer.Ensure(); // ask for *something*, then use whatever is available (usually much much more) - SetBuffer(buffer.Memory, args); + SetBuffer(buffer.Buffer, args); // await async for the io work to be completed await Socket.ReceiveSignalAsync(args); @@ -607,7 +607,7 @@ private async Task ReadFromReaderAndWriteToSocketAsync() } // unsafe+async not good friends - private unsafe void SetBuffer(Memory memory, SocketAsyncEventArgs args, int ignore = 0) + private unsafe void SetBuffer(Buffer memory, SocketAsyncEventArgs args, int ignore = 0) { ArraySegment segment; if (!memory.TryGetArray(out segment)) diff --git a/src/System.IO.Pipelines.Networking.Windows.RIO/RioTcpConnection.cs b/src/System.IO.Pipelines.Networking.Windows.RIO/RioTcpConnection.cs index bb5bb0c92ed..1b3437a3f32 100644 --- a/src/System.IO.Pipelines.Networking.Windows.RIO/RioTcpConnection.cs +++ b/src/System.IO.Pipelines.Networking.Windows.RIO/RioTcpConnection.cs @@ -64,7 +64,7 @@ internal RioTcpConnection(IntPtr socket, long connectionId, IntPtr requestQueue, private void ProcessReceives() { _buffer = _input.Writer.Alloc(2048); - var receiveBufferSeg = GetSegmentFromMemory(_buffer.Memory); + var receiveBufferSeg = GetSegmentFromMemory(_buffer.Buffer); if (!_rio.RioReceive(_requestQueue, ref receiveBufferSeg, 1, RioReceiveFlags.None, 0)) { @@ -111,7 +111,7 @@ private async Task ProcessSends() _output.Reader.Complete(); } - private Task SendAsync(Memory memory, bool endOfMessage) + private Task SendAsync(Buffer memory, bool endOfMessage) { if (!IsReadyToSend) { @@ -130,7 +130,7 @@ private Task SendAsync(Memory memory, bool endOfMessage) return _completedTask; } - private async Task SendAsyncAwaited(Memory memory, bool endOfMessage) + private async Task SendAsyncAwaited(Buffer memory, bool endOfMessage) { await ReadyToSend; @@ -219,7 +219,7 @@ public void ReceiveEndComplete() _buffer.FlushAsync(); } - private unsafe RioBufferSegment GetSegmentFromMemory(Memory memory) + private unsafe RioBufferSegment GetSegmentFromMemory(Buffer memory) { void* pointer; if (!memory.TryGetPointer(out pointer)) diff --git a/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs b/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs index a5cf1f4a39d..bd97d2c0c51 100644 --- a/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs +++ b/src/System.IO.Pipelines.Text.Primitives/PipelineTextOutput.cs @@ -30,7 +30,7 @@ public Span Buffer { EnsureBuffer(); - return _writableBuffer.Memory.Span; + return _writableBuffer.Buffer.Span; } } diff --git a/src/System.IO.Pipelines/BufferSegment.cs b/src/System.IO.Pipelines/BufferSegment.cs index 7693b9b3187..19ee3856bd9 100644 --- a/src/System.IO.Pipelines/BufferSegment.cs +++ b/src/System.IO.Pipelines/BufferSegment.cs @@ -35,23 +35,23 @@ internal class BufferSegment : IDisposable /// /// The buffer being tracked /// - private OwnedMemory _buffer; + private OwnedBuffer _owned; - private Memory _memory; + private Buffer _buffer; - public BufferSegment(OwnedMemory buffer) + public BufferSegment(OwnedBuffer buffer) { - _buffer = buffer; + _owned = buffer; Start = 0; End = 0; - _buffer.AddReference(); - _memory = _buffer.Memory; + _owned.AddReference(); + _buffer = _owned.Buffer; } - public BufferSegment(OwnedMemory buffer, int start, int end) + public BufferSegment(OwnedBuffer buffer, int start, int end) { - _buffer = buffer; + _owned = buffer; Start = start; End = end; ReadOnly = true; @@ -61,14 +61,14 @@ public BufferSegment(OwnedMemory buffer, int start, int end) var unowned = buffer as UnownedBuffer; if (unowned != null) { - _buffer = unowned.MakeCopy(start, end - start, out Start, out End); + _owned = unowned.MakeCopy(start, end - start, out Start, out End); } - _buffer.AddReference(); - _memory = _buffer.Memory; + _owned.AddReference(); + _buffer = _owned.Buffer; } - public Memory Memory => _memory; + public Buffer Buffer => _buffer; /// /// If true, data should not be written into the backing block after the End offset. Data between start and end should never be modified @@ -88,13 +88,13 @@ public BufferSegment(OwnedMemory buffer, int start, int end) public void Dispose() { - Debug.Assert(_buffer.HasOutstandingReferences); + Debug.Assert(_owned.HasOutstandingReferences); - _buffer.Release(); + _owned.Release(); - if (!_buffer.HasOutstandingReferences) + if (!_owned.HasOutstandingReferences) { - _buffer.Dispose(); + _owned.Dispose(); } } @@ -106,7 +106,7 @@ public void Dispose() public override string ToString() { var builder = new StringBuilder(); - var data = _buffer.Memory.Slice(Start, ReadableBytes).Span; + var data = _owned.Buffer.Slice(Start, ReadableBytes).Span; for (int i = 0; i < ReadableBytes; i++) { @@ -122,24 +122,24 @@ public static BufferSegment Clone(ReadCursor beginBuffer, ReadCursor endBuffer, if (beginOrig == endOrig) { - lastSegment = new BufferSegment(beginOrig._buffer, beginBuffer.Index, endBuffer.Index); + lastSegment = new BufferSegment(beginOrig._owned, beginBuffer.Index, endBuffer.Index); return lastSegment; } - var beginClone = new BufferSegment(beginOrig._buffer, beginBuffer.Index, beginOrig.End); + var beginClone = new BufferSegment(beginOrig._owned, beginBuffer.Index, beginOrig.End); var endClone = beginClone; beginOrig = beginOrig.Next; while (beginOrig != endOrig) { - endClone.Next = new BufferSegment(beginOrig._buffer, beginOrig.Start, beginOrig.End); + endClone.Next = new BufferSegment(beginOrig._owned, beginOrig.Start, beginOrig.End); endClone = endClone.Next; beginOrig = beginOrig.Next; } - lastSegment = new BufferSegment(endOrig._buffer, endOrig.Start, endBuffer.Index); + lastSegment = new BufferSegment(endOrig._owned, endOrig.Start, endBuffer.Index); endClone.Next = lastSegment; return beginClone; diff --git a/src/System.IO.Pipelines/MemoryEnumerator.cs b/src/System.IO.Pipelines/MemoryEnumerator.cs index f1f3e63254f..1512a610651 100644 --- a/src/System.IO.Pipelines/MemoryEnumerator.cs +++ b/src/System.IO.Pipelines/MemoryEnumerator.cs @@ -1,43 +1,45 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Buffers; + namespace System.IO.Pipelines { /// /// An enumerator over the /// - public struct MemoryEnumerator + public struct BufferEnumerator { private SegmentEnumerator _segmentEnumerator; - private Memory _current; + private Buffer _current; /// /// /// - public MemoryEnumerator(ReadCursor start, ReadCursor end) + public BufferEnumerator(ReadCursor start, ReadCursor end) { _segmentEnumerator = new SegmentEnumerator(start, end); - _current = default(Memory); + _current = default(Buffer); } /// - /// The current + /// The current /// - public Memory Current => _current; + public Buffer Current => _current; /// - /// Moves to the next in the + /// Moves to the next in the /// /// public bool MoveNext() { if (!_segmentEnumerator.MoveNext()) { - _current = Memory.Empty; + _current = Buffer.Empty; return false; } var current = _segmentEnumerator.Current; - _current = current.Segment.Memory.Slice(current.Start, current.Length); + _current = current.Segment.Buffer.Slice(current.Start, current.Length); return true; } diff --git a/src/System.IO.Pipelines/MemoryPool.cs b/src/System.IO.Pipelines/MemoryPool.cs index 4c3a9ef4a56..ffd4929ea4d 100644 --- a/src/System.IO.Pipelines/MemoryPool.cs +++ b/src/System.IO.Pipelines/MemoryPool.cs @@ -68,7 +68,7 @@ public class MemoryPool : BufferPool private Action _slabDeallocationCallback; - public override OwnedMemory Rent(int size) + public override OwnedBuffer Rent(int size) { if (size > _blockLength) { diff --git a/src/System.IO.Pipelines/MemoryPoolBlock.cs b/src/System.IO.Pipelines/MemoryPoolBlock.cs index 23f78ae4f44..59e0c5a5a22 100644 --- a/src/System.IO.Pipelines/MemoryPoolBlock.cs +++ b/src/System.IO.Pipelines/MemoryPoolBlock.cs @@ -10,7 +10,7 @@ namespace System.IO.Pipelines /// Block tracking object used by the byte buffer memory pool. A slab is a large allocation which is divided into smaller blocks. The /// individual blocks are then treated as independent array segments. /// - public class MemoryPoolBlock : OwnedMemory + public class MemoryPoolBlock : OwnedBuffer { private readonly int _offset; private readonly int _length; @@ -84,7 +84,7 @@ internal static MemoryPoolBlock Create( public override string ToString() { var builder = new StringBuilder(); - SpanExtensions.AppendAsLiteral(Memory.Span, builder); + SpanExtensions.AppendAsLiteral(Buffer.Span, builder); return builder.ToString(); } diff --git a/src/System.IO.Pipelines/Pipe.cs b/src/System.IO.Pipelines/Pipe.cs index e62bfc2e7a4..26db92e1d17 100644 --- a/src/System.IO.Pipelines/Pipe.cs +++ b/src/System.IO.Pipelines/Pipe.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Buffers.Pools; +using System.Buffers; using System.Diagnostics; namespace System.IO.Pipelines @@ -88,7 +88,7 @@ public Pipe(BufferPool pool, PipeOptions options = null) _writerAwaitable = new PipeAwaitable(completed: true); } - internal Memory Memory => _writingHead?.Memory.Slice(_writingHead.End, _writingHead.WritableBytes) ?? Memory.Empty; + internal Buffer Buffer => _writingHead?.Buffer.Slice(_writingHead.End, _writingHead.WritableBytes) ?? Buffer.Empty; /// /// Allocates memory from the pipeline to write into. @@ -300,7 +300,7 @@ internal void AdvanceWriter(int bytesWritten) Debug.Assert(!_writingHead.ReadOnly); Debug.Assert(_writingHead.Next == null); - var buffer = _writingHead.Memory; + var buffer = _writingHead.Buffer; var bufferIndex = _writingHead.End + bytesWritten; Debug.Assert(bufferIndex <= buffer.Length); diff --git a/src/System.IO.Pipelines/PipeFactory.cs b/src/System.IO.Pipelines/PipeFactory.cs index a3b61ac3f53..5b104d9430e 100644 --- a/src/System.IO.Pipelines/PipeFactory.cs +++ b/src/System.IO.Pipelines/PipeFactory.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. -using System.Buffers.Pools; +using System.Buffers; namespace System.IO.Pipelines { diff --git a/src/System.IO.Pipelines/ReadCursor.cs b/src/System.IO.Pipelines/ReadCursor.cs index 9bc9fc2bc62..0fabe3a75c3 100644 --- a/src/System.IO.Pipelines/ReadCursor.cs +++ b/src/System.IO.Pipelines/ReadCursor.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Buffers; using System.Runtime.CompilerServices; using System.Text; @@ -186,11 +187,11 @@ private static void ThrowOutOfBoundsException() } [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal bool TryGetBuffer(ReadCursor end, out Memory data) + internal bool TryGetBuffer(ReadCursor end, out Buffer data) { if (IsDefault) { - data = Memory.Empty; + data = Buffer.Empty; return false; } @@ -203,11 +204,11 @@ internal bool TryGetBuffer(ReadCursor end, out Memory data) if (following > 0) { - data = segment.Memory.Slice(index, following); + data = segment.Buffer.Slice(index, following); return true; } - data = Memory.Empty; + data = Buffer.Empty; return false; } else @@ -216,7 +217,7 @@ internal bool TryGetBuffer(ReadCursor end, out Memory data) } } - private bool TryGetBufferMultiBlock(ReadCursor end, out Memory data) + private bool TryGetBufferMultiBlock(ReadCursor end, out Buffer data) { var segment = _segment; var index = _index; @@ -248,7 +249,7 @@ private bool TryGetBufferMultiBlock(ReadCursor end, out Memory data) if (wasLastSegment) { - data = Memory.Empty; + data = Buffer.Empty; return false; } else @@ -258,7 +259,7 @@ private bool TryGetBufferMultiBlock(ReadCursor end, out Memory data) } } - data = segment.Memory.Slice(index, following); + data = segment.Buffer.Slice(index, following); return true; } @@ -270,7 +271,7 @@ public override string ToString() } var sb = new StringBuilder(); - Span span = Segment.Memory.Span.Slice(Index, Segment.End - Index); + Span span = Segment.Buffer.Span.Slice(Index, Segment.End - Index); SpanExtensions.AppendAsLiteral(span, sb); return sb.ToString(); } diff --git a/src/System.IO.Pipelines/ReadCursorOperations.cs b/src/System.IO.Pipelines/ReadCursorOperations.cs index b3c86c5aa85..ff3a2c8a1bd 100644 --- a/src/System.IO.Pipelines/ReadCursorOperations.cs +++ b/src/System.IO.Pipelines/ReadCursorOperations.cs @@ -15,7 +15,7 @@ public static int Seek(ReadCursor begin, ReadCursor end, out ReadCursor result, { var segmentPart = enumerator.Current; var segment = segmentPart.Segment; - var span = segment.Memory.Span.Slice(segmentPart.Start, segmentPart.Length); + var span = segment.Buffer.Span.Slice(segmentPart.Start, segmentPart.Length); int index = span.IndexOf(byte0); if (index != -1) @@ -36,7 +36,7 @@ public static int Seek(ReadCursor begin, ReadCursor end, out ReadCursor result, { var segmentPart = enumerator.Current; var segment = segmentPart.Segment; - var span = segment.Memory.Span.Slice(segmentPart.Start, segmentPart.Length); + var span = segment.Buffer.Span.Slice(segmentPart.Start, segmentPart.Length); int index = span.IndexOf(byte0, byte1); @@ -58,7 +58,7 @@ public static int Seek(ReadCursor begin, ReadCursor end, out ReadCursor result, { var segmentPart = enumerator.Current; var segment = segmentPart.Segment; - var span = segment.Memory.Span.Slice(segmentPart.Start, segmentPart.Length); + var span = segment.Buffer.Span.Slice(segmentPart.Start, segmentPart.Length); int index = span.IndexOf(byte0, byte1, byte2); diff --git a/src/System.IO.Pipelines/ReadableBuffer.cs b/src/System.IO.Pipelines/ReadableBuffer.cs index 52fcdaf5019..f34dad3f183 100644 --- a/src/System.IO.Pipelines/ReadableBuffer.cs +++ b/src/System.IO.Pipelines/ReadableBuffer.cs @@ -10,7 +10,7 @@ namespace System.IO.Pipelines /// /// Represents a buffer that can read a sequential series of bytes. /// - public struct ReadableBuffer : ISequence> + public struct ReadableBuffer : ISequence> { private ReadCursor _start; private ReadCursor _end; @@ -21,7 +21,7 @@ public struct ReadableBuffer : ISequence> /// public int Length => _length; - int? ISequence>.Length => Length; + int? ISequence>.Length => Length; /// /// Determines if the is empty. @@ -29,15 +29,15 @@ public struct ReadableBuffer : ISequence> public bool IsEmpty => _length == 0; /// - /// Determins if the is a single . + /// Determins if the is a single . /// public bool IsSingleSpan => _start.Segment == _end.Segment; - public Memory First + public Buffer First { get { - _start.TryGetBuffer(_end, out Memory first); + _start.TryGetBuffer(_end, out Buffer first); return first; } } @@ -172,10 +172,10 @@ public void CopyTo(Span destination) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.destination); } - foreach (var memory in this) + foreach (var buffer in this) { - memory.Span.CopyTo(destination); - destination = destination.Slice(memory.Length); + buffer.Span.CopyTo(destination); + destination = destination.Slice(buffer.Length); } } @@ -196,9 +196,9 @@ public byte[] ToArray() public override string ToString() { var sb = new StringBuilder(); - foreach (var memory in this) + foreach (var buffer in this) { - SpanExtensions.AppendAsLiteral(memory.Span, sb); + SpanExtensions.AppendAsLiteral(buffer.Span, sb); } return sb.ToString(); } @@ -206,9 +206,9 @@ public override string ToString() /// /// Returns an enumerator over the /// - public MemoryEnumerator GetEnumerator() + public BufferEnumerator GetEnumerator() { - return new MemoryEnumerator(_start, _end); + return new BufferEnumerator(_start, _end); } internal void ClearCursors() @@ -250,14 +250,14 @@ public static ReadableBuffer Create(byte[] data, int offset, int length) ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.length); } - var buffer = new OwnedArray(data); + OwnedBuffer buffer = data; var segment = new BufferSegment(buffer); segment.Start = offset; segment.End = offset + length; return new ReadableBuffer(new ReadCursor(segment, offset), new ReadCursor(segment, offset + length)); } - bool ISequence>.TryGet(ref Position position, out ReadOnlyMemory item, bool advance) + bool ISequence>.TryGet(ref Position position, out ReadOnlyBuffer item, bool advance) { if (position == Position.First) { @@ -278,7 +278,7 @@ bool ISequence>.TryGet(ref Position position, out ReadOnlyM } else if (position == Position.AfterLast) { - item = default(ReadOnlyMemory); + item = default(ReadOnlyBuffer); return false; } @@ -293,11 +293,11 @@ bool ISequence>.TryGet(ref Position position, out ReadOnlyM } if (currentSegment == _end.Segment) { - item = currentSegment.Memory.Slice(currentSegment.Start, _end.Index - currentSegment.Start); + item = currentSegment.Buffer.Slice(currentSegment.Start, _end.Index - currentSegment.Start); } else { - item = currentSegment.Memory.Slice(currentSegment.Start, currentSegment.End - currentSegment.Start); + item = currentSegment.Buffer.Slice(currentSegment.Start, currentSegment.End - currentSegment.Start); } return true; } diff --git a/src/System.IO.Pipelines/ReadableBufferReader.cs b/src/System.IO.Pipelines/ReadableBufferReader.cs index 5edd153f1fd..37b05b6a7ec 100644 --- a/src/System.IO.Pipelines/ReadableBufferReader.cs +++ b/src/System.IO.Pipelines/ReadableBufferReader.cs @@ -90,7 +90,7 @@ private void MoveNext() var length = part.Length; if (length != 0) { - _currentSpan = part.Segment.Memory.Span.Slice(part.Start, length); + _currentSpan = part.Segment.Buffer.Span.Slice(part.Start, length); _index = 0; return; } diff --git a/src/System.IO.Pipelines/SegmentEnumerator.cs b/src/System.IO.Pipelines/SegmentEnumerator.cs index 881a301d6b9..5ac3a07fe53 100644 --- a/src/System.IO.Pipelines/SegmentEnumerator.cs +++ b/src/System.IO.Pipelines/SegmentEnumerator.cs @@ -27,12 +27,12 @@ public SegmentEnumerator(ReadCursor start, ReadCursor end) } /// - /// The current + /// The current /// public SegmentPart Current => _current; /// - /// Moves to the next in the + /// Moves to the next in the /// /// [MethodImpl(MethodImplOptions.AggressiveInlining)] diff --git a/src/System.IO.Pipelines/Testing/BufferUtilities.cs b/src/System.IO.Pipelines/Testing/BufferUtilities.cs index 6d8764f0a52..3bfcd25bec4 100644 --- a/src/System.IO.Pipelines/Testing/BufferUtilities.cs +++ b/src/System.IO.Pipelines/Testing/BufferUtilities.cs @@ -34,9 +34,9 @@ public static ReadableBuffer CreateBuffer(params byte[][] inputs) chars[dataOffset + j] = s[j]; } - // Create a segment that has offset relative to the OwnedMemory and OwnedMemory itself has offset relative to array - var ownedMemory = new OwnedArray(new ArraySegment(chars, memoryOffset, length * 3)); - var current = new BufferSegment(ownedMemory, length, length * 2); + // Create a segment that has offset relative to the OwnedBuffer and OwnedBuffer itself has offset relative to array + var ownedBuffer = OwnedBuffer.Create(new ArraySegment(chars, memoryOffset, length * 3)); + var current = new BufferSegment(ownedBuffer, length, length * 2); if (first == null) { first = current; diff --git a/src/System.IO.Pipelines/UnownedBuffer.cs b/src/System.IO.Pipelines/UnownedBuffer.cs index 1fb77f40ebf..27a1d89e31c 100644 --- a/src/System.IO.Pipelines/UnownedBuffer.cs +++ b/src/System.IO.Pipelines/UnownedBuffer.cs @@ -9,7 +9,7 @@ namespace System.IO.Pipelines /// /// Represents a buffer that is owned by an external component. /// - public class UnownedBuffer : OwnedMemory + public class UnownedBuffer : OwnedBuffer { private ArraySegment _buffer; @@ -18,14 +18,14 @@ public UnownedBuffer(ArraySegment buffer) : base(buffer.Array, buffer.Offs _buffer = buffer; } - public OwnedMemory MakeCopy(int offset, int length, out int newStart, out int newEnd) + public OwnedBuffer MakeCopy(int offset, int length, out int newStart, out int newEnd) { // Copy to a new Owned Buffer. var buffer = new byte[length]; - Buffer.BlockCopy(_buffer.Array, _buffer.Offset + offset, buffer, 0, length); + System.Buffer.BlockCopy(_buffer.Array, _buffer.Offset + offset, buffer, 0, length); newStart = 0; newEnd = length; - return new OwnedArray(buffer); + return buffer; } } } diff --git a/src/System.IO.Pipelines/WritableBuffer.cs b/src/System.IO.Pipelines/WritableBuffer.cs index d27bb2dc871..e899f21f13c 100644 --- a/src/System.IO.Pipelines/WritableBuffer.cs +++ b/src/System.IO.Pipelines/WritableBuffer.cs @@ -20,20 +20,20 @@ internal WritableBuffer(Pipe pipe) /// /// Available memory. /// - public Memory Memory => _pipe.Memory; + public Buffer Buffer => _pipe.Buffer; /// /// Returns the number of bytes currently written and uncommitted. /// public int BytesWritten => AsReadableBuffer().Length; - Span IOutput.Buffer => Memory.Span; + Span IOutput.Buffer => Buffer.Span; void IOutput.Enlarge(int desiredBufferLength) => Ensure(NewSize(desiredBufferLength)); private int NewSize(int desiredBufferLength) { - var currentSize = Memory.Length; + var currentSize = Buffer.Length; if(desiredBufferLength == 0) { if (currentSize <= 0) return 256; else return currentSize * 2; @@ -55,7 +55,7 @@ public ReadableBuffer AsReadableBuffer() /// /// number of bytes /// - /// Used when writing to directly. + /// Used when writing to directly. /// /// /// More requested than underlying can allocate in a contiguous block. @@ -78,7 +78,7 @@ public void Append(ReadableBuffer buffer) /// Moves forward the underlying 's write cursor but does not commit the data. /// /// number of bytes to be marked as written. - /// Forwards the start of available by . + /// Forwards the start of available by . /// is larger than the current data available data. /// is negative. public void Advance(int bytesWritten) diff --git a/src/System.IO.Pipelines/WritableBufferExtensions.cs b/src/System.IO.Pipelines/WritableBufferExtensions.cs index 54c6ba7eb33..fab72fb1538 100644 --- a/src/System.IO.Pipelines/WritableBufferExtensions.cs +++ b/src/System.IO.Pipelines/WritableBufferExtensions.cs @@ -15,15 +15,15 @@ public static class WritableBufferExtensions /// The to write public static void Write(this WritableBuffer buffer, ReadOnlySpan source) { - if (buffer.Memory.IsEmpty) + if (buffer.Buffer.IsEmpty) { buffer.Ensure(); } // Fast path, try copying to the available memory directly - if (source.Length <= buffer.Memory.Length) + if (source.Length <= buffer.Buffer.Length) { - source.CopyTo(buffer.Memory.Span); + source.CopyTo(buffer.Buffer.Span); buffer.Advance(source.Length); return; } @@ -33,7 +33,7 @@ public static void Write(this WritableBuffer buffer, ReadOnlySpan source) while (remaining > 0) { - var writable = Math.Min(remaining, buffer.Memory.Length); + var writable = Math.Min(remaining, buffer.Buffer.Length); buffer.Ensure(writable); @@ -42,7 +42,7 @@ public static void Write(this WritableBuffer buffer, ReadOnlySpan source) continue; } - source.Slice(offset, writable).CopyTo(buffer.Memory.Span); + source.Slice(offset, writable).CopyTo(buffer.Buffer.Span); remaining -= writable; offset += writable; diff --git a/src/System.Net.Libuv/System/Net/Libuv/Stream.cs b/src/System.Net.Libuv/System/Net/Libuv/Stream.cs index 716e92dd991..f8abc7e69bd 100644 --- a/src/System.Net.Libuv/System/Net/Libuv/Stream.cs +++ b/src/System.Net.Libuv/System/Net/Libuv/Stream.cs @@ -31,7 +31,7 @@ public void ReadStart() } } - public event Action> ReadCompleted; + public event Action> ReadCompleted; public event Action EndOfStream; public unsafe void TryWrite(byte[] data) @@ -81,7 +81,7 @@ public unsafe void TryWrite(byte[] data, int length) } } - public unsafe void TryWrite(Memory data) + public unsafe void TryWrite(Buffer data) { // This can work with Span because it's synchronous but we need pinning support EnsureNotDisposed(); @@ -144,8 +144,8 @@ void OnReadWindows(UVBuffer.Windows buffer, IntPtr bytesAvaliable) } else { - using (var owned = new OwnedNativeMemory((int)bytesRead, buffer.Buffer)) { - OnReadCompleted(owned.Memory); + using (var owned = new OwnedNativeBuffer((int)bytesRead, buffer.Buffer)) { + OnReadCompleted(owned.Buffer); //buffer.Dispose(); // TODO: owned memory frees the memory. this is bad; need to fix } } @@ -174,14 +174,14 @@ void OnReadUnix(UVBuffer.Unix buffer, IntPtr bytesAvaliable) } else { - using (var owned = new OwnedNativeMemory((int)bytesRead, buffer.Buffer)) { - OnReadCompleted(owned.Memory); + using (var owned = new OwnedNativeBuffer((int)bytesRead, buffer.Buffer)) { + OnReadCompleted(owned.Buffer); //buffer.Dispose(); // TODO: owned memory frees the memory. this is bad; need to fix } } } - void OnReadCompleted(Memory bytesRead) + void OnReadCompleted(Buffer bytesRead) { if (ReadCompleted != null) { diff --git a/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs b/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs index e3b7f609547..45827e704a8 100644 --- a/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs +++ b/src/System.Text.Formatting/System/Text/Formatting/Formatters/SequenceFormatter.cs @@ -9,15 +9,15 @@ namespace System.Text.Formatting { public static class SequenceFormatterExtensions { - public static SequenceFormatter CreateFormatter(this TSequence sequence, TextEncoder encoder = default(TextEncoder)) where TSequence : ISequence> + public static SequenceFormatter CreateFormatter(this TSequence sequence, TextEncoder encoder = default(TextEncoder)) where TSequence : ISequence> { return new SequenceFormatter(sequence, encoder); } } - public class SequenceFormatter : ITextOutput where TSequence : ISequence> + public class SequenceFormatter : ITextOutput where TSequence : ISequence> { - ISequence> _buffers; + ISequence> _buffers; TextEncoder _encoder; Position _currentPosition = Position.First; @@ -40,17 +40,17 @@ Span IOutput.Buffer } } - private Memory Current { + private Buffer Current { get { - Memory result; + Buffer result; if (!_buffers.TryGet(ref _currentPosition, out result, advance: false)) { throw new InvalidOperationException(); } return result; } } - private Memory Previous + private Buffer Previous { get { - Memory result; + Buffer result; if (!_buffers.TryGet(ref _previousPosition, out result, advance: false)) { throw new InvalidOperationException(); } return result; } @@ -68,7 +68,7 @@ void IOutput.Enlarge(int desiredBufferLength) _previousPosition = _currentPosition; _previousWrittenBytes = _currentWrittenBytes; - Memory span; + Buffer span; if (!_buffers.TryGet(ref _currentPosition, out span)) { throw new InvalidOperationException(); } diff --git a/src/System.Text.Formatting/System/Text/Parsing/SequenceParser.cs b/src/System.Text.Formatting/System/Text/Parsing/SequenceParser.cs index 42811c523aa..63fe3bcd62c 100644 --- a/src/System.Text.Formatting/System/Text/Parsing/SequenceParser.cs +++ b/src/System.Text.Formatting/System/Text/Parsing/SequenceParser.cs @@ -11,15 +11,15 @@ public static class TextSequenceExtensions { const int StackBufferSize = 128; - public static bool TryParseUInt64(this T memorySequence, out ulong value, out int consumed) where T : ISequence> + public static bool TryParseUInt64(this T bufferSequence, out ulong value, out int consumed) where T : ISequence> { value = default(uint); consumed = default(int); Position position = Position.First; // Fetch the first segment - ReadOnlyMemory first; - if (!memorySequence.TryGet(ref position, out first)) { + ReadOnlyBuffer first; + if (!bufferSequence.TryGet(ref position, out first)) { return false; } @@ -30,8 +30,8 @@ public static bool TryParseUInt64(this T memorySequence, out ulong value, out } // Apparently the we need data from the second segment to succesfully parse, and so fetch the second segment. - ReadOnlyMemory second; - if (!memorySequence.TryGet(ref position, out second)) { + ReadOnlyBuffer second; + if (!bufferSequence.TryGet(ref position, out second)) { // if there is no second segment and the first parsed succesfully, return the result of the parsing. if (parsed) return true; return false; @@ -52,9 +52,9 @@ public static bool TryParseUInt64(this T memorySequence, out ulong value, out second.CopyTo(free); free = free.Slice(second.Length); - ReadOnlyMemory next; + ReadOnlyBuffer next; while (free.Length > 0) { - if (memorySequence.TryGet(ref position, out next)) { + if (bufferSequence.TryGet(ref position, out next)) { if (next.Length > free.Length) next = next.Slice(0, free.Length); next.CopyTo(free); free = free.Slice(next.Length); @@ -77,22 +77,22 @@ public static bool TryParseUInt64(this T memorySequence, out ulong value, out // for invariant culture, we should never reach this point, as invariant uint text is never longer than 127 bytes. // I left this code here, as we will need it for custom cultures and possibly when we shrink the stack allocated buffer. - combinedSpan = memorySequence.ToSpan(); + combinedSpan = bufferSequence.ToSpan(); if (!PrimitiveParser.InvariantUtf8.TryParseUInt64(first.Span, out value, out consumed)) { return false; } return true; } - public static bool TryParseUInt32(this T memorySequence, out uint value, out int consumed) where T : ISequence> + public static bool TryParseUInt32(this T bufferSequence, out uint value, out int consumed) where T : ISequence> { value = default(uint); consumed = default(int); Position position = Position.First; // Fetch the first segment - ReadOnlyMemory first; - if (!memorySequence.TryGet(ref position, out first)) { + ReadOnlyBuffer first; + if (!bufferSequence.TryGet(ref position, out first)) { return false; } @@ -103,8 +103,8 @@ public static bool TryParseUInt32(this T memorySequence, out uint value, out } // Apparently the we need data from the second segment to succesfully parse, and so fetch the second segment. - ReadOnlyMemory second; - if (!memorySequence.TryGet(ref position, out second)) { + ReadOnlyBuffer second; + if (!bufferSequence.TryGet(ref position, out second)) { // if there is no second segment and the first parsed succesfully, return the result of the parsing. if (parsed) return true; return false; @@ -125,9 +125,9 @@ public static bool TryParseUInt32(this T memorySequence, out uint value, out second.CopyTo(free); free = free.Slice(second.Length); - ReadOnlyMemory next; + ReadOnlyBuffer next; while (free.Length > 0) { - if (memorySequence.TryGet(ref position, out next)) { + if (bufferSequence.TryGet(ref position, out next)) { if (next.Length > free.Length) next = next.Slice(0, free.Length); next.CopyTo(free); free = free.Slice(next.Length); @@ -150,7 +150,7 @@ public static bool TryParseUInt32(this T memorySequence, out uint value, out // for invariant culture, we should never reach this point, as invariant uint text is never longer than 127 bytes. // I left this code here, as we will need it for custom cultures and possibly when we shrink the stack allocated buffer. - combinedSpan = memorySequence.ToSpan(); + combinedSpan = bufferSequence.ToSpan(); if (!PrimitiveParser.InvariantUtf8.TryParseUInt32(combinedSpan, out value, out consumed)) { return false; } diff --git a/src/System.Text.Http/System/Text/Http/HttpRequest.cs b/src/System.Text.Http/System/Text/Http/HttpRequest.cs index 034c28e5c2e..1e63f65a7a9 100644 --- a/src/System.Text.Http/System/Text/Http/HttpRequest.cs +++ b/src/System.Text.Http/System/Text/Http/HttpRequest.cs @@ -12,7 +12,7 @@ public struct HttpRequestLine { public HttpMethod Method; public HttpVersion Version; - public Memory RequestUri; + public Buffer RequestUri; public override string ToString() { @@ -152,7 +152,7 @@ public static string ToString(this ReadOnlyBytes? bytes, TextEncoder encoder) return ToString(bytes.Value, encoder); } - public static string ToString(this Memory bytes, TextEncoder encoder) + public static string ToString(this Buffer bytes, TextEncoder encoder) { if (encoder.Encoding == TextEncoder.EncodingName.Utf8) { @@ -170,7 +170,7 @@ public static string ToString(this ReadOnlyBytes bytes, TextEncoder encoder) if (encoder.Encoding == TextEncoder.EncodingName.Utf8) { var position = Position.First; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8String(segment.Span)); @@ -195,7 +195,7 @@ public static Utf8String ToUtf8String(this ReadOnlyBytes bytes, TextEncoder enco if (encoder.Encoding == TextEncoder.EncodingName.Utf8) { var position = Position.First; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (bytes.TryGet(ref position, out segment, true)) { sb.Append(new Utf8String(segment.Span)); diff --git a/src/System.Text.Json/System/Text/Json/JsonParseObject.cs b/src/System.Text.Json/System/Text/Json/JsonParseObject.cs index 76457fe2fc0..26beb49c4e8 100644 --- a/src/System.Text.Json/System/Text/Json/JsonParseObject.cs +++ b/src/System.Text.Json/System/Text/Json/JsonParseObject.cs @@ -11,7 +11,7 @@ namespace System.Text.Json public struct JsonObject : IDisposable { private BufferPool _pool; - private OwnedMemory _dbMemory; + private OwnedBuffer _dbMemory; private ReadOnlySpan _db; private ReadOnlySpan _values; @@ -29,7 +29,7 @@ public static JsonObject Parse(ReadOnlySpan utf8Json, BufferPool pool = nu return result; } - internal JsonObject(ReadOnlySpan values, ReadOnlySpan db, BufferPool pool = null, OwnedMemory dbMemory = null) + internal JsonObject(ReadOnlySpan values, ReadOnlySpan db, BufferPool pool = null, OwnedBuffer dbMemory = null) { _db = db; _values = values; diff --git a/src/System.Text.Json/System/Text/Json/JsonParser.cs b/src/System.Text.Json/System/Text/Json/JsonParser.cs index 7b6773c4c10..138e2941a6a 100644 --- a/src/System.Text.Json/System/Text/Json/JsonParser.cs +++ b/src/System.Text.Json/System/Text/Json/JsonParser.cs @@ -37,7 +37,7 @@ static DbRow() internal struct TwoStacks { - Memory _memory; + Buffer _memory; int topOfStackObj; int topOfStackArr; int capacity; @@ -54,7 +54,7 @@ public bool IsFull { } } - public TwoStacks(Memory db) + public TwoStacks(Buffer db) { _memory = db; topOfStackObj = _memory.Length; @@ -102,7 +102,7 @@ public int PopArray() return value; } - internal void Resize(Memory newStackMemory) + internal void Resize(Buffer newStackMemory) { _memory.Slice(0, Math.Max(objectStackCount, arrayStackCount) * 8).Span.CopyTo(newStackMemory.Span); _memory = newStackMemory; @@ -111,10 +111,10 @@ internal void Resize(Memory newStackMemory) internal struct JsonParser { - private Memory _db; + private Buffer _db; private ReadOnlySpan _values; // TODO: this should be ReadOnlyMemory - private Memory _scratchMemory; - private OwnedMemory _scratchManager; + private Buffer _scratchMemory; + private OwnedBuffer _scratchManager; BufferPool _pool; TwoStacks _stack; @@ -144,9 +144,9 @@ private enum JsonTokenType public JsonObject Parse(ReadOnlySpan utf8Json, BufferPool pool = null) { _pool = pool; - if (_pool == null) _pool = ManagedBufferPool.Shared; + if (_pool == null) _pool = BufferPool.Default; _scratchManager = _pool.Rent(utf8Json.Length * 4); - _scratchMemory = _scratchManager.Memory; + _scratchMemory = _scratchManager.Buffer; int dbLength = _scratchMemory.Length / 2; _db = _scratchMemory.Slice(0, dbLength); @@ -219,11 +219,11 @@ private void ResizeDb() var newScratch = _pool.Rent(_scratchMemory.Length * 2); int dbLength = newScratch.Length / 2; - var newDb = newScratch.Memory.Slice(0, dbLength); + var newDb = newScratch.Buffer.Slice(0, dbLength); _db.Slice(0, _valuesIndex).Span.CopyTo(newDb.Span); _db = newDb; - var newStackMemory = newScratch.Memory.Slice(dbLength); + var newStackMemory = newScratch.Buffer.Slice(dbLength); _stack.Resize(newStackMemory); _scratchManager.Dispose(); _scratchManager = newScratch; diff --git a/tests/System.Buffers.Experimental.Tests/BytesReader.cs b/tests/System.Buffers.Experimental.Tests/BytesReader.cs index 8c49cf837f0..c8c1c3c549d 100644 --- a/tests/System.Buffers.Experimental.Tests/BytesReader.cs +++ b/tests/System.Buffers.Experimental.Tests/BytesReader.cs @@ -173,7 +173,7 @@ public static string ToString(this ReadOnlyBytes bytes, TextEncoder encoder) if (encoder.Encoding == TextEncoder.EncodingName.Utf8) { var position = Position.First; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (bytes.TryGet(ref position, out segment)) { sb.Append(new Utf8String(segment.Span)); diff --git a/tests/System.Buffers.Experimental.Tests/MemoryTests.cs b/tests/System.Buffers.Experimental.Tests/MemoryTests.cs index 8d47ff4853b..77dd296baf5 100644 --- a/tests/System.Buffers.Experimental.Tests/MemoryTests.cs +++ b/tests/System.Buffers.Experimental.Tests/MemoryTests.cs @@ -10,12 +10,12 @@ public class MemoryTests [Fact] public void SimpleTestS() { - using(var owned = new OwnedNativeMemory(1024)) { + using(var owned = new OwnedNativeBuffer(1024)) { var span = owned.Span; span[10] = 10; unsafe { Assert.Equal(10, owned.Pointer[10]); } - var memory = owned.Memory; + var memory = owned.Buffer; var array = memory.ToArray(); Assert.Equal(owned.Length, array.Length); Assert.Equal(10, array[10]); @@ -25,14 +25,14 @@ public void SimpleTestS() Assert.Equal(10, copy[0]); } - using (OwnedPinnedArray owned = new byte[1024]) { + using (OwnedPinnedBuffer owned = new byte[1024]) { var span = owned.Span; span[10] = 10; Assert.Equal(10, owned.Array[10]); unsafe { Assert.Equal(10, owned.Pointer[10]); } - var memory = owned.Memory; + var memory = owned.Buffer; var array = memory.ToArray(); Assert.Equal(owned.Length, array.Length); Assert.Equal(10, array[10]); @@ -46,7 +46,7 @@ public void SimpleTestS() [Fact] public void NativeMemoryLifetime() { - var owner = new OwnedNativeMemory(1024); + var owner = new OwnedNativeBuffer(1024); TestLifetime(owner); } @@ -55,17 +55,17 @@ public unsafe void PinnedArrayMemoryLifetime() { var bytes = new byte[1024]; fixed (byte* pBytes = bytes) { - var owner = new OwnedPinnedArray(bytes, pBytes); + var owner = new OwnedPinnedBuffer(bytes, pBytes); TestLifetime(owner); } } - static void TestLifetime(OwnedMemory owned) + static void TestLifetime(OwnedBuffer owned) { - Memory copyStoredForLater; + Buffer copyStoredForLater; try { - Memory memory = owned.Memory; - Memory memorySlice = memory.Slice(10); + Buffer memory = owned.Buffer; + Buffer memorySlice = memory.Slice(10); copyStoredForLater = memorySlice; var r = memorySlice.Reserve(); try { diff --git a/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs b/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs index 74779a60c6c..6c6b1c21102 100644 --- a/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs +++ b/tests/System.Buffers.Experimental.Tests/ReadOnlyBytesTests.cs @@ -13,7 +13,7 @@ public partial class ReadOnlyBytesTests [Fact] public void SingleSegmentBasics() { - ReadOnlyMemory buffer = new byte[] { 1, 2, 3, 4, 5, 6 }; + ReadOnlyBuffer buffer = new byte[] { 1, 2, 3, 4, 5, 6 }; var bytes = new ReadOnlyBytes(buffer); var sliced = bytes.Slice(1, 3); var span = sliced.First.Span; @@ -46,7 +46,7 @@ public void MultiSegmentBasics() public void SingleSegmentSlicing() { var array = new byte[] { 0, 1, 2, 3, 4, 5, 6 }; - ReadOnlyMemory buffer = array; + ReadOnlyBuffer buffer = array; var bytes = new ReadOnlyBytes(buffer); ReadOnlyBytes sliced = bytes; @@ -83,7 +83,7 @@ public void MultiSegmentSlicing() public void SingleSegmentCopyToKnownLength() { var array = new byte[] { 0, 1, 2, 3, 4, 5, 6 }; - ReadOnlyMemory buffer = array; + ReadOnlyBuffer buffer = array; var bytes = new ReadOnlyBytes(buffer, null, array.Length); { // copy to equal @@ -116,7 +116,7 @@ public void SingleSegmentCopyToKnownLength() public void SingleSegmentCopyToUnknownLength() { var array = new byte[] { 0, 1, 2, 3, 4, 5, 6 }; - ReadOnlyMemory buffer = array; + ReadOnlyBuffer buffer = array; var bytes = new ReadOnlyBytes(buffer, null, -1); { // copy to equal @@ -232,7 +232,7 @@ public void ReadOnlyBytesEnumeration() var bytes = new ReadOnlyBytes(buffer); var position = new Position(); int length = 0; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (bytes.TryGet(ref position, out segment)) { length += segment.Length; @@ -258,7 +258,7 @@ public void ReadOnlyTailBytesEnumeration() multibytes = multibytes.Slice(i); var position = new Position(); var length = 0; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (multibytes.TryGet(ref position, out segment)) { length += segment.Length; @@ -276,7 +276,7 @@ public void ReadOnlyFrontBytesEnumeration() multibytes = multibytes.Slice(0, i); var position = new Position(); var length = 0; - ReadOnlyMemory segment; + ReadOnlyBuffer segment; while (multibytes.TryGet(ref position, out segment)) { length += segment.Length; diff --git a/tests/System.Buffers.Primitives.Tests/MemoryTests.cs b/tests/System.Buffers.Primitives.Tests/MemoryTests.cs index f88ac67e8b4..f5a8c3301ee 100644 --- a/tests/System.Buffers.Primitives.Tests/MemoryTests.cs +++ b/tests/System.Buffers.Primitives.Tests/MemoryTests.cs @@ -15,15 +15,16 @@ public class MemoryTests public void SimpleTestS() { { - OwnedArray owned = new byte[1024]; + var array = new byte[1024]; + OwnedBuffer owned = array; var span = owned.Span; span[10] = 10; - Assert.Equal(10, owned.Array[10]); + Assert.Equal(10, array[10]); - var memory = owned.Memory; - var array = memory.ToArray(); + var memory = owned.Buffer; + var toArrayResult = memory.ToArray(); Assert.Equal(owned.Length, array.Length); - Assert.Equal(10, array[10]); + Assert.Equal(10, toArrayResult[10]); Span copy = new byte[20]; memory.Slice(10, 20).CopyTo(copy); @@ -34,17 +35,18 @@ public void SimpleTestS() [Fact] public void ArrayMemoryLifetime() { - var owner = new OwnedArray(1024); - TestLifetime(owner); + var array = new byte[1024]; + OwnedBuffer owned = array; + TestLifetime(owned); } - static void TestLifetime(OwnedMemory owned) + static void TestLifetime(OwnedBuffer owned) { - Memory copyStoredForLater; + Buffer copyStoredForLater; try { - Memory memory = owned.Memory; - Memory memorySlice = memory.Slice(10); + Buffer memory = owned.Buffer; + Buffer memorySlice = memory.Slice(10); copyStoredForLater = memorySlice; var r = memorySlice.Reserve(); try @@ -78,15 +80,16 @@ static void TestLifetime(OwnedMemory owned) public void RacyAccess() { for(int k = 0; k < 1000; k++) { - var owners = new OwnedArray[128]; - var memories = new Memory[owners.Length]; + var owners = new OwnedBuffer[128]; + var memories = new Buffer[owners.Length]; var reserves = new DisposableReservation[owners.Length]; var disposeSuccesses = new bool[owners.Length]; var reserveSuccesses = new bool[owners.Length]; for (int i = 0; i < owners.Length; i++) { - owners[i] = new OwnedArray(1024); - memories[i] = owners[i].Memory; + var array = new byte[1024]; + owners[i] = array; + memories[i] = owners[i].Buffer; } var dispose_task = Task.Run(() => { @@ -173,12 +176,12 @@ public void ReservationPerformance(int number, int size, int threads, ReferenceC ReferenceCountingSettings.OwnedMemory = m; Benchmark.Iterate( () => { - var owners = new OwnedMemory[number]; - var memories = new Memory[owners.Length]; + var owners = new OwnedBuffer[number]; + var memories = new Buffer[owners.Length]; for (int i = 0; i < owners.Length; i++) { owners[i] = new AutoPooledMemory(size); - memories[i] = owners[i].Memory; + memories[i] = owners[i].Buffer; } var tasks = new List(threads); @@ -211,7 +214,7 @@ public void ReservationPerformance(int number, int size, int threads, ReferenceC public unsafe void ReferenceCounting() { var owned = new CustomMemory(); - var memory = owned.Memory; + var memory = owned.Buffer; Assert.Equal(0, owned.OnZeroRefencesCount); Assert.False(owned.HasOutstandingReferences); using (memory.Reserve()) { @@ -225,8 +228,8 @@ public unsafe void ReferenceCounting() [Fact] public void AutoDispose() { - OwnedMemory owned = new AutoPooledMemory(1000); - var memory = owned.Memory; + OwnedBuffer owned = new AutoPooledMemory(1000); + var memory = owned.Buffer; Assert.Equal(false, owned.IsDisposed); var reservation = memory.Reserve(); Assert.Equal(false, owned.IsDisposed); @@ -240,8 +243,8 @@ public void AutoDispose() public void PinAddReferenceReleaseTest() { var array = new byte[1024]; - OwnedArray owned = array; - var memory = owned.Memory; + OwnedBuffer owned = array; + var memory = owned.Buffer; Assert.False(owned.HasOutstandingReferences); var h = memory.Pin(); Assert.True(owned.HasOutstandingReferences); @@ -252,7 +255,7 @@ public void PinAddReferenceReleaseTest() [Fact] public void MemoryHandleFreeUninitialized() { - var h = default(MemoryHandle); + var h = default(BufferHandle); h.Free(); } @@ -260,8 +263,8 @@ public void MemoryHandleFreeUninitialized() public void MemoryHandleDoubleFree() { var array = new byte[1024]; - OwnedArray owned = array; - var memory = owned.Memory; + OwnedBuffer owned = array; + var memory = owned.Buffer; var h = memory.Pin(); Assert.True(owned.HasOutstandingReferences); owned.AddReference(); @@ -280,8 +283,8 @@ WeakReference LeakHandle() // Creates an object that is both Pinned with a MemoryHandle, // and has a weak reference. var array = new byte[1024]; - OwnedArray owned = array; - var memory = owned.Memory; + OwnedBuffer owned = array; + var memory = owned.Buffer; memory.Pin(); return new WeakReference(array); } @@ -303,7 +306,7 @@ void PinGCArrayTest() } } - class CustomMemory : OwnedMemory + class CustomMemory : OwnedBuffer { int _onZeroRefencesCount; @@ -317,7 +320,7 @@ protected override void OnZeroReferences() } } - class AutoDisposeMemory : OwnedMemory + class AutoDisposeMemory : OwnedBuffer { public AutoDisposeMemory(T[] array) : base(array, 0, array.Length) { AddReference(); diff --git a/tests/System.IO.Pipelines.Performance.Tests/Enumerators.cs b/tests/System.IO.Pipelines.Performance.Tests/Enumerators.cs index fe8b1d7ad0e..48ca2740fdd 100644 --- a/tests/System.IO.Pipelines.Performance.Tests/Enumerators.cs +++ b/tests/System.IO.Pipelines.Performance.Tests/Enumerators.cs @@ -22,7 +22,7 @@ public void MemoryEnumerator() { for (int i = 0; i < InnerLoopCount; i++) { - var enumerator = new MemoryEnumerator(_readableBuffer.Start, _readableBuffer.End); + var enumerator = new BufferEnumerator(_readableBuffer.Start, _readableBuffer.End); while (enumerator.MoveNext()) { var memory = enumerator.Current; diff --git a/tests/System.IO.Pipelines.Tests/PipelineReaderWriterFacts.cs b/tests/System.IO.Pipelines.Tests/PipelineReaderWriterFacts.cs index ec541b94f88..e69cb63a2a4 100644 --- a/tests/System.IO.Pipelines.Tests/PipelineReaderWriterFacts.cs +++ b/tests/System.IO.Pipelines.Tests/PipelineReaderWriterFacts.cs @@ -40,7 +40,7 @@ public async Task ReaderShouldNotGetUnflushedBytesWhenOverflowingSegments() // Fill the block with stuff leaving 5 bytes at the end var buffer = _pipe.Writer.Alloc(1); - var len = buffer.Memory.Length; + var len = buffer.Buffer.Length; // Fill the buffer with garbage // block 1 -> block2 // [padding..hello] -> [ world ] @@ -401,7 +401,7 @@ public async Task HelloWorldAcrossTwoBlocks() Assert.False(buffer.IsSingleSpan); var helloBuffer = buffer.Slice(blockSize - 5); Assert.False(helloBuffer.IsSingleSpan); - var memory = new List>(); + var memory = new List>(); foreach (var m in helloBuffer) { memory.Add(m); @@ -579,7 +579,7 @@ private class DisposeTrackingBufferPool : BufferPool { private DisposeTrackingOwnedMemory _memory = new DisposeTrackingOwnedMemory(new byte[1]); - public override OwnedMemory Rent(int size) + public override OwnedBuffer Rent(int size) { return _memory; } @@ -591,7 +591,7 @@ protected override void Dispose(bool disposing) } - private class DisposeTrackingOwnedMemory : OwnedMemory + private class DisposeTrackingOwnedMemory : OwnedBuffer { public DisposeTrackingOwnedMemory(byte[] array) : base(array) { diff --git a/tests/System.IO.Pipelines.Tests/PipelineWriterFacts.cs b/tests/System.IO.Pipelines.Tests/PipelineWriterFacts.cs index 64c5b55987e..2f6d20e7179 100644 --- a/tests/System.IO.Pipelines.Tests/PipelineWriterFacts.cs +++ b/tests/System.IO.Pipelines.Tests/PipelineWriterFacts.cs @@ -9,7 +9,7 @@ using System.IO.Pipelines.Text.Primitives; using Xunit; using System.Text.Formatting; -using System.Buffers.Pools; +using System.Buffers; namespace System.IO.Pipelines.Tests { @@ -112,7 +112,7 @@ public async Task StreamAsPipelineWriterUsesUnderlyingWriter() private class MyCustomStream : Stream, IPipeWriter { - private readonly Pipe _pipe = new Pipe(ManagedBufferPool.Shared); + private readonly Pipe _pipe = new Pipe(BufferPool.Default); public override bool CanRead => true; diff --git a/tests/System.IO.Pipelines.Tests/ReadableBufferFacts.cs b/tests/System.IO.Pipelines.Tests/ReadableBufferFacts.cs index 87873376624..8fbd98c03ac 100644 --- a/tests/System.IO.Pipelines.Tests/ReadableBufferFacts.cs +++ b/tests/System.IO.Pipelines.Tests/ReadableBufferFacts.cs @@ -216,7 +216,7 @@ private unsafe void TestIndexOfWorksForAllLocations(ref ReadableBuffer readBuffe { byte huntValue = (byte)~emptyValue; - var handles = new List(); + var handles = new List(); // we're going to fully index the final locations of the buffer, so that we // can mutate etc in constant time @@ -251,7 +251,7 @@ private unsafe void TestIndexOfWorksForAllLocations(ref ReadableBuffer readBuffe handles.Clear(); } - private static unsafe byte*[] BuildPointerIndex(ref ReadableBuffer readBuffer, List handles) + private static unsafe byte*[] BuildPointerIndex(ref ReadableBuffer readBuffer, List handles) { byte*[] addresses = new byte*[readBuffer.Length]; @@ -514,9 +514,9 @@ public void ReadableBufferSequenceWorks() { // empty buffer - var readable = output.AsReadableBuffer() as ISequence>; + var readable = output.AsReadableBuffer() as ISequence>; var position = Position.First; - ReadOnlyMemory memory; + ReadOnlyBuffer memory; int spanCount = 0; while (readable.TryGet(ref position, out memory)) { @@ -527,9 +527,9 @@ public void ReadableBufferSequenceWorks() } { - var readable = BufferUtilities.CreateBuffer(new byte[] { 1 }, new byte[] { 2, 2 }, new byte[] { 3, 3, 3 }) as ISequence>; + var readable = BufferUtilities.CreateBuffer(new byte[] { 1 }, new byte[] { 2, 2 }, new byte[] { 3, 3, 3 }) as ISequence>; var position = Position.First; - ReadOnlyMemory memory; + ReadOnlyBuffer memory; int spanCount = 0; while (readable.TryGet(ref position, out memory)) { diff --git a/tests/System.IO.Pipelines.Tests/UnownedBufferReaderFacts.cs b/tests/System.IO.Pipelines.Tests/UnownedBufferReaderFacts.cs index b92b0188abf..e7278e282e6 100644 --- a/tests/System.IO.Pipelines.Tests/UnownedBufferReaderFacts.cs +++ b/tests/System.IO.Pipelines.Tests/UnownedBufferReaderFacts.cs @@ -3,8 +3,7 @@ // See the LICENSE file in the project root for more information. using System; -using System.Buffers.Pools; -using System.IO; +using System.Buffers; using System.Text; using System.Threading; using System.Threading.Tasks; @@ -269,7 +268,7 @@ public async Task AccessingUnownedMemoryThrowsIfUsedAfterAdvance() var reader = stream.AsPipelineReader(); - var data = Memory.Empty; + var data = Buffer.Empty; while (true) { @@ -558,7 +557,7 @@ public async Task StreamAsPipelineReaderReadStream() private class StreamAndPipeReader : Stream, IPipeReader { - private readonly Pipe _pipe = new Pipe(ManagedBufferPool.Shared); + private readonly Pipe _pipe = new Pipe(BufferPool.Default); public override bool CanRead => true; diff --git a/tests/System.IO.Pipelines.Tests/WritableBufferFacts.cs b/tests/System.IO.Pipelines.Tests/WritableBufferFacts.cs index f0496840dec..66b62718995 100644 --- a/tests/System.IO.Pipelines.Tests/WritableBufferFacts.cs +++ b/tests/System.IO.Pipelines.Tests/WritableBufferFacts.cs @@ -66,7 +66,7 @@ public async Task WriteLargeDataBinary(int length) var output = pipe.Writer.Alloc(); output.Write(data); - var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks + var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); @@ -101,7 +101,7 @@ public async Task WriteLargeDataTextUtf8(int length) var output = pipe.Writer.Alloc(); output.Append(data, TextEncoder.Utf8); - var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks + var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); @@ -136,7 +136,7 @@ public async Task WriteLargeDataTextAscii(int length) var output = pipe.Writer.Alloc(); output.Append(data, TextEncoder.Utf8); - var foo = output.Memory.IsEmpty; // trying to see if .Memory breaks + var foo = output.Buffer.IsEmpty; // trying to see if .Memory breaks await output.FlushAsync(); pipe.Writer.Complete(); diff --git a/tests/System.Text.Formatting.Tests/SequenceFormatterTests.cs b/tests/System.Text.Formatting.Tests/SequenceFormatterTests.cs index 69057161f6c..09ba1ca9ca6 100644 --- a/tests/System.Text.Formatting.Tests/SequenceFormatterTests.cs +++ b/tests/System.Text.Formatting.Tests/SequenceFormatterTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.Buffers; using System.Collections.Sequences; using Xunit; @@ -11,7 +12,7 @@ public partial class SystemTextFormattingTests [Fact] public void SequenceFormatterBasics() { - var list = new ArrayList>(); + var list = new ArrayList>(); list.Add(new byte[10]); list.Add(new byte[10]); list.Add(new byte[10]); diff --git a/tests/System.Text.Formatting.Tests/SequenceTests.cs b/tests/System.Text.Formatting.Tests/SequenceTests.cs index 61e252c02ad..0b2904215a7 100644 --- a/tests/System.Text.Formatting.Tests/SequenceTests.cs +++ b/tests/System.Text.Formatting.Tests/SequenceTests.cs @@ -1,5 +1,7 @@ // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Buffers; using System.Collections.Sequences; using System.Text.Utf8; using Xunit; @@ -45,9 +47,9 @@ public void ParseUInt64(ulong expectedValue, int expectedConsumed, string[] segm Assert.Equal(expectedConsumed, consumed); } - static ArrayList> ToUtf8Buffers(params string[] segments) + static ArrayList> ToUtf8Buffers(params string[] segments) { - var buffers = new ArrayList>(); + var buffers = new ArrayList>(); foreach (var segment in segments) { buffers.Add(new Utf8String(segment).Bytes.ToArray()); }