tal.c 22 KB

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