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>
16 #define NOTIFY_IS_DESTRUCTOR 512
18 /* 32-bit type field, first byte 0 in either endianness. */
20 CHILDREN = 0x00c1d500,
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; /* NOTIFIER */
49 enum tal_notify_type types;
51 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
52 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
59 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
60 &null_parent.c.hdr, NULL },
63 { { &null_parent.c.children.n,
64 &null_parent.c.children.n } }
69 static void *(*allocfn)(size_t size) = malloc;
70 static void *(*resizefn)(void *, size_t size) = realloc;
71 static void (*freefn)(void *) = free;
72 static void (*errorfn)(const char *msg) = (void *)abort;
74 static inline void COLD call_error(const char *msg)
79 static bool get_destroying_bit(struct children *parent_child)
81 return (size_t)parent_child & 1;
84 static void set_destroying_bit(struct children **parent_child)
86 *parent_child = (void *)((size_t)*parent_child | 1);
89 static struct children *ignore_destroying_bit(struct children *parent_child)
91 return (void *)((size_t)parent_child & ~(size_t)1);
94 static bool initialized = false;
96 /* This means valgrind can see leaks. */
97 static void tal_cleanup(void)
101 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
104 /* Cleanup any taken pointers. */
108 /* For allocation failures inside ccan/take */
109 static void take_alloc_failed(const void *p)
114 /* We carefully start all real properties with a zero byte. */
115 static bool is_literal(const struct prop_hdr *prop)
117 return ((char *)prop)[0] != 0;
121 static const void *bounds_start, *bounds_end;
123 static void update_bounds(const void *new, size_t size)
125 if (unlikely(!bounds_start)) {
127 bounds_end = (char *)new + size;
128 } else if (new < bounds_start)
130 else if ((char *)new + size > (char *)bounds_end)
131 bounds_end = (char *)new + size;
134 static bool in_bounds(const void *p)
137 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
138 || (p >= bounds_start && p <= bounds_end);
141 static void update_bounds(const void *new, size_t size)
145 static bool in_bounds(const void *p)
151 static void check_bounds(const void *p)
154 call_error("Not a valid header");
157 static struct tal_hdr *to_tal_hdr(const void *ctx)
161 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
163 check_bounds(ignore_destroying_bit(t->parent_child));
164 check_bounds(t->list.next);
165 check_bounds(t->list.prev);
166 if (t->prop && !is_literal(t->prop))
167 check_bounds(t->prop);
171 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
174 return &null_parent.hdr;
175 return to_tal_hdr(ctx);
178 static void *from_tal_hdr(const struct tal_hdr *hdr)
180 return (void *)(hdr + 1);
184 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
186 if (hdr == &null_parent.hdr)
188 return from_tal_hdr(hdr);
191 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
193 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
197 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
203 static void notify(const struct tal_hdr *ctx,
204 enum tal_notify_type type, const void *info)
206 const struct prop_hdr *p;
208 for (p = ctx->prop; p; p = p->next) {
213 if (p->type != NOTIFIER)
215 n = (struct notifier *)p;
216 if (n->types & type) {
217 if (n->types & NOTIFY_IS_DESTRUCTOR)
218 n->u.destroy(from_tal_hdr(ctx));
220 n->u.notifyfn(from_tal_hdr(ctx), type,
226 static void *allocate(size_t size)
230 /* Don't hand silly sizes to malloc. */
231 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
232 call_error("allocation size overflow");
238 call_error("allocation failed");
240 update_bounds(ret, size);
244 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
249 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
250 if (is_literal(*p)) {
255 if ((*p)->type == type)
261 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
263 struct prop_hdr **p = find_property_ptr(parent, type);
270 static void init_property(struct prop_hdr *hdr,
271 struct tal_hdr *parent,
275 hdr->next = parent->prop;
279 static struct notifier *add_notifier_property(struct tal_hdr *t,
280 enum tal_notify_type types,
282 enum tal_notify_type,
285 struct notifier *prop = allocate(sizeof(*prop));
287 init_property(&prop->hdr, t, NOTIFIER);
289 prop->u.notifyfn = fn;
294 static bool del_notifier_property(struct tal_hdr *t,
296 enum tal_notify_type, void *))
300 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
305 if ((*p)->type != NOTIFIER)
307 n = (struct notifier *)*p;
308 if (n->u.notifyfn == fn) {
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 children *add_child_property(struct tal_hdr *parent,
330 struct tal_hdr *child)
332 struct children *prop = allocate(sizeof(*prop));
334 init_property(&prop->hdr, parent, CHILDREN);
335 prop->parent = parent;
336 list_head_init(&prop->children);
341 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
343 struct children *children = find_property(parent, CHILDREN);
346 if (unlikely(!initialized)) {
348 take_allocfail(take_alloc_failed);
351 children = add_child_property(parent, child);
355 list_add(&children->children, &child->list);
356 child->parent_child = children;
360 static void del_tree(struct tal_hdr *t, const tal_t *orig)
362 struct prop_hdr **prop, *p, *next;
364 /* Already being destroyed? Don't loop. */
365 if (unlikely(get_destroying_bit(t->parent_child)))
368 set_destroying_bit(&t->parent_child);
370 /* Call free notifiers. */
371 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
373 /* Now free children and groups. */
374 prop = find_property_ptr(t, CHILDREN);
377 struct children *c = (struct children *)*prop;
379 while ((i = list_top(&c->children, struct tal_hdr, list))) {
385 /* Finally free our properties. */
386 for (p = t->prop; p && !is_literal(p); p = next) {
393 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
395 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
397 child = allocate(sizeof(struct tal_hdr) + size);
401 memset(from_tal_hdr(child), 0, size);
402 child->prop = (void *)label;
403 if (!add_child(parent, child)) {
408 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(debug_tal(child)));
409 return from_tal_hdr(debug_tal(child));
412 void *tal_free(const tal_t *ctx)
416 int saved_errno = errno;
417 t = debug_tal(to_tal_hdr(ctx));
418 notify(ignore_destroying_bit(t->parent_child)->parent,
419 TAL_NOTIFY_DEL_CHILD, ctx);
427 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
430 struct tal_hdr *newpar, *t, *old_parent;
432 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
433 t = debug_tal(to_tal_hdr(ctx));
435 /* Unlink it from old parent. */
437 old_parent = ignore_destroying_bit(t->parent_child)->parent;
439 if (unlikely(!add_child(newpar, t))) {
440 /* We can always add to old parent, becuase it has a
441 * children property already. */
442 if (!add_child(old_parent, t))
447 notify(t, TAL_NOTIFY_STEAL, new_parent);
452 bool tal_add_destructor_(tal_t *ctx, void (*destroy)(void *me))
454 tal_t *t = debug_tal(to_tal_hdr(ctx));
455 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
459 bool tal_add_notifier_(tal_t *ctx, enum tal_notify_type types,
460 void (*callback)(tal_t *, enum tal_notify_type, void *))
462 tal_t *t = debug_tal(to_tal_hdr(ctx));
466 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
467 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
468 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
469 | TAL_NOTIFY_ADD_NOTIFIER
470 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
472 /* Don't call notifier about itself: set types after! */
473 n = add_notifier_property(t, 0, callback);
477 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
482 bool tal_del_notifier_(tal_t *ctx,
483 void (*callback)(tal_t *, enum tal_notify_type, void *))
485 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
488 ret = del_notifier_property(t, callback);
490 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
494 bool tal_del_destructor_(tal_t *ctx, void (*destroy)(void *me))
496 return tal_del_notifier_(ctx, (void *)destroy);
499 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
501 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
502 struct prop_hdr **prop = find_property_ptr(t, NAME);
504 /* Get rid of any old name */
506 struct name *name = (struct name *)*prop;
507 if (is_literal(&name->hdr))
510 *prop = name->hdr.next;
515 if (literal && name[0]) {
518 /* Append literal. */
519 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
520 *p = (struct prop_hdr *)name;
521 } else if (!add_name_property(t, name))
525 notify(t, TAL_NOTIFY_RENAME, name);
529 const char *tal_name(const tal_t *t)
533 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
537 if (is_literal(&n->hdr))
538 return (const char *)n;
542 /* Start one past first child: make stopping natural in circ. list. */
543 static struct tal_hdr *first_child(struct tal_hdr *parent)
545 struct children *child;
547 child = find_property(parent, CHILDREN);
551 return list_top(&child->children, struct tal_hdr, list);
554 tal_t *tal_first(const tal_t *root)
556 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
561 return from_tal_hdr(c);
564 tal_t *tal_next(const tal_t *root, const tal_t *prev)
566 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
571 return from_tal_hdr(c);
573 top = to_tal_hdr_or_null(root);
575 struct tal_hdr *next;
576 struct list_node *end;
578 end = &ignore_destroying_bit(t->parent_child)->children.n;
580 next = list_entry(t->list.next, struct tal_hdr, list);
581 if (&next->list != end)
582 return from_tal_hdr(next);
584 /* OK, go back to parent. */
585 t = ignore_destroying_bit(t->parent_child)->parent;
591 tal_t *tal_parent(const tal_t *ctx)
598 t = debug_tal(to_tal_hdr(ctx));
599 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
601 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
604 bool tal_resize_(tal_t **ctxp, size_t size)
606 struct tal_hdr *old_t, *t;
607 struct children *child;
609 old_t = debug_tal(to_tal_hdr(*ctxp));
611 /* Don't hand silly sizes to realloc. */
612 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
613 call_error("Reallocation size overflow");
617 t = resizefn(old_t, size + sizeof(struct tal_hdr));
619 call_error("Reallocation failure");
623 /* If it didn't move, we're done! */
625 update_bounds(t, size + sizeof(struct tal_hdr));
627 /* Fix up linked list pointers. */
628 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
629 t->list.next->prev = t->list.prev->next = &t->list;
631 /* Fix up child property's parent pointer. */
632 child = find_property(t, CHILDREN);
634 assert(child->parent == old_t);
637 *ctxp = from_tal_hdr(debug_tal(t));
638 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
640 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
645 char *tal_strdup(const tal_t *ctx, const char *p)
647 /* We have to let through NULL for take(). */
648 return tal_dup(ctx, char, p, p ? strlen(p) + 1: 1, 0);
651 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
656 /* We have to let through NULL for take(). */
664 ret = tal_dup(ctx, char, p, len, 1);
670 void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
675 /* Beware overflow! */
676 if (n + extra < n || n + extra + sizeof(struct tal_hdr) < n) {
677 call_error("dup size overflow");
686 if (unlikely(!tal_resize_((void **)&p, n + extra)))
688 if (unlikely(!tal_steal(ctx, p)))
692 ret = tal_alloc_(ctx, n + extra, false, label);
698 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
704 ret = tal_vasprintf(ctx, fmt, ap);
710 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
716 if (!fmt && taken(fmt))
719 /* A decent guess to start. */
720 max = strlen(fmt) * 2;
721 buf = tal_arr(ctx, char, max);
726 ret = vsnprintf(buf, max, fmt, ap2);
731 if (!tal_resize(&buf, max *= 2))
739 void tal_set_backend(void *(*alloc_fn)(size_t size),
740 void *(*resize_fn)(void *, size_t size),
741 void (*free_fn)(void *),
742 void (*error_fn)(const char *msg))
747 resizefn = resize_fn;
754 #ifdef CCAN_TAL_DEBUG
755 static void dump_node(unsigned int indent, const struct tal_hdr *t)
758 const struct prop_hdr *p;
760 for (i = 0; i < indent; i++)
763 for (p = t->prop; p; p = p->next) {
768 printf(" \"%s\"", (const char *)p);
773 c = (struct children *)p;
774 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
776 c->children.n.prev, c->children.n.next);
779 n = (struct name *)p;
780 printf(" NAME(%p):%s", p, n->name);
783 no = (struct notifier *)p;
784 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
787 printf(" **UNKNOWN(%p):%i**", p, p->type);
793 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
795 struct children *children;
799 children = find_property(t, CHILDREN);
803 list_for_each(&children->children, i, list)
804 tal_dump_(level + 1, i);
810 tal_dump_(0, &null_parent.hdr);
812 #endif /* CCAN_TAL_DEBUG */
815 static bool check_err(struct tal_hdr *t, const char *errorstr,
819 /* Try not to malloc: it may be corrupted. */
820 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
821 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
827 static bool check_node(struct children *parent_child,
828 struct tal_hdr *t, const char *errorstr)
831 struct name *name = NULL;
832 struct children *children = NULL;
835 return check_err(t, errorstr, "invalid pointer");
837 if (ignore_destroying_bit(t->parent_child) != parent_child)
838 return check_err(t, errorstr, "incorrect parent");
840 for (p = t->prop; p; p = p->next) {
843 return check_err(t, errorstr,
844 "has extra literal");
845 name = (struct name *)p;
849 return check_err(t, errorstr,
850 "has bad property pointer");
855 return check_err(t, errorstr,
856 "has two child nodes");
857 children = (struct children *)p;
863 return check_err(t, errorstr,
865 name = (struct name *)p;
868 return check_err(t, errorstr, "has unknown property");
874 if (!list_check(&children->children, errorstr))
876 list_for_each(&children->children, i, list) {
877 if (!check_node(children, i, errorstr))
884 bool tal_check(const tal_t *ctx, const char *errorstr)
886 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
888 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
891 bool tal_check(const tal_t *ctx, const char *errorstr)