Skip to content
This repository has been archived by the owner on Aug 2, 2023. It is now read-only.

Commit

Permalink
renamed buffer related types (#1343)
Browse files Browse the repository at this point in the history
  • Loading branch information
KrzysztofCwalina authored Mar 20, 2017
1 parent 91a112c commit 3bd0ffd
Show file tree
Hide file tree
Showing 62 changed files with 466 additions and 448 deletions.
4 changes: 2 additions & 2 deletions samples/LibuvWithNonAllocatingFormatters/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ static void RunLoop(bool log)
}

var segment = formatter.Formatted;
using (var memory = new OwnedPinnedArray<byte>(segment.Array)) {
connection.TryWrite(memory.Memory.Slice(segment.Offset, segment.Count));
using (var memory = new OwnedPinnedBuffer<byte>(segment.Array)) {
connection.TryWrite(memory.Buffer.Slice(segment.Offset, segment.Count));
connection.Dispose();
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Microsoft.Net.Http
{
class OwnedBuffer : OwnedMemory<byte>, IMemoryList<byte>, IReadOnlyMemoryList<byte>
class OwnedBuffer : OwnedBuffer<byte>, IBufferList<byte>, IReadOnlyBufferList<byte>
{
public const int DefaultBufferSize = 1024;
internal OwnedBuffer _next;
Expand All @@ -25,42 +25,42 @@ static void Free(byte[] array)
{
}

public Memory<byte> First => Memory;
public Buffer<byte> First => Buffer;

public IMemoryList<byte> Rest => _next;
public IBufferList<byte> Rest => _next;

public int WrittenByteCount => _written;

ReadOnlyMemory<byte> IReadOnlyMemoryList<byte>.First => Memory;
ReadOnlyBuffer<byte> IReadOnlyBufferList<byte>.First => Buffer;

IReadOnlyMemoryList<byte> IReadOnlyMemoryList<byte>.Rest => _next;
IReadOnlyBufferList<byte> IReadOnlyBufferList<byte>.Rest => _next;

// TODO: maybe these should be renamed to no conflict with OwnedMemory<T>.Length?
int? ISequence<Memory<byte>>.Length => null;
int? ISequence<ReadOnlyMemory<byte>>.Length => null;
// TODO: maybe these should be renamed to no conflict with OwnedBuffer<T>.Length?
int? ISequence<Buffer<byte>>.Length => null;
int? ISequence<ReadOnlyBuffer<byte>>.Length => null;

public int CopyTo(Span<byte> 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<byte> item, bool advance = true)
public bool TryGet(ref Position position, out Buffer<byte> 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<byte>); return false; }
else if (position.ObjectPosition == null) { item = default(Buffer<byte>); 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;
Expand All @@ -73,17 +73,17 @@ public bool TryGet(ref Position position, out Memory<byte> item, bool advance =
return true;
}

public bool TryGet(ref Position position, out ReadOnlyMemory<byte> item, bool advance = true)
public bool TryGet(ref Position position, out ReadOnlyBuffer<byte> 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<byte>); return false; }
else if (position.ObjectPosition == null) { item = default(ReadOnlyBuffer<byte>); 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,48 @@

namespace System.Buffers
{
public interface IMemoryList<T> : ISequence<Memory<T>>
public interface IBufferList<T> : ISequence<Buffer<T>>
{
int CopyTo(Span<T> buffer);
Memory<T> First { get; }
IMemoryList<T> Rest { get; }
Buffer<T> First { get; }
IBufferList<T> Rest { get; }
}

/// <summary>
/// Multi-segment buffer
/// </summary>
public struct ReadWriteBytes : IMemoryList<byte>
public struct ReadWriteBytes : IBufferList<byte>
{
Memory<byte> _first;
IMemoryList<byte> _rest;
Buffer<byte> _first;
IBufferList<byte> _rest;
int _length;

static readonly ReadWriteBytes s_empty = new ReadWriteBytes(Memory<byte>.Empty);
static readonly ReadWriteBytes s_empty = new ReadWriteBytes(Buffer<byte>.Empty);

public ReadWriteBytes(Memory<byte> first, IMemoryList<byte> rest, int length)
public ReadWriteBytes(Buffer<byte> first, IBufferList<byte> rest, int length)
{
_rest = rest;
_first = first;
_length = length;
}

public ReadWriteBytes(Memory<byte> first, IMemoryList<byte> rest) :
public ReadWriteBytes(Buffer<byte> first, IBufferList<byte> rest) :
this(first, rest, Unspecified)
{ }

public ReadWriteBytes(Memory<byte> memory) :
this(memory, null, memory.Length)
public ReadWriteBytes(Buffer<byte> buffer) :
this(buffer, null, buffer.Length)
{ }

public ReadWriteBytes(IMemoryList<byte> segments, int length) :
public ReadWriteBytes(IBufferList<byte> segments, int length) :
this(segments.First, segments.Rest, length)
{ }

public ReadWriteBytes(IMemoryList<byte> segments) :
public ReadWriteBytes(IBufferList<byte> segments) :
this(segments.First, segments.Rest, Unspecified)
{ }

public bool TryGet(ref Position position, out Memory<byte> value, bool advance = true)
public bool TryGet(ref Position position, out Buffer<byte> value, bool advance = true)
{
if (position == Position.First)
{
Expand All @@ -62,10 +62,10 @@ public bool TryGet(ref Position position, out Memory<byte> value, bool advance =
return true;
}

var rest = position.ObjectPosition as IMemoryList<byte>;
var rest = position.ObjectPosition as IBufferList<byte>;
if (rest == null)
{
value = default(Memory<byte>);
value = default(Buffer<byte>);
return false;
}

Expand All @@ -85,9 +85,9 @@ public bool TryGet(ref Position position, out Memory<byte> value, bool advance =
return true;
}

public Memory<Byte> First => _first;
public Buffer<byte> First => _first;

public IMemoryList<byte> Rest => _rest;
public IBufferList<byte> Rest => _rest;

public int? Length
{
Expand Down Expand Up @@ -147,7 +147,7 @@ ReadWriteBytes SliceRest(int index, int length)
{
if (First.Length == index && length == 0)
{
return new ReadWriteBytes(Memory<byte>.Empty);
return new ReadWriteBytes(Buffer<byte>.Empty);
}
else
{
Expand All @@ -171,7 +171,7 @@ public int ComputeLength()
if (_rest != null)
{
Position position = new Position();
Memory<byte> segment;
Buffer<byte> segment;
while (_rest.TryGet(ref position, out segment))
{
length += segment.Length;
Expand All @@ -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<byte>
class BufferListNode : IBufferList<byte>
{
internal Memory<byte> _first;
internal MemoryListNode _rest;
public Memory<byte> First => _first;
internal Buffer<byte> _first;
internal BufferListNode _rest;
public Buffer<byte> First => _first;

public int? Length
{
Expand All @@ -197,13 +197,13 @@ public int? Length
}
}

public IMemoryList<byte> Rest => _rest;
public IBufferList<byte> Rest => _rest;

public int CopyTo(Span<byte> buffer)
{
int copied = 0;
var position = Position.First;
Memory<byte> segment;
Buffer<byte> segment;
var free = buffer;
while (TryGet(ref position, out segment, true))
{
Expand All @@ -223,17 +223,17 @@ public int CopyTo(Span<byte> buffer)
return copied;
}

public bool TryGet(ref Position position, out Memory<byte> item, bool advance = true)
public bool TryGet(ref Position position, out Buffer<byte> item, bool advance = true)
{
if (position == Position.First)
{
item = _first;
if (advance) { position.IntegerPosition++; position.ObjectPosition = _rest; }
return true;
}
else if (position.ObjectPosition == null) { item = default(Memory<byte>); return false; }
else if (position.ObjectPosition == null) { item = default(Buffer<byte>); return false; }

var sequence = (MemoryListNode)position.ObjectPosition;
var sequence = (BufferListNode)position.ObjectPosition;
item = sequence._first;
if (advance)
{
Expand All @@ -253,18 +253,18 @@ public bool TryGet(ref Position position, out Memory<byte> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public unsafe int Send(ReadOnlySpan<byte> buffer)
}
}

public int Send(ReadOnlyMemory<byte> buffer)
public int Send(ReadOnlyBuffer<byte> buffer)
{
return Send(buffer.Span);
}
Expand Down Expand Up @@ -202,7 +202,7 @@ public unsafe int Receive(Span<byte> buffer)
}
}

public int Receive(Memory<byte> buffer)
public int Receive(Buffer<byte> buffer)
{
return Receive(buffer.Span);
}
Expand Down
20 changes: 10 additions & 10 deletions src/System.Buffers.Experimental/System/Buffers/BufferExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ namespace System.Buffers
{
public static class BufferExtensions
{
public static ReadOnlySpan<byte> ToSpan<T>(this T memorySequence) where T : ISequence<ReadOnlyMemory<byte>>
public static ReadOnlySpan<byte> ToSpan<T>(this T bufferSequence) where T : ISequence<ReadOnlyBuffer<byte>>
{
Position position = Position.First;
ReadOnlyMemory<byte> memory;
ResizableArray<byte> array = new ResizableArray<byte>(memorySequence.Length.GetValueOrDefault(1024));
while (memorySequence.TryGet(ref position, out memory))
ReadOnlyBuffer<byte> buffer;
ResizableArray<byte> array = new ResizableArray<byte>(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<byte> sequence, ReadOnlySpan<byte> value)
public static int IndexOf(this IReadOnlyBufferList<byte> sequence, ReadOnlySpan<byte> value)
{
var first = sequence.First.Span;
var index = first.IndexOf(value);
Expand All @@ -35,7 +35,7 @@ public static int IndexOf(this IReadOnlyMemoryList<byte> sequence, ReadOnlySpan<
return IndexOfStraddling(first, sequence.Rest, value);
}

public static int IndexOf(this IReadOnlyMemoryList<byte> sequence, byte value)
public static int IndexOf(this IReadOnlyBufferList<byte> sequence, byte value)
{
var first = sequence.First.Span;
var index = first.IndexOf(value);
Expand All @@ -53,7 +53,7 @@ public static int IndexOf(this IReadOnlyMemoryList<byte> 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<byte> first, IReadOnlyMemoryList<byte> rest, ReadOnlySpan<byte> value)
internal static int IndexOfStraddling(this ReadOnlySpan<byte> first, IReadOnlyBufferList<byte> rest, ReadOnlySpan<byte> value)
{
Debug.Assert(rest != null);

Expand Down Expand Up @@ -119,9 +119,9 @@ internal static int IndexOfStraddling(this ReadOnlySpan<byte> first, IReadOnlyMe
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int IndexOf(this ReadOnlyMemory<byte> memory, ReadOnlySpan<byte> values)
public static int IndexOf(this ReadOnlyBuffer<byte> buffer, ReadOnlySpan<byte> values)
{
return SpanExtensions.IndexOf(memory.Span, values);
return SpanExtensions.IndexOf(buffer.Span, values);
}
}
}
Loading

0 comments on commit 3bd0ffd

Please sign in to comment.