public void CopyPointerToPointer_Unsafe() { var size = _container.Size; unsafe { using var destContainer0 = new UnmanagedMemoryContainer(size); void *srcPointer = _container.IntPtr.ToPointer(); void *destPointer = destContainer0.IntPtr.ToPointer(); // void* srcPointer / void* destPointer Unsafe.CopyBlock(srcPointer, destPointer, (uint)size); Assert.Equal(_container.GetSum(), destContainer0.GetSum()); } unsafe { using var destContainer1 = new UnmanagedMemoryContainer(size); void *srcPointer = _container.IntPtr.ToPointer(); void *destPointer = destContainer1.IntPtr.ToPointer(); // Unalignedにする必要ない(使いたいだけ) Unsafe.CopyBlockUnaligned(srcPointer, destPointer, (uint)size); Assert.Equal(_container.GetSum(), destContainer1.GetSum()); } }
public void CopyPointerToPointer_Buffer() { unsafe { var size = _container.Size; using var destContainer = new UnmanagedMemoryContainer(size); void *srcPointer = _container.IntPtr.ToPointer(); void *destPointer = destContainer.IntPtr.ToPointer(); // void* srcPointer / void* destPointer Buffer.MemoryCopy(srcPointer, destPointer, destinationSizeInBytes: size, sourceBytesToCopy: size); Assert.Equal(_container.GetSum(), destContainer.GetSum()); } }
public void CopyArrayToPointer() { var size = 256; var srcArray = Enumerable.Range(0, size).Select(x => (byte)x).ToArray(); using var destContainer = new UnmanagedMemoryContainer(size); var destIntPtr = destContainer.IntPtr; Marshal.Copy(srcArray, startIndex: 0, destIntPtr, srcArray.Length); ulong sum = 0; foreach (var b in srcArray) { sum += b; } Assert.Equal(sum, destContainer.GetSum()); }