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 /* Use is_prop_hdr tell if this is a struct prop_hdr or string! */
33 /* XOR with TAL_PTR_OBFUSTICATOR */
34 intptr_t parent_child;
40 /* Use is_prop_hdr to tell if this is a struct prop_hdr or string! */
45 struct prop_hdr hdr; /* CHILDREN */
46 struct tal_hdr *parent;
47 struct list_head children; /* Head of siblings. */
51 struct prop_hdr hdr; /* NAME */
56 struct prop_hdr hdr; /* NOTIFIER */
57 enum tal_notify_type types;
59 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
60 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
61 void (*destroy2)(tal_t *, void *); /* If NOTIFY_EXTRA_ARG */
66 struct notifier_extra_arg {
71 #define EXTRA_ARG(n) (((struct notifier_extra_arg *)(n))->arg)
76 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
77 (char *)&null_parent.c.hdr, TAL_PTR_OBFUSTICATOR, 0 },
80 { { &null_parent.c.children.n,
81 &null_parent.c.children.n } }
86 static void *(*allocfn)(size_t size) = malloc;
87 static void *(*resizefn)(void *, size_t size) = realloc;
88 static void (*freefn)(void *) = free;
89 static void (*errorfn)(const char *msg) = (void *)abort;
90 /* Count on non-destrutor notifiers; often stays zero. */
91 static size_t notifiers = 0;
93 static inline void COLD call_error(const char *msg)
98 static bool get_destroying_bit(intptr_t parent_child)
100 return parent_child & 1;
103 static void set_destroying_bit(intptr_t *parent_child)
108 static struct children *ignore_destroying_bit(intptr_t parent_child)
110 return (void *)((parent_child ^ TAL_PTR_OBFUSTICATOR) & ~(intptr_t)1);
113 /* This means valgrind can see leaks. */
114 void tal_cleanup(void)
118 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
120 memset(i, 0, sizeof(*i));
123 /* Cleanup any taken pointers. */
127 /* We carefully start all real properties with a zero byte. */
128 static struct prop_hdr *is_prop_hdr(const char *ptr)
132 return (struct prop_hdr *)ptr;
136 static const void *bounds_start, *bounds_end;
138 static void update_bounds(const void *new, size_t size)
140 if (unlikely(!bounds_start)) {
142 bounds_end = (char *)new + size;
143 } else if (new < bounds_start)
145 else if ((char *)new + size > (char *)bounds_end)
146 bounds_end = (char *)new + size;
149 static bool in_bounds(const void *p)
152 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
153 || (p >= bounds_start && p <= bounds_end);
156 static void update_bounds(const void *new, size_t size)
160 static bool in_bounds(const void *p)
166 static void check_bounds(const void *p)
169 call_error("Not a valid header");
172 static struct tal_hdr *to_tal_hdr(const void *ctx)
176 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
178 check_bounds(ignore_destroying_bit(t->parent_child));
179 check_bounds(t->list.next);
180 check_bounds(t->list.prev);
182 struct prop_hdr *p = is_prop_hdr(t->prop);
189 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
192 return &null_parent.hdr;
193 return to_tal_hdr(ctx);
196 static void *from_tal_hdr(const struct tal_hdr *hdr)
198 return (void *)(hdr + 1);
201 static void *from_tal_hdr_or_null(const struct tal_hdr *hdr)
203 if (hdr == &null_parent.hdr)
205 return from_tal_hdr(hdr);
209 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
211 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
215 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
221 static void notify(const struct tal_hdr *ctx,
222 enum tal_notify_type type, const void *info,
226 const struct prop_hdr *p;
228 for (ptr = ctx->prop; ptr && (p = is_prop_hdr(ptr)) != NULL; ptr = p->next) {
231 if (p->type != NOTIFIER)
233 n = (struct notifier *)p;
234 if (n->types & type) {
236 if (n->types & NOTIFY_IS_DESTRUCTOR) {
237 /* Blatt this notifier in case it tries to
238 * tal_del_destructor() from inside */
239 union notifier_cb cb = n->u;
240 /* It's a union, so this NULLs destroy2 too! */
242 if (n->types & NOTIFY_EXTRA_ARG)
243 cb.destroy2(from_tal_hdr(ctx),
246 cb.destroy(from_tal_hdr(ctx));
248 n->u.notifyfn(from_tal_hdr_or_null(ctx), type,
254 static void *allocate(size_t size)
256 void *ret = allocfn(size);
258 call_error("allocation failed");
260 update_bounds(ret, size);
264 /* Returns a pointer to the pointer: can cast (*ret) to a (struct prop_ptr *) */
265 static char **find_property_ptr(struct tal_hdr *t, enum prop_type type)
270 /* NAME is special, as it can be a literal: see find_name_property */
271 assert(type != NAME);
272 for (ptr = &t->prop; *ptr; ptr = &p->next) {
273 if (!is_prop_hdr(*ptr))
275 p = (struct prop_hdr *)*ptr;
284 * *literal: true - char **, pointer to literal pointer.
285 * *literal: false - struct prop_hdr **, pointer to header ptr.
287 static char **find_name_property(struct tal_hdr *t, bool *literal)
292 for (ptr = &t->prop; *ptr; ptr = &p->next) {
293 if (!is_prop_hdr(*ptr)) {
297 p = (struct prop_hdr *)*ptr;
298 if (p->type == NAME) {
306 static void *find_property(struct tal_hdr *parent, enum prop_type type)
308 char **ptr = find_property_ptr(parent, type);
311 return (struct prop_hdr *)*ptr;
315 static void init_property(struct prop_hdr *hdr,
316 struct tal_hdr *parent,
320 hdr->next = parent->prop;
321 parent->prop = (char *)hdr;
324 static struct notifier *add_notifier_property(struct tal_hdr *t,
325 enum tal_notify_type types,
327 enum tal_notify_type,
331 struct notifier *prop;
333 if (types & NOTIFY_EXTRA_ARG)
334 prop = allocate(sizeof(struct notifier_extra_arg));
336 prop = allocate(sizeof(struct notifier));
339 init_property(&prop->hdr, t, NOTIFIER);
341 prop->u.notifyfn = fn;
342 if (types & NOTIFY_EXTRA_ARG)
343 EXTRA_ARG(prop) = extra_arg;
348 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
350 enum tal_notify_type,
352 bool match_extra_arg,
358 for (ptr = &t->prop; *ptr; ptr = &p->next) {
360 enum tal_notify_type types;
362 p = is_prop_hdr(*ptr);
366 if (p->type != NOTIFIER)
368 n = (struct notifier *)p;
369 if (n->u.notifyfn != fn)
373 if ((types & NOTIFY_EXTRA_ARG)
375 && extra_arg != EXTRA_ARG(n))
380 return types & ~(NOTIFY_IS_DESTRUCTOR|NOTIFY_EXTRA_ARG);
385 static struct name *add_name_property(struct tal_hdr *t, const char *name)
389 prop = allocate(sizeof(*prop) + strlen(name) + 1);
391 init_property(&prop->hdr, t, NAME);
392 strcpy(prop->name, name);
397 static struct children *add_child_property(struct tal_hdr *parent,
398 struct tal_hdr *child UNNEEDED)
400 struct children *prop = allocate(sizeof(*prop));
402 init_property(&prop->hdr, parent, CHILDREN);
403 prop->parent = parent;
404 list_head_init(&prop->children);
409 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
411 struct children *children = find_property(parent, CHILDREN);
414 children = add_child_property(parent, child);
418 list_add(&children->children, &child->list);
419 child->parent_child = (intptr_t)children ^ TAL_PTR_OBFUSTICATOR;
423 static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
425 struct prop_hdr *prop;
428 assert(!taken(from_tal_hdr(t)));
430 /* Already being destroyed? Don't loop. */
431 if (unlikely(get_destroying_bit(t->parent_child)))
434 set_destroying_bit(&t->parent_child);
436 /* Call free notifiers. */
437 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig, saved_errno);
439 /* Now free children and groups. */
440 prop = find_property(t, CHILDREN);
443 struct children *c = (struct children *)prop;
445 while ((i = list_top(&c->children, struct tal_hdr, list))) {
447 del_tree(i, orig, saved_errno);
451 /* Finally free our properties. */
452 for (ptr = t->prop; ptr && (prop = is_prop_hdr(ptr)); ptr = next) {
459 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
461 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
463 child = allocate(sizeof(struct tal_hdr) + size);
467 memset(from_tal_hdr(child), 0, size);
468 child->prop = (void *)label;
469 child->bytelen = size;
471 if (!add_child(parent, child)) {
477 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child), 0);
478 return from_tal_hdr(debug_tal(child));
481 static bool adjust_size(size_t *size, size_t count)
483 const size_t extra = sizeof(struct tal_hdr);
485 /* Multiplication wrap */
486 if (count && unlikely(*size * count / *size != count))
491 /* Make sure we don't wrap adding header. */
492 if (*size + extra < extra)
496 call_error("allocation size overflow");
500 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
503 if (!adjust_size(&size, count))
506 return tal_alloc_(ctx, size, clear, label);
509 void *tal_free(const tal_t *ctx)
513 int saved_errno = errno;
514 t = debug_tal(to_tal_hdr(ctx));
515 if (unlikely(get_destroying_bit(t->parent_child)))
518 notify(ignore_destroying_bit(t->parent_child)->parent,
519 TAL_NOTIFY_DEL_CHILD, ctx, saved_errno);
521 del_tree(t, ctx, saved_errno);
527 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
530 struct tal_hdr *newpar, *t, *old_parent;
532 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
533 t = debug_tal(to_tal_hdr(ctx));
535 /* Unlink it from old parent. */
537 old_parent = ignore_destroying_bit(t->parent_child)->parent;
539 if (unlikely(!add_child(newpar, t))) {
540 /* We can always add to old parent, because it has a
541 * children property already. */
542 if (!add_child(old_parent, t))
548 notify(t, TAL_NOTIFY_STEAL, new_parent, 0);
553 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
555 tal_t *t = debug_tal(to_tal_hdr(ctx));
556 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
557 (void *)destroy, NULL);
560 bool tal_add_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
563 tal_t *t = debug_tal(to_tal_hdr(ctx));
564 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR
566 (void *)destroy, arg);
569 /* We could support notifiers with an extra arg, but we didn't add to API */
570 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
571 void (*callback)(tal_t *, enum tal_notify_type, void *))
573 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ctx));
577 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
578 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
579 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
580 | TAL_NOTIFY_ADD_NOTIFIER
581 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
583 /* Don't call notifier about itself: set types after! */
584 n = add_notifier_property(t, 0, callback, NULL);
589 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback, 0);
592 if (types != TAL_NOTIFY_FREE)
597 bool tal_del_notifier_(const tal_t *ctx,
598 void (*callback)(tal_t *, enum tal_notify_type, void *),
599 bool match_extra_arg, void *extra_arg)
601 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ctx));
602 enum tal_notify_type types;
604 types = del_notifier_property(t, callback, match_extra_arg, extra_arg);
606 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback, 0);
607 if (types != TAL_NOTIFY_FREE)
614 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
616 return tal_del_notifier_(ctx, (void *)destroy, false, NULL);
619 bool tal_del_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
622 return tal_del_notifier_(ctx, (void *)destroy, true, arg);
625 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
627 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
631 /* Get rid of any old name */
632 nptr = find_name_property(t, &was_literal);
637 struct name *oldname;
639 oldname = (struct name *)*nptr;
640 *nptr = oldname->hdr.next;
645 if (literal && name[0]) {
647 struct prop_hdr *prop;
649 /* Append literal. */
650 for (ptr = &t->prop; *ptr; ptr = &prop->next) {
651 prop = is_prop_hdr(*ptr);
656 } else if (!add_name_property(t, name))
661 notify(t, TAL_NOTIFY_RENAME, name, 0);
665 const char *tal_name(const tal_t *t)
670 nptr = find_name_property(debug_tal(to_tal_hdr(t)), &literal);
676 return ((struct name *)(*nptr))->name;
679 size_t tal_bytelen(const tal_t *ptr)
681 /* NULL -> null_parent which has bytelen 0 */
682 struct tal_hdr *t = debug_tal(to_tal_hdr_or_null(ptr));
687 /* Start one past first child: make stopping natural in circ. list. */
688 static struct tal_hdr *first_child(struct tal_hdr *parent)
690 struct children *child;
692 child = find_property(parent, CHILDREN);
696 return list_top(&child->children, struct tal_hdr, list);
699 tal_t *tal_first(const tal_t *root)
701 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
706 return from_tal_hdr(c);
709 tal_t *tal_next(const tal_t *prev)
711 struct tal_hdr *next, *prevhdr = debug_tal(to_tal_hdr(prev));
712 struct list_head *head;
714 head = &ignore_destroying_bit(prevhdr->parent_child)->children;
715 next = list_next(head, prevhdr, list);
718 return from_tal_hdr(next);
721 tal_t *tal_parent(const tal_t *ctx)
728 t = debug_tal(to_tal_hdr(ctx));
729 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
731 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
734 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
736 struct tal_hdr *old_t, *t;
737 struct children *child;
739 old_t = debug_tal(to_tal_hdr(*ctxp));
741 if (!adjust_size(&size, count))
744 t = resizefn(old_t, sizeof(struct tal_hdr) + size);
746 call_error("Reallocation failure");
750 /* Clear between old end and new end. */
751 if (clear && size > t->bytelen) {
752 char *old_end = (char *)(t + 1) + t->bytelen;
753 memset(old_end, 0, size - t->bytelen);
758 update_bounds(t, sizeof(struct tal_hdr) + size);
760 /* If it didn't move, we're done! */
762 /* Fix up linked list pointers. */
763 t->list.next->prev = t->list.prev->next = &t->list;
765 /* Copy take() property. */
766 if (taken(from_tal_hdr(old_t)))
767 take(from_tal_hdr(t));
769 /* Fix up child property's parent pointer. */
770 child = find_property(t, CHILDREN);
772 assert(child->parent == old_t);
775 *ctxp = from_tal_hdr(debug_tal(t));
777 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t), 0);
780 notify(t, TAL_NOTIFY_RESIZE, (void *)size, 0);
785 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
790 old_len = debug_tal(to_tal_hdr(*ctxp))->bytelen;
792 /* Check for additive overflow */
793 if (old_len + count * size < old_len) {
794 call_error("dup size overflow");
798 /* Don't point src inside thing we're expanding! */
800 || (char *)src >= (char *)(*ctxp) + old_len);
802 if (!tal_resize_(ctxp, size, old_len/size + count, false))
805 memcpy((char *)*ctxp + old_len, src, count * size);
814 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
815 size_t n, size_t extra, bool nullok, const char *label)
818 size_t nbytes = size;
820 if (nullok && p == NULL) {
821 /* take(NULL) works. */
826 if (!adjust_size(&nbytes, n)) {
832 /* Beware addition overflow! */
834 call_error("dup size overflow");
843 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
845 if (unlikely(!tal_steal(ctx, p)))
850 ret = tal_alloc_arr_(ctx, size, n + extra, false, label);
852 memcpy(ret, p, nbytes);
856 void *tal_dup_talarr_(const tal_t *ctx, const tal_t *src TAKES, const char *label)
858 return tal_dup_(ctx, src, 1, tal_bytelen(src), 0, true, label);
861 void tal_set_backend(void *(*alloc_fn)(size_t size),
862 void *(*resize_fn)(void *, size_t size),
863 void (*free_fn)(void *),
864 void (*error_fn)(const char *msg))
869 resizefn = resize_fn;
876 #ifdef CCAN_TAL_DEBUG
877 static void dump_node(unsigned int indent, const struct tal_hdr *t)
880 const struct prop_hdr *prop;
883 for (i = 0; i < indent; i++)
884 fprintf(stderr, " ");
885 fprintf(stderr, "%p len=%zu", t, t->bytelen);
886 for (ptr = t->prop; ptr; ptr = prop->next) {
890 prop = is_prop_hdr(ptr);
892 fprintf(stderr, " \"%s\"", ptr);
895 switch (prop->type) {
897 c = (struct children *)prop;
898 fprintf(stderr, " CHILDREN(%p):parent=%p,children={%p,%p}",
900 c->children.n.prev, c->children.n.next);
903 n = (struct name *)prop;
904 fprintf(stderr, " NAME(%p):%s", prop, n->name);
907 no = (struct notifier *)prop;
908 fprintf(stderr, " NOTIFIER(%p):fn=%p", prop, no->u.notifyfn);
911 fprintf(stderr, " **UNKNOWN(%p):%i**", prop, prop->type);
914 fprintf(stderr, "\n");
917 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
919 struct children *children;
923 children = find_property((struct tal_hdr *)t, CHILDREN);
927 list_for_each(&children->children, i, list)
928 tal_dump_(level + 1, i);
934 tal_dump_(0, &null_parent.hdr);
936 #endif /* CCAN_TAL_DEBUG */
939 static bool check_err(struct tal_hdr *t, const char *errorstr,
943 /* Try not to malloc: it may be corrupted. */
944 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
945 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
951 static bool check_node(struct children *parent_child,
952 struct tal_hdr *t, const char *errorstr)
954 struct prop_hdr *prop;
956 struct name *name = NULL;
957 struct children *children = NULL;
960 return check_err(t, errorstr, "invalid pointer");
962 if (ignore_destroying_bit(t->parent_child) != parent_child)
963 return check_err(t, errorstr, "incorrect parent");
965 for (p = t->prop; p; p = prop->next) {
966 prop = is_prop_hdr(p);
969 return check_err(t, errorstr,
970 "has extra literal");
973 if (!in_bounds(prop))
974 return check_err(t, errorstr,
975 "has bad property pointer");
977 switch (prop->type) {
980 return check_err(t, errorstr,
981 "has two child nodes");
982 children = (struct children *)prop;
988 return check_err(t, errorstr,
990 name = (struct name *)prop;
993 return check_err(t, errorstr, "has unknown property");
999 if (!list_check(&children->children, errorstr))
1001 list_for_each(&children->children, i, list) {
1002 if (!check_node(children, i, errorstr))
1009 bool tal_check(const tal_t *ctx, const char *errorstr)
1011 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
1013 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
1016 bool tal_check(const tal_t *ctx, const char *errorstr)