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;
73 static bool initialized = false;
74 /* Count on non-destrutor notifiers; often stays zero. */
75 static size_t notifiers = 0;
77 static inline void COLD call_error(const char *msg)
82 static bool get_destroying_bit(struct children *parent_child)
84 return (size_t)parent_child & 1;
87 static void set_destroying_bit(struct children **parent_child)
89 *parent_child = (void *)((size_t)*parent_child | 1);
92 static struct children *ignore_destroying_bit(struct children *parent_child)
94 return (void *)((size_t)parent_child & ~(size_t)1);
97 /* This means valgrind can see leaks. */
98 static void tal_cleanup(void)
102 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
105 /* Cleanup any taken pointers. */
109 /* For allocation failures inside ccan/take */
110 static void take_alloc_failed(const void *p)
115 /* We carefully start all real properties with a zero byte. */
116 static bool is_literal(const struct prop_hdr *prop)
118 return ((char *)prop)[0] != 0;
122 static const void *bounds_start, *bounds_end;
124 static void update_bounds(const void *new, size_t size)
126 if (unlikely(!bounds_start)) {
128 bounds_end = (char *)new + size;
129 } else if (new < bounds_start)
131 else if ((char *)new + size > (char *)bounds_end)
132 bounds_end = (char *)new + size;
135 static bool in_bounds(const void *p)
138 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
139 || (p >= bounds_start && p <= bounds_end);
142 static void update_bounds(const void *new, size_t size)
146 static bool in_bounds(const void *p)
152 static void check_bounds(const void *p)
155 call_error("Not a valid header");
158 static struct tal_hdr *to_tal_hdr(const void *ctx)
162 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
164 check_bounds(ignore_destroying_bit(t->parent_child));
165 check_bounds(t->list.next);
166 check_bounds(t->list.prev);
167 if (t->prop && !is_literal(t->prop))
168 check_bounds(t->prop);
172 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
175 return &null_parent.hdr;
176 return to_tal_hdr(ctx);
179 static void *from_tal_hdr(const struct tal_hdr *hdr)
181 return (void *)(hdr + 1);
185 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
187 if (hdr == &null_parent.hdr)
189 return from_tal_hdr(hdr);
192 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
194 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
198 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
204 static void notify(const struct tal_hdr *ctx,
205 enum tal_notify_type type, const void *info)
207 const struct prop_hdr *p;
209 for (p = ctx->prop; p; p = p->next) {
214 if (p->type != NOTIFIER)
216 n = (struct notifier *)p;
217 if (n->types & type) {
218 if (n->types & NOTIFY_IS_DESTRUCTOR)
219 n->u.destroy(from_tal_hdr(ctx));
221 n->u.notifyfn(from_tal_hdr(ctx), type,
227 static void *allocate(size_t size)
231 /* Don't hand silly sizes to malloc. */
232 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
233 call_error("allocation size overflow");
239 call_error("allocation failed");
241 update_bounds(ret, size);
245 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
250 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
251 if (is_literal(*p)) {
256 if ((*p)->type == type)
262 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
264 struct prop_hdr **p = find_property_ptr(parent, type);
271 static void init_property(struct prop_hdr *hdr,
272 struct tal_hdr *parent,
276 hdr->next = parent->prop;
280 static struct notifier *add_notifier_property(struct tal_hdr *t,
281 enum tal_notify_type types,
283 enum tal_notify_type,
286 struct notifier *prop = allocate(sizeof(*prop));
288 init_property(&prop->hdr, t, NOTIFIER);
290 prop->u.notifyfn = fn;
295 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
297 enum tal_notify_type,
302 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
307 if ((*p)->type != NOTIFIER)
309 n = (struct notifier *)*p;
310 if (n->u.notifyfn == fn) {
311 enum tal_notify_type types = n->types;
314 return types & ~NOTIFY_IS_DESTRUCTOR;
320 static struct name *add_name_property(struct tal_hdr *t, const char *name)
324 prop = allocate(sizeof(*prop) + strlen(name) + 1);
326 init_property(&prop->hdr, t, NAME);
327 strcpy(prop->name, name);
332 static struct children *add_child_property(struct tal_hdr *parent,
333 struct tal_hdr *child)
335 struct children *prop = allocate(sizeof(*prop));
337 init_property(&prop->hdr, parent, CHILDREN);
338 prop->parent = parent;
339 list_head_init(&prop->children);
344 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
346 struct children *children = find_property(parent, CHILDREN);
349 if (unlikely(!initialized)) {
351 take_allocfail(take_alloc_failed);
354 children = add_child_property(parent, child);
358 list_add(&children->children, &child->list);
359 child->parent_child = children;
363 static void del_tree(struct tal_hdr *t, const tal_t *orig)
365 struct prop_hdr **prop, *p, *next;
367 /* Already being destroyed? Don't loop. */
368 if (unlikely(get_destroying_bit(t->parent_child)))
371 set_destroying_bit(&t->parent_child);
373 /* Call free notifiers. */
374 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
376 /* Now free children and groups. */
377 prop = find_property_ptr(t, CHILDREN);
380 struct children *c = (struct children *)*prop;
382 while ((i = list_top(&c->children, struct tal_hdr, list))) {
388 /* Finally free our properties. */
389 for (p = t->prop; p && !is_literal(p); p = next) {
396 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
398 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
400 child = allocate(sizeof(struct tal_hdr) + size);
404 memset(from_tal_hdr(child), 0, size);
405 child->prop = (void *)label;
406 if (!add_child(parent, child)) {
412 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child));
413 return from_tal_hdr(debug_tal(child));
416 void *tal_free(const tal_t *ctx)
420 int saved_errno = errno;
421 t = debug_tal(to_tal_hdr(ctx));
423 notify(ignore_destroying_bit(t->parent_child)->parent,
424 TAL_NOTIFY_DEL_CHILD, ctx);
432 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
435 struct tal_hdr *newpar, *t, *old_parent;
437 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
438 t = debug_tal(to_tal_hdr(ctx));
440 /* Unlink it from old parent. */
442 old_parent = ignore_destroying_bit(t->parent_child)->parent;
444 if (unlikely(!add_child(newpar, t))) {
445 /* We can always add to old parent, becuase it has a
446 * children property already. */
447 if (!add_child(old_parent, t))
453 notify(t, TAL_NOTIFY_STEAL, new_parent);
458 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
460 tal_t *t = debug_tal(to_tal_hdr(ctx));
461 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
465 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
466 void (*callback)(tal_t *, enum tal_notify_type, void *))
468 tal_t *t = debug_tal(to_tal_hdr(ctx));
472 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
473 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
474 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
475 | TAL_NOTIFY_ADD_NOTIFIER
476 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
478 /* Don't call notifier about itself: set types after! */
479 n = add_notifier_property(t, 0, callback);
484 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
487 if (types != TAL_NOTIFY_FREE)
492 bool tal_del_notifier_(const tal_t *ctx,
493 void (*callback)(tal_t *, enum tal_notify_type, void *))
495 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
496 enum tal_notify_type types;
498 types = del_notifier_property(t, callback);
500 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
501 if (types != TAL_NOTIFY_FREE)
508 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
510 return tal_del_notifier_(ctx, (void *)destroy);
513 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
515 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
516 struct prop_hdr **prop = find_property_ptr(t, NAME);
518 /* Get rid of any old name */
520 struct name *name = (struct name *)*prop;
521 if (is_literal(&name->hdr))
524 *prop = name->hdr.next;
529 if (literal && name[0]) {
532 /* Append literal. */
533 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
534 *p = (struct prop_hdr *)name;
535 } else if (!add_name_property(t, name))
540 notify(t, TAL_NOTIFY_RENAME, name);
544 const char *tal_name(const tal_t *t)
548 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
552 if (is_literal(&n->hdr))
553 return (const char *)n;
557 /* Start one past first child: make stopping natural in circ. list. */
558 static struct tal_hdr *first_child(struct tal_hdr *parent)
560 struct children *child;
562 child = find_property(parent, CHILDREN);
566 return list_top(&child->children, struct tal_hdr, list);
569 tal_t *tal_first(const tal_t *root)
571 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
576 return from_tal_hdr(c);
579 tal_t *tal_next(const tal_t *root, const tal_t *prev)
581 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
586 return from_tal_hdr(c);
588 top = to_tal_hdr_or_null(root);
590 struct tal_hdr *next;
591 struct list_node *end;
593 end = &ignore_destroying_bit(t->parent_child)->children.n;
595 next = list_entry(t->list.next, struct tal_hdr, list);
596 if (&next->list != end)
597 return from_tal_hdr(next);
599 /* OK, go back to parent. */
600 t = ignore_destroying_bit(t->parent_child)->parent;
606 tal_t *tal_parent(const tal_t *ctx)
613 t = debug_tal(to_tal_hdr(ctx));
614 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
616 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
619 bool tal_resize_(tal_t **ctxp, size_t size)
621 struct tal_hdr *old_t, *t;
622 struct children *child;
624 old_t = debug_tal(to_tal_hdr(*ctxp));
626 /* Don't hand silly sizes to realloc. */
627 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
628 call_error("Reallocation size overflow");
632 t = resizefn(old_t, size + sizeof(struct tal_hdr));
634 call_error("Reallocation failure");
638 /* If it didn't move, we're done! */
640 update_bounds(t, size + sizeof(struct tal_hdr));
642 /* Fix up linked list pointers. */
643 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
644 t->list.next->prev = t->list.prev->next = &t->list;
646 /* Fix up child property's parent pointer. */
647 child = find_property(t, CHILDREN);
649 assert(child->parent == old_t);
652 *ctxp = from_tal_hdr(debug_tal(t));
654 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
657 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
662 char *tal_strdup(const tal_t *ctx, const char *p)
664 /* We have to let through NULL for take(). */
665 return tal_dup(ctx, char, p, p ? strlen(p) + 1: 1, 0);
668 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
673 /* We have to let through NULL for take(). */
681 ret = tal_dup(ctx, char, p, len, 1);
687 void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
692 /* Beware overflow! */
693 if (n + extra < n || n + extra + sizeof(struct tal_hdr) < n) {
694 call_error("dup size overflow");
703 if (unlikely(!tal_resize_((void **)&p, n + extra)))
705 if (unlikely(!tal_steal(ctx, p)))
709 ret = tal_alloc_(ctx, n + extra, false, label);
715 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
721 ret = tal_vasprintf(ctx, fmt, ap);
727 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
733 if (!fmt && taken(fmt))
736 /* A decent guess to start. */
737 max = strlen(fmt) * 2;
738 buf = tal_arr(ctx, char, max);
743 ret = vsnprintf(buf, max, fmt, ap2);
748 if (!tal_resize(&buf, max *= 2))
756 void tal_set_backend(void *(*alloc_fn)(size_t size),
757 void *(*resize_fn)(void *, size_t size),
758 void (*free_fn)(void *),
759 void (*error_fn)(const char *msg))
764 resizefn = resize_fn;
771 #ifdef CCAN_TAL_DEBUG
772 static void dump_node(unsigned int indent, const struct tal_hdr *t)
775 const struct prop_hdr *p;
777 for (i = 0; i < indent; i++)
780 for (p = t->prop; p; p = p->next) {
785 printf(" \"%s\"", (const char *)p);
790 c = (struct children *)p;
791 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
793 c->children.n.prev, c->children.n.next);
796 n = (struct name *)p;
797 printf(" NAME(%p):%s", p, n->name);
800 no = (struct notifier *)p;
801 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
804 printf(" **UNKNOWN(%p):%i**", p, p->type);
810 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
812 struct children *children;
816 children = find_property(t, CHILDREN);
820 list_for_each(&children->children, i, list)
821 tal_dump_(level + 1, i);
827 tal_dump_(0, &null_parent.hdr);
829 #endif /* CCAN_TAL_DEBUG */
832 static bool check_err(struct tal_hdr *t, const char *errorstr,
836 /* Try not to malloc: it may be corrupted. */
837 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
838 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
844 static bool check_node(struct children *parent_child,
845 struct tal_hdr *t, const char *errorstr)
848 struct name *name = NULL;
849 struct children *children = NULL;
852 return check_err(t, errorstr, "invalid pointer");
854 if (ignore_destroying_bit(t->parent_child) != parent_child)
855 return check_err(t, errorstr, "incorrect parent");
857 for (p = t->prop; p; p = p->next) {
860 return check_err(t, errorstr,
861 "has extra literal");
862 name = (struct name *)p;
866 return check_err(t, errorstr,
867 "has bad property pointer");
872 return check_err(t, errorstr,
873 "has two child nodes");
874 children = (struct children *)p;
880 return check_err(t, errorstr,
882 name = (struct name *)p;
885 return check_err(t, errorstr, "has unknown property");
891 if (!list_check(&children->children, errorstr))
893 list_for_each(&children->children, i, list) {
894 if (!check_node(children, i, errorstr))
901 bool tal_check(const tal_t *ctx, const char *errorstr)
903 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
905 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
908 bool tal_check(const tal_t *ctx, const char *errorstr)