factor out BARF_UNLESS_COPYABLE

Move the common basic element type check of COPY_ARRAY and MOVE_ARRAY to a new macro. This reduces code duplication and simplifies adding more elaborate checks. Suggested-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 1, 2023 at 22:08 UTC 1891846fa4d439be7f9a1a32c062f62cd863df2b
1 file changed +5 -2
git-compat-util.h
+5 -2
@@ -1093,8 +1093,11 @@ int xstrncmpz(const char *s, const char *t, size_t len);
1093 #define CALLOC_ARRAY(x, alloc) (x) = xcalloc((alloc), sizeof(*(x)))
1094 #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc)))
1095
1096 +#define BARF_UNLESS_COPYABLE(dst, src) \
1097 + BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))
1098 +
1099 #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \
1097 - BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
1100 + BARF_UNLESS_COPYABLE((dst), (src)))
1101 static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
1102 {
1103 if (n)
@@ -1102,7 +1105,7 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size)
1105 }
1106
1107 #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \
1105 - BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src))))
1108 + BARF_UNLESS_COPYABLE((dst), (src)))
1109 static inline void move_array(void *dst, const void *src, size_t n, size_t size)
1110 {
1111 if (n)