mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 07:04:22 +01:00
common: Add tal_arr_remove helper
Suggested-by: Rusty Russell <@rustyrussell> Signed-off-by: Christian Decker <decker.christian@gmail.com>
This commit is contained in:
committed by
Rusty Russell
parent
906bfef4fd
commit
9ce3e960ec
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user