X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;f=ccan%2Ftal%2Fautoptr%2Fautoptr.h;fp=ccan%2Ftal%2Fautoptr%2Fautoptr.h;h=57996653d5c4a9f5ad20ea8fcdff2625c52c75fd;hb=97ac5832db26f5f836fff979b08f01e17d7216bd;hp=0000000000000000000000000000000000000000;hpb=58277ab6c8b4dd6bb66638b88bd8505f46fdcb07;p=ccan diff --git a/ccan/tal/autoptr/autoptr.h b/ccan/tal/autoptr/autoptr.h new file mode 100644 index 00000000..57996653 --- /dev/null +++ b/ccan/tal/autoptr/autoptr.h @@ -0,0 +1,47 @@ +/* MIT (BSD) license - see LICENSE file for details */ +#ifndef CCAN_TAL_AUTOPTR_H +#define CCAN_TAL_AUTOPTR_H +#include + +struct autonull; + +/** + * autonull_set_ptr - set a pointer, NULL it when pointer tal_free'd. + * @ctx: the tal context which owns this autonull. + * @pp: pointer to tal pointer + * @p: the tal pointer to set *@pp to. + * + * *@pp is set to @p. When @p is tal_free'd directly or indirectly, *@pp will + * be set to NULL. Or, if the returned object is freed, the callback is + * deactivated. + * + * Example: + * struct parent { + * struct child *c; + * }; + * struct child { + * const char *name; + * }; + * + * int main(void) + * { + * struct parent *p = tal(NULL, struct parent); + * struct child *c = tal(p, struct child); + * c->name = "Child"; + * + * autonull_set_ptr(p, &p->c, c); + * assert(p->c == c); + * + * // Automatically clears p->c. + * tal_free(c); + * assert(p->c == NULL); + * return 0; + * } + * + */ +#define autonull_set_ptr(ctx, pp, p) \ + autonull_set_ptr_((ctx), (pp) + 0*sizeof(*(pp) = (p)), (p)) + +struct autonull *autonull_set_ptr_(const tal_t *ctx, void *pp, const tal_t *p); + +#endif /* CCAN_TAL_AUTOPTR_H */