tal.c 23 KB

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