C# support for directly reading and writting to memory other than byte[]. For example, ByteBuffer can be initialized with a custom allocator which uses shared memory / memory mapped files. (#4886)

Public access to the backing buffer uses Span<T> instead of ArraySegment<T>.

Writing to the buffer now supports Span<T> in addition to T[].

To maintain backwards compatibility ENABLE_SPAN_T must be defined.
This commit is contained in:
Christopher Cifra
2018-08-23 12:05:31 -05:00
committed by Wouter van Oortmerssen
parent e1f48ad35a
commit d0321df8cf
10 changed files with 545 additions and 127 deletions

View File

@@ -110,14 +110,19 @@ namespace FlatBuffers.Test
Monster.FinishMonsterBuffer(fbb, mon);
}
// Dump to output directory so we can inspect later, if needed
#if ENABLE_SPAN_T
var data = fbb.DataBuffer.ToSizedArray();
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
File.WriteAllBytes(filename, data);
#else
using (var ms = fbb.DataBuffer.ToMemoryStream(fbb.DataBuffer.Position, fbb.Offset))
{
var data = ms.ToArray();
string filename = @"Resources/monsterdata_cstest" + (sizePrefix ? "_sp" : "") + ".mon";
File.WriteAllBytes(filename, data);
}
#endif
// Remove the size prefix if necessary for further testing
ByteBuffer dataBuffer = fbb.DataBuffer;
@@ -243,6 +248,19 @@ namespace FlatBuffers.Test
Assert.AreEqual(false, monster.Testbool);
#if ENABLE_SPAN_T
var nameBytes = monster.GetNameBytes();
Assert.AreEqual("MyMonster", Encoding.UTF8.GetString(nameBytes.ToArray(), 0, nameBytes.Length));
if (0 == monster.TestarrayofboolsLength)
{
Assert.IsFalse(monster.GetTestarrayofboolsBytes().Length != 0);
}
else
{
Assert.IsTrue(monster.GetTestarrayofboolsBytes().Length == 0);
}
#else
var nameBytes = monster.GetNameBytes().Value;
Assert.AreEqual("MyMonster", Encoding.UTF8.GetString(nameBytes.Array, nameBytes.Offset, nameBytes.Count));
@@ -254,6 +272,7 @@ namespace FlatBuffers.Test
{
Assert.IsTrue(monster.GetTestarrayofboolsBytes().HasValue);
}
#endif
}
[FlatBuffersTestMethod]