tal.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Licensed under BSD-MIT - see LICENSE file for details */
  2. #ifndef CCAN_TAL_H
  3. #define CCAN_TAL_H
  4. #include "config.h"
  5. #include <ccan/compiler/compiler.h>
  6. #include <ccan/likely/likely.h>
  7. #include <ccan/typesafe_cb/typesafe_cb.h>
  8. #include <ccan/str/str.h>
  9. #include <stdlib.h>
  10. #include <stdbool.h>
  11. #include <stdarg.h>
  12. /**
  13. * tal_t - convenient alias for void to mark tal pointers.
  14. *
  15. * Since any pointer can be a tal-allocated pointer, it's often
  16. * useful to use this typedef to mark them explicitly.
  17. */
  18. typedef void tal_t;
  19. /**
  20. * tal - basic allocator function
  21. * @ctx: NULL, or tal allocated object to be parent.
  22. * @type: the type to allocate.
  23. *
  24. * Allocates a specific type, with a given parent context. The name
  25. * of the object is a string of the type, but if CCAN_TAL_DEBUG is
  26. * defined it also contains the file and line which allocated it.
  27. *
  28. * Example:
  29. * int *p = tal(NULL, int);
  30. * *p = 1;
  31. */
  32. #define tal(ctx, type) \
  33. ((type *)tal_alloc_((ctx), sizeof(type), false, false, TAL_LABEL(type, "")))
  34. /**
  35. * talz - zeroing allocator function
  36. * @ctx: NULL, or tal allocated object to be parent.
  37. * @type: the type to allocate.
  38. *
  39. * Equivalent to tal() followed by memset() to zero.
  40. *
  41. * Example:
  42. * p = talz(NULL, int);
  43. * assert(*p == 0);
  44. */
  45. #define talz(ctx, type) \
  46. ((type *)tal_alloc_((ctx), sizeof(type), true, false, TAL_LABEL(type, "")))
  47. /**
  48. * tal_free - free a tal-allocated pointer.
  49. * @p: NULL, or tal allocated object to free.
  50. *
  51. * This calls the destructors for p (if any), then does the same for all its
  52. * children (recursively) before finally freeing the memory. It returns
  53. * NULL, for convenience.
  54. *
  55. * Note: errno is preserved by this call.
  56. *
  57. * Example:
  58. * p = tal_free(p);
  59. */
  60. void *tal_free(const tal_t *p);
  61. /**
  62. * tal_arr - allocate an array of objects.
  63. * @ctx: NULL, or tal allocated object to be parent.
  64. * @type: the type to allocate.
  65. * @count: the number to allocate.
  66. *
  67. * Note that an object allocated with tal_arr() has a length property;
  68. * see tal_count().
  69. *
  70. * Example:
  71. * p = tal_arr(NULL, int, 2);
  72. * p[0] = 0;
  73. * p[1] = 1;
  74. */
  75. #define tal_arr(ctx, type, count) \
  76. ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), false, \
  77. true, TAL_LABEL(type, "[]")))
  78. /**
  79. * tal_arrz - allocate an array of zeroed objects.
  80. * @ctx: NULL, or tal allocated object to be parent.
  81. * @type: the type to allocate.
  82. * @count: the number to allocate.
  83. *
  84. * Note that an object allocated with tal_arrz() has a length property;
  85. * see tal_count().
  86. *
  87. * Example:
  88. * p = tal_arrz(NULL, int, 2);
  89. * assert(p[0] == 0 && p[1] == 0);
  90. */
  91. #define tal_arrz(ctx, type, count) \
  92. ((type *)tal_alloc_arr_((ctx), sizeof(type), (count), true, \
  93. true, TAL_LABEL(type, "[]")))
  94. /**
  95. * tal_resize - enlarge or reduce a tal_arr[z].
  96. * @p: A pointer to the tal allocated array to resize.
  97. * @count: the number to allocate.
  98. *
  99. * This returns true on success (and may move *@p), or false on failure.
  100. * If @p has a length property, it is updated on success.
  101. *
  102. * Example:
  103. * tal_resize(&p, 100);
  104. */
  105. #define tal_resize(p, count) \
  106. tal_resize_((void **)(p), sizeof**(p), (count), false)
  107. /**
  108. * tal_resizez - enlarge or reduce a tal_arr[z]; zero out extra.
  109. * @p: A pointer to the tal allocated array to resize.
  110. * @count: the number to allocate.
  111. *
  112. * This returns true on success (and may move *@p), or false on failure.
  113. * If @p has a length property, it is updated on success.
  114. * On expand, new elements are memset to 0 bytes.
  115. *
  116. * Example:
  117. * tal_resizez(&p, 200);
  118. */
  119. #define tal_resizez(p, count) \
  120. tal_resize_((void **)(p), sizeof**(p), (count), true)
  121. /**
  122. * tal_steal - change the parent of a tal-allocated pointer.
  123. * @ctx: The new parent.
  124. * @ptr: The tal allocated object to move.
  125. *
  126. * This may need to perform an allocation, in which case it may fail; thus
  127. * it can return NULL, otherwise returns @ptr.
  128. */
  129. #if HAVE_STATEMENT_EXPR
  130. /* Weird macro avoids gcc's 'warning: value computed is not used'. */
  131. #define tal_steal(ctx, ptr) \
  132. ({ (tal_typeof(ptr) tal_steal_((ctx),(ptr))); })
  133. #else
  134. #define tal_steal(ctx, ptr) \
  135. (tal_typeof(ptr) tal_steal_((ctx),(ptr)))
  136. #endif
  137. /**
  138. * tal_add_destructor - add a callback function when this context is destroyed.
  139. * @ptr: The tal allocated object.
  140. * @function: the function to call before it's freed.
  141. *
  142. * This is a more convenient form of tal_add_notifier(@ptr,
  143. * TAL_NOTIFY_FREE, ...), in that the function prototype takes only @ptr.
  144. *
  145. * Note that this can only fail if your allocfn fails and your errorfn returns.
  146. */
  147. #define tal_add_destructor(ptr, function) \
  148. tal_add_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr)))
  149. /**
  150. * tal_del_destructor - remove a destructor callback function.
  151. * @ptr: The tal allocated object.
  152. * @function: the function to call before it's freed.
  153. *
  154. * If @function has not been successfully added as a destructor, this returns
  155. * false.
  156. */
  157. #define tal_del_destructor(ptr, function) \
  158. tal_del_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr)))
  159. enum tal_notify_type {
  160. TAL_NOTIFY_FREE = 1,
  161. TAL_NOTIFY_STEAL = 2,
  162. TAL_NOTIFY_MOVE = 4,
  163. TAL_NOTIFY_RESIZE = 8,
  164. TAL_NOTIFY_RENAME = 16,
  165. TAL_NOTIFY_ADD_CHILD = 32,
  166. TAL_NOTIFY_DEL_CHILD = 64,
  167. TAL_NOTIFY_ADD_NOTIFIER = 128,
  168. TAL_NOTIFY_DEL_NOTIFIER = 256
  169. };
  170. /**
  171. * tal_add_notifier - add a callback function when this context changes.
  172. * @ptr: The tal allocated object.
  173. * @types: Bitwise OR of the types the callback is interested in.
  174. * @callback: the function to call.
  175. *
  176. * Note that this can only fail if your allocfn fails and your errorfn
  177. * returns. Also note that notifiers are not reliable in the case
  178. * where an allocation fails, as they may be called before any
  179. * allocation is actually done.
  180. *
  181. * TAL_NOTIFY_FREE is called when @ptr is freed, either directly or
  182. * because an ancestor is freed: @info is the argument to tal_free().
  183. * It is exactly equivalent to a destructor, with more information.
  184. *
  185. * TAL_NOTIFY_STEAL is called when @ptr's parent changes: @info is the
  186. * new parent.
  187. *
  188. * TAL_NOTIFY_MOVE is called when @ptr is realloced (via tal_resize)
  189. * and moved. In this case, @ptr arg here is the new memory, and
  190. * @info is the old pointer.
  191. *
  192. * TAL_NOTIFY_RESIZE is called when @ptr is realloced via tal_resize:
  193. * @info is the new size, in bytes. If the pointer has moved,
  194. * TAL_NOTIFY_MOVE callbacks are called first.
  195. *
  196. * TAL_NOTIFY_ADD_CHILD/TAL_NOTIFY_DEL_CHILD are called when @ptr is
  197. * the context for a tal() allocating call, or a direct child is
  198. * tal_free()d: @info is the child. Note that TAL_NOTIFY_DEL_CHILD is
  199. * not called when this context is tal_free()d: TAL_NOTIFY_FREE is
  200. * considered sufficient for that case.
  201. *
  202. * TAL_NOTIFY_ADD_NOTIFIER/TAL_NOTIFIER_DEL_NOTIFIER are called when a
  203. * notifier is added or removed (not for this notifier): @info is the
  204. * callback. This is also called for tal_add_destructor and
  205. * tal_del_destructor.
  206. */
  207. #define tal_add_notifier(ptr, types, callback) \
  208. tal_add_notifier_((ptr), (types), \
  209. typesafe_cb_postargs(void, tal_t *, (callback), \
  210. (ptr), \
  211. enum tal_notify_type, void *))
  212. /**
  213. * tal_del_notifier - remove a notifier callback function.
  214. * @ptr: The tal allocated object.
  215. * @callback: the function to call.
  216. */
  217. #define tal_del_notifier(ptr, callback) \
  218. tal_del_notifier_((ptr), \
  219. typesafe_cb_postargs(void, void *, (callback), \
  220. (ptr), \
  221. enum tal_notify_type, void *))
  222. /**
  223. * tal_set_name - attach a name to a tal pointer.
  224. * @ptr: The tal allocated object.
  225. * @name: The name to use.
  226. *
  227. * The name is copied, unless we're certain it's a string literal.
  228. */
  229. #define tal_set_name(ptr, name) \
  230. tal_set_name_((ptr), (name), TAL_IS_LITERAL(name))
  231. /**
  232. * tal_name - get the name for a tal pointer.
  233. * @ptr: The tal allocated object.
  234. *
  235. * Returns NULL if no name has been set.
  236. */
  237. const char *tal_name(const tal_t *ptr);
  238. /**
  239. * tal_count - get the count of objects in a tal_arr.
  240. * @ptr: The tal allocated object array.
  241. *
  242. * Returns 0 if @ptr has no length property, but be aware that that is
  243. * also a valid size!
  244. */
  245. #define tal_count(p) (tal_len(p) / sizeof(*p))
  246. /**
  247. * tal_len - get the count of bytes in a tal_arr.
  248. * @ptr: The tal allocated object array.
  249. *
  250. * Returns 0 if @ptr has no length property, but be aware that that is
  251. * also a valid size!
  252. */
  253. size_t tal_len(const tal_t *ptr);
  254. /**
  255. * tal_first - get the first immediate tal object child.
  256. * @root: The tal allocated object to start with, or NULL.
  257. *
  258. * Returns NULL if there are no children.
  259. */
  260. tal_t *tal_first(const tal_t *root);
  261. /**
  262. * tal_next - get the next immediate tal object child.
  263. * @prev: The return value from tal_first or tal_next.
  264. *
  265. * Returns NULL if there are no more immediate children. This should be safe to
  266. * call on an altering tree unless @prev is no longer valid.
  267. */
  268. tal_t *tal_next(const tal_t *prev);
  269. /**
  270. * tal_parent - get the parent of a tal object.
  271. * @ctx: The tal allocated object.
  272. *
  273. * Returns the parent, which may be NULL. Returns NULL if @ctx is NULL.
  274. */
  275. tal_t *tal_parent(const tal_t *ctx);
  276. /**
  277. * tal_dup - duplicate an object.
  278. * @ctx: The tal allocated object to be parent of the result (may be NULL).
  279. * @type: the type (should match type of @p!)
  280. * @p: the object to copy (or reparented if take())
  281. */
  282. #define tal_dup(ctx, type, p) \
  283. ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \
  284. sizeof(type), 1, 0, \
  285. false, TAL_LABEL(type, "")))
  286. /**
  287. * tal_dup_arr - duplicate an array.
  288. * @ctx: The tal allocated object to be parent of the result (may be NULL).
  289. * @type: the type (should match type of @p!)
  290. * @p: the array to copy (or resized & reparented if take())
  291. * @n: the number of sizeof(type) entries to copy.
  292. * @extra: the number of extra sizeof(type) entries to allocate.
  293. */
  294. #define tal_dup_arr(ctx, type, p, n, extra) \
  295. ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \
  296. sizeof(type), (n), (extra), \
  297. true, TAL_LABEL(type, "[]")))
  298. /**
  299. * tal_set_backend - set the allocation or error functions to use
  300. * @alloc_fn: allocator or NULL (default is malloc)
  301. * @resize_fn: re-allocator or NULL (default is realloc)
  302. * @free_fn: free function or NULL (default is free)
  303. * @error_fn: called on errors or NULL (default is abort)
  304. *
  305. * The defaults are set up so tal functions never return NULL, but you
  306. * can override erorr_fn to change that. error_fn can return, and is
  307. * called if alloc_fn or resize_fn fail.
  308. *
  309. * If any parameter is NULL, that function is unchanged.
  310. */
  311. void tal_set_backend(void *(*alloc_fn)(size_t size),
  312. void *(*resize_fn)(void *, size_t size),
  313. void (*free_fn)(void *),
  314. void (*error_fn)(const char *msg));
  315. /**
  316. * tal_expand - expand a tal array with contents.
  317. * @a1p: a pointer to the tal array to expand.
  318. * @a2: the second array (can be take()).
  319. * @num2: the number of elements in the second array.
  320. *
  321. * Note that *@a1 and @a2 should be the same type. tal_count(@a1) will
  322. * be increased by @num2.
  323. *
  324. * Example:
  325. * int *arr1 = tal_arrz(NULL, int, 2);
  326. * int arr2[2] = { 1, 3 };
  327. *
  328. * tal_expand(&arr1, arr2, 2);
  329. * assert(tal_count(arr1) == 4);
  330. * assert(arr1[2] == 1);
  331. * assert(arr1[3] == 3);
  332. */
  333. #define tal_expand(a1p, a2, num2) \
  334. tal_expand_((void **)(a1p), (a2), sizeof**(a1p), \
  335. (num2) + 0*sizeof(*(a1p) == (a2)))
  336. /**
  337. * tal_cleanup - remove pointers from NULL node
  338. *
  339. * Internally, tal keeps a list of nodes allocated from @ctx NULL; this
  340. * prevents valgrind from noticing memory leaks. This re-initializes
  341. * that list to empty.
  342. *
  343. * It also calls take_cleanup() for you.
  344. */
  345. void tal_cleanup(void);
  346. /**
  347. * tal_check - sanity check a tal context and its children.
  348. * @ctx: a tal context, or NULL.
  349. * @errorstr: a string to prepend calls to error_fn, or NULL.
  350. *
  351. * This sanity-checks a tal tree (unless NDEBUG is defined, in which case
  352. * it simply returns true). If errorstr is not null, error_fn is called
  353. * when a problem is found, otherwise it is not.
  354. *
  355. * See also:
  356. * tal_set_backend()
  357. */
  358. bool tal_check(const tal_t *ctx, const char *errorstr);
  359. #ifdef CCAN_TAL_DEBUG
  360. /**
  361. * tal_dump - dump entire tal tree.
  362. *
  363. * This is a helper for debugging tal itself, which dumps all the tal internal
  364. * state.
  365. */
  366. void tal_dump(void);
  367. #endif
  368. /* Internal support functions */
  369. #ifndef TAL_LABEL
  370. #ifdef CCAN_TAL_NO_LABELS
  371. #define TAL_LABEL(type, arr) NULL
  372. #else
  373. #ifdef CCAN_TAL_DEBUG
  374. #define TAL_LABEL(type, arr) \
  375. __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
  376. #else
  377. #define TAL_LABEL(type, arr) stringify(type) arr
  378. #endif /* CCAN_TAL_DEBUG */
  379. #endif
  380. #endif
  381. #if HAVE_BUILTIN_CONSTANT_P
  382. #define TAL_IS_LITERAL(str) __builtin_constant_p(str)
  383. #else
  384. #define TAL_IS_LITERAL(str) (sizeof(&*(str)) != sizeof(char *))
  385. #endif
  386. bool tal_set_name_(tal_t *ctx, const char *name, bool literal);
  387. #if HAVE_TYPEOF
  388. #define tal_typeof(ptr) (__typeof__(ptr))
  389. #if HAVE_STATEMENT_EXPR
  390. /* Careful: ptr can be const foo *, ptype is foo *. Also, ptr could
  391. * be an array, eg "hello". */
  392. #define tal_typechk_(ptr, ptype) ({ __typeof__((ptr)+0) _p = (ptype)(ptr); _p; })
  393. #else
  394. #define tal_typechk_(ptr, ptype) (ptr)
  395. #endif
  396. #else /* !HAVE_TYPEOF */
  397. #define tal_typeof(ptr)
  398. #define tal_typechk_(ptr, ptype) (ptr)
  399. #endif
  400. void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear,
  401. bool add_length, const char *label);
  402. void *tal_alloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
  403. bool add_length, const char *label);
  404. void *tal_dup_(const tal_t *ctx, const void *p, size_t size,
  405. size_t n, size_t extra, bool add_length,
  406. const char *label);
  407. tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t);
  408. bool tal_resize_(tal_t **ctxp, size_t size, size_t count, bool clear);
  409. bool tal_expand_(tal_t **ctxp, const void *src, size_t size, size_t count);
  410. bool tal_add_destructor_(const tal_t *ctx, void (*destroy)(void *me));
  411. bool tal_del_destructor_(const tal_t *ctx, void (*destroy)(void *me));
  412. bool tal_add_notifier_(const tal_t *ctx, enum tal_notify_type types,
  413. void (*notify)(tal_t *ctx, enum tal_notify_type,
  414. void *info));
  415. bool tal_del_notifier_(const tal_t *ctx,
  416. void (*notify)(tal_t *ctx, enum tal_notify_type,
  417. void *info));
  418. #endif /* CCAN_TAL_H */