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,
31 struct list_node list;
32 struct prop_hdr *prop;
33 /* XOR with TAL_PTR_OBFUSTICATOR */
34 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; /* LENGTH */
59 struct prop_hdr hdr; /* NOTIFIER */
60 enum tal_notify_type types;
62 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
63 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
64 void (*destroy2)(tal_t *, void *); /* If NOTIFY_EXTRA_ARG */
69 struct notifier_extra_arg {
74 #define EXTRA_ARG(n) (((struct notifier_extra_arg *)(n))->arg)
79 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
80 &null_parent.c.hdr, TAL_PTR_OBFUSTICATOR },
83 { { &null_parent.c.children.n,
84 &null_parent.c.children.n } }
89 static void *(*allocfn)(size_t size) = malloc;
90 static void *(*resizefn)(void *, size_t size) = realloc;
91 static void (*freefn)(void *) = free;
92 static void (*errorfn)(const char *msg) = (void *)abort;
93 /* Count on non-destrutor notifiers; often stays zero. */
94 static size_t notifiers = 0;
96 static inline void COLD call_error(const char *msg)
101 static bool get_destroying_bit(intptr_t parent_child)
103 return parent_child & 1;
106 static void set_destroying_bit(intptr_t *parent_child)
111 static struct children *ignore_destroying_bit(intptr_t parent_child)
113 return (void *)((parent_child ^ TAL_PTR_OBFUSTICATOR) & ~(intptr_t)1);
116 /* This means valgrind can see leaks. */
117 void tal_cleanup(void)
121 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
123 memset(i, 0, sizeof(*i));
126 /* Cleanup any taken pointers. */
130 /* We carefully start all real properties with a zero byte. */
131 static bool is_literal(const struct prop_hdr *prop)
133 return ((char *)prop)[0] != 0;
137 static const void *bounds_start, *bounds_end;
139 static void update_bounds(const void *new, size_t size)
141 if (unlikely(!bounds_start)) {
143 bounds_end = (char *)new + size;
144 } else if (new < bounds_start)
146 else if ((char *)new + size > (char *)bounds_end)
147 bounds_end = (char *)new + size;
150 static bool in_bounds(const void *p)
153 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
154 || (p >= bounds_start && p <= bounds_end);
157 static void update_bounds(const void *new, size_t size)
161 static bool in_bounds(const void *p)
167 static void check_bounds(const void *p)
170 call_error("Not a valid header");
173 static struct tal_hdr *to_tal_hdr(const void *ctx)
177 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
179 check_bounds(ignore_destroying_bit(t->parent_child));
180 check_bounds(t->list.next);
181 check_bounds(t->list.prev);
182 if (t->prop && !is_literal(t->prop))
183 check_bounds(t->prop);
187 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
190 return &null_parent.hdr;
191 return to_tal_hdr(ctx);
194 static void *from_tal_hdr(const struct tal_hdr *hdr)
196 return (void *)(hdr + 1);
200 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
202 if (hdr == &null_parent.hdr)
204 return from_tal_hdr(hdr);
207 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
209 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
213 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
219 static void notify(const struct tal_hdr *ctx,
220 enum tal_notify_type type, const void *info,
223 const struct prop_hdr *p;
225 for (p = ctx->prop; p; p = p->next) {
230 if (p->type != NOTIFIER)
232 n = (struct notifier *)p;
233 if (n->types & type) {
235 if (n->types & NOTIFY_IS_DESTRUCTOR) {
236 if (n->types & NOTIFY_EXTRA_ARG)
237 n->u.destroy2(from_tal_hdr(ctx),
240 n->u.destroy(from_tal_hdr(ctx));
242 n->u.notifyfn(from_tal_hdr(ctx), type,
248 static void *allocate(size_t size)
250 void *ret = allocfn(size);
252 call_error("allocation failed");
254 update_bounds(ret, size);
258 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
263 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
264 if (is_literal(*p)) {
269 if ((*p)->type == type)
275 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
277 struct prop_hdr **p = find_property_ptr(parent, type);
284 static void init_property(struct prop_hdr *hdr,
285 struct tal_hdr *parent,
289 hdr->next = parent->prop;
293 static struct notifier *add_notifier_property(struct tal_hdr *t,
294 enum tal_notify_type types,
296 enum tal_notify_type,
300 struct notifier *prop;
302 if (types & NOTIFY_EXTRA_ARG)
303 prop = allocate(sizeof(struct notifier_extra_arg));
305 prop = allocate(sizeof(struct notifier));
308 init_property(&prop->hdr, t, NOTIFIER);
310 prop->u.notifyfn = fn;
311 if (types & NOTIFY_EXTRA_ARG)
312 EXTRA_ARG(prop) = extra_arg;
317 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
319 enum tal_notify_type,
321 bool match_extra_arg,
326 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
328 enum tal_notify_type types;
332 if ((*p)->type != NOTIFIER)
334 n = (struct notifier *)*p;
335 if (n->u.notifyfn != fn)
339 if ((types & NOTIFY_EXTRA_ARG)
341 && extra_arg != EXTRA_ARG(n))
346 return types & ~(NOTIFY_IS_DESTRUCTOR|NOTIFY_EXTRA_ARG);
351 static struct name *add_name_property(struct tal_hdr *t, const char *name)
355 prop = allocate(sizeof(*prop) + strlen(name) + 1);
357 init_property(&prop->hdr, t, NAME);
358 strcpy(prop->name, name);
363 static struct children *add_child_property(struct tal_hdr *parent,
364 struct tal_hdr *child UNNEEDED)
366 struct children *prop = allocate(sizeof(*prop));
368 init_property(&prop->hdr, parent, CHILDREN);
369 prop->parent = parent;
370 list_head_init(&prop->children);
375 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
377 struct children *children = find_property(parent, CHILDREN);
380 children = add_child_property(parent, child);
384 list_add(&children->children, &child->list);
385 child->parent_child = (intptr_t)children ^ TAL_PTR_OBFUSTICATOR;
389 static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
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 /* Call free notifiers. */
400 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig, saved_errno);
402 /* Now free children and groups. */
403 prop = find_property_ptr(t, CHILDREN);
406 struct children *c = (struct children *)*prop;
408 while ((i = list_top(&c->children, struct tal_hdr, list))) {
410 del_tree(i, orig, saved_errno);
414 /* Finally free our properties. */
415 for (p = t->prop; p && !is_literal(p); p = next) {
417 /* LENGTH is appended, so don't free separately! */
418 if (p->type != LENGTH)
424 static size_t extra_for_length(size_t size)
427 const size_t align = ALIGNOF(struct length);
429 /* Round up size, and add tailer. */
430 extra = ((size + align-1) & ~(align-1)) - size;
431 extra += sizeof(struct length);
435 void *tal_alloc_(const tal_t *ctx, size_t size,
436 bool clear, bool add_length, const char *label)
438 size_t req_size = size;
439 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
441 #ifdef CCAN_TAL_DEBUG
442 /* Always record length if debugging. */
446 size += extra_for_length(size);
448 child = allocate(sizeof(struct tal_hdr) + size);
452 memset(from_tal_hdr(child), 0, req_size);
453 child->prop = (void *)label;
456 struct length *lprop;
457 lprop = (struct length *)((char *)(child+1) + size) - 1;
458 init_property(&lprop->hdr, child, LENGTH);
459 lprop->len = req_size;
461 if (!add_child(parent, child)) {
467 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child), 0);
468 return from_tal_hdr(debug_tal(child));
471 static bool adjust_size(size_t *size, size_t count)
473 const size_t extra = sizeof(struct tal_hdr) + sizeof(struct length)*2;
475 /* Multiplication wrap */
476 if (count && unlikely(*size * count / *size != count))
481 /* Make sure we don't wrap adding header/tailer. */
482 if (*size + extra < extra)
486 call_error("allocation size overflow");
490 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
491 bool add_length, const char *label)
493 if (!adjust_size(&size, count))
496 return tal_alloc_(ctx, size, clear, add_length, label);
499 void *tal_free(const tal_t *ctx)
503 int saved_errno = errno;
504 t = debug_tal(to_tal_hdr(ctx));
506 notify(ignore_destroying_bit(t->parent_child)->parent,
507 TAL_NOTIFY_DEL_CHILD, ctx, saved_errno);
509 del_tree(t, ctx, saved_errno);
515 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
518 struct tal_hdr *newpar, *t, *old_parent;
520 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
521 t = debug_tal(to_tal_hdr(ctx));
523 /* Unlink it from old parent. */
525 old_parent = ignore_destroying_bit(t->parent_child)->parent;
527 if (unlikely(!add_child(newpar, t))) {
528 /* We can always add to old parent, becuase it has a
529 * children property already. */
530 if (!add_child(old_parent, t))
536 notify(t, TAL_NOTIFY_STEAL, new_parent, 0);
541 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
543 tal_t *t = debug_tal(to_tal_hdr(ctx));
544 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
545 (void *)destroy, NULL);
548 bool tal_add_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
551 tal_t *t = debug_tal(to_tal_hdr(ctx));
552 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR
554 (void *)destroy, arg);
557 /* We could support notifiers with an extra arg, but we didn't add to API */
558 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
559 void (*callback)(tal_t *, enum tal_notify_type, void *))
561 tal_t *t = debug_tal(to_tal_hdr(ctx));
565 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
566 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
567 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
568 | TAL_NOTIFY_ADD_NOTIFIER
569 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
571 /* Don't call notifier about itself: set types after! */
572 n = add_notifier_property(t, 0, callback, NULL);
577 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback, 0);
580 if (types != TAL_NOTIFY_FREE)
585 bool tal_del_notifier_(const tal_t *ctx,
586 void (*callback)(tal_t *, enum tal_notify_type, void *),
587 bool match_extra_arg, void *extra_arg)
589 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
590 enum tal_notify_type types;
592 types = del_notifier_property(t, callback, match_extra_arg, extra_arg);
594 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback, 0);
595 if (types != TAL_NOTIFY_FREE)
602 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
604 return tal_del_notifier_(ctx, (void *)destroy, false, NULL);
607 bool tal_del_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
610 return tal_del_notifier_(ctx, (void *)destroy, true, arg);
613 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
615 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
616 struct prop_hdr **prop = find_property_ptr(t, NAME);
618 /* Get rid of any old name */
620 struct name *name = (struct name *)*prop;
621 if (is_literal(&name->hdr))
624 *prop = name->hdr.next;
629 if (literal && name[0]) {
632 /* Append literal. */
633 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
634 *p = (struct prop_hdr *)name;
635 } else if (!add_name_property(t, name))
640 notify(t, TAL_NOTIFY_RENAME, name, 0);
644 const char *tal_name(const tal_t *t)
648 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
652 if (is_literal(&n->hdr))
653 return (const char *)n;
657 size_t tal_len(const tal_t *ptr)
664 l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
670 /* Start one past first child: make stopping natural in circ. list. */
671 static struct tal_hdr *first_child(struct tal_hdr *parent)
673 struct children *child;
675 child = find_property(parent, CHILDREN);
679 return list_top(&child->children, struct tal_hdr, list);
682 tal_t *tal_first(const tal_t *root)
684 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
689 return from_tal_hdr(c);
692 tal_t *tal_next(const tal_t *prev)
694 struct tal_hdr *next, *prevhdr = debug_tal(to_tal_hdr(prev));
695 struct list_head *head;
697 head = &ignore_destroying_bit(prevhdr->parent_child)->children;
698 next = list_next(head, prevhdr, list);
701 return from_tal_hdr(next);
704 tal_t *tal_parent(const tal_t *ctx)
711 t = debug_tal(to_tal_hdr(ctx));
712 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
714 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
717 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
719 struct tal_hdr *old_t, *t;
720 struct children *child;
721 struct prop_hdr **lenp;
725 old_t = debug_tal(to_tal_hdr(*ctxp));
727 if (!adjust_size(&size, count))
730 lenp = find_property_ptr(old_t, LENGTH);
732 /* Copy here, in case we're shrinking! */
733 len = *(struct length *)*lenp;
734 extra = extra_for_length(size);
735 } else /* If we don't have an old length, we can't clear! */
738 t = resizefn(old_t, sizeof(struct tal_hdr) + size + extra);
740 call_error("Reallocation failure");
744 /* Copy length to end. */
746 struct length *new_len;
748 /* Clear between old end and new end. */
749 if (clear && size > len.len) {
750 char *old_end = (char *)(t + 1) + len.len;
751 memset(old_end, 0, size - len.len);
754 new_len = (struct length *)((char *)(t + 1) + size
755 + extra - sizeof(len));
759 /* Be careful replacing next ptr; could be old hdr. */
760 if (lenp == &old_t->prop)
761 t->prop = &new_len->hdr;
763 *lenp = &new_len->hdr;
766 update_bounds(t, sizeof(struct tal_hdr) + size + extra);
768 /* If it didn't move, we're done! */
770 /* Fix up linked list pointers. */
771 t->list.next->prev = t->list.prev->next = &t->list;
773 /* Fix up child property's parent pointer. */
774 child = find_property(t, CHILDREN);
776 assert(child->parent == old_t);
779 *ctxp = from_tal_hdr(debug_tal(t));
781 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t), 0);
784 notify(t, TAL_NOTIFY_RESIZE, (void *)size, 0);
789 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
795 l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
798 /* Check for additive overflow */
799 if (old_len + count * size < old_len) {
800 call_error("dup size overflow");
804 /* Don't point src inside thing we're expanding! */
806 || (char *)src >= (char *)(*ctxp) + old_len);
808 if (!tal_resize_(ctxp, size, old_len/size + count, false))
811 memcpy((char *)*ctxp + old_len, src, count * size);
820 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
821 size_t n, size_t extra, bool add_length,
825 size_t nbytes = size;
827 if (!adjust_size(&nbytes, n)) {
833 /* Beware addition overflow! */
835 call_error("dup size overflow");
844 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
846 if (unlikely(!tal_steal(ctx, p)))
851 ret = tal_alloc_arr_(ctx, size, n + extra, false, add_length, label);
853 memcpy(ret, p, nbytes);
857 void tal_set_backend(void *(*alloc_fn)(size_t size),
858 void *(*resize_fn)(void *, size_t size),
859 void (*free_fn)(void *),
860 void (*error_fn)(const char *msg))
865 resizefn = resize_fn;
872 #ifdef CCAN_TAL_DEBUG
873 static void dump_node(unsigned int indent, const struct tal_hdr *t)
876 const struct prop_hdr *p;
878 for (i = 0; i < indent; i++)
881 for (p = t->prop; p; p = p->next) {
887 printf(" \"%s\"", (const char *)p);
892 c = (struct children *)p;
893 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
895 c->children.n.prev, c->children.n.next);
898 n = (struct name *)p;
899 printf(" NAME(%p):%s", p, n->name);
902 no = (struct notifier *)p;
903 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
906 l = (struct length *)p;
907 printf(" LENGTH(%p):len=%zu", p, l->len);
910 printf(" **UNKNOWN(%p):%i**", p, p->type);
916 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
918 struct children *children;
922 children = find_property(t, CHILDREN);
926 list_for_each(&children->children, i, list)
927 tal_dump_(level + 1, i);
933 tal_dump_(0, &null_parent.hdr);
935 #endif /* CCAN_TAL_DEBUG */
938 static bool check_err(struct tal_hdr *t, const char *errorstr,
942 /* Try not to malloc: it may be corrupted. */
943 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
944 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
950 static bool check_node(struct children *parent_child,
951 struct tal_hdr *t, const char *errorstr)
954 struct name *name = NULL;
955 struct children *children = NULL;
956 struct length *length = NULL;
959 return check_err(t, errorstr, "invalid pointer");
961 if (ignore_destroying_bit(t->parent_child) != parent_child)
962 return check_err(t, errorstr, "incorrect parent");
964 for (p = t->prop; p; p = p->next) {
967 return check_err(t, errorstr,
968 "has extra literal");
972 return check_err(t, errorstr,
973 "has bad property pointer");
978 return check_err(t, errorstr,
979 "has two child nodes");
980 children = (struct children *)p;
984 return check_err(t, errorstr,
986 length = (struct length *)p;
992 return check_err(t, errorstr,
994 name = (struct name *)p;
997 return check_err(t, errorstr, "has unknown property");
1003 if (!list_check(&children->children, errorstr))
1005 list_for_each(&children->children, i, list) {
1006 if (!check_node(children, i, errorstr))
1013 bool tal_check(const tal_t *ctx, const char *errorstr)
1015 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
1017 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
1020 bool tal_check(const tal_t *ctx, const char *errorstr)