Jacob Garber has posted comments on this change. ( https://review.coreboot.org/c/coreboot/+/32740 )
Change subject: lib.h: Add convenient memcpy() variants ......................................................................
Patch Set 1:
We can use _Generic from C11 and the __typeof__ extension from GCC to detect if something is a pointer or an array. First, we can define the macro
#define IS_COMPATIBLE(x, y) _Generic((x), __typeof__(y) : true, default : false)
This will switch on the type of x and check if it is compatible with y. Next, detecting if something is an array can be done using
#define IS_ARRAY(x) !IS_COMPATIBLE(&*x, x)
For pointers, & and * are inverses, so &*x and x will have the same type. For an array x of type T[N] however, &*x will have the type *T, which is incompatible with T[N]. For example,
int x[2]; int *y; static_assert(IS_ARRAY(x)); // will succeed! static_assert(IS_ARRAY(y)); // will fail!
void test(int x[2]) { // will fail, since x is not an array even though it looks like one static_assert(IS_ARRAY(x)); }
I think having a macro (or macros) specifically for copying arrays would be useful - it is certainly helpful in the patches Nico has based on this commit.
More _Generic macro magic can be read about here, which is the web page that inspired this post. http://www.robertgamble.net/2012/01/c11-generic-selections.html