From a2ca34ccf5da599d36d3c62f290e1d1021e32e15 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 7 Apr 2023 14:09:53 +0930 Subject: [PATCH] common: add tal_arr_insert helper to utils.h We have tal_arr_remove and tal_arr_append already. Signed-off-by: Rusty Russell --- common/utils.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/common/utils.h b/common/utils.h index dcfa111cb..2aadec5fc 100644 --- a/common/utils.h +++ b/common/utils.h @@ -80,11 +80,22 @@ void clear_softref_(const tal_t *outer, size_t outersize, void **ptr); * Remove an element from an array * * This will shift the elements past the removed element, changing - * their position in memory, so only use this for arrays of pointers. + * their position in memory, so only use this for simple arrays. */ #define tal_arr_remove(p, n) tal_arr_remove_((p), sizeof(**p), (n)) void tal_arr_remove_(void *p, size_t elemsize, size_t n); +/** + * Insert an element in an array + */ +#define tal_arr_insert(p, n, v) \ + do { \ + size_t n_ = tal_count(*(p)); \ + tal_resize((p), n_+1); \ + memmove(*(p) + n + 1, *(p) + n, (n_ - n) * sizeof(**(p))); \ + (*(p))[n] = (v); \ + } while(0) + /* Check for valid UTF-8 */ bool utf8_check(const void *buf, size_t buflen);