Skip to content

Commit

Permalink
[c#] Add OutputBuffer virtual resize buffer method
Browse files Browse the repository at this point in the history
`OutputBuffer` always uses `new byte[]` to create/resize its internal
buffer. Implementing a custom `IOutputBuffer` that uses a different
allocation scheme requires duplicating a lot of code in `OutputBuffer` and
`IntegerHelper`.

Add a virtual method `OutputBuffer.ResizeBuffer` that can be overridden to
use buffer allocators other than `new byte[]` (e.g.
`ArrayPool<byte>.Rent()`)
  • Loading branch information
smichtch authored Oct 12, 2021
1 parent 2fab213 commit 3cf8f71
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ different versioning scheme, following the Haskell community's

* IDL core version: TBD
* C++ version: TBD (minor version bump needed)
* C# NuGet version: TBD
* C# NuGet version: (minor version bump needed)
* `gbc` & compiler library: TBD

### C++ ###
Expand All @@ -34,6 +34,13 @@ different versioning scheme, following the Haskell community's
* Added an ability to apply transform to a schema.
* Added `noexcept` to `bond::blob`'s non-throwing functions.

### C# ###

* Added virtual method `OutputBuffer.ResizeBuffer` that can be overridden to
use buffer allocators other than `new byte[]` (e.g.
`ArrayPool<byte>.Rent()`). ([Pull request
\#1128](https://github.com/microsoft/bond/pull/1128))

## 9.0.5: 2021-04-14 ##

* IDL core version: 3.0
Expand Down
36 changes: 32 additions & 4 deletions cs/src/core/io/safe/OutputBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,22 @@ public virtual long Position
}

public OutputBuffer(int length = 64 * 1024)
: this(new byte[length])
{}
: this()
{
buffer = ResizeBuffer(null, length);
this.length = buffer.Length;
}

public OutputBuffer(byte[] buffer)
: this()
{
Debug.Assert(BitConverter.IsLittleEndian);
this.buffer = buffer;
length = buffer.Length;
}

private OutputBuffer()
{
Debug.Assert(BitConverter.IsLittleEndian);
position = 0;
}

Expand Down Expand Up @@ -233,7 +241,27 @@ internal virtual void Grow(int count)
length = minLength;
}

Array.Resize(ref buffer, length);
buffer = ResizeBuffer(buffer, length);
length = buffer.Length;
}

/// <summary>
/// Resize the internal buffer.
/// </summary>
/// <param name="buffer">Existing buffer.</param>
/// <param name="newSize">New buffer size.</param>
/// <returns>The new buffer.</returns>
/// <remarks>
/// <para>
/// Implementations are responsible for ensuring that the new buffer
/// is at least <paramref name="newSize"/> bytes large and that the
/// bytes in <paramref name="buffer"/> are copied to the new buffer.
/// </para>
/// </remarks>
protected virtual byte[] ResizeBuffer(byte[] buffer, int newSize)
{
Array.Resize(ref buffer, newSize);
return buffer;
}

#region layouts
Expand Down

0 comments on commit 3cf8f71

Please sign in to comment.