Implement buffer copy using different policies (#1599)

use Memory-Copy

Co-authored-by: Christian <6939810+chkr1011@users.noreply.github.com>
This commit is contained in:
老九
2022-12-03 13:17:02 +01:00
committed by GitHub
co-authored by Christian
parent 27f2474876
commit 862d76471b
7 changed files with 81 additions and 10 deletions
+29
View File
@@ -0,0 +1,29 @@
using System;
using System.Runtime.CompilerServices;
namespace MQTTnet.Internal
{
public static class Memory
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Copy(byte[] source, int sourceIndex, byte[] destination, int destinationIndex, int length)
{
#if NETCOREAPP3_1_OR_GREATER || NETSTANDARD2_1
source.AsSpan(sourceIndex, length).CopyTo(destination.AsSpan(destinationIndex, length));
#elif NET461_OR_GREATER || NETSTANDARD1_3_OR_GREATER
unsafe
{
fixed (byte* pSoure = &source[sourceIndex])
{
fixed (byte* pDestination = &destination[destinationIndex])
{
System.Buffer.MemoryCopy(pSoure, pDestination, length, length);
}
}
}
#else
Array.Copy(source, sourceIndex, destination, destinationIndex, length);
#endif
}
}
}