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>
15 #define NOTIFY_IS_DESTRUCTOR 512
17 /* 32-bit type field, first byte 0 in either endianness. */
19 CHILDREN = 0x00c1d500,
21 NOTIFIER = 0x00071f00,
26 struct list_node list;
27 struct prop_hdr *prop;
28 struct children *parent_child;
33 struct prop_hdr *next;
37 struct prop_hdr hdr; /* CHILDREN */
38 struct tal_hdr *parent;
39 struct list_head children; /* Head of siblings. */
43 struct prop_hdr hdr; /* NAME */
48 struct prop_hdr hdr; /* LENGTH */
53 struct prop_hdr hdr; /* NOTIFIER */
54 enum tal_notify_type types;
56 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
57 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
64 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
65 &null_parent.c.hdr, NULL },
68 { { &null_parent.c.children.n,
69 &null_parent.c.children.n } }
74 static void *(*allocfn)(size_t size) = malloc;
75 static void *(*resizefn)(void *, size_t size) = realloc;
76 static void (*freefn)(void *) = free;
77 static void (*errorfn)(const char *msg) = (void *)abort;
78 static bool initialized = false;
79 /* Count on non-destrutor notifiers; often stays zero. */
80 static size_t notifiers = 0;
82 static inline void COLD call_error(const char *msg)
87 static bool get_destroying_bit(struct children *parent_child)
89 return (size_t)parent_child & 1;
92 static void set_destroying_bit(struct children **parent_child)
94 *parent_child = (void *)((size_t)*parent_child | 1);
97 static struct children *ignore_destroying_bit(struct children *parent_child)
99 return (void *)((size_t)parent_child & ~(size_t)1);
102 /* This means valgrind can see leaks. */
103 static void tal_cleanup(void)
107 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
110 /* Cleanup any taken pointers. */
114 /* For allocation failures inside ccan/take */
115 static void take_alloc_failed(const void *p)
120 /* We carefully start all real properties with a zero byte. */
121 static bool is_literal(const struct prop_hdr *prop)
123 return ((char *)prop)[0] != 0;
127 static const void *bounds_start, *bounds_end;
129 static void update_bounds(const void *new, size_t size)
131 if (unlikely(!bounds_start)) {
133 bounds_end = (char *)new + size;
134 } else if (new < bounds_start)
136 else if ((char *)new + size > (char *)bounds_end)
137 bounds_end = (char *)new + size;
140 static bool in_bounds(const void *p)
143 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
144 || (p >= bounds_start && p <= bounds_end);
147 static void update_bounds(const void *new, size_t size)
151 static bool in_bounds(const void *p)
157 static void check_bounds(const void *p)
160 call_error("Not a valid header");
163 static struct tal_hdr *to_tal_hdr(const void *ctx)
167 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
169 check_bounds(ignore_destroying_bit(t->parent_child));
170 check_bounds(t->list.next);
171 check_bounds(t->list.prev);
172 if (t->prop && !is_literal(t->prop))
173 check_bounds(t->prop);
177 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
180 return &null_parent.hdr;
181 return to_tal_hdr(ctx);
184 static void *from_tal_hdr(const struct tal_hdr *hdr)
186 return (void *)(hdr + 1);
190 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
192 if (hdr == &null_parent.hdr)
194 return from_tal_hdr(hdr);
197 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
199 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
203 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
209 static void notify(const struct tal_hdr *ctx,
210 enum tal_notify_type type, const void *info)
212 const struct prop_hdr *p;
214 for (p = ctx->prop; p; p = p->next) {
219 if (p->type != NOTIFIER)
221 n = (struct notifier *)p;
222 if (n->types & type) {
223 if (n->types & NOTIFY_IS_DESTRUCTOR)
224 n->u.destroy(from_tal_hdr(ctx));
226 n->u.notifyfn(from_tal_hdr(ctx), type,
232 static void *allocate(size_t size)
234 void *ret = allocfn(size);
236 call_error("allocation failed");
238 update_bounds(ret, size);
242 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
247 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
248 if (is_literal(*p)) {
253 if ((*p)->type == type)
259 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
261 struct prop_hdr **p = find_property_ptr(parent, type);
268 static void init_property(struct prop_hdr *hdr,
269 struct tal_hdr *parent,
273 hdr->next = parent->prop;
277 static struct notifier *add_notifier_property(struct tal_hdr *t,
278 enum tal_notify_type types,
280 enum tal_notify_type,
283 struct notifier *prop = allocate(sizeof(*prop));
285 init_property(&prop->hdr, t, NOTIFIER);
287 prop->u.notifyfn = fn;
292 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
294 enum tal_notify_type,
299 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
304 if ((*p)->type != NOTIFIER)
306 n = (struct notifier *)*p;
307 if (n->u.notifyfn == fn) {
308 enum tal_notify_type types = n->types;
311 return types & ~NOTIFY_IS_DESTRUCTOR;
317 static struct name *add_name_property(struct tal_hdr *t, const char *name)
321 prop = allocate(sizeof(*prop) + strlen(name) + 1);
323 init_property(&prop->hdr, t, NAME);
324 strcpy(prop->name, name);
329 static struct length *add_length_property(struct tal_hdr *t, size_t count)
333 prop = allocate(sizeof(*prop));
335 init_property(&prop->hdr, t, LENGTH);
341 static struct children *add_child_property(struct tal_hdr *parent,
342 struct tal_hdr *child)
344 struct children *prop = allocate(sizeof(*prop));
346 init_property(&prop->hdr, parent, CHILDREN);
347 prop->parent = parent;
348 list_head_init(&prop->children);
353 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
355 struct children *children = find_property(parent, CHILDREN);
358 if (unlikely(!initialized)) {
360 take_allocfail(take_alloc_failed);
363 children = add_child_property(parent, child);
367 list_add(&children->children, &child->list);
368 child->parent_child = children;
372 static void del_tree(struct tal_hdr *t, const tal_t *orig)
374 struct prop_hdr **prop, *p, *next;
376 /* Already being destroyed? Don't loop. */
377 if (unlikely(get_destroying_bit(t->parent_child)))
380 set_destroying_bit(&t->parent_child);
382 /* Call free notifiers. */
383 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
385 /* Now free children and groups. */
386 prop = find_property_ptr(t, CHILDREN);
389 struct children *c = (struct children *)*prop;
391 while ((i = list_top(&c->children, struct tal_hdr, list))) {
397 /* Finally free our properties. */
398 for (p = t->prop; p && !is_literal(p); p = next) {
405 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
407 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
409 child = allocate(sizeof(struct tal_hdr) + size);
413 memset(from_tal_hdr(child), 0, size);
414 child->prop = (void *)label;
415 if (!add_child(parent, child)) {
421 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child));
422 return from_tal_hdr(debug_tal(child));
425 static bool adjust_size(size_t *size, size_t count)
427 /* Multiplication wrap */
428 if (count && unlikely(*size * count / *size != count))
433 /* Make sure we don't wrap adding header. */
434 if (*size + sizeof(struct tal_hdr) < sizeof(struct tal_hdr))
438 call_error("allocation size overflow");
442 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
443 bool add_count, const char *label)
447 if (!adjust_size(&size, count))
450 ret = tal_alloc_(ctx, size, clear, label);
451 if (likely(ret) && add_count) {
452 if (unlikely(!add_length_property(to_tal_hdr(ret), count)))
458 void *tal_free(const tal_t *ctx)
462 int saved_errno = errno;
463 t = debug_tal(to_tal_hdr(ctx));
465 notify(ignore_destroying_bit(t->parent_child)->parent,
466 TAL_NOTIFY_DEL_CHILD, ctx);
474 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
477 struct tal_hdr *newpar, *t, *old_parent;
479 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
480 t = debug_tal(to_tal_hdr(ctx));
482 /* Unlink it from old parent. */
484 old_parent = ignore_destroying_bit(t->parent_child)->parent;
486 if (unlikely(!add_child(newpar, t))) {
487 /* We can always add to old parent, becuase it has a
488 * children property already. */
489 if (!add_child(old_parent, t))
495 notify(t, TAL_NOTIFY_STEAL, new_parent);
500 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
502 tal_t *t = debug_tal(to_tal_hdr(ctx));
503 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
507 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
508 void (*callback)(tal_t *, enum tal_notify_type, void *))
510 tal_t *t = debug_tal(to_tal_hdr(ctx));
514 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
515 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
516 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
517 | TAL_NOTIFY_ADD_NOTIFIER
518 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
520 /* Don't call notifier about itself: set types after! */
521 n = add_notifier_property(t, 0, callback);
526 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
529 if (types != TAL_NOTIFY_FREE)
534 bool tal_del_notifier_(const tal_t *ctx,
535 void (*callback)(tal_t *, enum tal_notify_type, void *))
537 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
538 enum tal_notify_type types;
540 types = del_notifier_property(t, callback);
542 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
543 if (types != TAL_NOTIFY_FREE)
550 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
552 return tal_del_notifier_(ctx, (void *)destroy);
555 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
557 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
558 struct prop_hdr **prop = find_property_ptr(t, NAME);
560 /* Get rid of any old name */
562 struct name *name = (struct name *)*prop;
563 if (is_literal(&name->hdr))
566 *prop = name->hdr.next;
571 if (literal && name[0]) {
574 /* Append literal. */
575 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
576 *p = (struct prop_hdr *)name;
577 } else if (!add_name_property(t, name))
582 notify(t, TAL_NOTIFY_RENAME, name);
586 const char *tal_name(const tal_t *t)
590 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
594 if (is_literal(&n->hdr))
595 return (const char *)n;
599 size_t tal_count(const tal_t *ptr)
603 l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
609 /* Start one past first child: make stopping natural in circ. list. */
610 static struct tal_hdr *first_child(struct tal_hdr *parent)
612 struct children *child;
614 child = find_property(parent, CHILDREN);
618 return list_top(&child->children, struct tal_hdr, list);
621 tal_t *tal_first(const tal_t *root)
623 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
628 return from_tal_hdr(c);
631 tal_t *tal_next(const tal_t *root, const tal_t *prev)
633 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
638 return from_tal_hdr(c);
640 top = to_tal_hdr_or_null(root);
642 struct tal_hdr *next;
643 struct list_node *end;
645 end = &ignore_destroying_bit(t->parent_child)->children.n;
647 next = list_entry(t->list.next, struct tal_hdr, list);
648 if (&next->list != end)
649 return from_tal_hdr(next);
651 /* OK, go back to parent. */
652 t = ignore_destroying_bit(t->parent_child)->parent;
658 tal_t *tal_parent(const tal_t *ctx)
665 t = debug_tal(to_tal_hdr(ctx));
666 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
668 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
671 bool tal_resize_(tal_t **ctxp, size_t size, size_t count)
673 struct tal_hdr *old_t, *t;
674 struct children *child;
677 old_t = debug_tal(to_tal_hdr(*ctxp));
679 if (!adjust_size(&size, count))
682 t = resizefn(old_t, size + sizeof(struct tal_hdr));
684 call_error("Reallocation failure");
688 /* If it didn't move, we're done! */
690 update_bounds(t, size + sizeof(struct tal_hdr));
692 /* Fix up linked list pointers. */
693 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
694 t->list.next->prev = t->list.prev->next = &t->list;
696 /* Fix up child property's parent pointer. */
697 child = find_property(t, CHILDREN);
699 assert(child->parent == old_t);
702 *ctxp = from_tal_hdr(debug_tal(t));
704 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
706 len = find_property(t, LENGTH);
710 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
715 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
720 l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
722 /* Check for additive overflow */
723 if (l->count + count < count) {
724 call_error("dup size overflow");
728 /* Don't point src inside thing we're expanding! */
730 || (char *)src >= (char *)(*ctxp) + (size * l->count));
732 /* Note: updates l->count. */
733 if (!tal_resize_(ctxp, size, l->count + count))
736 memcpy((char *)*ctxp + size * (l->count - count),
746 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
747 size_t n, size_t extra, bool add_count,
751 size_t nbytes = size;
753 if (!adjust_size(&nbytes, n)) {
759 /* Beware addition overflow! */
761 call_error("dup size overflow");
770 if (unlikely(!tal_resize_((void **)&p, size, n + extra)))
772 if (unlikely(!tal_steal(ctx, p)))
777 ret = tal_alloc_arr_(ctx, size, n + extra, false, add_count, label);
779 memcpy(ret, p, nbytes);
783 void tal_set_backend(void *(*alloc_fn)(size_t size),
784 void *(*resize_fn)(void *, size_t size),
785 void (*free_fn)(void *),
786 void (*error_fn)(const char *msg))
791 resizefn = resize_fn;
798 #ifdef CCAN_TAL_DEBUG
799 static void dump_node(unsigned int indent, const struct tal_hdr *t)
802 const struct prop_hdr *p;
804 for (i = 0; i < indent; i++)
807 for (p = t->prop; p; p = p->next) {
813 printf(" \"%s\"", (const char *)p);
818 c = (struct children *)p;
819 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
821 c->children.n.prev, c->children.n.next);
824 n = (struct name *)p;
825 printf(" NAME(%p):%s", p, n->name);
828 no = (struct notifier *)p;
829 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
832 l = (struct length *)p;
833 printf(" LENGTH(%p):count=%zu", p, l->count);
836 printf(" **UNKNOWN(%p):%i**", p, p->type);
842 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
844 struct children *children;
848 children = find_property(t, CHILDREN);
852 list_for_each(&children->children, i, list)
853 tal_dump_(level + 1, i);
859 tal_dump_(0, &null_parent.hdr);
861 #endif /* CCAN_TAL_DEBUG */
864 static bool check_err(struct tal_hdr *t, const char *errorstr,
868 /* Try not to malloc: it may be corrupted. */
869 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
870 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
876 static bool check_node(struct children *parent_child,
877 struct tal_hdr *t, const char *errorstr)
880 struct name *name = NULL;
881 struct children *children = NULL;
882 struct length *length = NULL;
885 return check_err(t, errorstr, "invalid pointer");
887 if (ignore_destroying_bit(t->parent_child) != parent_child)
888 return check_err(t, errorstr, "incorrect parent");
890 for (p = t->prop; p; p = p->next) {
893 return check_err(t, errorstr,
894 "has extra literal");
895 name = (struct name *)p;
899 return check_err(t, errorstr,
900 "has bad property pointer");
905 return check_err(t, errorstr,
906 "has two child nodes");
907 children = (struct children *)p;
911 return check_err(t, errorstr,
913 length = (struct length *)p;
919 return check_err(t, errorstr,
921 name = (struct name *)p;
924 return check_err(t, errorstr, "has unknown property");
930 if (!list_check(&children->children, errorstr))
932 list_for_each(&children->children, i, list) {
933 if (!check_node(children, i, errorstr))
940 bool tal_check(const tal_t *ctx, const char *errorstr)
942 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
944 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
947 bool tal_check(const tal_t *ctx, const char *errorstr)