Skip to content

DirectBuffer - Support ArraySegment for underlying array #878

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Nov 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions csharp/sbe-dll/DirectBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,25 @@ public DirectBuffer(byte* pBuffer, int bufferLength, BufferOverflowDelegate buff
Wrap(pBuffer, bufferLength);
}

/// <summary>
/// Attach a view to a buffer owned by external code
/// </summary>
/// <param name="buffer">byte buffer</param>
public DirectBuffer(ArraySegment<byte> buffer) : this(buffer, null)
{
}

/// <summary>
/// Attach a view to a buffer owned by external code
/// </summary>
/// <param name="buffer">byte buffer</param>
/// <param name="bufferOverflow">delegate to allow reallocation of buffer</param>
public DirectBuffer(ArraySegment<byte> buffer, BufferOverflowDelegate bufferOverflow)
{
this.bufferOverflow = bufferOverflow;
Wrap(buffer);
}

/// <summary>
/// Creates a DirectBuffer that can later be wrapped
/// </summary>
Expand Down Expand Up @@ -114,6 +133,24 @@ public void Wrap(byte* pBuffer, int bufferLength)
_needToFreeGCHandle = false;
}

/// <summary>
/// Recycles an existing <see cref="DirectBuffer"/> from a byte buffer owned by external code
/// </summary>
/// <param name="buffer">buffer of bytes</param>
public void Wrap(ArraySegment<byte> buffer)
{
if (buffer == null) throw new ArgumentNullException(nameof(buffer));

FreeGCHandle();

// pin the buffer so it does not get moved around by GC, this is required since we use pointers
_pinnedGCHandle = GCHandle.Alloc(buffer.Array, GCHandleType.Pinned);
_needToFreeGCHandle = true;

_pBuffer = ((byte*)_pinnedGCHandle.AddrOfPinnedObject().ToPointer()) + buffer.Offset;
_capacity = buffer.Count;
}

/// <summary>
/// Capacity of the underlying buffer
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions csharp/sbe-tests/DirectBufferTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,40 @@ public void ConstructFromNativeBuffer()
}
}

[TestMethod]
public void ConstructFromByteArray()
{
var buffer = new byte[16];

const int value = 5;
const int index = 0;

using (var directBuffer = new DirectBuffer(buffer))
{
directBuffer.Int64PutLittleEndian(index, value);
Assert.AreEqual(value, buffer[index]);
}
}

[TestMethod]
public void ConstructFromArraySegment()
{
const int value = 5;
const int index = 0;
const int offset = 512;
const int size = 1024;

var bigBuffer = new byte[size];
var buffer = new ArraySegment<byte>(bigBuffer, offset, size - offset);

using (var directBuffer = new DirectBuffer(buffer))
{
directBuffer.Int64PutLittleEndian(index, value);
Assert.AreEqual(value, bigBuffer[offset + index]);
Assert.AreEqual(value, buffer.AsSpan<byte>()[index]);
}
}

[TestMethod]
public void Recycle()
{
Expand Down