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/alignof/alignof.h>
16 #define NOTIFY_IS_DESTRUCTOR 512
17 #define NOTIFY_EXTRA_ARG 1024
19 /* This makes our parent_child ptr stand out for to_tal_hdr checks */
20 #define TAL_PTR_OBFUSTICATOR ((intptr_t)0x1984200820142016ULL)
22 /* 32-bit type field, first byte 0 in either endianness. */
24 CHILDREN = 0x00c1d500,
26 NOTIFIER = 0x00071f00,
30 struct list_node list;
31 struct prop_hdr *prop;
32 /* XOR with TAL_PTR_OBFUSTICATOR */
33 intptr_t parent_child;
39 struct prop_hdr *next;
43 struct prop_hdr hdr; /* CHILDREN */
44 struct tal_hdr *parent;
45 struct list_head children; /* Head of siblings. */
49 struct prop_hdr hdr; /* NAME */
54 struct prop_hdr hdr; /* NOTIFIER */
55 enum tal_notify_type types;
57 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
58 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
59 void (*destroy2)(tal_t *, void *); /* If NOTIFY_EXTRA_ARG */
64 struct notifier_extra_arg {
69 #define EXTRA_ARG(n) (((struct notifier_extra_arg *)(n))->arg)
74 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
75 &null_parent.c.hdr, TAL_PTR_OBFUSTICATOR, 0 },
78 { { &null_parent.c.children.n,
79 &null_parent.c.children.n } }
84 static void *(*allocfn)(size_t size) = malloc;
85 static void *(*resizefn)(void *, size_t size) = realloc;
86 static void (*freefn)(void *) = free;
87 static void (*errorfn)(const char *msg) = (void *)abort;
88 /* Count on non-destrutor notifiers; often stays zero. */
89 static size_t notifiers = 0;
91 static inline void COLD call_error(const char *msg)
96 static bool get_destroying_bit(intptr_t parent_child)
98 return parent_child & 1;
101 static void set_destroying_bit(intptr_t *parent_child)
106 static struct children *ignore_destroying_bit(intptr_t parent_child)
108 return (void *)((parent_child ^ TAL_PTR_OBFUSTICATOR) & ~(intptr_t)1);
111 /* This means valgrind can see leaks. */
112 void tal_cleanup(void)
116 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
118 memset(i, 0, sizeof(*i));
121 /* Cleanup any taken pointers. */
125 /* We carefully start all real properties with a zero byte. */
126 static bool is_literal(const struct prop_hdr *prop)
128 return ((char *)prop)[0] != 0;
132 static const void *bounds_start, *bounds_end;
134 static void update_bounds(const void *new, size_t size)
136 if (unlikely(!bounds_start)) {
138 bounds_end = (char *)new + size;
139 } else if (new < bounds_start)
141 else if ((char *)new + size > (char *)bounds_end)
142 bounds_end = (char *)new + size;
145 static bool in_bounds(const void *p)
148 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
149 || (p >= bounds_start && p <= bounds_end);
152 static void update_bounds(const void *new, size_t size)
156 static bool in_bounds(const void *p)
162 static void check_bounds(const void *p)
165 call_error("Not a valid header");
168 static struct tal_hdr *to_tal_hdr(const void *ctx)
172 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
174 check_bounds(ignore_destroying_bit(t->parent_child));
175 check_bounds(t->list.next);
176 check_bounds(t->list.prev);
177 if (t->prop && !is_literal(t->prop))
178 check_bounds(t->prop);
182 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
185 return &null_parent.hdr;
186 return to_tal_hdr(ctx);
189 static void *from_tal_hdr(const struct tal_hdr *hdr)
191 return (void *)(hdr + 1);
194 static void *from_tal_hdr_or_null(const struct tal_hdr *hdr)
196 if (hdr == &null_parent.hdr)
198 return from_tal_hdr(hdr);
202 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
204 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
208 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
214 static void notify(const struct tal_hdr *ctx,
215 enum tal_notify_type type, const void *info,
218 const struct prop_hdr *p;
220 for (p = ctx->prop; p; p = p->next) {
225 if (p->type != NOTIFIER)
227 n = (struct notifier *)p;
228 if (n->types & type) {
230 if (n->types & NOTIFY_IS_DESTRUCTOR) {
231 if (n->types & NOTIFY_EXTRA_ARG)
232 n->u.destroy2(from_tal_hdr(ctx),
235 n->u.destroy(from_tal_hdr(ctx));
237 n->u.notifyfn(from_tal_hdr_or_null(ctx), type,
243 static void *allocate(size_t size)
245 void *ret = allocfn(size);
247 call_error("allocation failed");
249 update_bounds(ret, size);
253 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
258 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
259 if (is_literal(*p)) {
264 if ((*p)->type == type)
270 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
272 struct prop_hdr **p = find_property_ptr(parent, type);
279 static void init_property(struct prop_hdr *hdr,
280 struct tal_hdr *parent,
284 hdr->next = parent->prop;
288 static struct notifier *add_notifier_property(struct tal_hdr *t,
289 enum tal_notify_type types,
291 enum tal_notify_type,
295 struct notifier *prop;
297 if (types & NOTIFY_EXTRA_ARG)
298 prop = allocate(sizeof(struct notifier_extra_arg));
300 prop = allocate(sizeof(struct notifier));
303 init_property(&prop->hdr, t, NOTIFIER);
305 prop->u.notifyfn = fn;
306 if (types & NOTIFY_EXTRA_ARG)
307 EXTRA_ARG(prop) = extra_arg;
312 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
314 enum tal_notify_type,
316 bool match_extra_arg,
321 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
323 enum tal_notify_type types;
327 if ((*p)->type != NOTIFIER)
329 n = (struct notifier *)*p;
330 if (n->u.notifyfn != fn)
334 if ((types & NOTIFY_EXTRA_ARG)
336 && extra_arg != EXTRA_ARG(n))
341 return types & ~(NOTIFY_IS_DESTRUCTOR|NOTIFY_EXTRA_ARG);
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 UNNEEDED)
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 children = add_child_property(parent, child);
379 list_add(&children->children, &child->list);
380 child->parent_child = (intptr_t)children ^ TAL_PTR_OBFUSTICATOR;
384 static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
386 struct prop_hdr **prop, *p, *next;
388 /* Already being destroyed? Don't loop. */
389 if (unlikely(get_destroying_bit(t->parent_child)))
392 set_destroying_bit(&t->parent_child);
394 /* Call free notifiers. */
395 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig, saved_errno);
397 /* Now free children and groups. */
398 prop = find_property_ptr(t, CHILDREN);
401 struct children *c = (struct children *)*prop;
403 while ((i = list_top(&c->children, struct tal_hdr, list))) {
405 del_tree(i, orig, saved_errno);
409 /* Finally free our properties. */
410 for (p = t->prop; p && !is_literal(p); p = next) {
417 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
419 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
421 child = allocate(sizeof(struct tal_hdr) + size);
425 memset(from_tal_hdr(child), 0, size);
426 child->prop = (void *)label;
427 child->bytelen = size;
429 if (!add_child(parent, child)) {
435 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child), 0);
436 return from_tal_hdr(debug_tal(child));
439 static bool adjust_size(size_t *size, size_t count)
441 const size_t extra = sizeof(struct tal_hdr);
443 /* Multiplication wrap */
444 if (count && unlikely(*size * count / *size != count))
449 /* Make sure we don't wrap adding header. */
450 if (*size + extra < extra)
454 call_error("allocation size overflow");
458 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
461 if (!adjust_size(&size, count))
464 return tal_alloc_(ctx, size, clear, label);
467 void *tal_free(const tal_t *ctx)
471 int saved_errno = errno;
472 t = debug_tal(to_tal_hdr(ctx));
473 if (unlikely(get_destroying_bit(t->parent_child)))
476 notify(ignore_destroying_bit(t->parent_child)->parent,
477 TAL_NOTIFY_DEL_CHILD, ctx, saved_errno);
479 del_tree(t, ctx, saved_errno);
485 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
488 struct tal_hdr *newpar, *t, *old_parent;
490 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
491 t = debug_tal(to_tal_hdr(ctx));
493 /* Unlink it from old parent. */
495 old_parent = ignore_destroying_bit(t->parent_child)->parent;
497 if (unlikely(!add_child(newpar, t))) {
498 /* We can always add to old parent, because it has a
499 * children property already. */
500 if (!add_child(old_parent, t))
506 notify(t, TAL_NOTIFY_STEAL, new_parent, 0);
511 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
513 tal_t *t = debug_tal(to_tal_hdr(ctx));
514 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
515 (void *)destroy, NULL);
518 bool tal_add_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
521 tal_t *t = debug_tal(to_tal_hdr(ctx));
522 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR
524 (void *)destroy, arg);
527 /* We could support notifiers with an extra arg, but we didn't add to API */
528 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
529 void (*callback)(tal_t *, enum tal_notify_type, void *))
531 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ctx));
535 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
536 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
537 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
538 | TAL_NOTIFY_ADD_NOTIFIER
539 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
541 /* Don't call notifier about itself: set types after! */
542 n = add_notifier_property(t, 0, callback, NULL);
547 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback, 0);
550 if (types != TAL_NOTIFY_FREE)
555 bool tal_del_notifier_(const tal_t *ctx,
556 void (*callback)(tal_t *, enum tal_notify_type, void *),
557 bool match_extra_arg, void *extra_arg)
559 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ctx));
560 enum tal_notify_type types;
562 types = del_notifier_property(t, callback, match_extra_arg, extra_arg);
564 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback, 0);
565 if (types != TAL_NOTIFY_FREE)
572 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
574 return tal_del_notifier_(ctx, (void *)destroy, false, NULL);
577 bool tal_del_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
580 return tal_del_notifier_(ctx, (void *)destroy, true, arg);
583 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
585 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
586 struct prop_hdr **prop = find_property_ptr(t, NAME);
588 /* Get rid of any old name */
590 struct name *name = (struct name *)*prop;
591 if (is_literal(&name->hdr))
594 *prop = name->hdr.next;
599 if (literal && name[0]) {
602 /* Append literal. */
603 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
604 *p = (struct prop_hdr *)name;
605 } else if (!add_name_property(t, name))
610 notify(t, TAL_NOTIFY_RENAME, name, 0);
614 const char *tal_name(const tal_t *t)
618 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
622 if (is_literal(&n->hdr))
623 return (const char *)n;
627 size_t tal_bytelen(const tal_t *ptr)
629 /* NULL -> null_parent which has bytelen 0 */
630 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ptr));
635 /* Start one past first child: make stopping natural in circ. list. */
636 static struct tal_hdr *first_child(struct tal_hdr *parent)
638 struct children *child;
640 child = find_property(parent, CHILDREN);
644 return list_top(&child->children, struct tal_hdr, list);
647 tal_t *tal_first(const tal_t *root)
649 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
654 return from_tal_hdr(c);
657 tal_t *tal_next(const tal_t *prev)
659 struct tal_hdr *next, *prevhdr = debug_tal(to_tal_hdr(prev));
660 struct list_head *head;
662 head = &ignore_destroying_bit(prevhdr->parent_child)->children;
663 next = list_next(head, prevhdr, list);
666 return from_tal_hdr(next);
669 tal_t *tal_parent(const tal_t *ctx)
676 t = debug_tal(to_tal_hdr(ctx));
677 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
679 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
682 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
684 struct tal_hdr *old_t, *t;
685 struct children *child;
687 old_t = debug_tal(to_tal_hdr(*ctxp));
689 if (!adjust_size(&size, count))
692 t = resizefn(old_t, sizeof(struct tal_hdr) + size);
694 call_error("Reallocation failure");
698 /* Clear between old end and new end. */
699 if (clear && size > t->bytelen) {
700 char *old_end = (char *)(t + 1) + t->bytelen;
701 memset(old_end, 0, size - t->bytelen);
706 update_bounds(t, sizeof(struct tal_hdr) + size);
708 /* If it didn't move, we're done! */
710 /* Fix up linked list pointers. */
711 t->list.next->prev = t->list.prev->next = &t->list;
713 /* Fix up child property's parent pointer. */
714 child = find_property(t, CHILDREN);
716 assert(child->parent == old_t);
719 *ctxp = from_tal_hdr(debug_tal(t));
721 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t), 0);
724 notify(t, TAL_NOTIFY_RESIZE, (void *)size, 0);
729 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
734 old_len = debug_tal(to_tal_hdr(*ctxp))->bytelen;
736 /* Check for additive overflow */
737 if (old_len + count * size < old_len) {
738 call_error("dup size overflow");
742 /* Don't point src inside thing we're expanding! */
744 || (char *)src >= (char *)(*ctxp) + old_len);
746 if (!tal_resize_(ctxp, size, old_len/size + count, false))
749 memcpy((char *)*ctxp + old_len, src, count * size);
758 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
759 size_t n, size_t extra, const char *label)
762 size_t nbytes = size;
764 if (!adjust_size(&nbytes, n)) {
770 /* Beware addition overflow! */
772 call_error("dup size overflow");
781 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
783 if (unlikely(!tal_steal(ctx, p)))
788 ret = tal_alloc_arr_(ctx, size, n + extra, false, label);
790 memcpy(ret, p, nbytes);
794 void tal_set_backend(void *(*alloc_fn)(size_t size),
795 void *(*resize_fn)(void *, size_t size),
796 void (*free_fn)(void *),
797 void (*error_fn)(const char *msg))
802 resizefn = resize_fn;
809 #ifdef CCAN_TAL_DEBUG
810 static void dump_node(unsigned int indent, const struct tal_hdr *t)
813 const struct prop_hdr *p;
815 for (i = 0; i < indent; i++)
817 printf("%p len=%zu", t, t->bytelen);
818 for (p = t->prop; p; p = p->next) {
823 printf(" \"%s\"", (const char *)p);
828 c = (struct children *)p;
829 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
831 c->children.n.prev, c->children.n.next);
834 n = (struct name *)p;
835 printf(" NAME(%p):%s", p, n->name);
838 no = (struct notifier *)p;
839 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
842 printf(" **UNKNOWN(%p):%i**", p, p->type);
848 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
850 struct children *children;
854 children = find_property(t, CHILDREN);
858 list_for_each(&children->children, i, list)
859 tal_dump_(level + 1, i);
865 tal_dump_(0, &null_parent.hdr);
867 #endif /* CCAN_TAL_DEBUG */
870 static bool check_err(struct tal_hdr *t, const char *errorstr,
874 /* Try not to malloc: it may be corrupted. */
875 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
876 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
882 static bool check_node(struct children *parent_child,
883 struct tal_hdr *t, const char *errorstr)
886 struct name *name = NULL;
887 struct children *children = NULL;
890 return check_err(t, errorstr, "invalid pointer");
892 if (ignore_destroying_bit(t->parent_child) != parent_child)
893 return check_err(t, errorstr, "incorrect parent");
895 for (p = t->prop; p; p = p->next) {
898 return check_err(t, errorstr,
899 "has extra literal");
903 return check_err(t, errorstr,
904 "has bad property pointer");
909 return check_err(t, errorstr,
910 "has two child nodes");
911 children = (struct children *)p;
917 return check_err(t, errorstr,
919 name = (struct name *)p;
922 return check_err(t, errorstr, "has unknown property");
928 if (!list_check(&children->children, errorstr))
930 list_for_each(&children->children, i, list) {
931 if (!check_node(children, i, errorstr))
938 bool tal_check(const tal_t *ctx, const char *errorstr)
940 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
942 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
945 bool tal_check(const tal_t *ctx, const char *errorstr)