4f7d56784dd9f2ffb46131f98b7f6288981d1c62
[ccan] / ccan / tal / tal.c
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 <assert.h>
7 #include <stdio.h>
8 #include <stddef.h>
9 #include <string.h>
10 #include <limits.h>
11 #include <errno.h>
12
13 //#define TAL_DEBUG 1
14
15 #define NOTIFY_IS_DESTRUCTOR 512
16
17 /* 32-bit type field, first byte 0 in either endianness. */
18 enum prop_type {
19         CHILDREN = 0x00c1d500,
20         NAME = 0x00111100,
21         NOTIFIER = 0x00071f00,
22         LENGTH = 0x00515300
23 };
24
25 struct tal_hdr {
26         struct list_node list;
27         struct prop_hdr *prop;
28         struct children *parent_child;
29 };
30
31 struct prop_hdr {
32         enum prop_type type;
33         struct prop_hdr *next;
34 };
35
36 struct children {
37         struct prop_hdr hdr; /* CHILDREN */
38         struct tal_hdr *parent;
39         struct list_head children; /* Head of siblings. */
40 };
41
42 struct name {
43         struct prop_hdr hdr; /* NAME */
44         char name[];
45 };
46
47 struct length {
48         struct prop_hdr hdr; /* LENGTH */
49         size_t count;
50 };
51
52 struct notifier {
53         struct prop_hdr hdr; /* NOTIFIER */
54         enum tal_notify_type types;
55         union {
56                 void (*notifyfn)(tal_t *, enum tal_notify_type, void *);
57                 void (*destroy)(tal_t *); /* If NOTIFY_IS_DESTRUCTOR set */
58         } u;
59 };
60
61 static struct {
62         struct tal_hdr hdr;
63         struct children c;
64 } null_parent = { { { &null_parent.hdr.list, &null_parent.hdr.list },
65                     &null_parent.c.hdr, NULL },
66                   { { CHILDREN, NULL },
67                     &null_parent.hdr,
68                     { { &null_parent.c.children.n,
69                         &null_parent.c.children.n } }
70                   }
71 };
72
73
74 static void *(*allocfn)(size_t size) = malloc;
75 static void *(*resizefn)(void *, size_t size) = realloc;
76 static void (*freefn)(void *) = free;
77 static void (*errorfn)(const char *msg) = (void *)abort;
78 static bool initialized = false;
79 /* Count on non-destrutor notifiers; often stays zero. */
80 static size_t notifiers = 0;
81
82 static inline void COLD call_error(const char *msg)
83 {
84         errorfn(msg);
85 }
86
87 static bool get_destroying_bit(struct children *parent_child)
88 {
89         return (size_t)parent_child & 1;
90 }
91
92 static void set_destroying_bit(struct children **parent_child)
93 {
94         *parent_child = (void *)((size_t)*parent_child | 1);
95 }
96
97 static struct children *ignore_destroying_bit(struct children *parent_child)
98 {
99         return (void *)((size_t)parent_child & ~(size_t)1);
100 }
101
102 /* This means valgrind can see leaks. */
103 static void tal_cleanup(void)
104 {
105         struct tal_hdr *i;
106
107         while ((i = list_top(&null_parent.c.children, struct tal_hdr, list)))
108                 list_del(&i->list);
109
110         /* Cleanup any taken pointers. */
111         take_cleanup();
112 }
113
114 /* For allocation failures inside ccan/take */
115 static void take_alloc_failed(const void *p)
116 {
117         tal_free(p);
118 }
119
120 /* We carefully start all real properties with a zero byte. */
121 static bool is_literal(const struct prop_hdr *prop)
122 {
123         return ((char *)prop)[0] != 0;
124 }
125
126 #ifndef NDEBUG
127 static const void *bounds_start, *bounds_end;
128
129 static void update_bounds(const void *new, size_t size)
130 {
131         if (unlikely(!bounds_start)) {
132                 bounds_start = new;
133                 bounds_end = (char *)new + size;
134         } else if (new < bounds_start)
135                 bounds_start = new;
136         else if ((char *)new + size > (char *)bounds_end)
137                 bounds_end = (char *)new + size;
138 }
139
140 static bool in_bounds(const void *p)
141 {
142         return !p
143                 || (p >= (void *)&null_parent && p <= (void *)(&null_parent + 1))
144                 || (p >= bounds_start && p <= bounds_end);
145 }
146 #else
147 static void update_bounds(const void *new, size_t size)
148 {
149 }
150
151 static bool in_bounds(const void *p)
152 {
153         return true;
154 }
155 #endif
156
157 static void check_bounds(const void *p)
158 {
159         if (!in_bounds(p))
160                 call_error("Not a valid header");
161 }
162
163 static struct tal_hdr *to_tal_hdr(const void *ctx)
164 {
165         struct tal_hdr *t;
166
167         t = (struct tal_hdr *)((char *)ctx - sizeof(struct tal_hdr));
168         check_bounds(t);
169         check_bounds(ignore_destroying_bit(t->parent_child));
170         check_bounds(t->list.next);
171         check_bounds(t->list.prev);
172         if (t->prop && !is_literal(t->prop))
173                 check_bounds(t->prop);
174         return t;
175 }
176
177 static struct tal_hdr *to_tal_hdr_or_null(const void *ctx)
178 {
179         if (!ctx)
180                 return &null_parent.hdr;
181         return to_tal_hdr(ctx);
182 }
183
184 static void *from_tal_hdr(const struct tal_hdr *hdr)
185 {
186         return (void *)(hdr + 1);
187 }
188
189 #ifdef TAL_DEBUG
190 static void *from_tal_hdr_or_null(struct tal_hdr *hdr)
191 {
192         if (hdr == &null_parent.hdr)
193                 return NULL;
194         return from_tal_hdr(hdr);
195 }
196
197 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
198 {
199         tal_check(from_tal_hdr_or_null(tal), "TAL_DEBUG ");
200         return tal;
201 }
202 #else
203 static struct tal_hdr *debug_tal(struct tal_hdr *tal)
204 {
205         return tal;
206 }
207 #endif
208
209 static void notify(const struct tal_hdr *ctx,
210                    enum tal_notify_type type, const void *info)
211 {
212         const struct prop_hdr *p;
213
214         for (p = ctx->prop; p; p = p->next) {
215                 struct notifier *n;
216
217                 if (is_literal(p))
218                         break;
219                 if (p->type != NOTIFIER)
220                         continue;
221                 n = (struct notifier *)p;
222                 if (n->types & type) {
223                         if (n->types & NOTIFY_IS_DESTRUCTOR)
224                                 n->u.destroy(from_tal_hdr(ctx));
225                         else
226                                 n->u.notifyfn(from_tal_hdr(ctx), type,
227                                               (void *)info);
228                 }
229         }
230 }
231
232 static void *allocate(size_t size)
233 {
234         void *ret = allocfn(size);
235         if (!ret)
236                 call_error("allocation failed");
237         else
238                 update_bounds(ret, size);
239         return ret;
240 }
241
242 static struct prop_hdr **find_property_ptr(const struct tal_hdr *t,
243                                            enum prop_type type)
244 {
245         struct prop_hdr **p;
246
247         for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
248                 if (is_literal(*p)) {
249                         if (type == NAME)
250                                 return p;
251                         break;
252                 }
253                 if ((*p)->type == type)
254                         return p;
255         }
256         return NULL;
257 }
258
259 static void *find_property(const struct tal_hdr *parent, enum prop_type type)
260 {
261         struct prop_hdr **p = find_property_ptr(parent, type);
262
263         if (p)
264                 return *p;
265         return NULL;
266 }
267
268 static void init_property(struct prop_hdr *hdr,
269                           struct tal_hdr *parent,
270                           enum prop_type type)
271 {
272         hdr->type = type;
273         hdr->next = parent->prop;
274         parent->prop = hdr;
275 }
276
277 static struct notifier *add_notifier_property(struct tal_hdr *t,
278                                               enum tal_notify_type types,
279                                               void (*fn)(void *,
280                                                          enum tal_notify_type,
281                                                          void *))
282 {
283         struct notifier *prop = allocate(sizeof(*prop));
284         if (prop) {
285                 init_property(&prop->hdr, t, NOTIFIER);
286                 prop->types = types;
287                 prop->u.notifyfn = fn;
288         }
289         return prop;
290 }
291
292 static enum tal_notify_type del_notifier_property(struct tal_hdr *t,
293                                                   void (*fn)(tal_t *,
294                                                              enum tal_notify_type,
295                                                              void *))
296 {
297         struct prop_hdr **p;
298
299         for (p = (struct prop_hdr **)&t->prop; *p; p = &(*p)->next) {
300                 struct notifier *n;
301
302                 if (is_literal(*p))
303                         break;
304                 if ((*p)->type != NOTIFIER)
305                         continue;
306                 n = (struct notifier *)*p;
307                 if (n->u.notifyfn == fn) {
308                         enum tal_notify_type types = n->types;
309                         *p = (*p)->next;
310                         freefn(n);
311                         return types & ~NOTIFY_IS_DESTRUCTOR;
312                 }
313         }
314         return 0;
315 }
316
317 static struct name *add_name_property(struct tal_hdr *t, const char *name)
318 {
319         struct name *prop;
320
321         prop = allocate(sizeof(*prop) + strlen(name) + 1);
322         if (prop) {
323                 init_property(&prop->hdr, t, NAME);
324                 strcpy(prop->name, name);
325         }
326         return prop;
327 }
328
329 static struct length *add_length_property(struct tal_hdr *t, size_t count)
330 {
331         struct length *prop;
332
333         prop = allocate(sizeof(*prop));
334         if (prop) {
335                 init_property(&prop->hdr, t, LENGTH);
336                 prop->count = count;
337         }
338         return prop;
339 }
340
341 static struct children *add_child_property(struct tal_hdr *parent,
342                                            struct tal_hdr *child)
343 {
344         struct children *prop = allocate(sizeof(*prop));
345         if (prop) {
346                 init_property(&prop->hdr, parent, CHILDREN);
347                 prop->parent = parent;
348                 list_head_init(&prop->children);
349         }
350         return prop;
351 }
352
353 static bool add_child(struct tal_hdr *parent, struct tal_hdr *child)
354 {
355         struct children *children = find_property(parent, CHILDREN);
356
357         if (!children) {
358                 if (unlikely(!initialized)) {
359                         atexit(tal_cleanup);
360                         take_allocfail(take_alloc_failed);
361                         initialized = true;
362                 }
363                 children = add_child_property(parent, child);
364                 if (!children)
365                         return false;
366         }
367         list_add(&children->children, &child->list);
368         child->parent_child = children;
369         return true;
370 }
371
372 static void del_tree(struct tal_hdr *t, const tal_t *orig)
373 {
374         struct prop_hdr **prop, *p, *next;
375
376         /* Already being destroyed?  Don't loop. */
377         if (unlikely(get_destroying_bit(t->parent_child)))
378                 return;
379
380         set_destroying_bit(&t->parent_child);
381
382         /* Call free notifiers. */
383         notify(t, TAL_NOTIFY_FREE, (tal_t *)orig);
384
385         /* Now free children and groups. */
386         prop = find_property_ptr(t, CHILDREN);
387         if (prop) {
388                 struct tal_hdr *i;
389                 struct children *c = (struct children *)*prop;
390
391                 while ((i = list_top(&c->children, struct tal_hdr, list))) {
392                         list_del(&i->list);
393                         del_tree(i, orig);
394                 }
395         }
396
397         /* Finally free our properties. */
398         for (p = t->prop; p && !is_literal(p); p = next) {
399                 next = p->next;
400                 freefn(p);
401         }
402         freefn(t);
403 }
404
405 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
406 {
407         struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
408
409         child = allocate(sizeof(struct tal_hdr) + size);
410         if (!child)
411                 return NULL;
412         if (clear)
413                 memset(from_tal_hdr(child), 0, size);
414         child->prop = (void *)label;
415         if (!add_child(parent, child)) {
416                 freefn(child);
417                 return NULL;
418         }
419         debug_tal(parent);
420         if (notifiers)
421                 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child));
422         return from_tal_hdr(debug_tal(child));
423 }
424
425 static bool adjust_size(size_t *size, size_t count)
426 {
427         /* Multiplication wrap */
428         if (count && unlikely(*size * count / *size != count))
429                 goto overflow;
430
431         *size *= count;
432
433         /* Make sure we don't wrap adding header. */
434         if (*size + sizeof(struct tal_hdr) < sizeof(struct tal_hdr))
435                 goto overflow;
436         return true;
437 overflow:
438         call_error("allocation size overflow");
439         return false;
440 }
441
442 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
443                      bool add_count, const char *label)
444 {
445         void *ret;
446
447         if (!adjust_size(&size, count))
448                 return NULL;
449
450         ret = tal_alloc_(ctx, size, clear, label);
451         if (likely(ret) && add_count) {
452                 if (unlikely(!add_length_property(to_tal_hdr(ret), count)))
453                         ret = tal_free(ret);
454         }
455         return ret;
456 }
457
458 void *tal_free(const tal_t *ctx)
459 {
460         if (ctx) {
461                 struct tal_hdr *t;
462                 int saved_errno = errno;
463                 t = debug_tal(to_tal_hdr(ctx));
464                 if (notifiers)
465                         notify(ignore_destroying_bit(t->parent_child)->parent,
466                                TAL_NOTIFY_DEL_CHILD, ctx);
467                 list_del(&t->list);
468                 del_tree(t, ctx);
469                 errno = saved_errno;
470         }
471         return NULL;
472 }
473
474 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
475 {
476         if (ctx) {
477                 struct tal_hdr *newpar, *t, *old_parent;
478
479                 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
480                 t = debug_tal(to_tal_hdr(ctx));
481
482                 /* Unlink it from old parent. */
483                 list_del(&t->list);
484                 old_parent = ignore_destroying_bit(t->parent_child)->parent;
485
486                 if (unlikely(!add_child(newpar, t))) {
487                         /* We can always add to old parent, becuase it has a
488                          * children property already. */
489                         if (!add_child(old_parent, t))
490                                 abort();
491                         return NULL;
492                 }
493                 debug_tal(newpar);
494                 if (notifiers)
495                         notify(t, TAL_NOTIFY_STEAL, new_parent);
496         }
497         return (void *)ctx;
498 }
499
500 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
501 {
502         tal_t *t = debug_tal(to_tal_hdr(ctx));
503         return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
504                                      (void *)destroy);
505 }
506
507 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
508                        void (*callback)(tal_t *, enum tal_notify_type, void *))
509 {
510         tal_t *t = debug_tal(to_tal_hdr(ctx));
511         struct notifier *n;
512
513         assert(types);
514         assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
515                           | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
516                           | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
517                           | TAL_NOTIFY_ADD_NOTIFIER
518                           | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
519
520         /* Don't call notifier about itself: set types after! */
521         n = add_notifier_property(t, 0, callback);
522         if (unlikely(!n))
523                 return false;
524
525         if (notifiers)
526                 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
527
528         n->types = types;
529         if (types != TAL_NOTIFY_FREE)
530                 notifiers++;
531         return true;
532 }
533
534 bool tal_del_notifier_(const tal_t *ctx,
535                        void (*callback)(tal_t *, enum tal_notify_type, void *))
536 {
537         struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
538         enum tal_notify_type types;
539
540         types = del_notifier_property(t, callback);
541         if (types) {
542                 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
543                 if (types != TAL_NOTIFY_FREE)
544                         notifiers--;
545                 return true;
546         }
547         return false;
548 }
549
550 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
551 {
552         return tal_del_notifier_(ctx, (void *)destroy);
553 }
554
555 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
556 {
557         struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
558         struct prop_hdr **prop = find_property_ptr(t, NAME);
559
560         /* Get rid of any old name */
561         if (prop) {
562                 struct name *name = (struct name *)*prop;
563                 if (is_literal(&name->hdr))
564                         *prop = NULL;
565                 else {
566                         *prop = name->hdr.next;
567                         freefn(name);
568                 }
569         }
570
571         if (literal && name[0]) {
572                 struct prop_hdr **p;
573
574                 /* Append literal. */
575                 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
576                 *p = (struct prop_hdr *)name;
577         } else if (!add_name_property(t, name))
578                 return false;
579
580         debug_tal(t);
581         if (notifiers)
582                 notify(t, TAL_NOTIFY_RENAME, name);
583         return true;
584 }
585
586 const char *tal_name(const tal_t *t)
587 {
588         struct name *n;
589
590         n = find_property(debug_tal(to_tal_hdr(t)), NAME);
591         if (!n)
592                 return NULL;
593
594         if (is_literal(&n->hdr))
595                 return (const char *)n;
596         return n->name;
597 }
598
599 size_t tal_count(const tal_t *ptr)
600 {
601         struct length *l;
602
603         l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
604         if (!l)
605                 return 0;
606         return l->count;
607 }
608
609 /* Start one past first child: make stopping natural in circ. list. */
610 static struct tal_hdr *first_child(struct tal_hdr *parent)
611 {
612         struct children *child;
613
614         child = find_property(parent, CHILDREN);
615         if (!child)
616                 return NULL;
617
618         return list_top(&child->children, struct tal_hdr, list);
619 }
620
621 tal_t *tal_first(const tal_t *root)
622 {
623         struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
624
625         c = first_child(t);
626         if (!c)
627                 return NULL;
628         return from_tal_hdr(c);
629 }
630
631 tal_t *tal_next(const tal_t *root, const tal_t *prev)
632 {
633         struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
634
635         /* Children? */
636         c = first_child(t);
637         if (c)
638                 return from_tal_hdr(c);
639
640         top = to_tal_hdr_or_null(root);
641         do {
642                 struct tal_hdr *next;
643                 struct list_node *end;
644
645                 end = &ignore_destroying_bit(t->parent_child)->children.n;
646
647                 next = list_entry(t->list.next, struct tal_hdr, list);
648                 if (&next->list != end)
649                         return from_tal_hdr(next);
650
651                 /* OK, go back to parent. */
652                 t = ignore_destroying_bit(t->parent_child)->parent;
653         } while (t != top);
654
655         return NULL;
656 }
657
658 tal_t *tal_parent(const tal_t *ctx)
659 {
660         struct tal_hdr *t;
661
662         if (!ctx)
663                 return NULL;
664
665         t = debug_tal(to_tal_hdr(ctx));
666         if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
667                 return NULL;
668         return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
669 }
670
671 bool tal_resize_(tal_t **ctxp, size_t size, size_t count)
672 {
673         struct tal_hdr *old_t, *t;
674         struct children *child;
675         struct length *len;
676
677         old_t = debug_tal(to_tal_hdr(*ctxp));
678
679         if (!adjust_size(&size, count))
680                 return false;
681
682         t = resizefn(old_t, size + sizeof(struct tal_hdr));
683         if (!t) {
684                 call_error("Reallocation failure");
685                 return false;
686         }
687
688         /* If it didn't move, we're done! */
689         if (t != old_t) {
690                 update_bounds(t, size + sizeof(struct tal_hdr));
691
692                 /* Fix up linked list pointers. */
693                 if (list_entry(t->list.next, struct tal_hdr, list) != old_t)
694                         t->list.next->prev = t->list.prev->next = &t->list;
695
696                 /* Fix up child property's parent pointer. */
697                 child = find_property(t, CHILDREN);
698                 if (child) {
699                         assert(child->parent == old_t);
700                         child->parent = t;
701                 }
702                 *ctxp = from_tal_hdr(debug_tal(t));
703                 if (notifiers)
704                         notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
705         }
706         len = find_property(t, LENGTH);
707         if (len)
708                 len->count = count;
709         if (notifiers)
710                 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
711
712         return true;
713 }
714
715 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
716                size_t n, size_t extra, bool add_count,
717                const char *label)
718 {
719         void *ret;
720         size_t nbytes = size;
721
722         if (!adjust_size(&nbytes, n)) {
723                 if (taken(p))
724                         tal_free(p);
725                 return NULL;
726         }
727
728         /* Beware addition overflow! */
729         if (n + extra < n) {
730                 call_error("dup size overflow");
731                 if (taken(p))
732                         tal_free(p);
733                 return NULL;
734         }
735
736         if (taken(p)) {
737                 if (unlikely(!p))
738                         return NULL;
739                 if (unlikely(!tal_resize_((void **)&p, size, n + extra)))
740                         return tal_free(p);
741                 if (unlikely(!tal_steal(ctx, p)))
742                         return tal_free(p);
743                 return (void *)p;
744         }
745
746         ret = tal_alloc_arr_(ctx, size, n + extra, false, add_count, label);
747         if (ret)
748                 memcpy(ret, p, nbytes);
749         return ret;
750 }
751
752 void tal_set_backend(void *(*alloc_fn)(size_t size),
753                      void *(*resize_fn)(void *, size_t size),
754                      void (*free_fn)(void *),
755                      void (*error_fn)(const char *msg))
756 {
757         if (alloc_fn)
758                 allocfn = alloc_fn;
759         if (resize_fn)
760                 resizefn = resize_fn;
761         if (free_fn)
762                 freefn = free_fn;
763         if (error_fn)
764                 errorfn = error_fn;
765 }
766
767 #ifdef CCAN_TAL_DEBUG
768 static void dump_node(unsigned int indent, const struct tal_hdr *t)
769 {
770         unsigned int i;
771         const struct prop_hdr *p;
772
773         for (i = 0; i < indent; i++)
774                 printf("  ");
775         printf("%p", t);
776         for (p = t->prop; p; p = p->next) {
777                 struct children *c;
778                 struct name *n;
779                 struct notifier *no;
780                 struct length *l;
781                 if (is_literal(p)) {
782                         printf(" \"%s\"", (const char *)p);
783                         break;
784                 }
785                 switch (p->type) {
786                 case CHILDREN:
787                         c = (struct children *)p;
788                         printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
789                                p, c->parent,
790                                c->children.n.prev, c->children.n.next);
791                         break;
792                 case NAME:
793                         n = (struct name *)p;
794                         printf(" NAME(%p):%s", p, n->name);
795                         break;
796                 case NOTIFIER:
797                         no = (struct notifier *)p;
798                         printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
799                         break;
800                 case LENGTH:
801                         l = (struct length *)p;
802                         printf(" LENGTH(%p):count=%zu", p, l->count);
803                         break;
804                 default:
805                         printf(" **UNKNOWN(%p):%i**", p, p->type);
806                 }
807         }
808         printf("\n");
809 }
810
811 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
812 {
813         struct children *children;
814
815         dump_node(level, t);
816
817         children = find_property(t, CHILDREN);
818         if (children) {
819                 struct tal_hdr *i;
820
821                 list_for_each(&children->children, i, list)
822                         tal_dump_(level + 1, i);
823         }
824 }
825
826 void tal_dump(void)
827 {
828         tal_dump_(0, &null_parent.hdr);
829 }
830 #endif /* CCAN_TAL_DEBUG */
831
832 #ifndef NDEBUG
833 static bool check_err(struct tal_hdr *t, const char *errorstr,
834                       const char *errmsg)
835 {
836         if (errorstr) {
837                 /* Try not to malloc: it may be corrupted. */
838                 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
839                 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
840                 call_error(msg);
841         }
842         return false;
843 }
844
845 static bool check_node(struct children *parent_child,
846                        struct tal_hdr *t, const char *errorstr)
847 {
848         struct prop_hdr *p;
849         struct name *name = NULL;
850         struct children *children = NULL;
851         struct length *length = NULL;
852
853         if (!in_bounds(t))
854                 return check_err(t, errorstr, "invalid pointer");
855
856         if (ignore_destroying_bit(t->parent_child) != parent_child)
857                 return check_err(t, errorstr, "incorrect parent");
858
859         for (p = t->prop; p; p = p->next) {
860                 if (is_literal(p)) {
861                         if (name)
862                                 return check_err(t, errorstr,
863                                                  "has extra literal");
864                         name = (struct name *)p;
865                         break;
866                 }
867                 if (!in_bounds(p))
868                         return check_err(t, errorstr,
869                                          "has bad property pointer");
870
871                 switch (p->type) {
872                 case CHILDREN:
873                         if (children)
874                                 return check_err(t, errorstr,
875                                                  "has two child nodes");
876                         children = (struct children *)p;
877                         break;
878                 case LENGTH:
879                         if (length)
880                                 return check_err(t, errorstr,
881                                                  "has two lengths");
882                         length = (struct length *)p;
883                         break;
884                 case NOTIFIER:
885                         break;
886                 case NAME:
887                         if (name)
888                                 return check_err(t, errorstr,
889                                                  "has two names");
890                         name = (struct name *)p;
891                         break;
892                 default:
893                         return check_err(t, errorstr, "has unknown property");
894                 }
895         }
896         if (children) {
897                 struct tal_hdr *i;
898
899                 if (!list_check(&children->children, errorstr))
900                         return false;
901                 list_for_each(&children->children, i, list) {
902                         if (!check_node(children, i, errorstr))
903                                 return false;
904                 }
905         }
906         return true;
907 }
908
909 bool tal_check(const tal_t *ctx, const char *errorstr)
910 {
911         struct tal_hdr *t = to_tal_hdr_or_null(ctx);
912
913         return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
914 }
915 #else /* NDEBUG */
916 bool tal_check(const tal_t *ctx, const char *errorstr)
917 {
918         return true;
919 }
920 #endif