mirror of
https://github.com/dotnet/MQTTnet.git
synced 2026-10-20 19:58:57 +08:00
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:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user