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
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 /* 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 void tal_cleanup(void)
107 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list))) {
109 memset(i, 0, sizeof(*i));
112 /* Cleanup any taken pointers. */
116 /* We carefully start all real properties with a zero byte. */
117 static bool is_literal(const struct prop_hdr *prop)
119 return ((char *)prop)[0] != 0;
123 static const void *bounds_start, *bounds_end;
125 static void update_bounds(const void *new, size_t size)
127 if (unlikely(!bounds_start)) {
129 bounds_end = (char *)new + size;
130 } else if (new < bounds_start)
132 else if ((char *)new + size > (char *)bounds_end)
133 bounds_end = (char *)new + size;
136 static bool in_bounds(const void *p)
139 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
140 || (p >= bounds_start && p <= bounds_end);
143 static void update_bounds(const void *new, size_t size)
147 static bool in_bounds(const void *p)
153 static void check_bounds(const void *p)
156 call_error("Not a valid header");
159 static struct tal_hdr *to_tal_hdr(const void *ctx)
163 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
165 check_bounds(ignore_destroying_bit(t->parent_child));
166 check_bounds(t->list.next);
167 check_bounds(t->list.prev);
168 if (t->prop && !is_literal(t->prop))
169 check_bounds(t->prop);
173 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
176 return &null_parent.hdr;
177 return to_tal_hdr(ctx);
180 static void *from_tal_hdr(const struct tal_hdr *hdr)
182 return (void *)(hdr + 1);
186 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
188 if (hdr == &null_parent.hdr)
190 return from_tal_hdr(hdr);
193 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
195 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
199 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
205 static void notify(const struct tal_hdr *ctx,
206 enum tal_notify_type type, const void *info,
209 const struct prop_hdr *p;
211 for (p = ctx->prop; p; p = p->next) {
216 if (p->type != NOTIFIER)
218 n = (struct notifier *)p;
219 if (n->types & type) {
221 if (n->types & NOTIFY_IS_DESTRUCTOR)
222 n->u.destroy(from_tal_hdr(ctx));
224 n->u.notifyfn(from_tal_hdr(ctx), type,
230 static void *allocate(size_t size)
232 void *ret = allocfn(size);
234 call_error("allocation failed");
236 update_bounds(ret, size);
240 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
245 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
246 if (is_literal(*p)) {
251 if ((*p)->type == type)
257 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
259 struct prop_hdr **p = find_property_ptr(parent, type);
266 static void init_property(struct prop_hdr *hdr,
267 struct tal_hdr *parent,
271 hdr->next = parent->prop;
275 static struct notifier *add_notifier_property(struct tal_hdr *t,
276 enum tal_notify_type types,
278 enum tal_notify_type,
281 struct notifier *prop = allocate(sizeof(*prop));
283 init_property(&prop->hdr, t, NOTIFIER);
285 prop->u.notifyfn = fn;
290 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
292 enum tal_notify_type,
297 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
302 if ((*p)->type != NOTIFIER)
304 n = (struct notifier *)*p;
305 if (n->u.notifyfn == fn) {
306 enum tal_notify_type types = n->types;
309 return types & ~NOTIFY_IS_DESTRUCTOR;
315 static struct name *add_name_property(struct tal_hdr *t, const char *name)
319 prop = allocate(sizeof(*prop) + strlen(name) + 1);
321 init_property(&prop->hdr, t, NAME);
322 strcpy(prop->name, name);
327 static struct children *add_child_property(struct tal_hdr *parent,
328 struct tal_hdr *child UNNEEDED)
330 struct children *prop = allocate(sizeof(*prop));
332 init_property(&prop->hdr, parent, CHILDREN);
333 prop->parent = parent;
334 list_head_init(&prop->children);
339 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
341 struct children *children = find_property(parent, CHILDREN);
344 children = add_child_property(parent, child);
348 list_add(&children->children, &child->list);
349 child->parent_child = children;
353 static void del_tree(struct tal_hdr *t, const tal_t *orig, int saved_errno)
355 struct prop_hdr **prop, *p, *next;
357 /* Already being destroyed? Don't loop. */
358 if (unlikely(get_destroying_bit(t->parent_child)))
361 set_destroying_bit(&t->parent_child);
363 /* Call free notifiers. */
364 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig, saved_errno);
366 /* Now free children and groups. */
367 prop = find_property_ptr(t, CHILDREN);
370 struct children *c = (struct children *)*prop;
372 while ((i = list_top(&c->children, struct tal_hdr, list))) {
374 del_tree(i, orig, saved_errno);
378 /* Finally free our properties. */
379 for (p = t->prop; p && !is_literal(p); p = next) {
381 /* LENGTH is appended, so don't free separately! */
382 if (p->type != LENGTH)
388 static size_t extra_for_length(size_t size)
391 const size_t align = ALIGNOF(struct length);
393 /* Round up size, and add tailer. */
394 extra = ((size + align-1) & ~(align-1)) - size;
395 extra += sizeof(struct length);
399 void *tal_alloc_(const tal_t *ctx, size_t size,
400 bool clear, bool add_length, const char *label)
402 size_t req_size = size;
403 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
405 #ifdef CCAN_TAL_DEBUG
406 /* Always record length if debugging. */
410 size += extra_for_length(size);
412 child = allocate(sizeof(struct tal_hdr) + size);
416 memset(from_tal_hdr(child), 0, req_size);
417 child->prop = (void *)label;
420 struct length *lprop;
421 lprop = (struct length *)((char *)(child+1) + size) - 1;
422 init_property(&lprop->hdr, child, LENGTH);
423 lprop->len = req_size;
425 if (!add_child(parent, child)) {
431 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child), 0);
432 return from_tal_hdr(debug_tal(child));
435 static bool adjust_size(size_t *size, size_t count)
437 const size_t extra = sizeof(struct tal_hdr) + sizeof(struct length)*2;
439 /* Multiplication wrap */
440 if (count && unlikely(*size * count / *size != count))
445 /* Make sure we don't wrap adding header/tailer. */
446 if (*size + extra < extra)
450 call_error("allocation size overflow");
454 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
455 bool add_length, const char *label)
457 if (!adjust_size(&size, count))
460 return tal_alloc_(ctx, size, clear, add_length, label);
463 void *tal_free(const tal_t *ctx)
467 int saved_errno = errno;
468 t = debug_tal(to_tal_hdr(ctx));
470 notify(ignore_destroying_bit(t->parent_child)->parent,
471 TAL_NOTIFY_DEL_CHILD, ctx, saved_errno);
473 del_tree(t, ctx, saved_errno);
479 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
482 struct tal_hdr *newpar, *t, *old_parent;
484 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
485 t = debug_tal(to_tal_hdr(ctx));
487 /* Unlink it from old parent. */
489 old_parent = ignore_destroying_bit(t->parent_child)->parent;
491 if (unlikely(!add_child(newpar, t))) {
492 /* We can always add to old parent, becuase it has a
493 * children property already. */
494 if (!add_child(old_parent, t))
500 notify(t, TAL_NOTIFY_STEAL, new_parent, 0);
505 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
507 tal_t *t = debug_tal(to_tal_hdr(ctx));
508 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
512 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
513 void (*callback)(tal_t *, enum tal_notify_type, void *))
515 tal_t *t = debug_tal(to_tal_hdr(ctx));
519 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
520 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
521 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
522 | TAL_NOTIFY_ADD_NOTIFIER
523 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
525 /* Don't call notifier about itself: set types after! */
526 n = add_notifier_property(t, 0, callback);
531 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback, 0);
534 if (types != TAL_NOTIFY_FREE)
539 bool tal_del_notifier_(const tal_t *ctx,
540 void (*callback)(tal_t *, enum tal_notify_type, void *))
542 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
543 enum tal_notify_type types;
545 types = del_notifier_property(t, callback);
547 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback, 0);
548 if (types != TAL_NOTIFY_FREE)
555 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
557 return tal_del_notifier_(ctx, (void *)destroy);
560 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
562 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
563 struct prop_hdr **prop = find_property_ptr(t, NAME);
565 /* Get rid of any old name */
567 struct name *name = (struct name *)*prop;
568 if (is_literal(&name->hdr))
571 *prop = name->hdr.next;
576 if (literal && name[0]) {
579 /* Append literal. */
580 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
581 *p = (struct prop_hdr *)name;
582 } else if (!add_name_property(t, name))
587 notify(t, TAL_NOTIFY_RENAME, name, 0);
591 const char *tal_name(const tal_t *t)
595 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
599 if (is_literal(&n->hdr))
600 return (const char *)n;
604 size_t tal_len(const tal_t *ptr)
608 l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
614 /* Start one past first child: make stopping natural in circ. list. */
615 static struct tal_hdr *first_child(struct tal_hdr *parent)
617 struct children *child;
619 child = find_property(parent, CHILDREN);
623 return list_top(&child->children, struct tal_hdr, list);
626 tal_t *tal_first(const tal_t *root)
628 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
633 return from_tal_hdr(c);
636 tal_t *tal_next(const tal_t *prev)
638 struct tal_hdr *next, *prevhdr = debug_tal(to_tal_hdr(prev));
639 struct list_head *head;
641 head = &ignore_destroying_bit(prevhdr->parent_child)->children;
642 next = list_next(head, prevhdr, list);
645 return from_tal_hdr(next);
648 tal_t *tal_parent(const tal_t *ctx)
655 t = debug_tal(to_tal_hdr(ctx));
656 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
658 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
661 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
663 struct tal_hdr *old_t, *t;
664 struct children *child;
665 struct prop_hdr **lenp;
669 old_t = debug_tal(to_tal_hdr(*ctxp));
671 if (!adjust_size(&size, count))
674 lenp = find_property_ptr(old_t, LENGTH);
676 /* Copy here, in case we're shrinking! */
677 len = *(struct length *)*lenp;
678 extra = extra_for_length(size);
679 } else /* If we don't have an old length, we can't clear! */
682 t = resizefn(old_t, sizeof(struct tal_hdr) + size + extra);
684 call_error("Reallocation failure");
688 /* Copy length to end. */
690 struct length *new_len;
692 /* Clear between old end and new end. */
693 if (clear && size > len.len) {
694 char *old_end = (char *)(t + 1) + len.len;
695 memset(old_end, 0, size - len.len);
698 new_len = (struct length *)((char *)(t + 1) + size
699 + extra - sizeof(len));
703 /* Be careful replacing next ptr; could be old hdr. */
704 if (lenp == &old_t->prop)
705 t->prop = &new_len->hdr;
707 *lenp = &new_len->hdr;
710 update_bounds(t, sizeof(struct tal_hdr) + size + extra);
712 /* If it didn't move, we're done! */
714 /* Fix up linked list pointers. */
715 t->list.next->prev = t->list.prev->next = &t->list;
717 /* Fix up child property's parent pointer. */
718 child = find_property(t, CHILDREN);
720 assert(child->parent == old_t);
723 *ctxp = from_tal_hdr(debug_tal(t));
725 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t), 0);
728 notify(t, TAL_NOTIFY_RESIZE, (void *)size, 0);
733 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
739 l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
742 /* Check for additive overflow */
743 if (old_len + count * size < old_len) {
744 call_error("dup size overflow");
748 /* Don't point src inside thing we're expanding! */
750 || (char *)src >= (char *)(*ctxp) + old_len);
752 if (!tal_resize_(ctxp, size, old_len/size + count, false))
755 memcpy((char *)*ctxp + old_len, src, count * size);
764 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
765 size_t n, size_t extra, bool add_length,
769 size_t nbytes = size;
771 if (!adjust_size(&nbytes, n)) {
777 /* Beware addition overflow! */
779 call_error("dup size overflow");
788 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
790 if (unlikely(!tal_steal(ctx, p)))
795 ret = tal_alloc_arr_(ctx, size, n + extra, false, add_length, label);
797 memcpy(ret, p, nbytes);
801 void tal_set_backend(void *(*alloc_fn)(size_t size),
802 void *(*resize_fn)(void *, size_t size),
803 void (*free_fn)(void *),
804 void (*error_fn)(const char *msg))
809 resizefn = resize_fn;
816 #ifdef CCAN_TAL_DEBUG
817 static void dump_node(unsigned int indent, const struct tal_hdr *t)
820 const struct prop_hdr *p;
822 for (i = 0; i < indent; i++)
825 for (p = t->prop; p; p = p->next) {
831 printf(" \"%s\"", (const char *)p);
836 c = (struct children *)p;
837 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
839 c->children.n.prev, c->children.n.next);
842 n = (struct name *)p;
843 printf(" NAME(%p):%s", p, n->name);
846 no = (struct notifier *)p;
847 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
850 l = (struct length *)p;
851 printf(" LENGTH(%p):len=%zu", p, l->len);
854 printf(" **UNKNOWN(%p):%i**", p, p->type);
860 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
862 struct children *children;
866 children = find_property(t, CHILDREN);
870 list_for_each(&children->children, i, list)
871 tal_dump_(level + 1, i);
877 tal_dump_(0, &null_parent.hdr);
879 #endif /* CCAN_TAL_DEBUG */
882 static bool check_err(struct tal_hdr *t, const char *errorstr,
886 /* Try not to malloc: it may be corrupted. */
887 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
888 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
894 static bool check_node(struct children *parent_child,
895 struct tal_hdr *t, const char *errorstr)
898 struct name *name = NULL;
899 struct children *children = NULL;
900 struct length *length = NULL;
903 return check_err(t, errorstr, "invalid pointer");
905 if (ignore_destroying_bit(t->parent_child) != parent_child)
906 return check_err(t, errorstr, "incorrect parent");
908 for (p = t->prop; p; p = p->next) {
911 return check_err(t, errorstr,
912 "has extra literal");
916 return check_err(t, errorstr,
917 "has bad property pointer");
922 return check_err(t, errorstr,
923 "has two child nodes");
924 children = (struct children *)p;
928 return check_err(t, errorstr,
930 length = (struct length *)p;
936 return check_err(t, errorstr,
938 name = (struct name *)p;
941 return check_err(t, errorstr, "has unknown property");
947 if (!list_check(&children->children, errorstr))
949 list_for_each(&children->children, i, list) {
950 if (!check_node(children, i, errorstr))
957 bool tal_check(const tal_t *ctx, const char *errorstr)
959 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
961 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
964 bool tal_check(const tal_t *ctx, const char *errorstr)