X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftal%2Ftal.h;h=c486f9e8e194a99b51ffdb8117788bbae86a89ed;hp=a3a1549153fe4f42636b116d33162c774982df46;hb=HEAD;hpb=05ec83512c478e03f0af460d304483ed292337d1 diff --git a/ccan/tal/tal.h b/ccan/tal/tal.h index a3a15491..347a5e8c 100644 --- a/ccan/tal/tal.h +++ b/ccan/tal/tal.h @@ -11,6 +11,14 @@ #include #include +/* Define this for better optimization if you never override errfn + * to something tat returns */ +#ifdef CCAN_TAL_NEVER_RETURN_NULL +#define TAL_RETURN_PTR RETURNS_NONNULL +#else +#define TAL_RETURN_PTR +#endif /* CCAN_TAL_NEVER_RETURN_NULL */ + /** * tal_t - convenient alias for void to mark tal pointers. * @@ -352,10 +360,21 @@ tal_t *tal_parent(const tal_t *ctx); * tal_dup - duplicate an object. * @ctx: The tal allocated object to be parent of the result (may be NULL). * @type: the type (should match type of @p!) - * @p: the object to copy (or reparented if take()) + * @p: the object to copy (or reparented if take()). Must not be NULL. */ #define tal_dup(ctx, type, p) \ - tal_dup_label(ctx, type, p, TAL_LABEL(type, "")) + tal_dup_label(ctx, type, p, TAL_LABEL(type, ""), false) + +/** + * tal_dup_or_null - duplicate an object, or just pass NULL. + * @ctx: The tal allocated object to be parent of the result (may be NULL). + * @type: the type (should match type of @p!) + * @p: the object to copy (or reparented if take()) + * + * if @p is NULL, just return NULL, otherwise to tal_dup(). + */ +#define tal_dup_or_null(ctx, type, p) \ + tal_dup_label(ctx, type, p, TAL_LABEL(type, ""), true) /** * tal_dup_arr - duplicate an array. @@ -369,7 +388,17 @@ tal_t *tal_parent(const tal_t *ctx); tal_dup_arr_label(ctx, type, p, n, extra, TAL_LABEL(type, "[]")) - +/** + * tal_dup_arr - duplicate a tal array. + * @ctx: The tal allocated object to be parent of the result (may be NULL). + * @type: the type (should match type of @p!) + * @p: the tal array to copy (or resized & reparented if take()) + * + * The comon case of duplicating an entire tal array. + */ +#define tal_dup_talarr(ctx, type, p) \ + ((type *)tal_dup_talarr_((ctx), tal_typechk_(p, type *), \ + TAL_LABEL(type, "[]"))) /* Lower-level interfaces, where you want to supply your own label string. */ #define tal_label(ctx, type, label) \ ((type *)tal_alloc_((ctx), sizeof(type), false, label)) @@ -379,13 +408,13 @@ tal_t *tal_parent(const tal_t *ctx); ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), false, label)) #define tal_arrz_label(ctx, type, count, label) \ ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), true, label)) -#define tal_dup_label(ctx, type, p, label) \ +#define tal_dup_label(ctx, type, p, label, nullok) \ ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \ - sizeof(type), 1, 0, \ + sizeof(type), 1, 0, nullok, \ label)) #define tal_dup_arr_label(ctx, type, p, n, extra, label) \ ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \ - sizeof(type), (n), (extra), \ + sizeof(type), (n), (extra), false, \ label)) /** @@ -396,7 +425,8 @@ tal_t *tal_parent(const tal_t *ctx); * @error_fn: called on errors or NULL (default is abort) * * The defaults are set up so tal functions never return NULL, but you - * can override erorr_fn to change that. error_fn can return, and is + * can override error_fn to change that. error_fn can return (only if + * you haven't defined CCAN_TAL_NEVER_RETURN_NULL!), and is * called if alloc_fn or resize_fn fail. * * If any parameter is NULL, that function is unchanged. @@ -500,12 +530,16 @@ bool tal_set_name_(tal_t *ctx, const char *name, bool literal); #define tal_typechk_(ptr, ptype) (ptr) #endif -void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label); +void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label) + TAL_RETURN_PTR; void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear, - const char *label); + const char *label) + TAL_RETURN_PTR; void *tal_dup_(const tal_t *ctx, const void *p TAKES, size_t size, - size_t n, size_t extra, const char *label); + size_t n, size_t extra, bool nullok, const char *label); +void *tal_dup_talarr_(const tal_t *ctx, const tal_t *src TAKES, + const char *label); tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t);