X-Git-Url: https://git.ozlabs.org/?p=ccan;a=blobdiff_plain;f=ccan%2Ftal%2Flink%2Flink.h;fp=ccan%2Ftal%2Flink%2Flink.h;h=18d598b89905bd230bdd2e331cb11807bb149cd6;hp=0000000000000000000000000000000000000000;hb=72ce81407f677cf279eb46b1c90c34316067b674;hpb=e7ae27d64226dda9865f1e4b5d9a55adcee04694 diff --git a/ccan/tal/link/link.h b/ccan/tal/link/link.h new file mode 100644 index 00000000..18d598b8 --- /dev/null +++ b/ccan/tal/link/link.h @@ -0,0 +1,69 @@ +/* Licensed under BSD-MIT - see LICENSE file for details */ +#ifndef TAL_LINK_H +#define TAL_LINK_H +#include "config.h" +#include + +/** + * tal_linkable - set up a tal object to be linkable. + * @newobj - the newly allocated object (with a NULL parent) + * + * The object will be freed when @newobj is freed or the last talloc_link() + * is talloc_delink'ed. + * + * Returns @newobj or NULL (if an allocation fails). + * + * Example: + * int *shared_count; + * + * shared_count = tal_linkable(talz(NULL, int)); + * assert(shared_count); + */ +#define tal_linkable(newobj) \ + (tal_typeof(newobj) tal_linkable_((newobj))) + +/** + * tal_link - add a(nother) link to a linkable object. + * @ctx - the context to link to (parent of the resulting link) + * @obj - the object previously made linkable with talloc_linked(). + * + * If @ctx is non-NULL, the link will be a child of @ctx, and this freed + * when @ctx is. + * + * Returns NULL on failure (out of memory). + * + * Example: + * void *my_ctx = NULL; + * + * tal_link(my_ctx, shared_count); + */ +#if HAVE_STATEMENT_EXPR +/* Weird macro avoids gcc's 'warning: value computed is not used'. */ +#define tal_link(ctx, obj) \ + ({ tal_typeof(obj) tal_link_((ctx), (obj)); }) +#else +#define tal_link(ctx, obj) \ + (tal_typeof(obj) tal_link_((ctx), (obj))) +#endif + +/** + * tal_delink - explicitly remove a link from a linkable object. + * @ctx - the context to link to (parent of the resulting link) + * @obj - the object previously made linkable with talloc_linked(). + * + * Explicitly remove a link: normally it is implied by freeing @ctx. + * Removing the last link frees the object. If @obj is NULL, nothing + * is done. + * + * Example: + * tal_delink(my_ctx, shared_count); + */ +#define tal_delink(ctx, obj) \ + tal_delink_((ctx), (obj)) + +/* Internal helpers. */ +void *tal_linkable_(tal_t *newobj); +void *tal_link_(const tal_t *ctx, const tal_t *dest); +void tal_delink_(const tal_t *ctx, const tal_t *dest); + +#endif /* TAL_LINK_H */