[PATCH 1/2] add MOVE_ARRAY
- From
René Scharfe <l.s.r@web.de>
- Date
- Apr 7, 2017, 15:25 UTC
- Message-ID
- <c4c60ec9-641d-fb2e-046a-91e08615c8f7@web.de>
- In-Reply-To
- <96beb4c6-0569-0c12-8151-462c20be6a2a@suse.cz>
Add MOVE_ARRAY to complement COPY_ARRAY, which was added in 60566cbb. It provides the same convenience, safety and support for NULL pointers as representations of empty arrays, just as a wrapper for memmove(3) instead of memcpy(3).
Inspired-by: Martin Liska <mliska@suse.cz> Signed-off-by: Rene Scharfe <l.s.r@web.de> --- git-compat-util.h | 8 ++++++++ 1 file changed, 8 insertions(+)
diff --git a/git-compat-util.h b/git-compat-util.h index 8a4a3f85e7..3266a71fb4 100644 --- a/git-compat-util.h +++ b/git-compat-util.h @@ -812,6 +812,14 @@ static inline void copy_array(void *dst, const void *src, size_t n, size_t size) memcpy(dst, src, st_mult(size, n)); } +#define MOVE_ARRAY(dst, src, n) move_array((dst), (src), (n), sizeof(*(dst)) + \ + BUILD_ASSERT_OR_ZERO(sizeof(*(dst)) == sizeof(*(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)); +} + /* * These functions help you allocate structs with flex arrays, and copy * the data directly into the array. For example, if you had:
-- 2.12.2