From d612683ee892f9250077691357ca975d5604ec72 Mon Sep 17 00:00:00 2001 From: ahsonkhan Date: Tue, 10 Oct 2017 19:29:33 -0700 Subject: [PATCH] Move Binary APIs from Buffers.dll to Memory.dll. --- src/System.Buffers/System.Buffers.sln | 10 -- src/System.Buffers/ref/System.Buffers.cs | 80 +--------- src/System.Buffers/ref/System.Buffers.csproj | 3 - src/System.Buffers/src/System.Buffers.csproj | 17 +- .../System/Buffers/Binary/BinaryHelpers.cs | 43 ----- .../tests/Binary/TestHelpers.cs | 149 ------------------ .../System.Buffers.Performance.Tests.csproj | 25 --- .../tests/System.Buffers.Tests.csproj | 4 - src/System.Memory/ref/System.Memory.cs | 77 +++++++++ src/System.Memory/src/System.Memory.csproj | 8 +- .../src/System/Buffers/Binary/Reader.cs | 22 ++- .../System/Buffers/Binary/ReaderBigEndian.cs | 0 .../Buffers/Binary/ReaderLittleEndian.cs | 0 .../src/System/Buffers/Binary/Writer.cs | 22 ++- .../System/Buffers/Binary/WriterBigEndian.cs | 2 - .../Buffers/Binary/WriterLittleEndian.cs | 2 - .../tests/Binary/BinaryReaderUnitTests.cs | 1 + .../tests/Binary/BinaryWriterUnitTests.cs | 1 + .../Perf.Span.BinaryReadAndWrite.cs | 1 + .../System.Memory.Performance.Tests.csproj | 2 + .../tests/System.Memory.Tests.csproj | 6 +- src/System.Memory/tests/TestHelpers.cs | 97 ++++++++++++ 22 files changed, 229 insertions(+), 343 deletions(-) delete mode 100644 src/System.Buffers/src/System/Buffers/Binary/BinaryHelpers.cs delete mode 100644 src/System.Buffers/tests/Binary/TestHelpers.cs delete mode 100644 src/System.Buffers/tests/Performance/System.Buffers.Performance.Tests.csproj rename src/{System.Buffers => System.Memory}/src/System/Buffers/Binary/Reader.cs (85%) rename src/{System.Buffers => System.Memory}/src/System/Buffers/Binary/ReaderBigEndian.cs (100%) rename src/{System.Buffers => System.Memory}/src/System/Buffers/Binary/ReaderLittleEndian.cs (100%) rename src/{System.Buffers => System.Memory}/src/System/Buffers/Binary/Writer.cs (66%) rename src/{System.Buffers => System.Memory}/src/System/Buffers/Binary/WriterBigEndian.cs (99%) rename src/{System.Buffers => System.Memory}/src/System/Buffers/Binary/WriterLittleEndian.cs (99%) rename src/{System.Buffers => System.Memory}/tests/Binary/BinaryReaderUnitTests.cs (99%) rename src/{System.Buffers => System.Memory}/tests/Binary/BinaryWriterUnitTests.cs (99%) rename src/{System.Buffers => System.Memory}/tests/Performance/Perf.Span.BinaryReadAndWrite.cs (99%) diff --git a/src/System.Buffers/System.Buffers.sln b/src/System.Buffers/System.Buffers.sln index 3c45da077992..d6e3b1d105ff 100644 --- a/src/System.Buffers/System.Buffers.sln +++ b/src/System.Buffers/System.Buffers.sln @@ -20,8 +20,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{E107E9C1-E89 EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ref", "ref", "{2E666815-2EDB-464B-9DF6-380BF4789AD4}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Buffers.Performance.Tests", "tests\Performance\System.Buffers.Performance.Tests.csproj", "{8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5}" -EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -40,10 +38,6 @@ Global {11AE73F7-3532-47B9-8FF6-B4F22D76456C}.Debug|Any CPU.Build.0 = netstandard-Debug|Any CPU {11AE73F7-3532-47B9-8FF6-B4F22D76456C}.Release|Any CPU.ActiveCfg = netstandard-Release|Any CPU {11AE73F7-3532-47B9-8FF6-B4F22D76456C}.Release|Any CPU.Build.0 = netstandard-Release|Any CPU - {8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -52,9 +46,5 @@ Global {62E2AD5F-C8D0-45FB-B6A5-AED2C77F198C} = {1A2F9F4A-A032-433E-B914-ADD5992BB178} {2ADDB484-6F57-4D71-A3FE-A57EC6329A2B} = {E107E9C1-E893-4E87-987E-04EF0DCEAEFD} {11AE73F7-3532-47B9-8FF6-B4F22D76456C} = {2E666815-2EDB-464B-9DF6-380BF4789AD4} - {8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5} = {1A2F9F4A-A032-433E-B914-ADD5992BB178} - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {B1D0CB97-AED2-48E3-A49C-7C8CD75D3FE6} EndGlobalSection EndGlobal diff --git a/src/System.Buffers/ref/System.Buffers.cs b/src/System.Buffers/ref/System.Buffers.cs index a597f640a5fb..62fd63c1456a 100644 --- a/src/System.Buffers/ref/System.Buffers.cs +++ b/src/System.Buffers/ref/System.Buffers.cs @@ -15,82 +15,4 @@ public abstract class ArrayPool public abstract T[] Rent(int minimumLength); public abstract void Return(T[] array, bool clearArray = false); } -} - -namespace System.Buffers.Binary -{ - [System.CLSCompliantAttribute(false)] - public static class BinaryPrimitives - { - public static sbyte ReverseEndianness(sbyte value) { throw null; } - public static byte ReverseEndianness(byte value) { throw null; } - public static short ReverseEndianness(short value) { throw null; } - public static ushort ReverseEndianness(ushort value) { throw null; } - public static int ReverseEndianness(int value) { throw null; } - public static uint ReverseEndianness(uint value) { throw null; } - public static long ReverseEndianness(long value) { throw null; } - public static ulong ReverseEndianness(ulong value) { throw null; } - - public static T ReadMachineEndian(ReadOnlySpan buffer) where T : struct { throw null; } - public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T value) where T : struct { throw null; } - - public static short ReadInt16LittleEndian(ReadOnlySpan buffer) { throw null; } - public static int ReadInt32LittleEndian(ReadOnlySpan buffer) { throw null; } - public static long ReadInt64LittleEndian(ReadOnlySpan buffer) { throw null; } - public static ushort ReadUInt16LittleEndian(ReadOnlySpan buffer) { throw null; } - public static uint ReadUInt32LittleEndian(ReadOnlySpan buffer) { throw null; } - public static ulong ReadUInt64LittleEndian(ReadOnlySpan buffer) { throw null; } - - public static bool TryReadInt16LittleEndian(ReadOnlySpan buffer, out short value) { throw null; } - public static bool TryReadInt32LittleEndian(ReadOnlySpan buffer, out int value) { throw null; } - public static bool TryReadInt64LittleEndian(ReadOnlySpan buffer, out long value) { throw null; } - public static bool TryReadUInt16LittleEndian(ReadOnlySpan buffer, out ushort value) { throw null; } - public static bool TryReadUInt32LittleEndian(ReadOnlySpan buffer, out uint value) { throw null; } - public static bool TryReadUInt64LittleEndian(ReadOnlySpan buffer, out ulong value) { throw null; } - - public static short ReadInt16BigEndian(ReadOnlySpan buffer) { throw null; } - public static int ReadInt32BigEndian(ReadOnlySpan buffer) { throw null; } - public static long ReadInt64BigEndian(ReadOnlySpan buffer) { throw null; } - public static ushort ReadUInt16BigEndian(ReadOnlySpan buffer) { throw null; } - public static uint ReadUInt32BigEndian(ReadOnlySpan buffer) { throw null; } - public static ulong ReadUInt64BigEndian(ReadOnlySpan buffer) { throw null; } - - public static bool TryReadInt16BigEndian(ReadOnlySpan buffer, out short value) { throw null; } - public static bool TryReadInt32BigEndian(ReadOnlySpan buffer, out int value) { throw null; } - public static bool TryReadInt64BigEndian(ReadOnlySpan buffer, out long value) { throw null; } - public static bool TryReadUInt16BigEndian(ReadOnlySpan buffer, out ushort value) { throw null; } - public static bool TryReadUInt32BigEndian(ReadOnlySpan buffer, out uint value) { throw null; } - public static bool TryReadUInt64BigEndian(ReadOnlySpan buffer, out ulong value) { throw null; } - - public static void WriteMachineEndian(ref Span buffer, T value) where T : struct { throw null; } - public static bool TryWriteMachineEndian(ref Span buffer, T value) where T : struct { throw null; } - - public static void WriteInt16LittleEndian(Span buffer, short value) { throw null; } - public static void WriteInt32LittleEndian(Span buffer, int value) { throw null; } - public static void WriteInt64LittleEndian(Span buffer, long value) { throw null; } - public static void WriteUInt16LittleEndian(Span buffer, ushort value) { throw null; } - public static void WriteUInt32LittleEndian(Span buffer, uint value) { throw null; } - public static void WriteUInt64LittleEndian(Span buffer, ulong value) { throw null; } - - public static bool TryWriteInt16LittleEndian(Span buffer, short value) { throw null; } - public static bool TryWriteInt32LittleEndian(Span buffer, int value) { throw null; } - public static bool TryWriteInt64LittleEndian(Span buffer, long value) { throw null; } - public static bool TryWriteUInt16LittleEndian(Span buffer, ushort value) { throw null; } - public static bool TryWriteUInt32LittleEndian(Span buffer, uint value) { throw null; } - public static bool TryWriteUInt64LittleEndian(Span buffer, ulong value) { throw null; } - - public static void WriteInt16BigEndian(Span buffer, short value) { throw null; } - public static void WriteInt32BigEndian(Span buffer, int value) { throw null; } - public static void WriteInt64BigEndian(Span buffer, long value) { throw null; } - public static void WriteUInt16BigEndian(Span buffer, ushort value) { throw null; } - public static void WriteUInt32BigEndian(Span buffer, uint value) { throw null; } - public static void WriteUInt64BigEndian(Span buffer, ulong value) { throw null; } - - public static bool TryWriteInt16BigEndian(Span buffer, short value) { throw null; } - public static bool TryWriteInt32BigEndian(Span buffer, int value) { throw null; } - public static bool TryWriteInt64BigEndian(Span buffer, long value) { throw null; } - public static bool TryWriteUInt16BigEndian(Span buffer, ushort value) { throw null; } - public static bool TryWriteUInt32BigEndian(Span buffer, uint value) { throw null; } - public static bool TryWriteUInt64BigEndian(Span buffer, ulong value) { throw null; } - } -} +} \ No newline at end of file diff --git a/src/System.Buffers/ref/System.Buffers.csproj b/src/System.Buffers/ref/System.Buffers.csproj index 74d8e9c004db..4add92447c29 100644 --- a/src/System.Buffers/ref/System.Buffers.csproj +++ b/src/System.Buffers/ref/System.Buffers.csproj @@ -11,9 +11,6 @@ - - - diff --git a/src/System.Buffers/src/System.Buffers.csproj b/src/System.Buffers/src/System.Buffers.csproj index d7d70bf84d1b..0167dfdfbb94 100644 --- a/src/System.Buffers/src/System.Buffers.csproj +++ b/src/System.Buffers/src/System.Buffers.csproj @@ -4,9 +4,8 @@ {2ADDB484-6F57-4D71-A3FE-A57EC6329A2B} true - false $(OutputPath)$(MSBuildProjectName).xml - true + true true @@ -22,15 +21,6 @@ - - - - - - - - - @@ -40,9 +30,6 @@ - - - @@ -50,8 +37,6 @@ - - diff --git a/src/System.Buffers/src/System/Buffers/Binary/BinaryHelpers.cs b/src/System.Buffers/src/System/Buffers/Binary/BinaryHelpers.cs deleted file mode 100644 index b5a7e05ec18d..000000000000 --- a/src/System.Buffers/src/System/Buffers/Binary/BinaryHelpers.cs +++ /dev/null @@ -1,43 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Reflection; - -namespace System.Buffers.Binary -{ - internal static class BinaryHelpers - { - /// - /// Determine if a type is eligible for storage in unmanaged memory. - /// Portable equivalent of RuntimeHelpers.IsReferenceOrContainsReferences<T>() - /// - public static bool IsReferenceOrContainsReferences() => IsReferenceOrContainsReferencesCore(typeof(T)); - - private static bool IsReferenceOrContainsReferencesCore(Type type) - { - if (type.GetTypeInfo().IsPrimitive) // This is hopefully the common case. All types that return true for this are value types w/out embedded references. - return false; - - if (!type.GetTypeInfo().IsValueType) - return true; - - // If type is a Nullable<> of something, unwrap it first. - Type underlyingNullable = Nullable.GetUnderlyingType(type); - if (underlyingNullable != null) - type = underlyingNullable; - - if (type.GetTypeInfo().IsEnum) - return false; - - foreach (FieldInfo field in type.GetTypeInfo().DeclaredFields) - { - if (field.IsStatic) - continue; - if (IsReferenceOrContainsReferencesCore(field.FieldType)) - return true; - } - return false; - } - } -} diff --git a/src/System.Buffers/tests/Binary/TestHelpers.cs b/src/System.Buffers/tests/Binary/TestHelpers.cs deleted file mode 100644 index 8150c733962b..000000000000 --- a/src/System.Buffers/tests/Binary/TestHelpers.cs +++ /dev/null @@ -1,149 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. -// See the LICENSE file in the project root for more information. - -using System.Runtime.CompilerServices; -using System.Runtime.InteropServices; -using Xunit; - -using static System.Buffers.Binary.BinaryPrimitives; - -namespace System.Buffers.Binary.Tests -{ - public static class TestHelpers - { - - public static void Validate(Span span, T value) where T : struct - { - T read = ReadMachineEndian(span); - Assert.Equal(value, read); - span.Clear(); - } - - public delegate void AssertThrowsAction(Span span); - - // Cannot use standard Assert.Throws() when testing Span - Span and closures don't get along. - public static void AssertThrows(Span span, AssertThrowsAction action) where E:Exception - { - try - { - action(span); - Assert.False(true, "Expected exception: " + typeof(E).GetType()); - } - catch (E) - { - } - catch (Exception wrongException) - { - Assert.False(true, "Wrong exception thrown: Expected " + typeof(E).GetType() + ": Actual: " + wrongException.GetType()); - } - } - - public delegate void AssertThrowsActionReadOnly(ReadOnlySpan span); - - // Cannot use standard Assert.Throws() when testing Span - Span and closures don't get along. - public static void AssertThrows(ReadOnlySpan span, AssertThrowsActionReadOnly action) where E:Exception - { - try - { - action(span); - Assert.False(true, "Expected exception: " + typeof(E).GetType()); - } - catch (E) - { - } - catch (Exception wrongException) - { - Assert.False(true, "Wrong exception thrown: Expected " + typeof(E).GetType() + ": Actual: " + wrongException.GetType()); - } - } - - public static TestStructExplicit testExplicitStruct = new TestStructExplicit - { - S0 = short.MaxValue, - I0 = int.MaxValue, - L0 = long.MaxValue, - US0 = ushort.MaxValue, - UI0 = uint.MaxValue, - UL0 = ulong.MaxValue, - S1 = short.MinValue, - I1 = int.MinValue, - L1 = long.MinValue, - US1 = ushort.MinValue, - UI1 = uint.MinValue, - UL1 = ulong.MinValue - }; - - public static Span GetSpanBE() - { - Span spanBE = new byte[Unsafe.SizeOf()]; - - WriteInt16BigEndian(spanBE, testExplicitStruct.S0); - WriteInt32BigEndian(spanBE.Slice(2), testExplicitStruct.I0); - WriteInt64BigEndian(spanBE.Slice(6), testExplicitStruct.L0); - WriteUInt16BigEndian(spanBE.Slice(14), testExplicitStruct.US0); - WriteUInt32BigEndian(spanBE.Slice(16), testExplicitStruct.UI0); - WriteUInt64BigEndian(spanBE.Slice(20), testExplicitStruct.UL0); - WriteInt16BigEndian(spanBE.Slice(28), testExplicitStruct.S1); - WriteInt32BigEndian(spanBE.Slice(30), testExplicitStruct.I1); - WriteInt64BigEndian(spanBE.Slice(34), testExplicitStruct.L1); - WriteUInt16BigEndian(spanBE.Slice(42), testExplicitStruct.US1); - WriteUInt32BigEndian(spanBE.Slice(44), testExplicitStruct.UI1); - WriteUInt64BigEndian(spanBE.Slice(48), testExplicitStruct.UL1); - - Assert.Equal(56, spanBE.Length); - return spanBE; - } - - public static Span GetSpanLE() - { - Span spanLE = new byte[Unsafe.SizeOf()]; - - WriteInt16LittleEndian(spanLE, testExplicitStruct.S0); - WriteInt32LittleEndian(spanLE.Slice(2), testExplicitStruct.I0); - WriteInt64LittleEndian( spanLE.Slice(6), testExplicitStruct.L0); - WriteUInt16LittleEndian(spanLE.Slice(14), testExplicitStruct.US0); - WriteUInt32LittleEndian(spanLE.Slice(16), testExplicitStruct.UI0); - WriteUInt64LittleEndian(spanLE.Slice(20), testExplicitStruct.UL0); - WriteInt16LittleEndian(spanLE.Slice(28), testExplicitStruct.S1); - WriteInt32LittleEndian(spanLE.Slice(30), testExplicitStruct.I1); - WriteInt64LittleEndian(spanLE.Slice(34), testExplicitStruct.L1); - WriteUInt16LittleEndian(spanLE.Slice(42), testExplicitStruct.US1); - WriteUInt32LittleEndian(spanLE.Slice(44), testExplicitStruct.UI1); - WriteUInt64LittleEndian(spanLE.Slice(48), testExplicitStruct.UL1); - - Assert.Equal(56, spanLE.Length); - return spanLE; - } - - [StructLayout(LayoutKind.Explicit)] - public struct TestStructExplicit - { - [FieldOffset(0)] - public short S0; - [FieldOffset(2)] - public int I0; - [FieldOffset(6)] - public long L0; - [FieldOffset(14)] - public ushort US0; - [FieldOffset(16)] - public uint UI0; - [FieldOffset(20)] - public ulong UL0; - [FieldOffset(28)] - public short S1; - [FieldOffset(30)] - public int I1; - [FieldOffset(34)] - public long L1; - [FieldOffset(42)] - public ushort US1; - [FieldOffset(44)] - public uint UI1; - [FieldOffset(48)] - public ulong UL1; - } - } -} - diff --git a/src/System.Buffers/tests/Performance/System.Buffers.Performance.Tests.csproj b/src/System.Buffers/tests/Performance/System.Buffers.Performance.Tests.csproj deleted file mode 100644 index 756f8caf7ec7..000000000000 --- a/src/System.Buffers/tests/Performance/System.Buffers.Performance.Tests.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - - true - {8C1FA65F-4AE7-4627-8C2B-DE5EDF779DB5} - - - - - - - - - Common\System\PerfUtils.cs - - - - - {69e46a6f-9966-45a5-8945-2559fe337827} - PerfRunner - - - - \ No newline at end of file diff --git a/src/System.Buffers/tests/System.Buffers.Tests.csproj b/src/System.Buffers/tests/System.Buffers.Tests.csproj index ec40f66b796c..71a002e77aff 100644 --- a/src/System.Buffers/tests/System.Buffers.Tests.csproj +++ b/src/System.Buffers/tests/System.Buffers.Tests.csproj @@ -3,15 +3,11 @@ {62E2AD5F-C8D0-45FB-B6A5-AED2C77F198C} - true - - - diff --git a/src/System.Memory/ref/System.Memory.cs b/src/System.Memory/ref/System.Memory.cs index b38abb407a35..3d0c982f90b7 100644 --- a/src/System.Memory/ref/System.Memory.cs +++ b/src/System.Memory/ref/System.Memory.cs @@ -202,4 +202,81 @@ public abstract class OwnedMemory : IDisposable, IRetainable public abstract void Retain(); protected internal abstract bool TryGetArray(out ArraySegment arraySegment); } +} + +namespace System.Buffers.Binary +{ + public static class BinaryPrimitives + { + public static sbyte ReverseEndianness(sbyte value) { throw null; } + public static byte ReverseEndianness(byte value) { throw null; } + public static short ReverseEndianness(short value) { throw null; } + public static ushort ReverseEndianness(ushort value) { throw null; } + public static int ReverseEndianness(int value) { throw null; } + public static uint ReverseEndianness(uint value) { throw null; } + public static long ReverseEndianness(long value) { throw null; } + public static ulong ReverseEndianness(ulong value) { throw null; } + + public static T ReadMachineEndian(ReadOnlySpan buffer) where T : struct { throw null; } + public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T value) where T : struct { throw null; } + + public static short ReadInt16LittleEndian(ReadOnlySpan buffer) { throw null; } + public static int ReadInt32LittleEndian(ReadOnlySpan buffer) { throw null; } + public static long ReadInt64LittleEndian(ReadOnlySpan buffer) { throw null; } + public static ushort ReadUInt16LittleEndian(ReadOnlySpan buffer) { throw null; } + public static uint ReadUInt32LittleEndian(ReadOnlySpan buffer) { throw null; } + public static ulong ReadUInt64LittleEndian(ReadOnlySpan buffer) { throw null; } + + public static bool TryReadInt16LittleEndian(ReadOnlySpan buffer, out short value) { throw null; } + public static bool TryReadInt32LittleEndian(ReadOnlySpan buffer, out int value) { throw null; } + public static bool TryReadInt64LittleEndian(ReadOnlySpan buffer, out long value) { throw null; } + public static bool TryReadUInt16LittleEndian(ReadOnlySpan buffer, out ushort value) { throw null; } + public static bool TryReadUInt32LittleEndian(ReadOnlySpan buffer, out uint value) { throw null; } + public static bool TryReadUInt64LittleEndian(ReadOnlySpan buffer, out ulong value) { throw null; } + + public static short ReadInt16BigEndian(ReadOnlySpan buffer) { throw null; } + public static int ReadInt32BigEndian(ReadOnlySpan buffer) { throw null; } + public static long ReadInt64BigEndian(ReadOnlySpan buffer) { throw null; } + public static ushort ReadUInt16BigEndian(ReadOnlySpan buffer) { throw null; } + public static uint ReadUInt32BigEndian(ReadOnlySpan buffer) { throw null; } + public static ulong ReadUInt64BigEndian(ReadOnlySpan buffer) { throw null; } + + public static bool TryReadInt16BigEndian(ReadOnlySpan buffer, out short value) { throw null; } + public static bool TryReadInt32BigEndian(ReadOnlySpan buffer, out int value) { throw null; } + public static bool TryReadInt64BigEndian(ReadOnlySpan buffer, out long value) { throw null; } + public static bool TryReadUInt16BigEndian(ReadOnlySpan buffer, out ushort value) { throw null; } + public static bool TryReadUInt32BigEndian(ReadOnlySpan buffer, out uint value) { throw null; } + public static bool TryReadUInt64BigEndian(ReadOnlySpan buffer, out ulong value) { throw null; } + + public static void WriteMachineEndian(ref Span buffer, T value) where T : struct { throw null; } + public static bool TryWriteMachineEndian(ref Span buffer, T value) where T : struct { throw null; } + + public static void WriteInt16LittleEndian(Span buffer, short value) { throw null; } + public static void WriteInt32LittleEndian(Span buffer, int value) { throw null; } + public static void WriteInt64LittleEndian(Span buffer, long value) { throw null; } + public static void WriteUInt16LittleEndian(Span buffer, ushort value) { throw null; } + public static void WriteUInt32LittleEndian(Span buffer, uint value) { throw null; } + public static void WriteUInt64LittleEndian(Span buffer, ulong value) { throw null; } + + public static bool TryWriteInt16LittleEndian(Span buffer, short value) { throw null; } + public static bool TryWriteInt32LittleEndian(Span buffer, int value) { throw null; } + public static bool TryWriteInt64LittleEndian(Span buffer, long value) { throw null; } + public static bool TryWriteUInt16LittleEndian(Span buffer, ushort value) { throw null; } + public static bool TryWriteUInt32LittleEndian(Span buffer, uint value) { throw null; } + public static bool TryWriteUInt64LittleEndian(Span buffer, ulong value) { throw null; } + + public static void WriteInt16BigEndian(Span buffer, short value) { throw null; } + public static void WriteInt32BigEndian(Span buffer, int value) { throw null; } + public static void WriteInt64BigEndian(Span buffer, long value) { throw null; } + public static void WriteUInt16BigEndian(Span buffer, ushort value) { throw null; } + public static void WriteUInt32BigEndian(Span buffer, uint value) { throw null; } + public static void WriteUInt64BigEndian(Span buffer, ulong value) { throw null; } + + public static bool TryWriteInt16BigEndian(Span buffer, short value) { throw null; } + public static bool TryWriteInt32BigEndian(Span buffer, int value) { throw null; } + public static bool TryWriteInt64BigEndian(Span buffer, long value) { throw null; } + public static bool TryWriteUInt16BigEndian(Span buffer, ushort value) { throw null; } + public static bool TryWriteUInt32BigEndian(Span buffer, uint value) { throw null; } + public static bool TryWriteUInt64BigEndian(Span buffer, ulong value) { throw null; } + } } \ No newline at end of file diff --git a/src/System.Memory/src/System.Memory.csproj b/src/System.Memory/src/System.Memory.csproj index 6caeba7849f4..82be13e8c44b 100644 --- a/src/System.Memory/src/System.Memory.csproj +++ b/src/System.Memory/src/System.Memory.csproj @@ -24,6 +24,12 @@ + + + + + + @@ -49,7 +55,7 @@ - + diff --git a/src/System.Buffers/src/System/Buffers/Binary/Reader.cs b/src/System.Memory/src/System/Buffers/Binary/Reader.cs similarity index 85% rename from src/System.Buffers/src/System/Buffers/Binary/Reader.cs rename to src/System.Memory/src/System/Buffers/Binary/Reader.cs index 8f4c49d90084..da7518c4fbf0 100644 --- a/src/System.Buffers/src/System/Buffers/Binary/Reader.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Reader.cs @@ -98,10 +98,17 @@ public static ulong ReverseEndianness(ulong value) public static T ReadMachineEndian(ReadOnlySpan buffer) where T : struct { - if (BinaryHelpers.IsReferenceOrContainsReferences()) +#if netcoreapp + if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - Environment.FailFast($"Cannot read a span into the non-blittable type <{typeof(T).Name}>."); + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif if (Unsafe.SizeOf() > buffer.Length) { throw new ArgumentOutOfRangeException(); @@ -117,10 +124,17 @@ public static T ReadMachineEndian(ReadOnlySpan buffer) public static bool TryReadMachineEndian(ReadOnlySpan buffer, out T value) where T : struct { - if (BinaryHelpers.IsReferenceOrContainsReferences()) +#if netcoreapp + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) { - Environment.FailFast($"Cannot read a span into the non-blittable type <{typeof(T).Name}>."); + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } +#endif if (Unsafe.SizeOf() > (uint)buffer.Length) { value = default; diff --git a/src/System.Buffers/src/System/Buffers/Binary/ReaderBigEndian.cs b/src/System.Memory/src/System/Buffers/Binary/ReaderBigEndian.cs similarity index 100% rename from src/System.Buffers/src/System/Buffers/Binary/ReaderBigEndian.cs rename to src/System.Memory/src/System/Buffers/Binary/ReaderBigEndian.cs diff --git a/src/System.Buffers/src/System/Buffers/Binary/ReaderLittleEndian.cs b/src/System.Memory/src/System/Buffers/Binary/ReaderLittleEndian.cs similarity index 100% rename from src/System.Buffers/src/System/Buffers/Binary/ReaderLittleEndian.cs rename to src/System.Memory/src/System/Buffers/Binary/ReaderLittleEndian.cs diff --git a/src/System.Buffers/src/System/Buffers/Binary/Writer.cs b/src/System.Memory/src/System/Buffers/Binary/Writer.cs similarity index 66% rename from src/System.Buffers/src/System/Buffers/Binary/Writer.cs rename to src/System.Memory/src/System/Buffers/Binary/Writer.cs index 2c823bad7b09..fae724dedf8f 100644 --- a/src/System.Buffers/src/System/Buffers/Binary/Writer.cs +++ b/src/System.Memory/src/System/Buffers/Binary/Writer.cs @@ -16,10 +16,17 @@ public static partial class BinaryPrimitives public static void WriteMachineEndian(ref Span buffer, T value) where T : struct { - if (BinaryHelpers.IsReferenceOrContainsReferences()) +#if netcoreapp + if (RuntimeHelpers.IsReferenceOrContainsReferences()) { - Environment.FailFast($"Cannot write the non-blittable type <{typeof(T).Name}> into the span."); + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) + { + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); + } +#endif if ((uint)Unsafe.SizeOf() > (uint)buffer.Length) { throw new ArgumentOutOfRangeException(); @@ -35,10 +42,17 @@ public static void WriteMachineEndian(ref Span buffer, T value) public static bool TryWriteMachineEndian(ref Span buffer, T value) where T : struct { - if (BinaryHelpers.IsReferenceOrContainsReferences()) +#if netcoreapp + if (RuntimeHelpers.IsReferenceOrContainsReferences()) + { + throw new ArgumentException(SR.Format(SR.Argument_InvalidTypeWithPointersNotSupported, typeof(T))); + } +#else + if (SpanHelpers.IsReferenceOrContainsReferences()) { - Environment.FailFast($"Cannot write the non-blittable type <{typeof(T).Name}> into the span."); + ThrowHelper.ThrowArgumentException_InvalidTypeWithPointersNotSupported(typeof(T)); } +#endif if (Unsafe.SizeOf() > (uint)buffer.Length) { return false; diff --git a/src/System.Buffers/src/System/Buffers/Binary/WriterBigEndian.cs b/src/System.Memory/src/System/Buffers/Binary/WriterBigEndian.cs similarity index 99% rename from src/System.Buffers/src/System/Buffers/Binary/WriterBigEndian.cs rename to src/System.Memory/src/System/Buffers/Binary/WriterBigEndian.cs index 83199d42ee44..f25b771343b8 100644 --- a/src/System.Buffers/src/System/Buffers/Binary/WriterBigEndian.cs +++ b/src/System.Memory/src/System/Buffers/Binary/WriterBigEndian.cs @@ -4,8 +4,6 @@ using System.Runtime.CompilerServices; -using static System.Buffers.Binary.BinaryPrimitives; - namespace System.Buffers.Binary { public static partial class BinaryPrimitives diff --git a/src/System.Buffers/src/System/Buffers/Binary/WriterLittleEndian.cs b/src/System.Memory/src/System/Buffers/Binary/WriterLittleEndian.cs similarity index 99% rename from src/System.Buffers/src/System/Buffers/Binary/WriterLittleEndian.cs rename to src/System.Memory/src/System/Buffers/Binary/WriterLittleEndian.cs index f9594f81d387..0efb5025068c 100644 --- a/src/System.Buffers/src/System/Buffers/Binary/WriterLittleEndian.cs +++ b/src/System.Memory/src/System/Buffers/Binary/WriterLittleEndian.cs @@ -4,8 +4,6 @@ using System.Runtime.CompilerServices; -using static System.Buffers.Binary.BinaryPrimitives; - namespace System.Buffers.Binary { public static partial class BinaryPrimitives diff --git a/src/System.Buffers/tests/Binary/BinaryReaderUnitTests.cs b/src/System.Memory/tests/Binary/BinaryReaderUnitTests.cs similarity index 99% rename from src/System.Buffers/tests/Binary/BinaryReaderUnitTests.cs rename to src/System.Memory/tests/Binary/BinaryReaderUnitTests.cs index a19951e545b7..e68dd39703cd 100644 --- a/src/System.Buffers/tests/Binary/BinaryReaderUnitTests.cs +++ b/src/System.Memory/tests/Binary/BinaryReaderUnitTests.cs @@ -7,6 +7,7 @@ using Xunit; using static System.Buffers.Binary.BinaryPrimitives; +using static System.TestHelpers; namespace System.Buffers.Binary.Tests { diff --git a/src/System.Buffers/tests/Binary/BinaryWriterUnitTests.cs b/src/System.Memory/tests/Binary/BinaryWriterUnitTests.cs similarity index 99% rename from src/System.Buffers/tests/Binary/BinaryWriterUnitTests.cs rename to src/System.Memory/tests/Binary/BinaryWriterUnitTests.cs index 8b4728b9768c..4753166a4a39 100644 --- a/src/System.Buffers/tests/Binary/BinaryWriterUnitTests.cs +++ b/src/System.Memory/tests/Binary/BinaryWriterUnitTests.cs @@ -7,6 +7,7 @@ using Xunit; using static System.Buffers.Binary.BinaryPrimitives; +using static System.TestHelpers; namespace System.Buffers.Binary.Tests { diff --git a/src/System.Buffers/tests/Performance/Perf.Span.BinaryReadAndWrite.cs b/src/System.Memory/tests/Performance/Perf.Span.BinaryReadAndWrite.cs similarity index 99% rename from src/System.Buffers/tests/Performance/Perf.Span.BinaryReadAndWrite.cs rename to src/System.Memory/tests/Performance/Perf.Span.BinaryReadAndWrite.cs index fb11392856b6..dbcdc998fb1f 100644 --- a/src/System.Buffers/tests/Performance/Perf.Span.BinaryReadAndWrite.cs +++ b/src/System.Memory/tests/Performance/Perf.Span.BinaryReadAndWrite.cs @@ -7,6 +7,7 @@ using System.Net; using static System.Buffers.Binary.BinaryPrimitives; +using static System.TestHelpers; namespace System.Buffers.Binary.Tests { diff --git a/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj b/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj index f06b55483a29..c4b6188eab82 100644 --- a/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj +++ b/src/System.Memory/tests/Performance/System.Memory.Performance.Tests.csproj @@ -9,10 +9,12 @@ + + Common\System\PerfUtils.cs diff --git a/src/System.Memory/tests/System.Memory.Tests.csproj b/src/System.Memory/tests/System.Memory.Tests.csproj index 247e041ddbd8..28cffcde3fda 100644 --- a/src/System.Memory/tests/System.Memory.Tests.csproj +++ b/src/System.Memory/tests/System.Memory.Tests.csproj @@ -86,7 +86,7 @@ - + @@ -99,5 +99,9 @@ + + + + \ No newline at end of file diff --git a/src/System.Memory/tests/TestHelpers.cs b/src/System.Memory/tests/TestHelpers.cs index 7b46964a251b..8c40b3039f42 100644 --- a/src/System.Memory/tests/TestHelpers.cs +++ b/src/System.Memory/tests/TestHelpers.cs @@ -4,6 +4,9 @@ using Xunit; using System.Runtime.InteropServices; +using System.Runtime.CompilerServices; + +using static System.Buffers.Binary.BinaryPrimitives; namespace System { @@ -153,6 +156,100 @@ public static void ValidateReferenceType(this ReadOnlyMemory memory, param } } + public static void Validate(Span span, T value) where T : struct + { + T read = ReadMachineEndian(span); + Assert.Equal(value, read); + span.Clear(); + } + + public static TestStructExplicit testExplicitStruct = new TestStructExplicit + { + S0 = short.MaxValue, + I0 = int.MaxValue, + L0 = long.MaxValue, + US0 = ushort.MaxValue, + UI0 = uint.MaxValue, + UL0 = ulong.MaxValue, + S1 = short.MinValue, + I1 = int.MinValue, + L1 = long.MinValue, + US1 = ushort.MinValue, + UI1 = uint.MinValue, + UL1 = ulong.MinValue + }; + + public static Span GetSpanBE() + { + Span spanBE = new byte[Unsafe.SizeOf()]; + + WriteInt16BigEndian(spanBE, testExplicitStruct.S0); + WriteInt32BigEndian(spanBE.Slice(2), testExplicitStruct.I0); + WriteInt64BigEndian(spanBE.Slice(6), testExplicitStruct.L0); + WriteUInt16BigEndian(spanBE.Slice(14), testExplicitStruct.US0); + WriteUInt32BigEndian(spanBE.Slice(16), testExplicitStruct.UI0); + WriteUInt64BigEndian(spanBE.Slice(20), testExplicitStruct.UL0); + WriteInt16BigEndian(spanBE.Slice(28), testExplicitStruct.S1); + WriteInt32BigEndian(spanBE.Slice(30), testExplicitStruct.I1); + WriteInt64BigEndian(spanBE.Slice(34), testExplicitStruct.L1); + WriteUInt16BigEndian(spanBE.Slice(42), testExplicitStruct.US1); + WriteUInt32BigEndian(spanBE.Slice(44), testExplicitStruct.UI1); + WriteUInt64BigEndian(spanBE.Slice(48), testExplicitStruct.UL1); + + Assert.Equal(56, spanBE.Length); + return spanBE; + } + + public static Span GetSpanLE() + { + Span spanLE = new byte[Unsafe.SizeOf()]; + + WriteInt16LittleEndian(spanLE, testExplicitStruct.S0); + WriteInt32LittleEndian(spanLE.Slice(2), testExplicitStruct.I0); + WriteInt64LittleEndian( spanLE.Slice(6), testExplicitStruct.L0); + WriteUInt16LittleEndian(spanLE.Slice(14), testExplicitStruct.US0); + WriteUInt32LittleEndian(spanLE.Slice(16), testExplicitStruct.UI0); + WriteUInt64LittleEndian(spanLE.Slice(20), testExplicitStruct.UL0); + WriteInt16LittleEndian(spanLE.Slice(28), testExplicitStruct.S1); + WriteInt32LittleEndian(spanLE.Slice(30), testExplicitStruct.I1); + WriteInt64LittleEndian(spanLE.Slice(34), testExplicitStruct.L1); + WriteUInt16LittleEndian(spanLE.Slice(42), testExplicitStruct.US1); + WriteUInt32LittleEndian(spanLE.Slice(44), testExplicitStruct.UI1); + WriteUInt64LittleEndian(spanLE.Slice(48), testExplicitStruct.UL1); + + Assert.Equal(56, spanLE.Length); + return spanLE; + } + + [StructLayout(LayoutKind.Explicit)] + public struct TestStructExplicit + { + [FieldOffset(0)] + public short S0; + [FieldOffset(2)] + public int I0; + [FieldOffset(6)] + public long L0; + [FieldOffset(14)] + public ushort US0; + [FieldOffset(16)] + public uint UI0; + [FieldOffset(20)] + public ulong UL0; + [FieldOffset(28)] + public short S1; + [FieldOffset(30)] + public int I1; + [FieldOffset(34)] + public long L1; + [FieldOffset(42)] + public ushort US1; + [FieldOffset(44)] + public uint UI1; + [FieldOffset(48)] + public ulong UL1; + } + [StructLayout(LayoutKind.Sequential)] public sealed class TestClass {