talloc.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /* Licensed under LGPL - see LICENSE file for details */
  2. #ifndef CCAN_TAL_TALLOC_H
  3. #define CCAN_TAL_TALLOC_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 <talloc.h>
  10. #include <stdlib.h>
  11. #include <stdbool.h>
  12. #include <stdarg.h>
  13. /**
  14. * tal_t - convenient alias for void to mark tal pointers.
  15. *
  16. * Since any pointer can be a tal-allocated pointer, it's often
  17. * useful to use this typedef to mark them explicitly.
  18. */
  19. typedef TALLOC_CTX tal_t;
  20. /**
  21. * tal - basic allocator function
  22. * @ctx: NULL, or tal allocated object to be parent.
  23. * @type: the type to allocate.
  24. *
  25. * Allocates a specific type, with a given parent context. The name
  26. * of the object is a string of the type, but if CCAN_TAL_DEBUG is
  27. * defined it also contains the file and line which allocated it.
  28. *
  29. * Example:
  30. * int *p = tal(NULL, int);
  31. * *p = 1;
  32. */
  33. #define tal(ctx, type) \
  34. ((type *)tal_talloc_((ctx), sizeof(type), false, \
  35. TAL_LABEL(type, "")))
  36. /**
  37. * talz - zeroing allocator function
  38. * @ctx: NULL, or tal allocated object to be parent.
  39. * @type: the type to allocate.
  40. *
  41. * Equivalent to tal() followed by memset() to zero.
  42. *
  43. * Example:
  44. * p = talz(NULL, int);
  45. * assert(*p == 0);
  46. */
  47. #define talz(ctx, type) \
  48. ((type *)tal_talloc_((ctx), sizeof(type), true, \
  49. TAL_LABEL(type, "")))
  50. /**
  51. * tal_free - free a tal-allocated pointer.
  52. * @p: NULL, or tal allocated object to free.
  53. *
  54. * This calls the destructors for p (if any), then does the same for all its
  55. * children (recursively) before finally freeing the memory. It returns
  56. * NULL, for convenience.
  57. *
  58. * Note: errno is preserved by this call.
  59. *
  60. * Example:
  61. * p = tal_free(p);
  62. */
  63. #define tal_free(p) tal_talloc_free_(p)
  64. /**
  65. * tal_arr - allocate an array of objects.
  66. * @ctx: NULL, or tal allocated object to be parent.
  67. * @type: the type to allocate.
  68. * @count: the number to allocate.
  69. *
  70. * Note that an object allocated with tal_arr() has a length property;
  71. * see tal_count().
  72. *
  73. * Example:
  74. * p = tal_arr(NULL, int, 2);
  75. * p[0] = 0;
  76. * p[1] = 1;
  77. */
  78. #define tal_arr(ctx, type, count) \
  79. ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), false, \
  80. TAL_LABEL(type, "[]")))
  81. /**
  82. * tal_arrz - allocate an array of zeroed objects.
  83. * @ctx: NULL, or tal allocated object to be parent.
  84. * @type: the type to allocate.
  85. * @count: the number to allocate.
  86. *
  87. * Note that an object allocated with tal_arrz() has a length property;
  88. * see tal_count().
  89. *
  90. * Example:
  91. * p = tal_arrz(NULL, int, 2);
  92. * assert(p[0] == 0 && p[1] == 0);
  93. */
  94. #define tal_arrz(ctx, type, count) \
  95. ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), true, \
  96. TAL_LABEL(type, "[]")))
  97. /**
  98. * tal_resize - enlarge or reduce a tal_arr[z].
  99. * @p: A pointer to the tal allocated array to resize.
  100. * @count: the number to allocate.
  101. *
  102. * This returns true on success (and may move *@p), or false on failure.
  103. * If @p has a length property, it is updated on success.
  104. *
  105. * Example:
  106. * tal_resize(&p, 100);
  107. */
  108. #define tal_resize(p, count) \
  109. tal_talloc_resize_((void **)(p), sizeof**(p), (count))
  110. /**
  111. * tal_steal - change the parent of a tal-allocated pointer.
  112. * @ctx: The new parent.
  113. * @ptr: The tal allocated object to move.
  114. *
  115. * This may need to perform an allocation, in which case it may fail; thus
  116. * it can return NULL, otherwise returns @ptr.
  117. */
  118. #define tal_steal(ctx, ptr) talloc_steal((ctx), (ptr))
  119. /**
  120. * tal_add_destructor - add a callback function when this context is destroyed.
  121. * @ptr: The tal allocated object.
  122. * @function: the function to call before it's freed.
  123. *
  124. * This is a more convenient form of tal_add_notifier(@ptr,
  125. * TAL_NOTIFY_FREE, ...), in that the function prototype takes only @ptr.
  126. */
  127. #define tal_add_destructor(ptr, function) \
  128. tal_talloc_add_destructor_((ptr), typesafe_cb(void, void *, \
  129. (function), (ptr)))
  130. /**
  131. * tal_del_destructor - remove a destructor callback function.
  132. * @ptr: The tal allocated object.
  133. * @function: the function to call before it's freed.
  134. *
  135. * If @function has not been successfully added as a destructor, this returns
  136. * false.
  137. *
  138. * Note: you can't add more than one destructor with the talloc backend!
  139. */
  140. #define tal_del_destructor(ptr, function) \
  141. tal_talloc_del_destructor_((ptr), typesafe_cb(void, void *, \
  142. (function), (ptr)))
  143. /**
  144. * tal_set_name - attach a name to a tal pointer.
  145. * @ptr: The tal allocated object.
  146. * @name: The name to use.
  147. *
  148. * The name is copied, unless we're certain it's a string literal.
  149. */
  150. #define tal_set_name(ptr, name) \
  151. tal_talloc_set_name_((ptr), (name), TAL_TALLOC_IS_LITERAL(name))
  152. /**
  153. * tal_name - get the name for a tal pointer.
  154. * @ptr: The tal allocated object.
  155. *
  156. * Returns NULL if no name has been set.
  157. */
  158. #define tal_name(ptr) \
  159. tal_talloc_name_(ptr)
  160. /**
  161. * tal_count - get the count of objects in a tal_arr.
  162. * @ptr: The tal allocated object array.
  163. */
  164. #define tal_count(ptr) talloc_array_length(ptr)
  165. /**
  166. * tal_parent - get the parent of a tal object.
  167. * @ctx: The tal allocated object.
  168. *
  169. * Returns the parent, which may be NULL. Returns NULL if @ctx is NULL.
  170. */
  171. #define tal_parent(ctx) talloc_parent(ctx)
  172. /**
  173. * tal_dup - duplicate an object.
  174. * @ctx: The tal allocated object to be parent of the result (may be NULL).
  175. * @type: the type (should match type of @p!)
  176. * @p: the object to copy (or reparented if take())
  177. */
  178. #define tal_dup(ctx, type, p) \
  179. ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
  180. sizeof(type), 1, 0, \
  181. TAL_LABEL(type, "")))
  182. /**
  183. * tal_dup_arr - duplicate an array.
  184. * @ctx: The tal allocated object to be parent of the result (may be NULL).
  185. * @type: the type (should match type of @p!)
  186. * @p: the array to copy (or resized & reparented if take())
  187. * @n: the number of sizeof(type) entries to copy.
  188. * @extra: the number of extra sizeof(type) entries to allocate.
  189. */
  190. #define tal_dup_arr(ctx, type, p, n, extra) \
  191. ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
  192. sizeof(type), (n), (extra), \
  193. TAL_LABEL(type, "[]")))
  194. /**
  195. * tal_set_backend - set the allocation or error functions to use
  196. * @alloc_fn: NULL
  197. * @resize_fn: NULL
  198. * @free_fn: NULL
  199. * @error_fn: called on errors or NULL (default is abort)
  200. *
  201. * The defaults are set up so tal functions never return NULL, but you
  202. * can override error_fn to change that. error_fn can return, and is
  203. * called if malloc or realloc fail.
  204. */
  205. #define tal_set_backend(alloc_fn, resize_fn, free_fn, error_fn) \
  206. tal_talloc_set_backend_((alloc_fn), (resize_fn), (free_fn), (error_fn))
  207. /**
  208. * tal_expand - expand a tal array with contents.
  209. * @a1p: a pointer to the tal array to expand.
  210. * @a2: the second array (can be take()).
  211. * @num2: the number of elements in the second array.
  212. *
  213. * Note that *@a1 and @a2 should be the same type. tal_count(@a1) will
  214. * be increased by @num2.
  215. *
  216. * Example:
  217. * int *arr1 = tal_arrz(NULL, int, 2);
  218. * int arr2[2] = { 1, 3 };
  219. *
  220. * tal_expand(&arr1, arr2, 2);
  221. * assert(tal_count(arr1) == 4);
  222. * assert(arr1[2] == 1);
  223. * assert(arr1[3] == 3);
  224. */
  225. #define tal_expand(a1p, a2, num2) \
  226. tal_talloc_expand_((void **)(a1p), (a2), sizeof**(a1p), \
  227. (num2) + 0*sizeof(*(a1p) == (a2)))
  228. /**
  229. * tal_check - set the allocation or error functions to use
  230. * @ctx: a tal context, or NULL.
  231. * @errorstr: a string to prepend calls to error_fn, or NULL.
  232. *
  233. * This sanity-checks a tal tree (unless NDEBUG is defined, in which case
  234. * it simply returns true). If errorstr is not null, error_fn is called
  235. * when a problem is found, otherwise it is not.
  236. */
  237. #define tal_check(ctx, errorstr) \
  238. tal_talloc_check_((ctx), (errorstr))
  239. /* Internal support functions */
  240. #ifndef TAL_TALLOC_LABEL
  241. #ifdef CCAN_TAL_NO_LABELS
  242. #define TAL_LABEL(type, arr) NULL
  243. #else
  244. #ifdef CCAN_TAL_DEBUG
  245. #define TAL_LABEL(type, arr) \
  246. __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
  247. #else
  248. #define TAL_LABEL(type, arr) stringify(type) arr
  249. #endif /* CCAN_TAL_DEBUG */
  250. #endif
  251. #endif
  252. #if HAVE_BUILTIN_CONSTANT_P
  253. #define TAL_TALLOC_IS_LITERAL(str) __builtin_constant_p(str)
  254. #else
  255. #define TAL_TALLOC_IS_LITERAL(str) false
  256. #endif
  257. #if HAVE_TYPEOF && HAVE_STATEMENT_EXPR
  258. /* Careful: ptr can be const foo *, ptype is foo *. Also, ptr could
  259. * be an array, eg "hello". */
  260. #define tal_talloc_typechk_(ptr, ptype) ({ __typeof__((ptr)+0) _p = (ptype)(ptr); _p; })
  261. #else
  262. #define tal_talloc_typechk_(ptr, ptype) (ptr)
  263. #endif
  264. void *tal_talloc_(const tal_t *ctx, size_t bytes, bool clear,
  265. const char *label);
  266. void *tal_talloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
  267. const char *label);
  268. void *tal_talloc_free_(const tal_t *ctx);
  269. const char *tal_talloc_name_(const tal_t *ctx);
  270. bool tal_talloc_set_name_(tal_t *ctx, const char *name, bool literal);
  271. bool tal_talloc_add_destructor_(const tal_t *ctx, void (*destroy)(void *me));
  272. bool tal_talloc_del_destructor_(const tal_t *ctx, void (*destroy)(void *me));
  273. /* ccan/tal/str uses this, so define it. */
  274. #define tal_dup_(ctx, p, size, n, extra, add_count, label) \
  275. tal_talloc_dup_((ctx), (p), (size), (n), (extra), (label))
  276. void *tal_talloc_dup_(const tal_t *ctx, const void *p, size_t size,
  277. size_t n, size_t extra, const char *label);
  278. bool tal_talloc_resize_(tal_t **ctxp, size_t size, size_t count);
  279. bool tal_talloc_expand_(tal_t **ctxp, const void *src, size_t size, size_t count);
  280. bool tal_talloc_check_(const tal_t *ctx, const char *errorstr);
  281. void tal_talloc_set_backend_(void *(*alloc_fn)(size_t size),
  282. void *(*resize_fn)(void *, size_t size),
  283. void (*free_fn)(void *),
  284. void (*error_fn)(const char *msg));
  285. #endif /* CCAN_TAL_TALLOC_H */