1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
using System;
using System.Runtime.InteropServices;
using System.Linq;
using System.Diagnostics;
class name_space
{
static unsafe void my_memcpy( ulong[] srce, ulong[] dest )
{
int nbytes = Math.Min(srce.Length, dest.Length) * sizeof(ulong);
fixed( ulong* from = srce, to = dest )
{
byte* srce_bytes = (byte*)from;
byte* dest_bytes = (byte*)to;
for (int i = 0; i < nbytes; ++i) dest_bytes[i] = srce_bytes[i];
}
}
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "memcpy") ]
static extern UIntPtr libc_memcpy(UIntPtr dest, UIntPtr src, UIntPtr count);
static void Main(string[] args)
{
ulong[] a = { 1, 2, 3, 4, 5 };
ulong[] b = new ulong[a.Length];
ulong[] c = new ulong[a.Length];
ulong[] d = new ulong[a.Length];
my_memcpy(a,b);
a.CopyTo(c,0);
unsafe
{
fixed (void* dest = d, srce = a )
{ libc_memcpy((UIntPtr)dest, (UIntPtr)srce, (UIntPtr)(a.Length * sizeof(ulong))); }
}
var a_eq_b = Enumerable.SequenceEqual(a, b); // Linq
var a_eq_c = Enumerable.SequenceEqual(a, c);
var a_eq_d = Enumerable.SequenceEqual(a, d);
Debug.Assert(a_eq_b && a_eq_c && a_eq_d);
Console.Write("all four are equal? " + (a_eq_b && a_eq_c && a_eq_d).ToString() + '\n');
}
}
|