tal: add tal_resizez for zero-padded expansion.
[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 count;
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)
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 void *tal_alloc_(const tal_t *ctx, size_t size, bool clear, const char *label)
387 {
388         struct tal_hdr *child, *parent = debug_tal(to_tal_hdr_or_null(ctx));
389
390         child = allocate(sizeof(struct tal_hdr) + size);
391         if (!child)
392                 return NULL;
393         if (clear)
394                 memset(from_tal_hdr(child), 0, size);
395         child->prop = (void *)label;
396         if (!add_child(parent, child)) {
397                 freefn(child);
398                 return NULL;
399         }
400         debug_tal(parent);
401         if (notifiers)
402                 notify(parent, TAL_NOTIFY_ADD_CHILD, from_tal_hdr(child));
403         return from_tal_hdr(debug_tal(child));
404 }
405
406 static bool adjust_size(size_t *size, size_t count)
407 {
408         const size_t extra = sizeof(struct tal_hdr) + sizeof(struct length)*2;
409
410         /* Multiplication wrap */
411         if (count && unlikely(*size * count / *size != count))
412                 goto overflow;
413
414         *size *= count;
415
416         /* Make sure we don't wrap adding header/tailer. */
417         if (*size + extra < extra)
418                 goto overflow;
419         return true;
420 overflow:
421         call_error("allocation size overflow");
422         return false;
423 }
424
425 static size_t extra_for_length(size_t size)
426 {
427         size_t extra;
428         const size_t align = ALIGNOF(struct length);
429
430         /* Round up size, and add tailer. */
431         extra = ((size + align-1) & ~(align-1)) - size;
432         extra += sizeof(struct length);
433         return extra;
434 }
435
436 void *tal_alloc_arr_(const tal_t *ctx, size_t size, size_t count, bool clear,
437                      bool add_count, const char *label)
438 {
439         void *ret;
440
441         if (!adjust_size(&size, count))
442                 return NULL;
443
444         if (add_count)
445                 size += extra_for_length(size);
446
447         ret = tal_alloc_(ctx, size, clear, label);
448         if (unlikely(!ret))
449                 return ret;
450
451         if (add_count) {
452                 struct length *lprop;
453                 lprop = (struct length *)((char *)ret + size) - 1;
454                 init_property(&lprop->hdr, to_tal_hdr(ret), LENGTH);
455                 lprop->count = count;
456         }
457         return ret;
458 }
459
460 void *tal_free(const tal_t *ctx)
461 {
462         if (ctx) {
463                 struct tal_hdr *t;
464                 int saved_errno = errno;
465                 t = debug_tal(to_tal_hdr(ctx));
466                 if (notifiers)
467                         notify(ignore_destroying_bit(t->parent_child)->parent,
468                                TAL_NOTIFY_DEL_CHILD, ctx);
469                 list_del(&t->list);
470                 del_tree(t, ctx);
471                 errno = saved_errno;
472         }
473         return NULL;
474 }
475
476 void *tal_steal_(const tal_t *new_parent, const tal_t *ctx)
477 {
478         if (ctx) {
479                 struct tal_hdr *newpar, *t, *old_parent;
480
481                 newpar = debug_tal(to_tal_hdr_or_null(new_parent));
482                 t = debug_tal(to_tal_hdr(ctx));
483
484                 /* Unlink it from old parent. */
485                 list_del(&t->list);
486                 old_parent = ignore_destroying_bit(t->parent_child)->parent;
487
488                 if (unlikely(!add_child(newpar, t))) {
489                         /* We can always add to old parent, becuase it has a
490                          * children property already. */
491                         if (!add_child(old_parent, t))
492                                 abort();
493                         return NULL;
494                 }
495                 debug_tal(newpar);
496                 if (notifiers)
497                         notify(t, TAL_NOTIFY_STEAL, new_parent);
498         }
499         return (void *)ctx;
500 }
501
502 bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me))
503 {
504         tal_t *t = debug_tal(to_tal_hdr(ctx));
505         return add_notifier_property(t, TAL_NOTIFY_FREE|NOTIFY_IS_DESTRUCTOR,
506                                      (void *)destroy);
507 }
508
509 bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
510                        void (*callback)(tal_t *, enum tal_notify_type, void *))
511 {
512         tal_t *t = debug_tal(to_tal_hdr(ctx));
513         struct notifier *n;
514
515         assert(types);
516         assert((types & ~(TAL_NOTIFY_FREE | TAL_NOTIFY_STEAL | TAL_NOTIFY_MOVE
517                           | TAL_NOTIFY_RESIZE | TAL_NOTIFY_RENAME
518                           | TAL_NOTIFY_ADD_CHILD | TAL_NOTIFY_DEL_CHILD
519                           | TAL_NOTIFY_ADD_NOTIFIER
520                           | TAL_NOTIFY_DEL_NOTIFIER)) == 0);
521
522         /* Don't call notifier about itself: set types after! */
523         n = add_notifier_property(t, 0, callback);
524         if (unlikely(!n))
525                 return false;
526
527         if (notifiers)
528                 notify(t, TAL_NOTIFY_ADD_NOTIFIER, callback);
529
530         n->types = types;
531         if (types != TAL_NOTIFY_FREE)
532                 notifiers++;
533         return true;
534 }
535
536 bool tal_del_notifier_(const tal_t *ctx,
537                        void (*callback)(tal_t *, enum tal_notify_type, void *))
538 {
539         struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
540         enum tal_notify_type types;
541
542         types = del_notifier_property(t, callback);
543         if (types) {
544                 notify(t, TAL_NOTIFY_DEL_NOTIFIER, callback);
545                 if (types != TAL_NOTIFY_FREE)
546                         notifiers--;
547                 return true;
548         }
549         return false;
550 }
551
552 bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me))
553 {
554         return tal_del_notifier_(ctx, (void *)destroy);
555 }
556
557 bool tal_set_name_(tal_t *ctx, const char *name, bool literal)
558 {
559         struct tal_hdr *t = debug_tal(to_tal_hdr(ctx));
560         struct prop_hdr **prop = find_property_ptr(t, NAME);
561
562         /* Get rid of any old name */
563         if (prop) {
564                 struct name *name = (struct name *)*prop;
565                 if (is_literal(&name->hdr))
566                         *prop = NULL;
567                 else {
568                         *prop = name->hdr.next;
569                         freefn(name);
570                 }
571         }
572
573         if (literal && name[0]) {
574                 struct prop_hdr **p;
575
576                 /* Append literal. */
577                 for (p = &t->prop; *p && !is_literal(*p); p = &(*p)->next);
578                 *p = (struct prop_hdr *)name;
579         } else if (!add_name_property(t, name))
580                 return false;
581
582         debug_tal(t);
583         if (notifiers)
584                 notify(t, TAL_NOTIFY_RENAME, name);
585         return true;
586 }
587
588 const char *tal_name(const tal_t *t)
589 {
590         struct name *n;
591
592         n = find_property(debug_tal(to_tal_hdr(t)), NAME);
593         if (!n)
594                 return NULL;
595
596         if (is_literal(&n->hdr))
597                 return (const char *)n;
598         return n->name;
599 }
600
601 size_t tal_count(const tal_t *ptr)
602 {
603         struct length *l;
604
605         l = find_property(debug_tal(to_tal_hdr(ptr)), LENGTH);
606         if (!l)
607                 return 0;
608         return l->count;
609 }
610
611 /* Start one past first child: make stopping natural in circ. list. */
612 static struct tal_hdr *first_child(struct tal_hdr *parent)
613 {
614         struct children *child;
615
616         child = find_property(parent, CHILDREN);
617         if (!child)
618                 return NULL;
619
620         return list_top(&child->children, struct tal_hdr, list);
621 }
622
623 tal_t *tal_first(const tal_t *root)
624 {
625         struct tal_hdr *c, *t = debug_tal(to_tal_hdr_or_null(root));
626
627         c = first_child(t);
628         if (!c)
629                 return NULL;
630         return from_tal_hdr(c);
631 }
632
633 tal_t *tal_next(const tal_t *root, const tal_t *prev)
634 {
635         struct tal_hdr *c, *t = debug_tal(to_tal_hdr(prev)), *top;
636
637         /* Children? */
638         c = first_child(t);
639         if (c)
640                 return from_tal_hdr(c);
641
642         top = to_tal_hdr_or_null(root);
643         do {
644                 struct tal_hdr *next;
645                 struct list_node *end;
646
647                 end = &ignore_destroying_bit(t->parent_child)->children.n;
648
649                 next = list_entry(t->list.next, struct tal_hdr, list);
650                 if (&next->list != end)
651                         return from_tal_hdr(next);
652
653                 /* OK, go back to parent. */
654                 t = ignore_destroying_bit(t->parent_child)->parent;
655         } while (t != top);
656
657         return NULL;
658 }
659
660 tal_t *tal_parent(const tal_t *ctx)
661 {
662         struct tal_hdr *t;
663
664         if (!ctx)
665                 return NULL;
666
667         t = debug_tal(to_tal_hdr(ctx));
668         if (ignore_destroying_bit(t->parent_child)->parent == &null_parent.hdr)
669                 return NULL;
670         return from_tal_hdr(ignore_destroying_bit(t->parent_child)->parent);
671 }
672
673 bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear)
674 {
675         struct tal_hdr *old_t, *t;
676         struct children *child;
677         struct prop_hdr **lenp;
678         struct length len;
679         size_t extra = 0, elemsize = size;
680
681         old_t = debug_tal(to_tal_hdr(*ctxp));
682
683         if (!adjust_size(&size, count))
684                 return false;
685
686         lenp = find_property_ptr(old_t, LENGTH);
687         if (lenp) {
688                 /* Copy here, in case we're shrinking! */
689                 len = *(struct length *)*lenp;
690                 extra = extra_for_length(size);
691         } else /* If we don't have an old length, we can't clear! */
692                 assert(!clear);
693
694         t = resizefn(old_t, sizeof(struct tal_hdr) + size + extra);
695         if (!t) {
696                 call_error("Reallocation failure");
697                 return false;
698         }
699
700         /* Copy length to end. */
701         if (lenp) {
702                 struct length *new_len;
703
704                 /* Clear between old end and new end. */
705                 if (clear && count > len.count) {
706                         char *old_end = (char *)(t + 1) + len.count * elemsize;
707                         memset(old_end, 0, elemsize * (count - len.count));
708                 }
709
710                 new_len = (struct length *)((char *)(t + 1) + size);
711                 len.count = count;
712                 *new_len = len;
713
714                 /* Be careful replacing next ptr; could be old hdr. */
715                 if (lenp == &old_t->prop)
716                         t->prop = &new_len->hdr;
717                 else
718                         *lenp = &new_len->hdr;
719         }
720
721         update_bounds(t, sizeof(struct tal_hdr) + size + extra);
722
723         /* If it didn't move, we're done! */
724         if (t != old_t) {
725                 /* Fix up linked list pointers. */
726                 t->list.next->prev = t->list.prev->next = &t->list;
727
728                 /* Fix up child property's parent pointer. */
729                 child = find_property(t, CHILDREN);
730                 if (child) {
731                         assert(child->parent == old_t);
732                         child->parent = t;
733                 }
734                 *ctxp = from_tal_hdr(debug_tal(t));
735                 if (notifiers)
736                         notify(t, TAL_NOTIFY_MOVE, from_tal_hdr(old_t));
737         }
738         if (notifiers)
739                 notify(t, TAL_NOTIFY_RESIZE, (void *)size);
740
741         return true;
742 }
743
744 bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count)
745 {
746         struct length *l;
747         size_t old_count;
748         bool ret = false;
749
750         l = find_property(debug_tal(to_tal_hdr(*ctxp)), LENGTH);
751         old_count = l->count;
752
753         /* Check for additive overflow */
754         if (old_count + count < count) {
755                 call_error("dup size overflow");
756                 goto out;
757         }
758
759         /* Don't point src inside thing we're expanding! */
760         assert(src < *ctxp
761                || (char *)src >= (char *)(*ctxp) + (size * old_count));
762
763         if (!tal_resize_(ctxp, size, old_count + count, false))
764                 goto out;
765
766         memcpy((char *)*ctxp + size * old_count, src, count * size);
767         ret = true;
768
769 out:
770         if (taken(src))
771                 tal_free(src);
772         return ret;
773 }
774
775 void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
776                size_t n, size_t extra, bool add_count,
777                const char *label)
778 {
779         void *ret;
780         size_t nbytes = size;
781
782         if (!adjust_size(&nbytes, n)) {
783                 if (taken(p))
784                         tal_free(p);
785                 return NULL;
786         }
787
788         /* Beware addition overflow! */
789         if (n + extra < n) {
790                 call_error("dup size overflow");
791                 if (taken(p))
792                         tal_free(p);
793                 return NULL;
794         }
795
796         if (taken(p)) {
797                 if (unlikely(!p))
798                         return NULL;
799                 if (unlikely(!tal_resize_((void **)&p, size, n + extra, false)))
800                         return tal_free(p);
801                 if (unlikely(!tal_steal(ctx, p)))
802                         return tal_free(p);
803                 return (void *)p;
804         }
805
806         ret = tal_alloc_arr_(ctx, size, n + extra, false, add_count, label);
807         if (ret)
808                 memcpy(ret, p, nbytes);
809         return ret;
810 }
811
812 void tal_set_backend(void *(*alloc_fn)(size_t size),
813                      void *(*resize_fn)(void *, size_t size),
814                      void (*free_fn)(void *),
815                      void (*error_fn)(const char *msg))
816 {
817         if (alloc_fn)
818                 allocfn = alloc_fn;
819         if (resize_fn)
820                 resizefn = resize_fn;
821         if (free_fn)
822                 freefn = free_fn;
823         if (error_fn)
824                 errorfn = error_fn;
825 }
826
827 #ifdef CCAN_TAL_DEBUG
828 static void dump_node(unsigned int indent, const struct tal_hdr *t)
829 {
830         unsigned int i;
831         const struct prop_hdr *p;
832
833         for (i = 0; i < indent; i++)
834                 printf("  ");
835         printf("%p", t);
836         for (p = t->prop; p; p = p->next) {
837                 struct children *c;
838                 struct name *n;
839                 struct notifier *no;
840                 struct length *l;
841                 if (is_literal(p)) {
842                         printf(" \"%s\"", (const char *)p);
843                         break;
844                 }
845                 switch (p->type) {
846                 case CHILDREN:
847                         c = (struct children *)p;
848                         printf(" CHILDREN(%p):parent=%p,children={%p,%p}\n",
849                                p, c->parent,
850                                c->children.n.prev, c->children.n.next);
851                         break;
852                 case NAME:
853                         n = (struct name *)p;
854                         printf(" NAME(%p):%s", p, n->name);
855                         break;
856                 case NOTIFIER:
857                         no = (struct notifier *)p;
858                         printf(" NOTIFIER(%p):fn=%p", p, no->u.notifyfn);
859                         break;
860                 case LENGTH:
861                         l = (struct length *)p;
862                         printf(" LENGTH(%p):count=%zu", p, l->count);
863                         break;
864                 default:
865                         printf(" **UNKNOWN(%p):%i**", p, p->type);
866                 }
867         }
868         printf("\n");
869 }
870
871 static void tal_dump_(unsigned int level, const struct tal_hdr *t)
872 {
873         struct children *children;
874
875         dump_node(level, t);
876
877         children = find_property(t, CHILDREN);
878         if (children) {
879                 struct tal_hdr *i;
880
881                 list_for_each(&children->children, i, list)
882                         tal_dump_(level + 1, i);
883         }
884 }
885
886 void tal_dump(void)
887 {
888         tal_dump_(0, &null_parent.hdr);
889 }
890 #endif /* CCAN_TAL_DEBUG */
891
892 #ifndef NDEBUG
893 static bool check_err(struct tal_hdr *t, const char *errorstr,
894                       const char *errmsg)
895 {
896         if (errorstr) {
897                 /* Try not to malloc: it may be corrupted. */
898                 char msg[strlen(errorstr) + 20 + strlen(errmsg) + 1];
899                 sprintf(msg, "%s:%p %s", errorstr, from_tal_hdr(t), errmsg);
900                 call_error(msg);
901         }
902         return false;
903 }
904
905 static bool check_node(struct children *parent_child,
906                        struct tal_hdr *t, const char *errorstr)
907 {
908         struct prop_hdr *p;
909         struct name *name = NULL;
910         struct children *children = NULL;
911         struct length *length = NULL;
912
913         if (!in_bounds(t))
914                 return check_err(t, errorstr, "invalid pointer");
915
916         if (ignore_destroying_bit(t->parent_child) != parent_child)
917                 return check_err(t, errorstr, "incorrect parent");
918
919         for (p = t->prop; p; p = p->next) {
920                 if (is_literal(p)) {
921                         if (name)
922                                 return check_err(t, errorstr,
923                                                  "has extra literal");
924                         name = (struct name *)p;
925                         break;
926                 }
927                 if (!in_bounds(p))
928                         return check_err(t, errorstr,
929                                          "has bad property pointer");
930
931                 switch (p->type) {
932                 case CHILDREN:
933                         if (children)
934                                 return check_err(t, errorstr,
935                                                  "has two child nodes");
936                         children = (struct children *)p;
937                         break;
938                 case LENGTH:
939                         if (length)
940                                 return check_err(t, errorstr,
941                                                  "has two lengths");
942                         length = (struct length *)p;
943                         break;
944                 case NOTIFIER:
945                         break;
946                 case NAME:
947                         if (name)
948                                 return check_err(t, errorstr,
949                                                  "has two names");
950                         name = (struct name *)p;
951                         break;
952                 default:
953                         return check_err(t, errorstr, "has unknown property");
954                 }
955         }
956         if (children) {
957                 struct tal_hdr *i;
958
959                 if (!list_check(&children->children, errorstr))
960                         return false;
961                 list_for_each(&children->children, i, list) {
962                         if (!check_node(children, i, errorstr))
963                                 return false;
964                 }
965         }
966         return true;
967 }
968
969 bool tal_check(const tal_t *ctx, const char *errorstr)
970 {
971         struct tal_hdr *t = to_tal_hdr_or_null(ctx);
972
973         return check_node(ignore_destroying_bit(t->parent_child), t, errorstr);
974 }
975 #else /* NDEBUG */
976 bool tal_check(const tal_t *ctx, const char *errorstr)
977 {
978         return true;
979 }
980 #endif