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 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 children *add_child_property(struct tal_hdr *parent,
331 struct tal_hdr *child)
333 struct children *prop = allocate(sizeof(*prop));
335 init_property(&prop->hdr, parent, CHILDREN);
336 prop->parent = parent;
337 list_head_init(&prop->children);
342 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
344 struct children *children = find_property(parent, CHILDREN);
347 if (unlikely(!initialized)) {
349 take_allocfail(take_alloc_failed);
352 children = add_child_property(parent, child);
356 list_add(&children->children, &child->list);
357 child->parent_child = children;
361 static void del_tree(struct tal_hdr *t, const tal_t *orig)
363 struct prop_hdr **prop, *p, *next;
365 /* Already being destroyed? Don't loop. */
366 if (unlikely(get_destroying_bit(t->parent_child)))
369 set_destroying_bit(&t->parent_child);
371 /* Call free notifiers. */
372 notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
374 /* Now free children and groups. */
375 prop = find_property_ptr(t, CHILDREN);
378 struct children *c = (struct children *)*prop;
380 while ((i = list_top(&c->children, struct tal_hdr, list))) {
386 /* Finally free our properties. */
387 for (p = t->prop; p && !is_literal(p); p = next) {
389 /* LENGTH is appended, so don't free separately! */
390 if (p->type != LENGTH)
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 static bool adjust_size(size_t *size, size_t count)
418 const size_t extra = sizeof(struct tal_hdr) + sizeof(struct length)*2;
420 /* Multiplication wrap */
421 if (count && unlikely(*size * count / *size != count))
426 /* Make sure we don't wrap adding header/tailer. */
427 if (*size + extra < extra)
431 call_error("allocation size overflow");
435 static size_t extra_for_length(size_t size)
438 const size_t align = ALIGNOF(struct length);
440 /* Round up size, and add tailer. */
441 extra = ((size + align-1) & ~(align-1)) - size;
442 extra += sizeof(struct length);
446 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
447 bool add_count, const char *label)
451 if (!adjust_size(&size, count))
455 size += extra_for_length(size);
457 ret = tal_alloc_(ctx, size, clear, label);
462 struct length *lprop;
463 lprop = (struct length *)((char *)ret + size) - 1;
464 init_property(&lprop->hdr, to_tal_hdr(ret), LENGTH);
465 lprop->count = count;
470 void *tal_free(const tal_t *ctx)
474 int saved_errno = errno;
475 t = debug_tal(to_tal_hdr(ctx));
477 notify(ignore_destroying_bit(t->parent_child)->parent,
478 TAL_NOTIFY_DEL_CHILD, ctx);
486 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
489 struct tal_hdr *newpar, *t, *old_parent;
491 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
492 t = debug_tal(to_tal_hdr(ctx));
494 /* Unlink it from old parent. */
496 old_parent = ignore_destroying_bit(t->parent_child)->parent;
498 if (unlikely(!add_child(newpar, t))) {
499 /* We can always add to old parent, becuase it has a
500 * children property already. */
501 if (!add_child(old_parent, t))
507 notify(t, TAL_NOTIFY_STEAL, new_parent);
512 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
514 tal_t *t = debug_tal(to_tal_hdr(ctx));
515 return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
519 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
520 void (*callback)(tal_t *, enum tal_notify_type, void *))
522 tal_t *t = debug_tal(to_tal_hdr(ctx));
526 assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
527 | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
528 | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
529 | TAL_NOTIFY_ADD_NOTIFIER
530 | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
532 /* Don't call notifier about itself: set types after! */
533 n = add_notifier_property(t, 0, callback);
538 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
541 if (types != TAL_NOTIFY_FREE)
546 bool tal_del_notifier_(const tal_t *ctx,
547 void (*callback)(tal_t *, enum tal_notify_type, void *))
549 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
550 enum tal_notify_type types;
552 types = del_notifier_property(t, callback);
554 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
555 if (types != TAL_NOTIFY_FREE)
562 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
564 return tal_del_notifier_(ctx, (void *)destroy);
567 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
569 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
570 struct prop_hdr **prop = find_property_ptr(t, NAME);
572 /* Get rid of any old name */
574 struct name *name = (struct name *)*prop;
575 if (is_literal(&name->hdr))
578 *prop = name->hdr.next;
583 if (literal && name[0]) {
586 /* Append literal. */
587 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
588 *p = (struct prop_hdr *)name;
589 } else if (!add_name_property(t, name))
594 notify(t, TAL_NOTIFY_RENAME, name);
598 const char *tal_name(const tal_t *t)
602 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
606 if (is_literal(&n->hdr))
607 return (const char *)n;
611 size_t tal_count(const tal_t *ptr)
615 l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
621 /* Start one past first child: make stopping natural in circ. list. */
622 static struct tal_hdr *first_child(struct tal_hdr *parent)
624 struct children *child;
626 child = find_property(parent, CHILDREN);
630 return list_top(&child->children, struct tal_hdr, list);
633 tal_t *tal_first(const tal_t *root)
635 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
640 return from_tal_hdr(c);
643 tal_t *tal_next(const tal_t *root, const tal_t *prev)
645 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
650 return from_tal_hdr(c);
652 top = to_tal_hdr_or_null(root);
654 struct tal_hdr *next;
655 struct list_node *end;
657 end = &ignore_destroying_bit(t->parent_child)->children.n;
659 next = list_entry(t->list.next, struct tal_hdr, list);
660 if (&next->list != end)
661 return from_tal_hdr(next);
663 /* OK, go back to parent. */
664 t = ignore_destroying_bit(t->parent_child)->parent;
670 tal_t *tal_parent(const tal_t *ctx)
677 t = debug_tal(to_tal_hdr(ctx));
678 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
680 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
683 bool tal_resize_(tal_t **ctxp, size_t size, size_t count)
685 struct tal_hdr *old_t, *t;
686 struct children *child;
687 struct prop_hdr **lenp;
691 old_t = debug_tal(to_tal_hdr(*ctxp));
693 if (!adjust_size(&size, count))
696 lenp = find_property_ptr(old_t, LENGTH);
698 /* Copy here, in case we're shrinking! */
699 len = *(struct length *)*lenp;
700 extra = extra_for_length(size);
703 t = resizefn(old_t, sizeof(struct tal_hdr) + size + extra);
705 call_error("Reallocation failure");
709 /* Copy length to end. */
711 struct length *new_len;
713 new_len = (struct length *)((char *)(t + 1) + size);
717 /* Be careful replacing next ptr; could be old hdr. */
718 if (lenp == &old_t->prop)
719 t->prop = &new_len->hdr;
721 *lenp = &new_len->hdr;
724 update_bounds(t, sizeof(struct tal_hdr) + size + extra);
726 /* If it didn't move, we're done! */
728 /* Fix up linked list pointers. */
729 t->list.next->prev = t->list.prev->next = &t->list;
731 /* Fix up child property's parent pointer. */
732 child = find_property(t, CHILDREN);
734 assert(child->parent == old_t);
737 *ctxp = from_tal_hdr(debug_tal(t));
739 notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
742 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
747 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
753 l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
754 old_count = l->count;
756 /* Check for additive overflow */
757 if (old_count + count < count) {
758 call_error("dup size overflow");
762 /* Don't point src inside thing we're expanding! */
764 || (char *)src >= (char *)(*ctxp) + (size * old_count));
766 if (!tal_resize_(ctxp, size, old_count + count))
769 memcpy((char *)*ctxp + size * old_count, src, count * size);
778 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
779 size_t n, size_t extra, bool add_count,
783 size_t nbytes = size;
785 if (!adjust_size(&nbytes, n)) {
791 /* Beware addition overflow! */
793 call_error("dup size overflow");
802 if (unlikely(!tal_resize_((void **)&p, size, n + extra)))
804 if (unlikely(!tal_steal(ctx, p)))
809 ret = tal_alloc_arr_(ctx, size, n + extra, false, add_count, label);
811 memcpy(ret, p, nbytes);
815 void tal_set_backend(void *(*alloc_fn)(size_t size),
816 void *(*resize_fn)(void *, size_t size),
817 void (*free_fn)(void *),
818 void (*error_fn)(const char *msg))
823 resizefn = resize_fn;
830 #ifdef CCAN_TAL_DEBUG
831 static void dump_node(unsigned int indent, const struct tal_hdr *t)
834 const struct prop_hdr *p;
836 for (i = 0; i < indent; i++)
839 for (p = t->prop; p; p = p->next) {
845 printf(" \"%s\"", (const char *)p);
850 c = (struct children *)p;
851 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
853 c->children.n.prev, c->children.n.next);
856 n = (struct name *)p;
857 printf(" NAME(%p):%s", p, n->name);
860 no = (struct notifier *)p;
861 printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
864 l = (struct length *)p;
865 printf(" LENGTH(%p):count=%zu", p, l->count);
868 printf(" **UNKNOWN(%p):%i**", p, p->type);
874 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
876 struct children *children;
880 children = find_property(t, CHILDREN);
884 list_for_each(&children->children, i, list)
885 tal_dump_(level + 1, i);
891 tal_dump_(0, &null_parent.hdr);
893 #endif /* CCAN_TAL_DEBUG */
896 static bool check_err(struct tal_hdr *t, const char *errorstr,
900 /* Try not to malloc: it may be corrupted. */
901 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
902 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
908 static bool check_node(struct children *parent_child,
909 struct tal_hdr *t, const char *errorstr)
912 struct name *name = NULL;
913 struct children *children = NULL;
914 struct length *length = NULL;
917 return check_err(t, errorstr, "invalid pointer");
919 if (ignore_destroying_bit(t->parent_child) != parent_child)
920 return check_err(t, errorstr, "incorrect parent");
922 for (p = t->prop; p; p = p->next) {
925 return check_err(t, errorstr,
926 "has extra literal");
927 name = (struct name *)p;
931 return check_err(t, errorstr,
932 "has bad property pointer");
937 return check_err(t, errorstr,
938 "has two child nodes");
939 children = (struct children *)p;
943 return check_err(t, errorstr,
945 length = (struct length *)p;
951 return check_err(t, errorstr,
953 name = (struct name *)p;
956 return check_err(t, errorstr, "has unknown property");
962 if (!list_check(&children->children, errorstr))
964 list_for_each(&children->children, i, list) {
965 if (!check_node(children, i, errorstr))
972 bool tal_check(const tal_t *ctx, const char *errorstr)
974 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
976 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
979 bool tal_check(const tal_t *ctx, const char *errorstr)