1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #include <ccan/tal/tal.h>
3 #include <ccan/compiler/compiler.h>
4 #include <ccan/list/list.h>
5 #include <ccan/take/take.h>
16 /* 32-bit type field, first byte 0 in either endianness. */
18 CHILDREN = 0x00c1d500,
19 DESTRUCTOR = 0x00de5700,
25 struct list_node list;
26 struct prop_hdr *prop;
27 struct children *parent_child;
32 struct prop_hdr *next;
36 struct prop_hdr hdr; /* CHILDREN */
37 struct tal_hdr *parent;
38 struct list_head children; /* Head of siblings. */
42 struct prop_hdr hdr; /* DESTRUCTOR */
43 void (*destroy)(void *me);
47 struct prop_hdr hdr; /* NAME */
52 struct prop_hdr hdr; /* NOTIFIER */
53 enum tal_notify_type types;
54 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
60 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
61 &null_parent.c.hdr, NULL },
64 { { &null_parent.c.children.n,
65 &null_parent.c.children.n } }
70 static void *(*allocfn)(size_t size) = malloc;
71 static void *(*resizefn)(void *, size_t size) = realloc;
72 static void (*freefn)(void *) = free;
73 static void (*errorfn)(const char *msg) = (void *)abort;
75 static inline void COLD call_error(const char *msg)
80 static bool get_destroying_bit(struct children *parent_child)
82 return (size_t)parent_child & 1;
85 static void set_destroying_bit(struct children **parent_child)
87 *parent_child = (void *)((size_t)*parent_child | 1);
90 static struct children *ignore_destroying_bit(struct children *parent_child)
92 return (void *)((size_t)parent_child & ~(size_t)1);
95 static bool initialized = false;
97 /* This means valgrind can see leaks. */
98 static void tal_cleanup(void)
102 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
105 /* Cleanup any taken pointers. */
109 /* For allocation failures inside ccan/take */
110 static void take_alloc_failed(const void *p)
115 /* We carefully start all real properties with a zero byte. */
116 static bool is_literal(const struct prop_hdr *prop)
118 return ((char *)prop)[0] != 0;
122 static const void *bounds_start, *bounds_end;
124 static void update_bounds(const void *new, size_t size)
126 if (unlikely(!bounds_start)) {
128 bounds_end = (char *)new + size;
129 } else if (new < bounds_start)
131 else if ((char *)new + size > (char *)bounds_end)
132 bounds_end = (char *)new + size;
135 static bool in_bounds(const void *p)
138 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
139 || (p >= bounds_start && p <= bounds_end);
142 static void update_bounds(const void *new, size_t size)
146 static bool in_bounds(const void *p)
152 static void check_bounds(const void *p)
155 call_error("Not a valid header");
158 static struct tal_hdr *to_tal_hdr(const void *ctx)
162 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
164 check_bounds(ignore_destroying_bit(t->parent_child));
165 check_bounds(t->list.next);
166 check_bounds(t->list.prev);
167 if (t->prop && !is_literal(t->prop))
168 check_bounds(t->prop);
172 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
175 return &null_parent.hdr;
176 return to_tal_hdr(ctx);
179 static void *from_tal_hdr(const struct tal_hdr *hdr)
181 return (void *)(hdr + 1);
185 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
187 if (hdr == &null_parent.hdr)
189 return from_tal_hdr(hdr);
192 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
194 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
198 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
204 static void notify(const struct tal_hdr *ctx,
205 enum tal_notify_type type, const void *info)
207 const struct prop_hdr *p;
209 for (p = ctx->prop; p; p = p->next) {
214 if (p->type != NOTIFIER)
216 n = (struct notifier *)p;
218 n->notifyfn(from_tal_hdr(ctx), type, (void *)info);
222 static void *allocate(size_t size)
226 /* Don't hand silly sizes to malloc. */
227 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
228 call_error("allocation size overflow");
234 call_error("allocation failed");
236 update_bounds(ret, size);
240 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
245 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
246 if (is_literal(*p)) {
251 if ((*p)->type == type)
257 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
259 struct prop_hdr **p = find_property_ptr(parent, type);
266 static void init_property(struct prop_hdr *hdr,
267 struct tal_hdr *parent,
271 hdr->next = parent->prop;
275 static struct destructor *add_destructor_property(struct tal_hdr *t,
276 void (*destroy)(void *))
278 struct destructor *prop = allocate(sizeof(*prop));
280 init_property(&prop->hdr, t, DESTRUCTOR);
281 prop->destroy = destroy;
286 static bool del_destructor_property(struct tal_hdr *t,
287 void (*destroy)(void *))
291 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
292 struct destructor *d;
296 if ((*p)->type != DESTRUCTOR)
298 d = (struct destructor *)*p;
299 if (d->destroy == destroy) {
308 static struct notifier *add_notifier_property(struct tal_hdr *t,
309 enum tal_notify_type types,
311 enum tal_notify_type,
314 struct notifier *prop = allocate(sizeof(*prop));
316 init_property(&prop->hdr, t, NOTIFIER);
323 static bool del_notifier_property(struct tal_hdr *t,
325 enum tal_notify_type, void *))
329 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
334 if ((*p)->type != NOTIFIER)
336 n = (struct notifier *)*p;
337 if (n->notifyfn == fn) {
346 static struct name *add_name_property(struct tal_hdr *t, const char *name)
350 prop = allocate(sizeof(*prop) + strlen(name) + 1);
352 init_property(&prop->hdr, t, NAME);
353 strcpy(prop->name, name);
358 static struct children *add_child_property(struct tal_hdr *parent,
359 struct tal_hdr *child)
361 struct children *prop = allocate(sizeof(*prop));
363 init_property(&prop->hdr, parent, CHILDREN);
364 prop->parent = parent;
365 list_head_init(&prop->children);
370 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
372 struct children *children = find_property(parent, CHILDREN);
375 if (unlikely(!initialized)) {
377 take_allocfail(take_alloc_failed);
380 children = add_child_property(parent, child);
384 list_add(&children->children, &child->list);
385 child->parent_child = children;
389 static void del_tree(struct tal_hdr *t, const tal_t *orig)
391 struct prop_hdr **prop, *p, *next;
393 /* Already being destroyed? Don't loop. */
394 if (unlikely(get_destroying_bit(t->parent_child)))
397 set_destroying_bit(&t->parent_child);
399 /* Carefully call destructors, removing as we go. */
400 while ((prop = find_property_ptr(t, DESTRUCTOR))) {
401 struct destructor *d = (struct destructor *)*prop;
402 d->destroy(from_tal_hdr(t));
407 /* Call free notifiers. */
408 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
410 /* Now free children and groups. */
411 prop = find_property_ptr(t, CHILDREN);
414 struct children *c = (struct children *)*prop;
416 while ((i = list_top(&c->children, struct tal_hdr, list))) {
422 /* Finally free our properties. */
423 for (p = t->prop; p && !is_literal(p); p = next) {
430 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
432 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
434 child = allocate(sizeof(struct tal_hdr) + size);
438 memset(from_tal_hdr(child), 0, size);
439 child->prop = (void *)label;
440 if (!add_child(parent, child)) {
445 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(debug_tal(child)));
446 return from_tal_hdr(debug_tal(child));
449 void *tal_free(const tal_t *ctx)
453 int saved_errno = errno;
454 t = debug_tal(to_tal_hdr(ctx));
455 notify(ignore_destroying_bit(t->parent_child)->parent,
456 TAL_NOTIFY_DEL_CHILD, ctx);
464 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
467 struct tal_hdr *newpar, *t, *old_parent;
469 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
470 t = debug_tal(to_tal_hdr(ctx));
472 /* Unlink it from old parent. */
474 old_parent = ignore_destroying_bit(t->parent_child)->parent;
476 if (unlikely(!add_child(newpar, t))) {
477 /* We can always add to old parent, becuase it has a
478 * children property already. */
479 if (!add_child(old_parent, t))
484 notify(t, TAL_NOTIFY_STEAL, new_parent);
489 bool tal_add_destructor_(tal_t *ctx, void (*destroy)(void *me))
491 tal_t *t = debug_tal(to_tal_hdr(ctx));
492 return add_destructor_property(t, destroy);
495 bool tal_add_notifier_(tal_t *ctx, enum tal_notify_type types,
496 void (*callback)(tal_t *, enum tal_notify_type, void *))
498 tal_t *t = debug_tal(to_tal_hdr(ctx));
502 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
503 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
504 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
505 | TAL_NOTIFY_ADD_NOTIFIER
506 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
508 /* Don't call notifier about itself: set types after! */
509 n = add_notifier_property(t, 0, callback);
513 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
518 bool tal_del_notifier_(tal_t *ctx,
519 void (*callback)(tal_t *, enum tal_notify_type, void *))
521 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
524 ret = del_notifier_property(t, callback);
526 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
530 bool tal_del_destructor_(tal_t *ctx, void (*destroy)(void *me))
532 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
533 return del_destructor_property(t, destroy);
536 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
538 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
539 struct prop_hdr **prop = find_property_ptr(t, NAME);
541 /* Get rid of any old name */
543 struct name *name = (struct name *)*prop;
544 if (is_literal(&name->hdr))
547 *prop = name->hdr.next;
552 if (literal && name[0]) {
555 /* Append literal. */
556 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
557 *p = (struct prop_hdr *)name;
558 } else if (!add_name_property(t, name))
562 notify(t, TAL_NOTIFY_RENAME, name);
566 const char *tal_name(const tal_t *t)
570 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
574 if (is_literal(&n->hdr))
575 return (const char *)n;
579 /* Start one past first child: make stopping natural in circ. list. */
580 static struct tal_hdr *first_child(struct tal_hdr *parent)
582 struct children *child;
584 child = find_property(parent, CHILDREN);
588 return list_top(&child->children, struct tal_hdr, list);
591 tal_t *tal_first(const tal_t *root)
593 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
598 return from_tal_hdr(c);
601 tal_t *tal_next(const tal_t *root, const tal_t *prev)
603 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
608 return from_tal_hdr(c);
610 top = to_tal_hdr_or_null(root);
612 struct tal_hdr *next;
613 struct list_node *end;
615 end = &ignore_destroying_bit(t->parent_child)->children.n;
617 next = list_entry(t->list.next, struct tal_hdr, list);
618 if (&next->list != end)
619 return from_tal_hdr(next);
621 /* OK, go back to parent. */
622 t = ignore_destroying_bit(t->parent_child)->parent;
628 tal_t *tal_parent(const tal_t *ctx)
635 t = debug_tal(to_tal_hdr(ctx));
636 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
638 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
641 bool tal_resize_(tal_t **ctxp, size_t size)
643 struct tal_hdr *old_t, *t;
644 struct children *child;
646 old_t = debug_tal(to_tal_hdr(*ctxp));
648 /* Don't hand silly sizes to realloc. */
649 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
650 call_error("Reallocation size overflow");
654 t = resizefn(old_t, size + sizeof(struct tal_hdr));
656 call_error("Reallocation failure");
660 /* If it didn't move, we're done! */
662 update_bounds(t, size + sizeof(struct tal_hdr));
664 /* Fix up linked list pointers. */
665 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
666 t->list.next->prev = t->list.prev->next = &t->list;
668 /* Fix up child property's parent pointer. */
669 child = find_property(t, CHILDREN);
671 assert(child->parent == old_t);
674 *ctxp = from_tal_hdr(debug_tal(t));
675 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
677 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
682 char *tal_strdup(const tal_t *ctx, const char *p)
684 /* We have to let through NULL for take(). */
685 return tal_dup(ctx, char, p, p ? strlen(p) + 1: 1, 0);
688 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
693 /* We have to let through NULL for take(). */
701 ret = tal_dup(ctx, char, p, len, 1);
707 void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
712 /* Beware overflow! */
713 if (n + extra < n || n + extra + sizeof(struct tal_hdr) < n) {
714 call_error("dup size overflow");
723 if (unlikely(!tal_resize_((void **)&p, n + extra)))
725 if (unlikely(!tal_steal(ctx, p)))
729 ret = tal_alloc_(ctx, n + extra, false, label);
735 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
741 ret = tal_vasprintf(ctx, fmt, ap);
747 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
753 if (!fmt && taken(fmt))
756 /* A decent guess to start. */
757 max = strlen(fmt) * 2;
758 buf = tal_arr(ctx, char, max);
763 ret = vsnprintf(buf, max, fmt, ap2);
768 if (!tal_resize(&buf, max *= 2))
776 void tal_set_backend(void *(*alloc_fn)(size_t size),
777 void *(*resize_fn)(void *, size_t size),
778 void (*free_fn)(void *),
779 void (*error_fn)(const char *msg))
784 resizefn = resize_fn;
791 #ifdef CCAN_TAL_DEBUG
792 static void dump_node(unsigned int indent, const struct tal_hdr *t)
795 const struct prop_hdr *p;
797 for (i = 0; i < indent; i++)
800 for (p = t->prop; p; p = p->next) {
802 struct destructor *d;
806 printf(" \"%s\"", (const char *)p);
811 c = (struct children *)p;
812 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
814 c->children.n.prev, c->children.n.next);
817 d = (struct destructor *)p;
818 printf(" DESTRUCTOR(%p):fn=%p", p, d->destroy);
821 n = (struct name *)p;
822 printf(" NAME(%p):%s", p, n->name);
825 no = (struct notifier *)p;
826 printf(" NOTIFIER(%p):fn=%p", p, no->notifyfn);
829 printf(" **UNKNOWN(%p):%i**", p, p->type);
835 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
837 struct children *children;
841 children = find_property(t, CHILDREN);
845 list_for_each(&children->children, i, list)
846 tal_dump_(level + 1, i);
852 tal_dump_(0, &null_parent.hdr);
854 #endif /* CCAN_TAL_DEBUG */
857 static bool check_err(struct tal_hdr *t, const char *errorstr,
861 /* Try not to malloc: it may be corrupted. */
862 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
863 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
869 static bool check_node(struct children *parent_child,
870 struct tal_hdr *t, const char *errorstr)
873 struct name *name = NULL;
874 struct children *children = NULL;
877 return check_err(t, errorstr, "invalid pointer");
879 if (ignore_destroying_bit(t->parent_child) != parent_child)
880 return check_err(t, errorstr, "incorrect parent");
882 for (p = t->prop; p; p = p->next) {
885 return check_err(t, errorstr,
886 "has extra literal");
887 name = (struct name *)p;
891 return check_err(t, errorstr,
892 "has bad property pointer");
897 return check_err(t, errorstr,
898 "has two child nodes");
899 children = (struct children *)p;
906 return check_err(t, errorstr,
908 name = (struct name *)p;
911 return check_err(t, errorstr, "has unknown property");
917 if (!list_check(&children->children, errorstr))
919 list_for_each(&children->children, i, list) {
920 if (!check_node(children, i, errorstr))
927 bool tal_check(const tal_t *ctx, const char *errorstr)
929 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
931 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
934 bool tal_check(const tal_t *ctx, const char *errorstr)