Skip to content

Commit

Permalink
ccan: update to latest version which allows destructors to self-delete.
Browse files Browse the repository at this point in the history
Also, a fix for compiling with NDEBUG.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Feb 17, 2020
1 parent 7de03e9 commit 9d9480f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
2 changes: 1 addition & 1 deletion ccan/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-2494-g4f20b75c
CCAN version: init-2495-gc910bdce
13 changes: 9 additions & 4 deletions ccan/ccan/tal/tal.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ struct name {
struct notifier {
struct prop_hdr hdr; /* NOTIFIER */
enum tal_notify_type types;
union {
union notifier_cb {
void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
void (*destroy2)(tal_t *, void *); /* If NOTIFY_EXTRA_ARG */
Expand Down Expand Up @@ -228,11 +228,16 @@ static void notify(const struct tal_hdr *ctx,
if (n->types & type) {
errno = saved_errno;
if (n->types & NOTIFY_IS_DESTRUCTOR) {
/* Blatt this notifier in case it tries to
* tal_del_destructor() from inside */
union notifier_cb cb = n->u;
/* It's a union, so this NULLs destroy2 too! */
n->u.destroy = NULL;
if (n->types & NOTIFY_EXTRA_ARG)
n->u.destroy2(from_tal_hdr(ctx),
EXTRA_ARG(n));
cb.destroy2(from_tal_hdr(ctx),
EXTRA_ARG(n));
else
n->u.destroy(from_tal_hdr(ctx));
cb.destroy(from_tal_hdr(ctx));
} else
n->u.notifyfn(from_tal_hdr_or_null(ctx), type,
(void *)info);
Expand Down
6 changes: 4 additions & 2 deletions ccan/ccan/tal/tal.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ void *tal_free(const tal_t *p);
* @function: the function to call before it's freed.
*
* If @function has not been successfully added as a destructor, this returns
* false.
* false. Note that if we're inside the destructor call itself, this will
* return false.
*/
#define tal_del_destructor(ptr, function) \
tal_del_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr)))
Expand Down Expand Up @@ -195,7 +196,8 @@ void *tal_free(const tal_t *p);
* @function: the function to call before it's freed.
*
* If @function has not been successfully added as a destructor, this returns
* false.
* false. Note that if we're inside the destructor call itself, this will
* return false.
*/
#define tal_del_destructor(ptr, function) \
tal_del_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr)))
Expand Down
14 changes: 13 additions & 1 deletion ccan/ccan/tal/test/run-destructor.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ static void destroy_inc(char *p UNNEEDED)
destroy_count++;
}

static void remove_own_destructor(char *p)
{
destroy_count++;
ok1(!tal_del_destructor(p, remove_own_destructor));
}

int main(void)
{
char *child2;

plan_tests(18);
plan_tests(21);

destroy_count = 0;
parent = tal(NULL, char);
Expand Down Expand Up @@ -63,6 +69,12 @@ int main(void)
tal_free(parent);
ok1(destroy_count == 4);

destroy_count = 0;
parent = tal(NULL, char);
ok1(tal_add_destructor(parent, remove_own_destructor));
tal_free(parent);
ok1(destroy_count == 1);

tal_cleanup();
return exit_status();
}
2 changes: 1 addition & 1 deletion ccan/ccan/timer/timer.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void timer_init(struct timer *t)
list_node_init(&t->list);
}

static bool list_node_initted(const struct list_node *n)
static inline bool list_node_initted(const struct list_node *n)
{
return n->prev == n;
}
Expand Down

0 comments on commit 9d9480f

Please sign in to comment.