Best interface for memcpy() (and the string.h family of functions)

kinke noone at nowhere.com
Wed May 29 20:50:45 UTC 2019


On Wednesday, 29 May 2019 at 11:46:28 UTC, Stefanos Baziotis 
wrote:
> My initial pick was void memcpyD(T)(T* dst, const T* src), but 
> it was proposed
> that `ref` instead of pointers might be better.

ref would only work when copying one instance at a time. Many 
times, you'll want to copy a contiguous array of a length only 
known at runtime (and definitely NOT invoke memcpy in a loop, so 
that the implementation can e.g. use SIMD streaming when copying 
gazillions of 32-bit pixels).

I'd suggest a structure similar to this, minimizing bloat:

// int a, b;            memcpyD(&a, &b);
// int[4] a, b;         memcpyD(&a, &b);
// int[16] a; int[4] b; memcpyD!4(&a[8], b.ptr);
void memcpyD(size_t length = 1, T)(T* dst, const T* src)
{
     pickBestImpl!(T.alignof, length * T.sizeof)(dst, src);
}

void memcpyD(T)(T* dst, const T* src, size_t length)
{
     pickBestImpl!(T.alignof)(dst, src, length * T.sizeof);
}

private:

/* These 2 will probably share most logic, the first one just 
exploiting a
  * static size. A common mixin might come in handy (e.g., 
switching from
  * runtime-if to static-if).
  */
void pickBestImpl(size_t alignment, size_t size)(void* dst, const 
void* src);
void pickBestImpl(size_t alignment)(void* dst, const void* src, 
size_t size);


More information about the Digitalmars-d mailing list