Exemple #1
0
        internal static unsafe void CopyTo <T>(ref T destination, ref T source, int elementsCount)
        {
            if (elementsCount == 0)
            {
                return;
            }

            if (Unsafe.AreSame(ref destination, ref source))
            {
                return;
            }

            if (!JitHelpers.ContainsReferences <T>())
            {
                fixed(byte *pDestination = &Unsafe.As <T, byte>(ref destination))
                {
                    fixed(byte *pSource = &Unsafe.As <T, byte>(ref source))
                    {
#if BIT64
                        Buffer.Memmove(pDestination, pSource, (ulong)elementsCount * (ulong)Unsafe.SizeOf <T>());
#else
                        Buffer.Memmove(pDestination, pSource, (uint)elementsCount * (uint)Unsafe.SizeOf <T>());
#endif
                    }
                }
            }
            else
            {
                if (JitHelpers.ByRefLessThan(ref destination, ref source)) // copy forward
                {
                    for (int i = 0; i < elementsCount; i++)
                    {
                        Unsafe.Add(ref destination, i) = Unsafe.Add(ref source, i);
                    }
                }
                else // copy backward to avoid overlapping issues
                {
                    for (int i = elementsCount - 1; i >= 0; i--)
                    {
                        Unsafe.Add(ref destination, i) = Unsafe.Add(ref source, i);
                    }
                }
            }
        }