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 check_bounds(ignore_destroying_bit(t->parent_child));
158 check_bounds(t->list.next);
159 check_bounds(t->list.prev);
160 if (t->prop && !is_literal(t->prop))
161 check_bounds(t->prop);
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 bool del_destructor_property(struct tal_hdr *t,
262 void (*destroy)(void *))
266 for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
267 struct destructor *d;
271 if ((*p)->type != DESTRUCTOR)
273 d = (struct destructor *)*p;
274 if (d->destroy == destroy) {
283 static struct name *add_name_property(struct tal_hdr *t, const char *name)
287 prop = allocate(sizeof(*prop) + strlen(name) + 1);
289 init_property(&prop->hdr, t, NAME);
290 strcpy(prop->name, name);
295 static struct children *add_child_property(struct tal_hdr *parent,
296 struct tal_hdr *child)
298 struct children *prop = allocate(sizeof(*prop));
300 init_property(&prop->hdr, parent, CHILDREN);
301 prop->parent = parent;
302 list_head_init(&prop->children);
307 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
309 struct children *children = find_property(parent, CHILDREN);
312 if (unlikely(!initialized)) {
314 take_allocfail(take_alloc_failed);
317 children = add_child_property(parent, child);
321 list_add(&children->children, &child->list);
322 child->parent_child = children;
326 static void del_tree(struct tal_hdr *t)
328 struct prop_hdr **prop, *p, *next;
330 /* Already being destroyed? Don't loop. */
331 if (unlikely(get_destroying_bit(t->parent_child)))
334 set_destroying_bit(&t->parent_child);
336 /* Carefully call destructors, removing as we go. */
337 while ((prop = find_property_ptr(t, DESTRUCTOR))) {
338 struct destructor *d = (struct destructor *)*prop;
339 d->destroy(from_tal_hdr(t));
344 /* Now free children and groups. */
345 prop = find_property_ptr(t, CHILDREN);
348 struct children *c = (struct children *)*prop;
350 while ((i = list_top(&c->children, struct tal_hdr, list))) {
356 /* Finally free our properties. */
357 for (p = t->prop; p && !is_literal(p); p = next) {
364 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
366 struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
368 child = allocate(sizeof(struct tal_hdr) + size);
372 memset(from_tal_hdr(child), 0, size);
373 child->prop = (void *)label;
374 if (!add_child(parent, child)) {
379 return from_tal_hdr(debug_tal(child));
382 void *tal_free(const tal_t *ctx)
386 int saved_errno = errno;
387 t = debug_tal(to_tal_hdr(ctx));
395 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
398 struct tal_hdr *newpar, *t, *old_parent;
400 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
401 t = debug_tal(to_tal_hdr(ctx));
403 /* Unlink it from old parent. */
405 old_parent = ignore_destroying_bit(t->parent_child)->parent;
407 if (unlikely(!add_child(newpar, t))) {
408 /* We can always add to old parent, becuase it has a
409 * children property already. */
410 if (!add_child(old_parent, t))
419 bool tal_add_destructor_(tal_t *ctx, void (*destroy)(void *me))
421 return add_destructor_property(debug_tal(to_tal_hdr(ctx)), destroy);
424 bool tal_del_destructor_(tal_t *ctx, void (*destroy)(void *me))
426 return del_destructor_property(debug_tal(to_tal_hdr(ctx)), destroy);
429 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
431 struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
432 struct prop_hdr **prop = find_property_ptr(t, NAME);
434 /* Get rid of any old name */
436 struct name *name = (struct name *)*prop;
437 if (is_literal(&name->hdr))
440 *prop = name->hdr.next;
445 if (literal && name[0]) {
448 /* Append literal. */
449 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
450 *p = (struct prop_hdr *)name;
453 if (!add_name_property(t, name))
459 const char *tal_name(const tal_t *t)
463 n = find_property(debug_tal(to_tal_hdr(t)), NAME);
467 if (is_literal(&n->hdr))
468 return (const char *)n;
472 /* Start one past first child: make stopping natural in circ. list. */
473 static struct tal_hdr *first_child(struct tal_hdr *parent)
475 struct children *child;
477 child = find_property(parent, CHILDREN);
481 return list_top(&child->children, struct tal_hdr, list);
484 tal_t *tal_first(const tal_t *root)
486 struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
491 return from_tal_hdr(c);
494 tal_t *tal_next(const tal_t *root, const tal_t *prev)
496 struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
501 return from_tal_hdr(c);
503 top = to_tal_hdr_or_null(root);
505 struct tal_hdr *next;
506 struct list_node *end;
508 end = &ignore_destroying_bit(t->parent_child)->children.n;
510 next = list_entry(t->list.next, struct tal_hdr, list);
511 if (&next->list != end)
512 return from_tal_hdr(next);
514 /* OK, go back to parent. */
515 t = ignore_destroying_bit(t->parent_child)->parent;
521 tal_t *tal_parent(const tal_t *ctx)
528 t = debug_tal(to_tal_hdr(ctx));
529 if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
531 return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
534 bool tal_resize_(tal_t **ctxp, size_t size)
536 struct tal_hdr *old_t, *t;
537 struct children *child;
539 old_t = debug_tal(to_tal_hdr(*ctxp));
541 /* Don't hand silly sizes to realloc. */
542 if (size >> (CHAR_BIT*sizeof(size) - 1)) {
543 call_error("Reallocation size overflow");
547 t = resizefn(old_t, size + sizeof(struct tal_hdr));
549 call_error("Reallocation failure");
553 /* If it didn't move, we're done! */
556 update_bounds(t, size + sizeof(struct tal_hdr));
558 /* Fix up linked list pointers. */
559 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
560 t->list.next->prev = t->list.prev->next = &t->list;
562 /* Fix up child property's parent pointer. */
563 child = find_property(t, CHILDREN);
565 assert(child->parent == old_t);
568 *ctxp = from_tal_hdr(debug_tal(t));
572 char *tal_strdup(const tal_t *ctx, const char *p)
574 /* We have to let through NULL for take(). */
575 return tal_dup(ctx, char, p, p ? strlen(p) + 1: 1, 0);
578 char *tal_strndup(const tal_t *ctx, const char *p, size_t n)
583 /* We have to let through NULL for take(). */
591 ret = tal_dup(ctx, char, p, len, 1);
597 void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
602 /* Beware overflow! */
603 if (n + extra < n || n + extra + sizeof(struct tal_hdr) < n) {
604 call_error("dup size overflow");
613 if (unlikely(!tal_resize_((void **)&p, n + extra)))
615 if (unlikely(!tal_steal(ctx, p)))
619 ret = tal_alloc_(ctx, n + extra, false, label);
625 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...)
631 ret = tal_vasprintf(ctx, fmt, ap);
637 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
643 if (!fmt && taken(fmt))
646 /* A decent guess to start. */
647 max = strlen(fmt) * 2;
648 buf = tal_arr(ctx, char, max);
653 ret = vsnprintf(buf, max, fmt, ap2);
658 if (!tal_resize(&buf, max *= 2))
666 void tal_set_backend(void *(*alloc_fn)(size_t size),
667 void *(*resize_fn)(void *, size_t size),
668 void (*free_fn)(void *),
669 void (*error_fn)(const char *msg))
674 resizefn = resize_fn;
681 #ifdef CCAN_TAL_DEBUG
682 static void dump_node(unsigned int indent, const struct tal_hdr *t)
685 const struct prop_hdr *p;
687 for (i = 0; i < indent; i++)
690 for (p = t->prop; p; p = p->next) {
692 struct destructor *d;
695 printf(" \"%s\"", (const char *)p);
700 c = (struct children *)p;
701 printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
703 c->children.n.prev, c->children.n.next);
706 d = (struct destructor *)p;
707 printf(" DESTRUCTOR(%p):fn=%p", p, d->destroy);
710 n = (struct name *)p;
711 printf(" NAME(%p):%s", p, n->name);
714 printf(" **UNKNOWN(%p):%i**", p, p->type);
720 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
722 struct children *children;
726 children = find_property(t, CHILDREN);
730 list_for_each(&children->children, i, list)
731 tal_dump_(level + 1, i);
737 tal_dump_(0, &null_parent.hdr);
739 #endif /* CCAN_TAL_DEBUG */
742 static bool check_err(struct tal_hdr *t, const char *errorstr,
746 /* Try not to malloc: it may be corrupted. */
747 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
748 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
754 static bool check_node(struct children *parent_child,
755 struct tal_hdr *t, const char *errorstr)
758 struct name *name = NULL;
759 struct children *children = NULL;
762 return check_err(t, errorstr, "invalid pointer");
764 if (ignore_destroying_bit(t->parent_child) != parent_child)
765 return check_err(t, errorstr, "incorrect parent");
767 for (p = t->prop; p; p = p->next) {
770 return check_err(t, errorstr,
771 "has extra literal");
772 name = (struct name *)p;
776 return check_err(t, errorstr,
777 "has bad property pointer");
782 return check_err(t, errorstr,
783 "has two child nodes");
784 children = (struct children *)p;
790 return check_err(t, errorstr,
792 name = (struct name *)p;
795 return check_err(t, errorstr, "has unknown property");
801 if (!list_check(&children->children, errorstr))
803 list_for_each(&children->children, i, list) {
804 if (!check_node(children, i, errorstr))
811 bool tal_check(const tal_t *ctx, const char *errorstr)
813 struct tal_hdr *t = to_tal_hdr_or_null(ctx);
815 return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
818 bool tal_check(const tal_t *ctx, const char *errorstr)