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