[C#]Change to ENABLE_SPAN_T that doesn't require UNSAFE_BYTEBUFFER. (#6073)

* ENABLE_SPAN_T doesn't require UNSAFE_BYTEBUFFER.

Change to ENABLE_SPAN_T that doesn't require UNSAFE_BYTEBUFFER.

* Selectable framework.

Changed target framework to allow selection of 2.0 or 2.1 (or higher)

* Added target framework version check.

* Add core test project.

* Added run on .Net Core.
This commit is contained in:
春条 2020-09-24 05:16:00 +09:00 committed by GitHub
parent ab139d6bea
commit e0bbaa6f9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 249 additions and 43 deletions

View File

@ -42,19 +42,15 @@ using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
using System.Buffers.Binary;
#endif
#if ENABLE_SPAN_T && !UNSAFE_BYTEBUFFER
#error ENABLE_SPAN_T requires UNSAFE_BYTEBUFFER to also be defined
#endif
namespace FlatBuffers
{
public abstract class ByteBufferAllocator
{
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public abstract Span<byte> Span { get; }
public abstract ReadOnlySpan<byte> ReadOnlySpan { get; }
public abstract Memory<byte> Memory { get; }
@ -102,7 +98,7 @@ namespace FlatBuffers
InitBuffer();
}
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public override Span<byte> Span => _buffer;
public override ReadOnlySpan<byte> ReadOnlySpan => _buffer;
public override Memory<byte> Memory => _buffer;
@ -112,7 +108,7 @@ namespace FlatBuffers
private void InitBuffer()
{
Length = _buffer.Length;
#if !ENABLE_SPAN_T
#if !ENABLE_SPAN_T || !NETSTANDARD2_1
Buffer = _buffer;
#endif
}
@ -224,7 +220,7 @@ namespace FlatBuffers
return SizeOf<T>() * x.Length;
}
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public static int ArraySize<T>(Span<T> x)
{
return SizeOf<T>() * x.Length;
@ -233,7 +229,7 @@ namespace FlatBuffers
// Get a portion of the buffer casted into an array of type T, given
// the buffer position and length.
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public T[] ToArray<T>(int pos, int len)
where T : struct
{
@ -261,7 +257,7 @@ namespace FlatBuffers
return ToArray<byte>(0, Length);
}
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public ReadOnlyMemory<byte> ToReadOnlyMemory(int pos, int len)
{
return _buffer.ReadOnlyMemory.Slice(pos, len);
@ -324,7 +320,7 @@ namespace FlatBuffers
((input & 0xFF00000000000000UL) >> 56));
}
#if !UNSAFE_BYTEBUFFER
#if !UNSAFE_BYTEBUFFER && !ENABLE_SPAN_T
// Helper functions for the safe (but slower) version.
protected void WriteLittleEndian(int offset, int count, ulong data)
{
@ -364,7 +360,46 @@ namespace FlatBuffers
}
return r;
}
#endif // !UNSAFE_BYTEBUFFER
#elif ENABLE_SPAN_T && NETSTANDARD2_1
protected void WriteLittleEndian(int offset, int count, ulong data)
{
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
{
_buffer.Span[offset + i] = (byte)(data >> i * 8);
}
}
else
{
for (int i = 0; i < count; i++)
{
_buffer.Span[offset + count - 1 - i] = (byte)(data >> i * 8);
}
}
}
protected ulong ReadLittleEndian(int offset, int count)
{
AssertOffsetAndLength(offset, count);
ulong r = 0;
if (BitConverter.IsLittleEndian)
{
for (int i = 0; i < count; i++)
{
r |= (ulong)_buffer.Span[offset + i] << i * 8;
}
}
else
{
for (int i = 0; i < count; i++)
{
r |= (ulong)_buffer.Span[offset + count - 1 - i] << i * 8;
}
}
return r;
}
#endif // !UNSAFE_BYTEBUFFER && !ENABLE_SPAN_T
private void AssertOffsetAndLength(int offset, int length)
{
@ -375,7 +410,7 @@ namespace FlatBuffers
#endif
}
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public void PutSbyte(int offset, sbyte value)
{
@ -423,17 +458,12 @@ namespace FlatBuffers
PutByte(offset, value);
}
#if ENABLE_SPAN_T
public unsafe void PutStringUTF8(int offset, string value)
#if ENABLE_SPAN_T && NETSTANDARD2_1
public void PutStringUTF8(int offset, string value)
{
AssertOffsetAndLength(offset, value.Length);
fixed (char* s = value)
{
fixed (byte* buffer = &MemoryMarshal.GetReference(_buffer.Span))
{
Encoding.UTF8.GetBytes(s, value.Length, buffer + offset, Length - offset);
}
}
Encoding.UTF8.GetBytes(value.AsSpan().Slice(0, value.Length),
_buffer.Span.Slice(offset));
}
#else
public void PutStringUTF8(int offset, string value)
@ -454,7 +484,7 @@ namespace FlatBuffers
public unsafe void PutUshort(int offset, ushort value)
{
AssertOffsetAndLength(offset, sizeof(ushort));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
Span<byte> span = _buffer.Span.Slice(offset);
BinaryPrimitives.WriteUInt16LittleEndian(span, value);
#else
@ -475,7 +505,7 @@ namespace FlatBuffers
public unsafe void PutUint(int offset, uint value)
{
AssertOffsetAndLength(offset, sizeof(uint));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
Span<byte> span = _buffer.Span.Slice(offset);
BinaryPrimitives.WriteUInt32LittleEndian(span, value);
#else
@ -496,7 +526,7 @@ namespace FlatBuffers
public unsafe void PutUlong(int offset, ulong value)
{
AssertOffsetAndLength(offset, sizeof(ulong));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
Span<byte> span = _buffer.Span.Slice(offset);
BinaryPrimitives.WriteUInt64LittleEndian(span, value);
#else
@ -512,7 +542,7 @@ namespace FlatBuffers
public unsafe void PutFloat(int offset, float value)
{
AssertOffsetAndLength(offset, sizeof(float));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.Span))
#else
fixed (byte* ptr = _buffer.Buffer)
@ -532,7 +562,7 @@ namespace FlatBuffers
public unsafe void PutDouble(int offset, double value)
{
AssertOffsetAndLength(offset, sizeof(double));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.Span))
#else
fixed (byte* ptr = _buffer.Buffer)
@ -605,7 +635,7 @@ namespace FlatBuffers
#endif // UNSAFE_BYTEBUFFER
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public sbyte GetSbyte(int index)
{
AssertOffsetAndLength(index, sizeof(sbyte));
@ -631,13 +661,10 @@ namespace FlatBuffers
}
#endif
#if ENABLE_SPAN_T
public unsafe string GetStringUTF8(int startPos, int len)
#if ENABLE_SPAN_T && NETSTANDARD2_1
public string GetStringUTF8(int startPos, int len)
{
fixed (byte* buffer = &MemoryMarshal.GetReference(_buffer.ReadOnlySpan.Slice(startPos)))
{
return Encoding.UTF8.GetString(buffer, len);
}
return Encoding.UTF8.GetString(_buffer.Span.Slice(startPos, len));
}
#else
public string GetStringUTF8(int startPos, int len)
@ -656,7 +683,7 @@ namespace FlatBuffers
public unsafe ushort GetUshort(int offset)
{
AssertOffsetAndLength(offset, sizeof(ushort));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt16LittleEndian(span);
#else
@ -677,7 +704,7 @@ namespace FlatBuffers
public unsafe uint GetUint(int offset)
{
AssertOffsetAndLength(offset, sizeof(uint));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt32LittleEndian(span);
#else
@ -698,7 +725,7 @@ namespace FlatBuffers
public unsafe ulong GetUlong(int offset)
{
AssertOffsetAndLength(offset, sizeof(ulong));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
ReadOnlySpan<byte> span = _buffer.ReadOnlySpan.Slice(offset);
return BinaryPrimitives.ReadUInt64LittleEndian(span);
#else
@ -714,7 +741,7 @@ namespace FlatBuffers
public unsafe float GetFloat(int offset)
{
AssertOffsetAndLength(offset, sizeof(float));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.ReadOnlySpan))
#else
fixed (byte* ptr = _buffer.Buffer)
@ -735,7 +762,7 @@ namespace FlatBuffers
public unsafe double GetDouble(int offset)
{
AssertOffsetAndLength(offset, sizeof(double));
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
fixed (byte* ptr = &MemoryMarshal.GetReference(_buffer.ReadOnlySpan))
#else
fixed (byte* ptr = _buffer.Buffer)
@ -834,7 +861,7 @@ namespace FlatBuffers
offset -= numBytes;
AssertOffsetAndLength(offset, numBytes);
// if we are LE, just do a block copy
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
MemoryMarshal.Cast<T, byte>(x).CopyTo(_buffer.Span.Slice(offset, numBytes));
#else
Buffer.BlockCopy(x, 0, _buffer.Buffer, offset, numBytes);
@ -853,7 +880,7 @@ namespace FlatBuffers
return offset;
}
#if ENABLE_SPAN_T
#if ENABLE_SPAN_T && NETSTANDARD2_1
public int Put<T>(int offset, Span<T> x)
where T : struct
{

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
</PropertyGroup>
<ItemGroup>

View File

@ -0,0 +1,156 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Remove="Properties\AssemblyInfo.cs" />
</ItemGroup>
<PropertyGroup Condition="'$(UnsafeByteBuffer)' == 'true'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);UNSAFE_BYTEBUFFER</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition="'$(EnableSpanT)' == 'true'">
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);ENABLE_SPAN_T</DefineConstants>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\..\net\FlatBuffers\ByteBuffer.cs">
<Link>FlatBuffers\ByteBuffer.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\ByteBufferUtil.cs">
<Link>FlatBuffers\ByteBufferUtil.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\IFlatbufferObject.cs">
<Link>FlatBuffers\IFlatbufferObject.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\Offset.cs">
<Link>FlatBuffers\Offset.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\FlatBufferBuilder.cs">
<Link>FlatBuffers\FlatBufferBuilder.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\FlatBufferConstants.cs">
<Link>FlatBuffers\FlatBufferConstants.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\Struct.cs">
<Link>FlatBuffers\Struct.cs</Link>
</Compile>
<Compile Include="..\..\net\FlatBuffers\Table.cs">
<Link>FlatBuffers\Table.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example2\Monster.cs">
<Link>MyGame\Example2\Monster.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Any.cs">
<Link>MyGame\Example\Any.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\AnyAmbiguousAliases.cs">
<Link>MyGame\Example\AnyAmbiguousAliases.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\AnyUniqueAliases.cs">
<Link>MyGame\Example\AnyUniqueAliases.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Color.cs">
<Link>MyGame\Example\Color.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Race.cs">
<Link>MyGame\Example\Race.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Monster.cs">
<Link>MyGame\Example\Monster.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Referrable.cs">
<Link>MyGame\Example\Referrable.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Stat.cs">
<Link>MyGame\Example\Stat.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Test.cs">
<Link>MyGame\Example\Test.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\TestSimpleTableWithEnum.cs">
<Link>MyGame\Example\TestSimpleTableWithEnum.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\TypeAliases.cs">
<Link>MyGame\Example\TypeAliases.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Vec3.cs">
<Link>MyGame\Example\Vec3.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\Ability.cs">
<Link>MyGame\Example\Ability.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\ArrayTable.cs">
<Link>MyGame\Example\ArrayTable.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\ArrayStruct.cs">
<Link>MyGame\Example\ArrayStruct.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\NestedStruct.cs">
<Link>MyGame\Example\NestedStruct.cs</Link>
</Compile>
<Compile Include="..\MyGame\Example\TestEnum.cs">
<Link>MyGame\Example\TestEnum.cs</Link>
</Compile>
<Compile Include="..\MyGame\InParentNamespace.cs">
<Link>MyGame\InParentNamespace.cs</Link>
</Compile>
<Compile Include="..\namespace_test\NamespaceA\NamespaceB\EnumInNestedNS.cs">
<Link>NamespaceA\NamespaceB\EnumInNestedNS.cs</Link>
</Compile>
<Compile Include="..\namespace_test\NamespaceA\NamespaceB\StructInNestedNS.cs">
<Link>NamespaceA\NamespaceB\StructInNestedNS.cs</Link>
</Compile>
<Compile Include="..\namespace_test\NamespaceA\NamespaceB\TableInNestedNS.cs">
<Link>NamespaceA\NamespaceB\TableInNestedNS.cs</Link>
</Compile>
<Compile Include="..\namespace_test\NamespaceA\TableInFirstNS.cs">
<Link>NamespaceA\TableInFirstNS.cs</Link>
</Compile>
<Compile Include="..\union_vector\Attacker.cs">
<Link>union_vector\Attacker.cs</Link>
</Compile>
<Compile Include="..\union_vector\BookReader.cs">
<Link>union_vector\BookReader.cs</Link>
</Compile>
<Compile Include="..\union_vector\Character.cs">
<Link>union_vector\Character.cs</Link>
</Compile>
<Compile Include="..\union_vector\Movie.cs">
<Link>union_vector\Movie.cs</Link>
</Compile>
<Compile Include="..\union_vector\Rapunzel.cs">
<Link>union_vector\Rapunzel.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<None Remove=".gitignore" />
<None Remove="clean.sh" />
<None Remove="NetTest.sh" />
<None Remove="packages.config" />
<None Remove="README.md" />
</ItemGroup>
<ItemGroup>
<Content Include="..\monsterdata_test.mon">
<Link>Resources\monsterdata_test.mon</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="..\monsterdata_test.json">
<Link>Resources\monsterdata_test.json</Link>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

View File

@ -1,6 +1,7 @@
#!/bin/sh
PROJ_FILE=FlatBuffers.Test.csproj
CORE_PROJ_FILE=FlatBuffers.Core.Test.csproj
TEMP_DOTNET_DIR=.dotnet_tmp
TEMP_BIN=.tmp
@ -29,3 +30,25 @@ rm -fr $TEMP_BIN
rm FlatBuffers.Test.sln
rm -rf obj
$DOTNET new sln
$DOTNET sln add $CORE_PROJ_FILE
$DOTNET restore -r linux-x64 $CORE_PROJ_FILE
# Testing C# on Linux using .Net Core.
msbuild -property:Configuration=Release,OutputPath=$TEMP_BIN -verbosity:minimal $CORE_PROJ_FILE
$TEMP_BIN/FlatBuffers.Core.Test.exe
rm -fr $TEMP_BIN
# Repeat with unsafe versions
msbuild -property:Configuration=Release,UnsafeByteBuffer=true,OutputPath=$TEMP_BIN -verbosity:minimal $CORE_PROJ_FILE
$TEMP_BIN/FlatBuffers.Core.Test.exe
rm -fr $TEMP_BIN
# Repeat with SpanT versions
msbuild -property:Configuration=Release,EnableSpanT=true,OutputPath=$TEMP_BIN -verbosity:minimal $CORE_PROJ_FILE
$TEMP_BIN/FlatBuffers.Core.Test.exe
rm -fr $TEMP_BIN
rm FlatBuffers.Core.Test.sln
rm -rf obj