Commit d8f49e18 authored by Derek Bailey's avatar Derek Bailey Committed by Wouter van Oortmerssen

Mono Fix for Unsafe Mode (#4887)

* Added preprocessor define for C++ if Template Aliases are supported by the compiler

* Revert "Revert "Performance Increase of Vector of Structures using .NET BlockCopy (#4830)""

This reverts commit 1f5eae5d.

* Put<T> method was inside #if UNSAFE_BYTEBUFFER which caused compilation failure when building in unsafe mode

* Revert "Added preprocessor define for C++ if Template Aliases are supported by the compiler"

This reverts commit a75af7352127c261baf0b6cca5cb823e13e78f11.
parent 1f5eae5d
......@@ -30,6 +30,7 @@
//
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
......@@ -91,19 +92,78 @@ namespace FlatBuffers
public byte[] ToArray(int pos, int len)
{
byte[] arr = new byte[len];
Buffer.BlockCopy(_buffer, pos, arr, 0, len);
return ToArray<byte>(pos, len);
}
/// <summary>
/// A lookup of type sizes. Used instead of Marshal.SizeOf() which has additional
/// overhead, but also is compatible with generic functions for simplified code.
/// </summary>
private static Dictionary<Type, int> genericSizes = new Dictionary<Type, int>()
{
{ typeof(bool), sizeof(bool) },
{ typeof(float), sizeof(float) },
{ typeof(double), sizeof(double) },
{ typeof(sbyte), sizeof(sbyte) },
{ typeof(byte), sizeof(byte) },
{ typeof(short), sizeof(short) },
{ typeof(ushort), sizeof(ushort) },
{ typeof(int), sizeof(int) },
{ typeof(uint), sizeof(uint) },
{ typeof(ulong), sizeof(ulong) },
{ typeof(long), sizeof(long) },
};
/// <summary>
/// Get the wire-size (in bytes) of a type supported by flatbuffers.
/// </summary>
/// <param name="t">The type to get the wire size of</param>
/// <returns></returns>
public static int SizeOf<T>()
{
return genericSizes[typeof(T)];
}
/// <summary>
/// Checks if the Type provided is supported as scalar value
/// </summary>
/// <typeparam name="T">The Type to check</typeparam>
/// <returns>True if the type is a scalar type that is supported, falsed otherwise</returns>
public static bool IsSupportedType<T>()
{
return genericSizes.ContainsKey(typeof(T));
}
/// <summary>
/// Get the wire-size (in bytes) of an typed array
/// </summary>
/// <typeparam name="T">The type of the array</typeparam>
/// <param name="x">The array to get the size of</param>
/// <returns>The number of bytes the array takes on wire</returns>
public static int ArraySize<T>(T[] x)
{
return SizeOf<T>() * x.Length;
}
// Get a portion of the buffer casted into an array of type T, given
// the buffer position and length.
public T[] ToArray<T>(int pos, int len)
where T: struct
{
AssertOffsetAndLength(pos, len);
T[] arr = new T[len];
Buffer.BlockCopy(_buffer, pos, arr, 0, ArraySize(arr));
return arr;
}
public byte[] ToSizedArray()
{
return ToArray(Position, Length - Position);
return ToArray<byte>(Position, Length - Position);
}
public byte[] ToFullArray()
{
return ToArray(0, Length);
return ToArray<byte>(0, Length);
}
public ArraySegment<byte> ToArraySegment(int pos, int len)
......@@ -370,6 +430,58 @@ namespace FlatBuffers
#endif // UNSAFE_BYTEBUFFER
/// <summary>
/// Copies an array of type T into this buffer, ending at the given
/// offset into this buffer. The starting offset is calculated based on the length
/// of the array and is the value returned.
/// </summary>
/// <typeparam name="T">The type of the input data (must be a struct)</typeparam>
/// <param name="offset">The offset into this buffer where the copy will end</param>
/// <param name="x">The array to copy data from</param>
/// <returns>The 'start' location of this buffer now, after the copy completed</returns>
public int Put<T>(int offset, T[] x)
where T : struct
{
if(x == null)
{
throw new ArgumentNullException("Cannot put a null array");
}
if(x.Length == 0)
{
throw new ArgumentException("Cannot put an empty array");
}
if(!IsSupportedType<T>())
{
throw new ArgumentException("Cannot put an array of type "
+ typeof(T) + " into this buffer");
}
if (BitConverter.IsLittleEndian)
{
int numBytes = ByteBuffer.ArraySize(x);
offset -= numBytes;
AssertOffsetAndLength(offset, numBytes);
// if we are LE, just do a block copy
Buffer.BlockCopy(x, 0, _buffer, offset, numBytes);
}
else
{
throw new NotImplementedException("Big Endian Support not implemented yet " +
"for putting typed arrays");
// if we are BE, we have to swap each element by itself
//for(int i = x.Length - 1; i >= 0; i--)
//{
// todo: low priority, but need to genericize the Put<T>() functions
//}
}
return offset;
}
public sbyte GetSbyte(int index)
{
AssertOffsetAndLength(index, sizeof(sbyte));
......
......@@ -179,6 +179,18 @@ namespace FlatBuffers
_bb.PutFloat(_space -= sizeof(float), x);
}
/// <summary>
/// Puts an array of type T into this builder at the
/// current offset
/// </summary>
/// <typeparam name="T">The type of the input data </typeparam>
/// <param name="x">The array to copy data from</param>
public void Put<T>(T[] x)
where T : struct
{
_space = _bb.Put(_space, x);
}
public void PutDouble(double x)
{
_bb.PutDouble(_space -= sizeof(double), x);
......@@ -245,6 +257,37 @@ namespace FlatBuffers
/// <param name="x">The `float` to add to the buffer.</param>
public void AddFloat(float x) { Prep(sizeof(float), 0); PutFloat(x); }
/// <summary>
/// Add an array of type T to the buffer (aligns the data and grows if necessary).
/// </summary>
/// <typeparam name="T">The type of the input data</typeparam>
/// <param name="x">The array to copy data from</param>
public void Add<T>(T[] x)
where T : struct
{
if (x == null)
{
throw new ArgumentNullException("Cannot add a null array");
}
if( x.Length == 0)
{
// don't do anything if the array is empty
return;
}
if(!ByteBuffer.IsSupportedType<T>())
{
throw new ArgumentException("Cannot add this Type array to the builder");
}
int size = ByteBuffer.SizeOf<T>();
// Need to prep on size (for data alignment) and then we pass the
// rest of the length (minus 1) as additional bytes
Prep(size, size * (x.Length - 1));
Put(x);
}
/// <summary>
/// Add a `double` to the buffer (aligns the data and grows if necessary).
/// </summary>
......
......@@ -94,6 +94,29 @@ namespace FlatBuffers
return bb.ToArraySegment(pos, len);
}
// Get the data of a vector whoses offset is stored at "offset" in this object as an
// T[]. If the vector is not present in the ByteBuffer, then a null value will be
// returned.
public T[] __vector_as_array<T>(int offset)
where T : struct
{
if(!BitConverter.IsLittleEndian)
{
throw new NotSupportedException("Getting typed arrays on a Big Endian " +
"system is not support");
}
var o = this.__offset(offset);
if (0 == o)
{
return null;
}
var pos = this.__vector(o);
var len = this.__vector_len(o);
return bb.ToArray<T>(pos, len);
}
// Initialize any Table-derived type to point to the union at the given offset.
public T __union<T>(int offset) where T : struct, IFlatbufferObject
{
......
......@@ -1119,6 +1119,18 @@ class GeneralGenerator : public BaseGenerator {
code += lang_.accessor_prefix + "__vector_as_arraysegment(";
code += NumToString(field.value.offset);
code += "); }\n";
// For direct blockcopying the data into a typed array
code += " public ";
code += GenTypeBasic(field.value.type.VectorType());
code += "[] Get";
code += MakeCamel(field.name, lang_.first_camel_upper);
code += "Array() { return ";
code += lang_.accessor_prefix + "__vector_as_array<";
code += GenTypeBasic(field.value.type.VectorType());
code += ">(";
code += NumToString(field.value.offset);
code += "); }\n";
break;
default: break;
}
......@@ -1341,6 +1353,19 @@ class GeneralGenerator : public BaseGenerator {
code += ".Value";
code += "); return ";
code += "builder." + FunctionStart('E') + "ndVector(); }\n";
// For C#, include a block copy method signature.
if (lang_.language == IDLOptions::kCSharp) {
code += " public static " + GenVectorOffsetType() + " ";
code += FunctionStart('C') + "reate";
code += MakeCamel(field.name);
code += "VectorBlock(FlatBufferBuilder builder, ";
code += GenTypeBasic(vector_type) + "[] data) ";
code += "{ builder." + FunctionStart('S') + "tartVector(";
code += NumToString(elem_size);
code += ", data." + FunctionStart('L') + "ength, ";
code += NumToString(alignment);
code += "); builder.Add(data); return builder.EndVector(); }\n";
}
}
// Generate a method to start a vector, data to be added manually
// after.
......
......@@ -100,7 +100,7 @@ namespace FlatBuffers.Test
Assert.AreEqual(0x0A, buffer[3]);
}
#if !BYTEBUFFER_NO_BOUNDS_CHECK
#if !BYTEBUFFER_NO_BOUNDS_CHECK
[FlatBuffersTestMethod]
public void ByteBuffer_PutIntCannotPutAtOffsetPastLength()
{
......@@ -178,7 +178,7 @@ namespace FlatBuffers.Test
public void ByteBuffer_GetByteChecksOffset()
{
var uut = new ByteBuffer(1);
Assert.Throws<ArgumentOutOfRangeException>(()=>uut.Get(1));
Assert.Throws<ArgumentOutOfRangeException>(() => uut.Get(1));
}
#endif
......@@ -344,5 +344,269 @@ namespace FlatBuffers.Test
uut.Position = 1; uut = uut.Duplicate();
Assert.AreEqual(0x0A, uut.Get(3));
}
[FlatBuffersTestMethod]
public void ByteBuffer_To_Array_Float()
{
const int len = 9;
// Construct the data array
var fData = new float[len];
fData[0] = 1.0079F;
fData[1] = 4.0026F;
fData[2] = 6.941F;
fData[3] = 9.0122F;
fData[4] = 10.811F;
fData[5] = 12.0107F;
fData[6] = 14.0067F;
fData[7] = 15.9994F;
fData[8] = 18.9984F;
// Tranfer it to a byte array
var buffer = new byte[sizeof(float) * fData.Length];
Buffer.BlockCopy(fData, 0, buffer, 0, buffer.Length);
// Create the Byte Buffer from byte array
var uut = new ByteBuffer(buffer);
// Get the full array back out and ensure they are equivalent
var bbArray = uut.ToArray<float>(0, len);
Assert.ArrayEqual(fData, bbArray);
// Get a portion of the full array back out and ensure the
// subrange agrees
var bbArray2 = uut.ToArray<float>(4, len - 1);
Assert.AreEqual(bbArray2.Length, len - 1);
for (int i = 1; i < len - 1; i++)
{
Assert.AreEqual(fData[i], bbArray2[i - 1]);
}
// Get a sub portion of the full array back out and ensure the
// subrange agrees
var bbArray3 = uut.ToArray<float>(8, len - 4);
Assert.AreEqual(bbArray3.Length, len - 4);
for (int i = 2; i < len - 4; i++)
{
Assert.AreEqual(fData[i], bbArray3[i - 2]);
}
}
public void ByteBuffer_Put_Array_Helper<T>(T[] data, int typeSize)
where T : struct
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// Put the data into the buffer and make sure the offset is
// calculated correctly
int nOffset = uut.Put(1024, data);
Assert.AreEqual(1024 - typeSize * data.Length, nOffset);
// Get the full array back out and ensure they are equivalent
var bbArray = uut.ToArray<T>(nOffset, data.Length);
Assert.ArrayEqual(data, bbArray);
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Float()
{
const int len = 9;
// Construct the data array
var data = new float[len];
data[0] = 1.0079F;
data[1] = 4.0026F;
data[2] = 6.941F;
data[3] = 9.0122F;
data[4] = 10.811F;
data[5] = 12.0107F;
data[6] = 14.0067F;
data[7] = 15.9994F;
data[8] = 18.9984F;
ByteBuffer_Put_Array_Helper(data, sizeof(float));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Double()
{
const int len = 9;
// Construct the data array
var data = new double[len];
data[0] = 1.0079;
data[1] = 4.0026;
data[2] = 6.941;
data[3] = 9.0122;
data[4] = 10.811;
data[5] = 12.0107;
data[6] = 14.0067;
data[7] = 15.9994;
data[8] = 18.9984;
ByteBuffer_Put_Array_Helper(data, sizeof(double));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Int()
{
const int len = 9;
// Construct the data array
var data = new int[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(int));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_UInt()
{
const int len = 9;
// Construct the data array
var data = new uint[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(uint));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Bool()
{
const int len = 9;
// Construct the data array
var data = new bool[len];
data[0] = true;
data[1] = true;
data[2] = false;
data[3] = true;
data[4] = false;
data[5] = true;
data[6] = true;
data[7] = true;
data[8] = false;
ByteBuffer_Put_Array_Helper(data, sizeof(bool));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Long()
{
const int len = 9;
// Construct the data array
var data = new long[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(long));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Byte()
{
const int len = 9;
// Construct the data array
var data = new byte[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(byte));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_SByte()
{
const int len = 9;
// Construct the data array
var data = new sbyte[len];
data[0] = 1;
data[1] = 4;
data[2] = 6;
data[3] = 9;
data[4] = 10;
data[5] = 12;
data[6] = 14;
data[7] = 15;
data[8] = 18;
ByteBuffer_Put_Array_Helper(data, sizeof(sbyte));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Null_Throws()
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// create a null array and try to put it into the buffer
float[] data = null;
Assert.Throws<ArgumentNullException>(() => uut.Put(1024, data));
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_Empty_Throws()
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// create an array of length == 0, and try to put it into the buffer
float[] data = new float[0];
Assert.Throws<ArgumentException>(() => uut.Put(1024, data));
}
private struct dummyStruct
{
int a;
float b;
}
[FlatBuffersTestMethod]
public void ByteBuffer_Put_Array_IncorrectType_Throws()
{
// Create the Byte Buffer
var uut = new ByteBuffer(1024);
// Create an array of dummy structures that shouldn't be
// able to be put into the buffer
var data = new dummyStruct[10];
Assert.Throws<ArgumentException>(() => uut.Put(1024, data));
}
}
}
......@@ -14,6 +14,8 @@
* limitations under the License.
*/
using System;
namespace FlatBuffers.Test
{
[FlatBuffersTestClass]
......@@ -245,5 +247,108 @@ namespace FlatBuffers.Test
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_Add_Array_Float()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
const int len = 9;
// Construct the data array
var data = new float[len];
data[0] = 1.0079F;
data[1] = 4.0026F;
data[2] = 6.941F;
data[3] = 9.0122F;
data[4] = 10.811F;
data[5] = 12.0107F;
data[6] = 14.0067F;
data[7] = 15.9994F;
data[8] = 18.9984F;
fbb.Add(data);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset + sizeof(float) * data.Length);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_Add_Array_Bool()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
const int len = 9;
// Construct the data array
var data = new bool[len];
data[0] = true;
data[1] = true;
data[2] = false;
data[3] = true;
data[4] = false;
data[5] = true;
data[6] = true;
data[7] = true;
data[8] = false;
fbb.Add(data);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset + sizeof(bool) * data.Length);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_Add_Array_Double()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
const int len = 9;
// Construct the data array
var data = new double[len];
data[0] = 1.0079;
data[1] = 4.0026;
data[2] = 6.941;
data[3] = 9.0122;
data[4] = 10.811;
data[5] = 12.0107;
data[6] = 14.0067;
data[7] = 15.9994;
data[8] = 18.9984;
fbb.Add(data);
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset + sizeof(double) * data.Length);
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_Add_Array_Null_Throws()
{
var fbb = CreateBuffer(false);
// Construct the data array
float[] data = null;
Assert.Throws<ArgumentNullException>(() => fbb.Add(data));
}
[FlatBuffersTestMethod]
public void FlatBufferBuilder_Add_Array_Empty_Noop()
{
var fbb = CreateBuffer(false);
var storedOffset = fbb.Offset;
// Construct an empty data array
float[] data = new float[0];
fbb.Add(data);
// Make sure the offset didn't change since nothing
// was really added
var endOffset = fbb.Offset;
Assert.AreEqual(endOffset, storedOffset);
}
}
}
......@@ -221,6 +221,16 @@ namespace FlatBuffers.Test
}
Assert.AreEqual(10, invsum);
// Get the inventory as an array and subtract the
// sum to get it back to 0
var inventoryArray = monster.GetInventoryArray();
Assert.AreEqual(5, inventoryArray.Length);
foreach(var inv in inventoryArray)
{
invsum -= inv;
}
Assert.AreEqual(0, invsum);
var test0 = monster.Test4(0).Value;
var test1 = monster.Test4(1).Value;
Assert.AreEqual(2, monster.Test4Length);
......
This diff is collapsed.
......@@ -19,6 +19,7 @@ public struct Stat : IFlatbufferObject
public string Id { get { int o = __p.__offset(4); return o != 0 ? __p.__string(o + __p.bb_pos) : null; } }
public ArraySegment<byte>? GetIdBytes() { return __p.__vector_as_arraysegment(4); }
public byte[] GetIdArray() { return __p.__vector_as_array<byte>(4); }
public long Val { get { int o = __p.__offset(6); return o != 0 ? __p.bb.GetLong(o + __p.bb_pos) : (long)0; } }
public bool MutateVal(long val) { int o = __p.__offset(6); if (o != 0) { __p.bb.PutLong(o + __p.bb_pos, val); return true; } else { return false; } }
public ushort Count { get { int o = __p.__offset(8); return o != 0 ? __p.bb.GetUshort(o + __p.bb_pos) : (ushort)0; } }
......
......@@ -40,10 +40,12 @@ public struct TypeAliases : IFlatbufferObject
public sbyte V8(int j) { int o = __p.__offset(24); return o != 0 ? __p.bb.GetSbyte(__p.__vector(o) + j * 1) : (sbyte)0; }
public int V8Length { get { int o = __p.__offset(24); return o != 0 ? __p.__vector_len(o) : 0; } }
public ArraySegment<byte>? GetV8Bytes() { return __p.__vector_as_arraysegment(24); }
public sbyte[] GetV8Array() { return __p.__vector_as_array<sbyte>(24); }
public bool MutateV8(int j, sbyte v8) { int o = __p.__offset(24); if (o != 0) { __p.bb.PutSbyte(__p.__vector(o) + j * 1, v8); return true; } else { return false; } }
public double Vf64(int j) { int o = __p.__offset(26); return o != 0 ? __p.bb.GetDouble(__p.__vector(o) + j * 8) : (double)0; }
public int Vf64Length { get { int o = __p.__offset(26); return o != 0 ? __p.__vector_len(o) : 0; } }
public ArraySegment<byte>? GetVf64Bytes() { return __p.__vector_as_arraysegment(26); }
public double[] GetVf64Array() { return __p.__vector_as_array<double>(26); }
public bool MutateVf64(int j, double vf64) { int o = __p.__offset(26); if (o != 0) { __p.bb.PutDouble(__p.__vector(o) + j * 8, vf64); return true; } else { return false; } }
public static Offset<TypeAliases> CreateTypeAliases(FlatBufferBuilder builder,
......@@ -88,9 +90,11 @@ public struct TypeAliases : IFlatbufferObject
public static void AddF64(FlatBufferBuilder builder, double f64) { builder.AddDouble(9, f64, 0.0); }
public static void AddV8(FlatBufferBuilder builder, VectorOffset v8Offset) { builder.AddOffset(10, v8Offset.Value, 0); }
public static VectorOffset CreateV8Vector(FlatBufferBuilder builder, sbyte[] data) { builder.StartVector(1, data.Length, 1); for (int i = data.Length - 1; i >= 0; i--) builder.AddSbyte(data[i]); return builder.EndVector(); }
public static VectorOffset CreateV8VectorBlock(FlatBufferBuilder builder, sbyte[] data) { builder.StartVector(1, data.Length, 1); builder.Add(data); return builder.EndVector(); }
public static void StartV8Vector(FlatBufferBuilder builder, int numElems) { builder.StartVector(1, numElems, 1); }
public static void AddVf64(FlatBufferBuilder builder, VectorOffset vf64Offset) { builder.AddOffset(11, vf64Offset.Value, 0); }
public static VectorOffset CreateVf64Vector(FlatBufferBuilder builder, double[] data) { builder.StartVector(8, data.Length, 8); for (int i = data.Length - 1; i >= 0; i--) builder.AddDouble(data[i]); return builder.EndVector(); }
public static VectorOffset CreateVf64VectorBlock(FlatBufferBuilder builder, double[] data) { builder.StartVector(8, data.Length, 8); builder.Add(data); return builder.EndVector(); }
public static void StartVf64Vector(FlatBufferBuilder builder, int numElems) { builder.StartVector(8, numElems, 8); }
public static Offset<TypeAliases> EndTypeAliases(FlatBufferBuilder builder) {
int o = builder.EndObject();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment