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>
6 #include <ccan/alignof/alignof.h>
16 #define NOTIFY_IS_DESTRUCTOR 512
17 #define NOTIFY_EXTRA_ARG 1024
19 /* 32-bit type field, first byte 0 in either endianness. */
21 CHILDREN = 0x00c1d500,
23 NOTIFIER = 0x00071f00,
28 struct list_node list;
29 struct prop_hdr *prop;
30 struct children *parent_child;
35 struct prop_hdr *next;
39 struct prop_hdr hdr; /* CHILDREN */
40 struct tal_hdr *parent;
41 struct list_head children; /* Head of siblings. */
45 struct prop_hdr hdr; /* NAME */
50 struct prop_hdr hdr; /* LENGTH */
55 struct prop_hdr hdr; /* NOTIFIER */
56 enum tal_notify_type types;
58 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
59 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
60 void (*destroy2)(tal_t *, void *); /* If NOTIFY_EXTRA_ARG */
65 struct notifier_extra_arg {
70 #define EXTRA_ARG(n) (((struct notifier_extra_arg *)(n))->arg)
75 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
76 &null_parent.c.hdr, NULL },
79 { { &null_parent.c.children.n,
80 &null_parent.c.children.n } }
85 static void *(*allocfn)(size_t size) = malloc;
86 static void *(*resizefn)(void *, size_t size) = realloc;
87 static void (*freefn)(void *) = free;
88 static void (*errorfn)(const char *msg) = (void *)abort;
89 /* Count on non-destrutor notifiers; often stays zero. */
90 static size_t notifiers = 0;
92 static inline void COLD call_error(const char *msg)
97 static bool get_destroying_bit(struct children *parent_child)
99 return (size_t)parent_child & 1;
102 static void set_destroying_bit(struct children **parent_child)
104 *parent_child = (void *)((size_t)*parent_child | 1);
107 static struct children *ignore_destroying_bit(struct children *parent_child)
109 return (void *)((size_t)parent_child & ~(size_t)1);
112 /* This means valgrind can see leaks. */
113 void tal_cleanup(void)
117 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
119 memset(i, 0, sizeof(*i));
122 /* Cleanup any taken pointers. */
126 /* We carefully start all real properties with a zero byte. */
127 static bool is_literal(const struct prop_hdr *prop)
129 return ((char *)prop)[0] != 0;
133 static const void *bounds_start, *bounds_end;
135 static void update_bounds(const void *new, size_t size)
137 if (unlikely(!bounds_start)) {
139 bounds_end = (char *)new + size;
140 } else if (new < bounds_start)
142 else if ((char *)new + size > (char *)bounds_end)
143 bounds_end = (char *)new + size;
146 static bool in_bounds(const void *p)
149 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
150 || (p >= bounds_start && p <= bounds_end);
153 static void update_bounds(const void *new, size_t size)
157 static bool in_bounds(const void *p)
163 static void check_bounds(const void *p)
166 call_error("Not a valid header");
169 static struct tal_hdr *to_tal_hdr(const void *ctx)
173 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
175 check_bounds(ignore_destroying_bit(t->parent_child));
176 check_bounds(t->list.next);
177 check_bounds(t->list.prev);
178 if (t->prop && !is_literal(t->prop))
179 check_bounds(t->prop);
183 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
186 return &null_parent.hdr;
187 return to_tal_hdr(ctx);
190 static void *from_tal_hdr(const struct tal_hdr *hdr)
192 return (void *)(hdr + 1);
196 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
198 if (hdr == &null_parent.hdr)
200 return from_tal_hdr(hdr);
203 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
205 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
209 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
215 static void notify(const struct tal_hdr *ctx,
216 enum tal_notify_type type, const void *info,
219 const struct prop_hdr *p;
221 for (p = ctx->prop; p; p = p->next) {
226 if (p->type != NOTIFIER)
228 n = (struct notifier *)p;
229 if (n->types & type) {
231 if (n->types & NOTIFY_IS_DESTRUCTOR) {
232 if (n->types & NOTIFY_EXTRA_ARG)
233 n->u.destroy2(from_tal_hdr(ctx),
236 n->u.destroy(from_tal_hdr(ctx));
238 n->u.notifyfn(from_tal_hdr(ctx), type,
244 static void *allocate(size_t size)
246 void *ret = allocfn(size);
248 call_error("allocation failed");
250 update_bounds(ret, size);
254 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
259 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
260 if (is_literal(*p)) {
265 if ((*p)->type == type)
271 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
273 struct prop_hdr **p = find_property_ptr(parent, type);
280 static void init_property(struct prop_hdr *hdr,
281 struct tal_hdr *parent,
285 hdr->next = parent->prop;
289 static struct notifier *add_notifier_property(struct tal_hdr *t,
290 enum tal_notify_type types,
292 enum tal_notify_type,
296 struct notifier *prop;
298 if (types & NOTIFY_EXTRA_ARG)
299 prop = allocate(sizeof(struct notifier_extra_arg));
301 prop = allocate(sizeof(struct notifier));
304 init_property(&prop->hdr, t, NOTIFIER);
306 prop->u.notifyfn = fn;
307 if (types & NOTIFY_EXTRA_ARG)
308 EXTRA_ARG(prop) = extra_arg;
313 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
315 enum tal_notify_type,
317 bool match_extra_arg,
322 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
324 enum tal_notify_type types;
328 if ((*p)->type != NOTIFIER)
330 n = (struct notifier *)*p;
331 if (n->u.notifyfn != fn)
335 if ((types & NOTIFY_EXTRA_ARG)
337 && extra_arg != EXTRA_ARG(n))
342 return types & ~(NOTIFY_IS_DESTRUCTOR|NOTIFY_EXTRA_ARG);
347 static struct name *add_name_property(struct tal_hdr *t, const char *name)
351 prop = allocate(sizeof(*prop) + strlen(name) + 1);
353 init_property(&prop->hdr, t, NAME);
354 strcpy(prop->name, name);
359 static struct children *add_child_property(struct tal_hdr *parent,
360 struct tal_hdr *child UNNEEDED)
362 struct children *prop = allocate(sizeof(*prop));
364 init_property(&prop->hdr, parent, CHILDREN);
365 prop->parent = parent;
366 list_head_init(&prop->children);
371 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
373 struct children *children = find_property(parent, CHILDREN);
376 children = add_child_property(parent, child);
380 list_add(&children->children, &child->list);
381 child->parent_child = children;
385 static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
387 struct prop_hdr **prop, *p, *next;
389 /* Already being destroyed? Don't loop. */
390 if (unlikely(get_destroying_bit(t->parent_child)))
393 set_destroying_bit(&t->parent_child);
395 /* Call free notifiers. */
396 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig, saved_errno);
398 /* Now free children and groups. */
399 prop = find_property_ptr(t, CHILDREN);
402 struct children *c = (struct children *)*prop;
404 while ((i = list_top(&c->children, struct tal_hdr, list))) {
406 del_tree(i, orig, saved_errno);
410 /* Finally free our properties. */
411 for (p = t->prop; p && !is_literal(p); p = next) {
413 /* LENGTH is appended, so don't free separately! */
414 if (p->type != LENGTH)
420 static size_t extra_for_length(size_t size)
423 const size_t align = ALIGNOF(struct length);
425 /* Round up size, and add tailer. */
426 extra = ((size + align-1) & ~(align-1)) - size;
427 extra += sizeof(struct length);
431 void *tal_alloc_(const tal_t *ctx, size_t size,
432 bool clear, bool add_length, const char *label)
434 size_t req_size = size;
435 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
437 #ifdef CCAN_TAL_DEBUG
438 /* Always record length if debugging. */
442 size += extra_for_length(size);
444 child = allocate(sizeof(struct tal_hdr) + size);
448 memset(from_tal_hdr(child), 0, req_size);
449 child->prop = (void *)label;
452 struct length *lprop;
453 lprop = (struct length *)((char *)(child+1) + size) - 1;
454 init_property(&lprop->hdr, child, LENGTH);
455 lprop->len = req_size;
457 if (!add_child(parent, child)) {
463 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child), 0);
464 return from_tal_hdr(debug_tal(child));
467 static bool adjust_size(size_t *size, size_t count)
469 const size_t extra = sizeof(struct tal_hdr) + sizeof(struct length)*2;
471 /* Multiplication wrap */
472 if (count && unlikely(*size * count / *size != count))
477 /* Make sure we don't wrap adding header/tailer. */
478 if (*size + extra < extra)
482 call_error("allocation size overflow");
486 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
487 bool add_length, const char *label)
489 if (!adjust_size(&size, count))
492 return tal_alloc_(ctx, size, clear, add_length, label);
495 void *tal_free(const tal_t *ctx)
499 int saved_errno = errno;
500 t = debug_tal(to_tal_hdr(ctx));
502 notify(ignore_destroying_bit(t->parent_child)->parent,
503 TAL_NOTIFY_DEL_CHILD, ctx, saved_errno);
505 del_tree(t, ctx, saved_errno);
511 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
514 struct tal_hdr *newpar, *t, *old_parent;
516 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
517 t = debug_tal(to_tal_hdr(ctx));
519 /* Unlink it from old parent. */
521 old_parent = ignore_destroying_bit(t->parent_child)->parent;
523 if (unlikely(!add_child(newpar, t))) {
524 /* We can always add to old parent, becuase it has a
525 * children property already. */
526 if (!add_child(old_parent, t))
532 notify(t, TAL_NOTIFY_STEAL, new_parent, 0);
537 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
539 tal_t *t = debug_tal(to_tal_hdr(ctx));
540 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
541 (void *)destroy, NULL);
544 bool tal_add_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
547 tal_t *t = debug_tal(to_tal_hdr(ctx));
548 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR
550 (void *)destroy, arg);
553 /* We could support notifiers with an extra arg, but we didn't add to API */
554 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
555 void (*callback)(tal_t *, enum tal_notify_type, void *))
557 tal_t *t = debug_tal(to_tal_hdr(ctx));
561 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
562 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
563 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
564 | TAL_NOTIFY_ADD_NOTIFIER
565 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
567 /* Don't call notifier about itself: set types after! */
568 n = add_notifier_property(t, 0, callback, NULL);
573 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback, 0);
576 if (types != TAL_NOTIFY_FREE)
581 bool tal_del_notifier_(const tal_t *ctx,
582 void (*callback)(tal_t *, enum tal_notify_type, void *),
583 bool match_extra_arg, void *extra_arg)
585 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
586 enum tal_notify_type types;
588 types = del_notifier_property(t, callback, match_extra_arg, extra_arg);
590 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback, 0);
591 if (types != TAL_NOTIFY_FREE)
598 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
600 return tal_del_notifier_(ctx, (void *)destroy, false, NULL);
603 bool tal_del_destructor2_(const tal_t *ctx, void (*destroy)(void *me, void *arg),
606 return tal_del_notifier_(ctx, (void *)destroy, true, arg);
609 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
611 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
612 struct prop_hdr **prop = find_property_ptr(t, NAME);
614 /* Get rid of any old name */
616 struct name *name = (struct name *)*prop;
617 if (is_literal(&name->hdr))
620 *prop = name->hdr.next;
625 if (literal && name[0]) {
628 /* Append literal. */
629 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
630 *p = (struct prop_hdr *)name;
631 } else if (!add_name_property(t, name))
636 notify(t, TAL_NOTIFY_RENAME, name, 0);
640 const char *tal_name(const tal_t *t)
644 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
648 if (is_literal(&n->hdr))
649 return (const char *)n;
653 size_t tal_len(const tal_t *ptr)
660 l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
666 /* Start one past first child: make stopping natural in circ. list. */
667 static struct tal_hdr *first_child(struct tal_hdr *parent)
669 struct children *child;
671 child = find_property(parent, CHILDREN);
675 return list_top(&child->children, struct tal_hdr, list);
678 tal_t *tal_first(const tal_t *root)
680 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
685 return from_tal_hdr(c);
688 tal_t *tal_next(const tal_t *prev)
690 struct tal_hdr *next, *prevhdr = debug_tal(to_tal_hdr(prev));
691 struct list_head *head;
693 head = &ignore_destroying_bit(prevhdr->parent_child)->children;
694 next = list_next(head, prevhdr, list);
697 return from_tal_hdr(next);
700 tal_t *tal_parent(const tal_t *ctx)
707 t = debug_tal(to_tal_hdr(ctx));
708 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
710 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
713 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
715 struct tal_hdr *old_t, *t;
716 struct children *child;
717 struct prop_hdr **lenp;
721 old_t = debug_tal(to_tal_hdr(*ctxp));
723 if (!adjust_size(&size, count))
726 lenp = find_property_ptr(old_t, LENGTH);
728 /* Copy here, in case we're shrinking! */
729 len = *(struct length *)*lenp;
730 extra = extra_for_length(size);
731 } else /* If we don't have an old length, we can't clear! */
734 t = resizefn(old_t, sizeof(struct tal_hdr) + size + extra);
736 call_error("Reallocation failure");
740 /* Copy length to end. */
742 struct length *new_len;
744 /* Clear between old end and new end. */
745 if (clear && size > len.len) {
746 char *old_end = (char *)(t + 1) + len.len;
747 memset(old_end, 0, size - len.len);
750 new_len = (struct length *)((char *)(t + 1) + size
751 + extra - sizeof(len));
755 /* Be careful replacing next ptr; could be old hdr. */
756 if (lenp == &old_t->prop)
757 t->prop = &new_len->hdr;
759 *lenp = &new_len->hdr;
762 update_bounds(t, sizeof(struct tal_hdr) + size + extra);
764 /* If it didn't move, we're done! */
766 /* Fix up linked list pointers. */
767 t->list.next->prev = t->list.prev->next = &t->list;
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)
791 l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
794 /* Check for additive overflow */
795 if (old_len + count * size < old_len) {
796 call_error("dup size overflow");
800 /* Don't point src inside thing we're expanding! */
802 || (char *)src >= (char *)(*ctxp) + old_len);
804 if (!tal_resize_(ctxp, size, old_len/size + count, false))
807 memcpy((char *)*ctxp + old_len, src, count * size);
816 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
817 size_t n, size_t extra, bool add_length,
821 size_t nbytes = size;
823 if (!adjust_size(&nbytes, n)) {
829 /* Beware addition overflow! */
831 call_error("dup size overflow");
840 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
842 if (unlikely(!tal_steal(ctx, p)))
847 ret = tal_alloc_arr_(ctx, size, n + extra, false, add_length, label);
849 memcpy(ret, p, nbytes);
853 void tal_set_backend(void *(*alloc_fn)(size_t size),
854 void *(*resize_fn)(void *, size_t size),
855 void (*free_fn)(void *),
856 void (*error_fn)(const char *msg))
861 resizefn = resize_fn;
868 #ifdef CCAN_TAL_DEBUG
869 static void dump_node(unsigned int indent, const struct tal_hdr *t)
872 const struct prop_hdr *p;
874 for (i = 0; i < indent; i++)
877 for (p = t->prop; p; p = p->next) {
883 printf(" \"%s\"", (const char *)p);
888 c = (struct children *)p;
889 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
891 c->children.n.prev, c->children.n.next);
894 n = (struct name *)p;
895 printf(" NAME(%p):%s", p, n->name);
898 no = (struct notifier *)p;
899 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
902 l = (struct length *)p;
903 printf(" LENGTH(%p):len=%zu", p, l->len);
906 printf(" **UNKNOWN(%p):%i**", p, p->type);
912 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
914 struct children *children;
918 children = find_property(t, CHILDREN);
922 list_for_each(&children->children, i, list)
923 tal_dump_(level + 1, i);
929 tal_dump_(0, &null_parent.hdr);
931 #endif /* CCAN_TAL_DEBUG */
934 static bool check_err(struct tal_hdr *t, const char *errorstr,
938 /* Try not to malloc: it may be corrupted. */
939 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
940 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
946 static bool check_node(struct children *parent_child,
947 struct tal_hdr *t, const char *errorstr)
950 struct name *name = NULL;
951 struct children *children = NULL;
952 struct length *length = NULL;
955 return check_err(t, errorstr, "invalid pointer");
957 if (ignore_destroying_bit(t->parent_child) != parent_child)
958 return check_err(t, errorstr, "incorrect parent");
960 for (p = t->prop; p; p = p->next) {
963 return check_err(t, errorstr,
964 "has extra literal");
968 return check_err(t, errorstr,
969 "has bad property pointer");
974 return check_err(t, errorstr,
975 "has two child nodes");
976 children = (struct children *)p;
980 return check_err(t, errorstr,
982 length = (struct length *)p;
988 return check_err(t, errorstr,
990 name = (struct name *)p;
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)