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