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,
22 NOTIFIER = 0x00071f00,
27 struct list_node list;
28 struct prop_hdr *prop;
29 struct children *parent_child;
34 struct prop_hdr *next;
38 struct prop_hdr hdr; /* CHILDREN */
39 struct tal_hdr *parent;
40 struct list_head children; /* Head of siblings. */
44 struct prop_hdr hdr; /* NAME */
49 struct prop_hdr hdr; /* LENGTH */
54 struct prop_hdr hdr; /* NOTIFIER */
55 enum tal_notify_type types;
57 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
58 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
65 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
66 &null_parent.c.hdr, NULL },
69 { { &null_parent.c.children.n,
70 &null_parent.c.children.n } }
75 static void *(*allocfn)(size_t size) = malloc;
76 static void *(*resizefn)(void *, size_t size) = realloc;
77 static void (*freefn)(void *) = free;
78 static void (*errorfn)(const char *msg) = (void *)abort;
79 static bool initialized = false;
80 /* Count on non-destrutor notifiers; often stays zero. */
81 static size_t notifiers = 0;
83 static inline void COLD call_error(const char *msg)
88 static bool get_destroying_bit(struct children *parent_child)
90 return (size_t)parent_child & 1;
93 static void set_destroying_bit(struct children **parent_child)
95 *parent_child = (void *)((size_t)*parent_child | 1);
98 static struct children *ignore_destroying_bit(struct children *parent_child)
100 return (void *)((size_t)parent_child & ~(size_t)1);
103 /* This means valgrind can see leaks. */
104 static void tal_cleanup(void)
108 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
111 /* Cleanup any taken pointers. */
115 /* For allocation failures inside ccan/take */
116 static void take_alloc_failed(const void *p)
121 /* We carefully start all real properties with a zero byte. */
122 static bool is_literal(const struct prop_hdr *prop)
124 return ((char *)prop)[0] != 0;
128 static const void *bounds_start, *bounds_end;
130 static void update_bounds(const void *new, size_t size)
132 if (unlikely(!bounds_start)) {
134 bounds_end = (char *)new + size;
135 } else if (new < bounds_start)
137 else if ((char *)new + size > (char *)bounds_end)
138 bounds_end = (char *)new + size;
141 static bool in_bounds(const void *p)
144 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
145 || (p >= bounds_start && p <= bounds_end);
148 static void update_bounds(const void *new, size_t size)
152 static bool in_bounds(const void *p)
158 static void check_bounds(const void *p)
161 call_error("Not a valid header");
164 static struct tal_hdr *to_tal_hdr(const void *ctx)
168 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
170 check_bounds(ignore_destroying_bit(t->parent_child));
171 check_bounds(t->list.next);
172 check_bounds(t->list.prev);
173 if (t->prop && !is_literal(t->prop))
174 check_bounds(t->prop);
178 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
181 return &null_parent.hdr;
182 return to_tal_hdr(ctx);
185 static void *from_tal_hdr(const struct tal_hdr *hdr)
187 return (void *)(hdr + 1);
191 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
193 if (hdr == &null_parent.hdr)
195 return from_tal_hdr(hdr);
198 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
200 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
204 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
210 static void notify(const struct tal_hdr *ctx,
211 enum tal_notify_type type, const void *info)
213 const struct prop_hdr *p;
215 for (p = ctx->prop; p; p = p->next) {
220 if (p->type != NOTIFIER)
222 n = (struct notifier *)p;
223 if (n->types & type) {
224 if (n->types & NOTIFY_IS_DESTRUCTOR)
225 n->u.destroy(from_tal_hdr(ctx));
227 n->u.notifyfn(from_tal_hdr(ctx), type,
233 static void *allocate(size_t size)
235 void *ret = allocfn(size);
237 call_error("allocation failed");
239 update_bounds(ret, size);
243 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
248 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
249 if (is_literal(*p)) {
254 if ((*p)->type == type)
260 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
262 struct prop_hdr **p = find_property_ptr(parent, type);
269 static void init_property(struct prop_hdr *hdr,
270 struct tal_hdr *parent,
274 hdr->next = parent->prop;
278 static struct notifier *add_notifier_property(struct tal_hdr *t,
279 enum tal_notify_type types,
281 enum tal_notify_type,
284 struct notifier *prop = allocate(sizeof(*prop));
286 init_property(&prop->hdr, t, NOTIFIER);
288 prop->u.notifyfn = fn;
293 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
295 enum tal_notify_type,
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) {
309 enum tal_notify_type types = n->types;
312 return types & ~NOTIFY_IS_DESTRUCTOR;
318 static struct name *add_name_property(struct tal_hdr *t, const char *name)
322 prop = allocate(sizeof(*prop) + strlen(name) + 1);
324 init_property(&prop->hdr, t, NAME);
325 strcpy(prop->name, name);
330 static struct length *add_length_property(struct tal_hdr *t, size_t count)
334 prop = allocate(sizeof(*prop));
336 init_property(&prop->hdr, t, LENGTH);
342 static struct children *add_child_property(struct tal_hdr *parent,
343 struct tal_hdr *child)
345 struct children *prop = allocate(sizeof(*prop));
347 init_property(&prop->hdr, parent, CHILDREN);
348 prop->parent = parent;
349 list_head_init(&prop->children);
354 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
356 struct children *children = find_property(parent, CHILDREN);
359 if (unlikely(!initialized)) {
361 take_allocfail(take_alloc_failed);
364 children = add_child_property(parent, child);
368 list_add(&children->children, &child->list);
369 child->parent_child = children;
373 static void del_tree(struct tal_hdr *t, const tal_t *orig)
375 struct prop_hdr **prop, *p, *next;
377 /* Already being destroyed? Don't loop. */
378 if (unlikely(get_destroying_bit(t->parent_child)))
381 set_destroying_bit(&t->parent_child);
383 /* Call free notifiers. */
384 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
386 /* Now free children and groups. */
387 prop = find_property_ptr(t, CHILDREN);
390 struct children *c = (struct children *)*prop;
392 while ((i = list_top(&c->children, struct tal_hdr, list))) {
398 /* Finally free our properties. */
399 for (p = t->prop; p && !is_literal(p); p = next) {
406 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
408 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
410 child = allocate(sizeof(struct tal_hdr) + size);
414 memset(from_tal_hdr(child), 0, size);
415 child->prop = (void *)label;
416 if (!add_child(parent, child)) {
422 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child));
423 return from_tal_hdr(debug_tal(child));
426 static bool adjust_size(size_t *size, size_t count)
428 /* Multiplication wrap */
429 if (count && unlikely(*size * count / *size != count))
434 /* Make sure we don't wrap adding header. */
435 if (*size + sizeof(struct tal_hdr) < sizeof(struct tal_hdr))
439 call_error("allocation size overflow");
443 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
444 bool add_count, const char *label)
448 if (!adjust_size(&size, count))
451 ret = tal_alloc_(ctx, size, clear, label);
452 if (likely(ret) && add_count) {
453 if (unlikely(!add_length_property(to_tal_hdr(ret), count)))
459 void *tal_free(const tal_t *ctx)
463 int saved_errno = errno;
464 t = debug_tal(to_tal_hdr(ctx));
466 notify(ignore_destroying_bit(t->parent_child)->parent,
467 TAL_NOTIFY_DEL_CHILD, ctx);
475 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
478 struct tal_hdr *newpar, *t, *old_parent;
480 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
481 t = debug_tal(to_tal_hdr(ctx));
483 /* Unlink it from old parent. */
485 old_parent = ignore_destroying_bit(t->parent_child)->parent;
487 if (unlikely(!add_child(newpar, t))) {
488 /* We can always add to old parent, becuase it has a
489 * children property already. */
490 if (!add_child(old_parent, t))
496 notify(t, TAL_NOTIFY_STEAL, new_parent);
501 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
503 tal_t *t = debug_tal(to_tal_hdr(ctx));
504 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
508 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
509 void (*callback)(tal_t *, enum tal_notify_type, void *))
511 tal_t *t = debug_tal(to_tal_hdr(ctx));
515 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
516 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
517 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
518 | TAL_NOTIFY_ADD_NOTIFIER
519 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
521 /* Don't call notifier about itself: set types after! */
522 n = add_notifier_property(t, 0, callback);
527 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
530 if (types != TAL_NOTIFY_FREE)
535 bool tal_del_notifier_(const tal_t *ctx,
536 void (*callback)(tal_t *, enum tal_notify_type, void *))
538 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
539 enum tal_notify_type types;
541 types = del_notifier_property(t, callback);
543 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
544 if (types != TAL_NOTIFY_FREE)
551 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
553 return tal_del_notifier_(ctx, (void *)destroy);
556 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
558 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
559 struct prop_hdr **prop = find_property_ptr(t, NAME);
561 /* Get rid of any old name */
563 struct name *name = (struct name *)*prop;
564 if (is_literal(&name->hdr))
567 *prop = name->hdr.next;
572 if (literal && name[0]) {
575 /* Append literal. */
576 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
577 *p = (struct prop_hdr *)name;
578 } else if (!add_name_property(t, name))
583 notify(t, TAL_NOTIFY_RENAME, name);
587 const char *tal_name(const tal_t *t)
591 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
595 if (is_literal(&n->hdr))
596 return (const char *)n;
600 size_t tal_count(const tal_t *ptr)
604 l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
610 /* Start one past first child: make stopping natural in circ. list. */
611 static struct tal_hdr *first_child(struct tal_hdr *parent)
613 struct children *child;
615 child = find_property(parent, CHILDREN);
619 return list_top(&child->children, struct tal_hdr, list);
622 tal_t *tal_first(const tal_t *root)
624 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
629 return from_tal_hdr(c);
632 tal_t *tal_next(const tal_t *root, const tal_t *prev)
634 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
639 return from_tal_hdr(c);
641 top = to_tal_hdr_or_null(root);
643 struct tal_hdr *next;
644 struct list_node *end;
646 end = &ignore_destroying_bit(t->parent_child)->children.n;
648 next = list_entry(t->list.next, struct tal_hdr, list);
649 if (&next->list != end)
650 return from_tal_hdr(next);
652 /* OK, go back to parent. */
653 t = ignore_destroying_bit(t->parent_child)->parent;
659 tal_t *tal_parent(const tal_t *ctx)
666 t = debug_tal(to_tal_hdr(ctx));
667 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
669 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
672 bool tal_resize_(tal_t **ctxp, size_t size, size_t count)
674 struct tal_hdr *old_t, *t;
675 struct children *child;
678 old_t = debug_tal(to_tal_hdr(*ctxp));
680 if (!adjust_size(&size, count))
683 t = resizefn(old_t, size + sizeof(struct tal_hdr));
685 call_error("Reallocation failure");
689 /* If it didn't move, we're done! */
691 update_bounds(t, size + sizeof(struct tal_hdr));
693 /* Fix up linked list pointers. */
694 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
695 t->list.next->prev = t->list.prev->next = &t->list;
697 /* Fix up child property's parent pointer. */
698 child = find_property(t, CHILDREN);
700 assert(child->parent == old_t);
703 *ctxp = from_tal_hdr(debug_tal(t));
705 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
707 len = find_property(t, LENGTH);
711 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
716 char *tal_strdup(const tal_t *ctx, const char *p)
718 /* We have to let through NULL for take(). */
719 return tal_dup_(ctx, p, 1, p ? strlen(p) + 1: 1, 0, false,
720 TAL_LABEL(char, "[]"));
723 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
728 /* We have to let through NULL for take(). */
736 ret = tal_dup_(ctx, p, 1, len, 1, false, TAL_LABEL(char, "[]"));
742 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
743 size_t n, size_t extra, bool add_count,
747 size_t nbytes = size;
749 if (!adjust_size(&nbytes, n)) {
755 /* Beware addition overflow! */
757 call_error("dup size overflow");
766 if (unlikely(!tal_resize_((void **)&p, size, n + extra)))
768 if (unlikely(!tal_steal(ctx, p)))
773 ret = tal_alloc_arr_(ctx, size, n + extra, false, add_count, label);
775 memcpy(ret, p, nbytes);
779 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
785 ret = tal_vasprintf(ctx, fmt, ap);
791 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
797 if (!fmt && taken(fmt))
800 /* A decent guess to start. */
801 max = strlen(fmt) * 2;
802 buf = tal_arr(ctx, char, max);
807 ret = vsnprintf(buf, max, fmt, ap2);
812 if (!tal_resize(&buf, max *= 2))
820 void tal_set_backend(void *(*alloc_fn)(size_t size),
821 void *(*resize_fn)(void *, size_t size),
822 void (*free_fn)(void *),
823 void (*error_fn)(const char *msg))
828 resizefn = resize_fn;
835 #ifdef CCAN_TAL_DEBUG
836 static void dump_node(unsigned int indent, const struct tal_hdr *t)
839 const struct prop_hdr *p;
841 for (i = 0; i < indent; i++)
844 for (p = t->prop; p; p = p->next) {
850 printf(" \"%s\"", (const char *)p);
855 c = (struct children *)p;
856 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
858 c->children.n.prev, c->children.n.next);
861 n = (struct name *)p;
862 printf(" NAME(%p):%s", p, n->name);
865 no = (struct notifier *)p;
866 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
869 l = (struct length *)p;
870 printf(" LENGTH(%p):count=%zu", p, l->count);
873 printf(" **UNKNOWN(%p):%i**", p, p->type);
879 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
881 struct children *children;
885 children = find_property(t, CHILDREN);
889 list_for_each(&children->children, i, list)
890 tal_dump_(level + 1, i);
896 tal_dump_(0, &null_parent.hdr);
898 #endif /* CCAN_TAL_DEBUG */
901 static bool check_err(struct tal_hdr *t, const char *errorstr,
905 /* Try not to malloc: it may be corrupted. */
906 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
907 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
913 static bool check_node(struct children *parent_child,
914 struct tal_hdr *t, const char *errorstr)
917 struct name *name = NULL;
918 struct children *children = NULL;
919 struct length *length = NULL;
922 return check_err(t, errorstr, "invalid pointer");
924 if (ignore_destroying_bit(t->parent_child) != parent_child)
925 return check_err(t, errorstr, "incorrect parent");
927 for (p = t->prop; p; p = p->next) {
930 return check_err(t, errorstr,
931 "has extra literal");
932 name = (struct name *)p;
936 return check_err(t, errorstr,
937 "has bad property pointer");
942 return check_err(t, errorstr,
943 "has two child nodes");
944 children = (struct children *)p;
948 return check_err(t, errorstr,
950 length = (struct length *)p;
956 return check_err(t, errorstr,
958 name = (struct name *)p;
961 return check_err(t, errorstr, "has unknown property");
967 if (!list_check(&children->children, errorstr))
969 list_for_each(&children->children, i, list) {
970 if (!check_node(children, i, errorstr))
977 bool tal_check(const tal_t *ctx, const char *errorstr)
979 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
981 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
984 bool tal_check(const tal_t *ctx, const char *errorstr)