]> git.ozlabs.org Git - ccan/blob - ccan/tal/autoptr/autoptr.c
Makefile: fix fastcheck.
[ccan] / ccan / tal / autoptr / autoptr.c
1 /* MIT (BSD) license - see LICENSE file for details */
2 #include <ccan/tal/autoptr/autoptr.h>
3
4 struct autonull {
5         void **pp;
6 };
7
8 static void autonull_remove(struct autonull *a);
9 static void autonull_null_out(tal_t *p UNNEEDED, struct autonull *a)
10 {
11         void **pp = a->pp;
12         tal_del_destructor(a, autonull_remove);
13         tal_free(a);
14         *pp = NULL;
15 }
16
17 static void autonull_remove(struct autonull *a)
18 {
19         /* Don't NULL us out now. */
20         tal_del_destructor2(*a->pp, autonull_null_out, a);
21 }
22
23 struct autonull *autonull_set_ptr_(const tal_t *ctx, void *pp, const tal_t *p)
24 {
25         struct autonull *a = tal(ctx, struct autonull);
26         a->pp = (void **)pp;
27         *a->pp = (void *)p;
28
29         /* If p is freed, NULL out a->pp */
30         tal_add_destructor2(*a->pp, autonull_null_out, a);
31
32         /* If they free autonull, it removes other destructor. */
33         tal_add_destructor(a, autonull_remove);
34         return a;
35 }