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 /* 32-bit type field, first byte 0 in either endianness. */
18 CHILDREN = 0x00c1d500,
19 DESTRUCTOR = 0x00de5700,
24 struct list_node list;
25 struct prop_hdr *prop;
26 struct children *parent_child;
31 struct prop_hdr *next;
35 struct prop_hdr hdr; /* CHILDREN */
36 struct tal_hdr *parent;
37 struct list_head children; /* Head of siblings. */
41 struct prop_hdr hdr; /* DESTRUCTOR */
42 void (*destroy)(void *me);
46 struct prop_hdr hdr; /* NAME */
53 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
54 &null_parent.c.hdr, NULL },
57 { { &null_parent.c.children.n,
58 &null_parent.c.children.n } }
63 static void *(*allocfn)(size_t size) = malloc;
64 static void *(*resizefn)(void *, size_t size) = realloc;
65 static void (*freefn)(void *) = free;
66 static void (*errorfn)(const char *msg) = (void *)abort;
68 static inline void COLD call_error(const char *msg)
73 static bool get_destroying_bit(struct children *parent_child)
75 return (size_t)parent_child & 1;
78 static void set_destroying_bit(struct children **parent_child)
80 *parent_child = (void *)((size_t)*parent_child | 1);
83 static struct children *ignore_destroying_bit(struct children *parent_child)
85 return (void *)((size_t)parent_child & ~(size_t)1);
88 static bool initialized = false;
90 /* This means valgrind can see leaks. */
91 static void tal_cleanup(void)
95 while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
98 /* Cleanup any taken pointers. */
102 /* For allocation failures inside ccan/take */
103 static void take_alloc_failed(const void *p)
108 /* We carefully start all real properties with a zero byte. */
109 static bool is_literal(const struct prop_hdr *prop)
111 return ((char *)prop)[0] != 0;
115 static const void *bounds_start, *bounds_end;
117 static void update_bounds(const void *new, size_t size)
119 if (unlikely(!bounds_start)) {
121 bounds_end = (char *)new + size;
122 } else if (new < bounds_start)
124 else if ((char *)new + size > (char *)bounds_end)
125 bounds_end = (char *)new + size;
128 static bool in_bounds(const void *p)
131 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
132 || (p >= bounds_start && p <= bounds_end);
135 static void update_bounds(const void *new, size_t size)
139 static bool in_bounds(const void *p)
145 static void check_bounds(const void *p)
148 call_error("Not a valid header");
151 static struct tal_hdr *to_tal_hdr(const void *ctx)
155 t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
157 if (t->prop && !is_literal(t->prop))
158 check_bounds(t->prop);
159 check_bounds(ignore_destroying_bit(t->parent_child));
160 check_bounds(t->list.next);
161 check_bounds(t->list.prev);
165 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
168 return &null_parent.hdr;
169 return to_tal_hdr(ctx);
172 static void *from_tal_hdr(struct tal_hdr *hdr)
178 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
180 if (hdr == &null_parent.hdr)
182 return from_tal_hdr(hdr);
185 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
187 tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
191 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
197 static void *allocate(size_t size)
201 /* Don't hand silly sizes to malloc. */
202 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
203 call_error("allocation size overflow");
209 call_error("allocation failed");
211 update_bounds(ret, size);
215 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
220 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
221 if (is_literal(*p)) {
226 if ((*p)->type == type)
232 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
234 struct prop_hdr **p = find_property_ptr(parent, type);
241 static void init_property(struct prop_hdr *hdr,
242 struct tal_hdr *parent,
246 hdr->next = parent->prop;
250 static struct destructor *add_destructor_property(struct tal_hdr *t,
251 void (*destroy)(void *))
253 struct destructor *prop = allocate(sizeof(*prop));
255 init_property(&prop->hdr, t, DESTRUCTOR);
256 prop->destroy = destroy;
261 static struct name *add_name_property(struct tal_hdr *t, const char *name)
265 prop = allocate(sizeof(*prop) + strlen(name) + 1);
267 init_property(&prop->hdr, t, NAME);
268 strcpy(prop->name, name);
273 static struct children *add_child_property(struct tal_hdr *parent,
274 struct tal_hdr *child)
276 struct children *prop = allocate(sizeof(*prop));
278 init_property(&prop->hdr, parent, CHILDREN);
279 prop->parent = parent;
280 list_head_init(&prop->children);
285 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
287 struct children *children = find_property(parent, CHILDREN);
290 if (unlikely(!initialized)) {
292 take_allocfail(take_alloc_failed);
295 children = add_child_property(parent, child);
299 list_add(&children->children, &child->list);
300 child->parent_child = children;
304 static void del_tree(struct tal_hdr *t)
306 struct prop_hdr **prop, *p, *next;
308 /* Already being destroyed? Don't loop. */
309 if (unlikely(get_destroying_bit(t->parent_child)))
312 set_destroying_bit(&t->parent_child);
314 /* Carefully call destructors, removing as we go. */
315 while ((prop = find_property_ptr(t, DESTRUCTOR))) {
316 struct destructor *d = (struct destructor *)*prop;
317 d->destroy(from_tal_hdr(t));
322 /* Now free children and groups. */
323 prop = find_property_ptr(t, CHILDREN);
326 struct children *c = (struct children *)*prop;
328 while ((i = list_top(&c->children, struct tal_hdr, list))) {
334 /* Finally free our properties. */
335 for (p = t->prop; p && !is_literal(p); p = next) {
342 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
344 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
346 child = allocate(sizeof(struct tal_hdr) + size);
350 memset(from_tal_hdr(child), 0, size);
351 child->prop = (void *)label;
352 if (!add_child(parent, child)) {
357 return from_tal_hdr(debug_tal(child));
360 void *tal_free(const tal_t *ctx)
364 int saved_errno = errno;
365 t = debug_tal(to_tal_hdr(ctx));
373 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
376 struct tal_hdr *newpar, *t, *old_parent;
378 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
379 t = debug_tal(to_tal_hdr(ctx));
381 /* Unlink it from old parent. */
383 old_parent = ignore_destroying_bit(t->parent_child)->parent;
385 if (unlikely(!add_child(newpar, t))) {
386 /* We can always add to old parent, becuase it has a
387 * children property already. */
388 if (!add_child(old_parent, t))
397 bool tal_add_destructor_(tal_t *ctx, void (*destroy)(void *me))
399 return add_destructor_property(debug_tal(to_tal_hdr(ctx)), destroy);
402 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
404 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
405 struct prop_hdr **prop = find_property_ptr(t, NAME);
407 /* Get rid of any old name */
409 struct name *name = (struct name *)*prop;
410 if (is_literal(&name->hdr))
413 *prop = name->hdr.next;
418 if (literal && name[0]) {
421 /* Append literal. */
422 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
423 *p = (struct prop_hdr *)name;
426 if (!add_name_property(t, name))
432 const char *tal_name(const tal_t *t)
436 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
440 if (is_literal(&n->hdr))
441 return (const char *)n;
445 /* Start one past first child: make stopping natural in circ. list. */
446 static struct tal_hdr *first_child(struct tal_hdr *parent)
448 struct children *child;
450 child = find_property(parent, CHILDREN);
454 return list_top(&child->children, struct tal_hdr, list);
457 tal_t *tal_first(const tal_t *root)
459 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
464 return from_tal_hdr(c);
467 tal_t *tal_next(const tal_t *root, const tal_t *prev)
469 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
474 return from_tal_hdr(c);
476 top = to_tal_hdr_or_null(root);
478 struct tal_hdr *next;
479 struct list_node *end;
481 end = &ignore_destroying_bit(t->parent_child)->children.n;
483 next = list_entry(t->list.next, struct tal_hdr, list);
484 if (&next->list != end)
485 return from_tal_hdr(next);
487 /* OK, go back to parent. */
488 t = ignore_destroying_bit(t->parent_child)->parent;
494 tal_t *tal_parent(const tal_t *ctx)
501 t = debug_tal(to_tal_hdr(ctx));
502 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
504 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
507 bool tal_resize_(tal_t **ctxp, size_t size)
509 struct tal_hdr *old_t, *t;
510 struct children *child;
512 old_t = debug_tal(to_tal_hdr(*ctxp));
514 /* Don't hand silly sizes to realloc. */
515 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
516 call_error("Reallocation size overflow");
520 t = resizefn(old_t, size + sizeof(struct tal_hdr));
522 call_error("Reallocation failure");
526 /* If it didn't move, we're done! */
529 update_bounds(t, size + sizeof(struct tal_hdr));
531 /* Fix up linked list pointers. */
532 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
533 t->list.next->prev = t->list.prev->next = &t->list;
535 /* Fix up child property's parent pointer. */
536 child = find_property(t, CHILDREN);
538 assert(child->parent == old_t);
541 *ctxp = from_tal_hdr(debug_tal(t));
545 char *tal_strdup(const tal_t *ctx, const char *p)
547 /* We have to let through NULL for take(). */
548 return tal_dup(ctx, char, p, p ? strlen(p) + 1: 1, 0);
551 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
556 /* We have to let through NULL for take(). */
564 ret = tal_dup(ctx, char, p, len, 1);
570 void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
575 /* Beware overflow! */
576 if (n + extra < n || n + extra + sizeof(struct tal_hdr) < n) {
577 call_error("dup size overflow");
586 if (unlikely(!tal_resize_((void **)&p, n + extra)))
588 if (unlikely(!tal_steal(ctx, p)))
592 ret = tal_alloc_(ctx, n + extra, false, label);
598 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
604 ret = tal_vasprintf(ctx, fmt, ap);
610 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
616 if (!fmt && taken(fmt))
619 /* A decent guess to start. */
620 max = strlen(fmt) * 2;
621 buf = tal_arr(ctx, char, max);
626 ret = vsnprintf(buf, max, fmt, ap2);
631 if (!tal_resize(&buf, max *= 2))
639 void tal_set_backend(void *(*alloc_fn)(size_t size),
640 void *(*resize_fn)(void *, size_t size),
641 void (*free_fn)(void *),
642 void (*error_fn)(const char *msg))
647 resizefn = resize_fn;
654 #ifdef CCAN_TAL_DEBUG
655 static void dump_node(unsigned int indent, const struct tal_hdr *t)
658 const struct prop_hdr *p;
660 for (i = 0; i < indent; i++)
663 for (p = t->prop; p; p = p->next) {
665 struct destructor *d;
668 printf(" \"%s\"", (const char *)p);
673 c = (struct children *)p;
674 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
676 c->children.n.prev, c->children.n.next);
679 d = (struct destructor *)p;
680 printf(" DESTRUCTOR(%p):fn=%p", p, d->destroy);
683 n = (struct name *)p;
684 printf(" NAME(%p):%s", p, n->name);
687 printf(" **UNKNOWN(%p):%i**", p, p->type);
693 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
695 struct children *children;
699 children = find_property(t, CHILDREN);
703 list_for_each(&children->children, i, list)
704 tal_dump_(level + 1, i);
710 tal_dump_(0, &null_parent.hdr);
712 #endif /* CCAN_TAL_DEBUG */
715 static bool check_err(struct tal_hdr *t, const char *errorstr,
719 /* Try not to malloc: it may be corrupted. */
720 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
721 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
727 static bool check_node(struct children *parent_child,
728 struct tal_hdr *t, const char *errorstr)
731 struct name *name = NULL;
732 struct children *children = NULL;
735 return check_err(t, errorstr, "invalid pointer");
737 if (ignore_destroying_bit(t->parent_child) != parent_child)
738 return check_err(t, errorstr, "incorrect parent");
740 for (p = t->prop; p; p = p->next) {
743 return check_err(t, errorstr,
744 "has extra literal");
745 name = (struct name *)p;
749 return check_err(t, errorstr,
750 "has bad property pointer");
755 return check_err(t, errorstr,
756 "has two child nodes");
757 children = (struct children *)p;
763 return check_err(t, errorstr,
765 name = (struct name *)p;
768 return check_err(t, errorstr, "has unknown property");
774 if (!list_check(&children->children, errorstr))
776 list_for_each(&children->children, i, list) {
777 if (!check_node(children, i, errorstr))
784 bool tal_check(const tal_t *ctx, const char *errorstr)
786 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
788 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
791 bool tal_check(const tal_t *ctx, const char *errorstr)