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