[PATCH v2 3/4] add DUP_ARRAY
- From
René Scharfe <l.s.r@web.de>
- Date
- Jan 1, 2023, 21:14 UTC
- Message-ID
- <04e99a14-a25b-7e47-8bf3-49442c877c89@web.de>
- In-Reply-To
- <9bc1bd74-f72c-1b43-df7c-950815babb03@web.de>
Add a macro for allocating and populating a shallow copy of an array. It is intended to replace a sequence like this:
ALLOC_ARRAY(dst, n); COPY_ARRAY(dst, src, n);
With the less repetitve:
DUP_ARRAY(dst, src, n);
It checks whether the types of source and destination are compatible to ensure the copy can be used safely.
An easier alternative would be to only consider the source and return a void pointer, that could be used like this:
dst = ARRAY_DUP(src, n);
That would be more versatile, as it could be used in declarations as well. Making it type-safe would require the use of typeof_unqual from C23, though.
So use the safe and compatible variant for now.
Signed-off-by: René Scharfe <l.s.r@web.de> --- Why typeof_unqual and not just typeof? To remove const when duplicating a const array.
git-compat-util.h | 5 +++++ 1 file changed, 5 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h index e81bb14fc9..44abb240ae 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -1115,6 +1115,11 @@ static inline void move_array(void *dst, const void *src, size_t n, size_t size) 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: -- 2.39.0