diff --git a/common/utils.c b/common/utils.c index f6d28c8b7..216544b47 100644 --- a/common/utils.c +++ b/common/utils.c @@ -49,3 +49,15 @@ void clean_tmpctx(void) while ((p = tal_first(tmpctx)) != NULL) tal_free(p); } + +void tal_arr_remove_(void *p, size_t elemsize, size_t n) +{ + // p is a pointer-to-pointer for tal_resize. + char *objp = *(char **)p; + size_t len = tal_bytelen(objp); + assert(len % elemsize == 0); + assert((n + 1) * elemsize <= len); + memmove(objp + elemsize * n, objp + elemsize * (n+1), + len - (elemsize * (n+1))); + tal_resize((char **)p, len - elemsize); +} diff --git a/common/utils.h b/common/utils.h index e43c82c22..5cff73610 100644 --- a/common/utils.h +++ b/common/utils.h @@ -29,6 +29,15 @@ u8 *tal_hexdata(const tal_t *ctx, const void *str, size_t len); (tal_resize((p), tal_count(*(p))+1), (*p) + tal_count(*(p))-1) #endif +/** + * 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. + */ +#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); + /* Use the POSIX C locale. */ void setup_locale(void);