diff options
author | Junio C Hamano <gitster@pobox.com> | 2023-01-22 04:21:58 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2023-01-22 04:21:58 +0300 |
commit | 60ce816cb637f8fac5e8e5b8a53dff8707b2c04c (patch) | |
tree | 54c95811c38748e928c7e788812844d85e8a059d /git-compat-util.h | |
parent | 90c47b3fbabd6ffecfd234911841892b562cfe9e (diff) | |
parent | 6e578410960d9ceb35ec98ad4b6fc711f1a9c85c (diff) |
Merge branch 'rs/dup-array'
Code cleaning.
* rs/dup-array:
use DUP_ARRAY
add DUP_ARRAY
do full type check in BARF_UNLESS_COPYABLE
factor out BARF_UNLESS_COPYABLE
mingw: make argv2 in try_shell_exec() non-const
Diffstat (limited to 'git-compat-util.h')
-rw-r--r-- | git-compat-util.h | 15 |
1 files changed, 13 insertions, 2 deletions
diff --git a/git-compat-util.h b/git-compat-util.h index 7f84430d0e..6240fc0795 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -97,8 +97,14 @@ struct strbuf; # define BARF_UNLESS_AN_ARRAY(arr) \ BUILD_ASSERT_OR_ZERO(!__builtin_types_compatible_p(__typeof__(arr), \ __typeof__(&(arr)[0]))) +# define BARF_UNLESS_COPYABLE(dst, src) \ + BUILD_ASSERT_OR_ZERO(__builtin_types_compatible_p(__typeof__(*(dst)), \ + __typeof__(*(src)))) #else # define BARF_UNLESS_AN_ARRAY(arr) 0 +# define BARF_UNLESS_COPYABLE(dst, src) \ + BUILD_ASSERT_OR_ZERO(0 ? ((*(dst) = *(src)), 0) : \ + sizeof(*(dst)) == sizeof(*(src))) #endif /* * ARRAY_SIZE - get the number of elements in a visible array @@ -1102,7 +1108,7 @@ int xstrncmpz(const char *s, const char *t, size_t len); #define REALLOC_ARRAY(x, alloc) (x) = xrealloc((x), st_mult(sizeof(*(x)), (alloc))) #define COPY_ARRAY(dst, src, n) copy_array((dst), (src), (n), sizeof(*(dst)) + \ - BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) + BARF_UNLESS_COPYABLE((dst), (src))) static inline void copy_array(void *dst, const void *src, size_t n, size_t size) { if (n) @@ -1110,13 +1116,18 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size) } #define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ - BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(src)))) + BARF_UNLESS_COPYABLE((dst), (src))) static inline void move_array(void *dst, const void *src, size_t n, size_t size) { if (n) memmove(dst, src, st_mult(size, n)); } +#define DUP_ARRAY(dst, src, n) do { \ + size_t dup_array_n_ = (n); \ + COPY_ARRAY(ALLOC_ARRAY((dst), dup_array_n_), (src), dup_array_n_); \ +} while (0) + /* * These functions help you allocate structs with flex arrays, and copy * the data directly into the array. For example, if you had: |