X-Git-Url: http://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftal%2Ftal.h;h=0b050759f74ed97829661de5dc2488af74264fe9;hp=091eb034c1fbfb979f30f9021266c7c93278e695;hb=8d14165ac039a97617f2b63ee70754b855776013;hpb=5582b011948769779b0d839d35873c2bc557f9cb diff --git a/ccan/tal/tal.h b/ccan/tal/tal.h index 091eb034..0b050759 100644 --- a/ccan/tal/tal.h +++ b/ccan/tal/tal.h @@ -110,7 +110,22 @@ void *tal_free(const tal_t *p); * tal_resize(&p, 100); */ #define tal_resize(p, count) \ - tal_resize_((void **)(p), sizeof**(p), (count)) + tal_resize_((void **)(p), sizeof**(p), (count), false) + +/** + * tal_resizez - enlarge or reduce a tal_arr[z]; zero out extra. + * @p: A pointer to the tal allocated array to resize. + * @count: the number to allocate. + * + * This returns true on success (and may move *@p), or false on failure. + * If @p has a length property, it is updated on success. + * On expand, new elements are memset to 0 bytes. + * + * Example: + * tal_resizez(&p, 200); + */ +#define tal_resizez(p, count) \ + tal_resize_((void **)(p), sizeof**(p), (count), true) /** * tal_steal - change the parent of a tal-allocated pointer. @@ -305,6 +320,39 @@ void tal_set_backend(void *(*alloc_fn)(size_t size), void (*free_fn)(void *), void (*error_fn)(const char *msg)); +/** + * tal_expand - expand a tal array with contents. + * @a1p: a pointer to the tal array to expand. + * @a2: the second array (can be take()). + * @num2: the number of elements in the second array. + * + * Note that *@a1 and @a2 should be the same type. tal_count(@a1) will + * be increased by @num2. + * + * Example: + * int *arr1 = tal_arrz(NULL, int, 2); + * int arr2[2] = { 1, 3 }; + * + * tal_expand(&arr1, arr2, 2); + * assert(tal_count(arr1) == 4); + * assert(arr1[2] == 1); + * assert(arr1[3] == 3); + */ +#define tal_expand(a1p, a2, num2) \ + tal_expand_((void **)(a1p), (a2), sizeof**(a1p), \ + (num2) + 0*sizeof(*(a1p) == (a2))) + +/** + * tal_cleanup - remove pointers from NULL node + * + * Internally, tal keeps a list of nodes allocated from @ctx NULL; this + * prevents valgrind from noticing memory leaks. This re-initializes + * that list to empty. + * + * It also calls take_cleanup() for you. + */ +void tal_cleanup(void); + /** * tal_check - set the allocation or error functions to use @@ -373,7 +421,8 @@ void *tal_dup_(const tal_t *ctx, const void *p, size_t size, tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t); -bool tal_resize_(tal_t **ctxp, size_t size, size_t count); +bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear); +bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count); bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me)); bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me));