runtime: Add CopyBlock/InitBlock overloads for ref byte

class Unsafe
{
   void CopyBlock(ref byte destination, ref byte source, uint byteCount);
   void CopyBlock(ref byte destination, ref byte source, UIntPtr byteCount);
   void CopyBlockUnaligned(ref byte destination, ref byte source, uint byteCount);
   void CopyBlockUnaligned(ref byte destination, ref byte source, UIntPtr byteCount);
   void InitBlock(ref byte destination, uint byteCount);
   void InitBlock(ref byte destination, UIntPtr byteCount);
   void InitBlockUnaligned(ref byte destination, uint byteCount);
   void InitBlockUnaligned(ref byte destination, UIntPtr byteCount);
}

Thin wrappers over the cpblk and initblk IL instructions to make them accessible from C#. Needed to implement Span<T>.CopyTo() efficiently.

Since this is intended to be a wrapper around the “cpblk” IL instruction, the behavior is undefined if the ranges overlap. (This suggests Span<T>.CopyTo() needs to handle three cases:

  • No overlap - Use Unsafe.CopyTo<T>() - enjoy the perf.
  • Overlap one way - Use forward for loop.
  • Overlap the other way - Use backward for loop.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 59 (52 by maintainers)

Most upvoted comments

We should also consider whether to add Fill like https://github.com/dotnet/corefx/issues/6695 to provide functionality like this on Span<T>, I could make an API proposal for that too, if this is of interest.

ContainsReferences: This doesn’t seem like it belongs in Unsafe. There’s nothing inherently unsafe about it

It is in the same boat as Unsafe.SizeOf<T>() and several other existing APIs. It is not inherently unsafe, but it is only useful for low-level unsafe programming.