1 /* Licensed under LGPL - see LICENSE file for details */
2 #ifndef CCAN_TAL_TALLOC_H
3 #define CCAN_TAL_TALLOC_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 <ccan/take/take.h>
16 * tal_t - convenient alias for void to mark tal pointers.
18 * Since any pointer can be a tal-allocated pointer, it's often
19 * useful to use this typedef to mark them explicitly.
21 typedef TALLOC_CTX tal_t;
24 * tal - basic allocator function
25 * @ctx: NULL, or tal allocated object to be parent.
26 * @type: the type to allocate.
28 * Allocates a specific type, with a given parent context. The name
29 * of the object is a string of the type, but if CCAN_TAL_DEBUG is
30 * defined it also contains the file and line which allocated it.
33 * int *p = tal(NULL, int);
36 #define tal(ctx, type) \
37 ((type *)tal_talloc_((ctx), sizeof(type), false, \
41 * talz - zeroing allocator function
42 * @ctx: NULL, or tal allocated object to be parent.
43 * @type: the type to allocate.
45 * Equivalent to tal() followed by memset() to zero.
48 * p = talz(NULL, int);
51 #define talz(ctx, type) \
52 ((type *)tal_talloc_((ctx), sizeof(type), true, \
56 * tal_free - free a tal-allocated pointer.
57 * @p: NULL, or tal allocated object to free.
59 * This calls the destructors for p (if any), then does the same for all its
60 * children (recursively) before finally freeing the memory. It returns
61 * NULL, for convenience.
63 * Note: errno is preserved by this call.
68 #define tal_free(p) tal_talloc_free_(p)
71 * tal_arr - allocate an array of objects.
72 * @ctx: NULL, or tal allocated object to be parent.
73 * @type: the type to allocate.
74 * @count: the number to allocate.
76 * Note that an object allocated with tal_arr() has a length property;
80 * p = tal_arr(NULL, int, 2);
84 #define tal_arr(ctx, type, count) \
85 ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), false, \
86 TAL_LABEL(type, "[]")))
89 * tal_arrz - allocate an array of zeroed objects.
90 * @ctx: NULL, or tal allocated object to be parent.
91 * @type: the type to allocate.
92 * @count: the number to allocate.
94 * Note that an object allocated with tal_arrz() has a length property;
98 * p = tal_arrz(NULL, int, 2);
99 * assert(p[0] == 0 && p[1] == 0);
101 #define tal_arrz(ctx, type, count) \
102 ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), true, \
103 TAL_LABEL(type, "[]")))
106 * tal_resize - enlarge or reduce a tal_arr[z].
107 * @p: A pointer to the tal allocated array to resize.
108 * @count: the number to allocate.
110 * This returns true on success (and may move *@p), or false on failure.
111 * If @p has a length property, it is updated on success.
114 * tal_resize(&p, 100);
116 #define tal_resize(p, count) \
117 tal_talloc_resize_((void **)(p), sizeof**(p), (count))
120 * tal_steal - change the parent of a tal-allocated pointer.
121 * @ctx: The new parent.
122 * @ptr: The tal allocated object to move.
124 * This may need to perform an allocation, in which case it may fail; thus
125 * it can return NULL, otherwise returns @ptr.
127 #define tal_steal(ctx, ptr) talloc_steal((ctx), (ptr))
130 * tal_add_destructor - add a callback function when this context is destroyed.
131 * @ptr: The tal allocated object.
132 * @function: the function to call before it's freed.
134 * This is a more convenient form of tal_add_notifier(@ptr,
135 * TAL_NOTIFY_FREE, ...), in that the function prototype takes only @ptr.
137 #define tal_add_destructor(ptr, function) \
138 tal_talloc_add_destructor_((ptr), typesafe_cb(void, void *, \
142 * tal_del_destructor - remove a destructor callback function.
143 * @ptr: The tal allocated object.
144 * @function: the function to call before it's freed.
146 * If @function has not been successfully added as a destructor, this returns
149 * Note: you can't add more than one destructor with the talloc backend!
151 #define tal_del_destructor(ptr, function) \
152 tal_talloc_del_destructor_((ptr), typesafe_cb(void, void *, \
156 * tal_set_name - attach a name to a tal pointer.
157 * @ptr: The tal allocated object.
158 * @name: The name to use.
160 * The name is copied, unless we're certain it's a string literal.
162 #define tal_set_name(ptr, name) \
163 tal_talloc_set_name_((ptr), (name), TAL_TALLOC_IS_LITERAL(name))
166 * tal_name - get the name for a tal pointer.
167 * @ptr: The tal allocated object.
169 * Returns NULL if no name has been set.
171 #define tal_name(ptr) \
172 tal_talloc_name_(ptr)
175 * tal_count - get the count of objects in a tal_arr.
176 * @ptr: The tal allocated object array.
178 #define tal_count(ptr) talloc_array_length(ptr)
181 * tal_parent - get the parent of a tal object.
182 * @ctx: The tal allocated object.
184 * Returns the parent, which may be NULL. Returns NULL if @ctx is NULL.
186 #define tal_parent(ctx) talloc_parent(ctx)
189 * tal_dup - duplicate an object.
190 * @ctx: The tal allocated object to be parent of the result (may be NULL).
191 * @type: the type (should match type of @p!)
192 * @p: the object to copy (or reparented if take())
194 #define tal_dup(ctx, type, p) \
195 ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
196 sizeof(type), 1, 0, \
197 TAL_LABEL(type, "")))
200 * tal_dup_arr - duplicate an array.
201 * @ctx: The tal allocated object to be parent of the result (may be NULL).
202 * @type: the type (should match type of @p!)
203 * @p: the array to copy (or resized & reparented if take())
204 * @n: the number of sizeof(type) entries to copy.
205 * @extra: the number of extra sizeof(type) entries to allocate.
207 #define tal_dup_arr(ctx, type, p, n, extra) \
208 ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
209 sizeof(type), (n), (extra), \
210 TAL_LABEL(type, "[]")))
214 * tal_set_backend - set the allocation or error functions to use
218 * @error_fn: called on errors or NULL (default is abort)
220 * The defaults are set up so tal functions never return NULL, but you
221 * can override error_fn to change that. error_fn can return, and is
222 * called if malloc or realloc fail.
224 #define tal_set_backend(alloc_fn, resize_fn, free_fn, error_fn) \
225 tal_talloc_set_backend_((alloc_fn), (resize_fn), (free_fn), (error_fn))
228 * tal_expand - expand a tal array with contents.
229 * @a1p: a pointer to the tal array to expand.
230 * @a2: the second array (can be take()).
231 * @num2: the number of elements in the second array.
233 * Note that *@a1 and @a2 should be the same type. tal_count(@a1) will
234 * be increased by @num2.
237 * int *arr1 = tal_arrz(NULL, int, 2);
238 * int arr2[2] = { 1, 3 };
240 * tal_expand(&arr1, arr2, 2);
241 * assert(tal_count(arr1) == 4);
242 * assert(arr1[2] == 1);
243 * assert(arr1[3] == 3);
245 #define tal_expand(a1p, a2, num2) \
246 tal_talloc_expand_((void **)(a1p), (a2), sizeof**(a1p), \
247 (num2) + 0*sizeof(*(a1p) == (a2)))
251 * tal_check - set the allocation or error functions to use
252 * @ctx: a tal context, or NULL.
253 * @errorstr: a string to prepend calls to error_fn, or NULL.
255 * This sanity-checks a tal tree (unless NDEBUG is defined, in which case
256 * it simply returns true). If errorstr is not null, error_fn is called
257 * when a problem is found, otherwise it is not.
259 #define tal_check(ctx, errorstr) \
260 tal_talloc_check_((ctx), (errorstr))
263 /* Internal support functions */
264 #ifndef TAL_TALLOC_LABEL
265 #ifdef CCAN_TAL_NO_LABELS
266 #define TAL_LABEL(type, arr) NULL
268 #ifdef CCAN_TAL_DEBUG
269 #define TAL_LABEL(type, arr) \
270 __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
272 #define TAL_LABEL(type, arr) stringify(type) arr
273 #endif /* CCAN_TAL_DEBUG */
277 #if HAVE_BUILTIN_CONSTANT_P
278 #define TAL_TALLOC_IS_LITERAL(str) __builtin_constant_p(str)
280 #define TAL_TALLOC_IS_LITERAL(str) false
283 #if HAVE_TYPEOF && HAVE_STATEMENT_EXPR
284 /* Careful: ptr can be const foo *, ptype is foo *. Also, ptr could
285 * be an array, eg "hello". */
286 #define tal_talloc_typechk_(ptr, ptype) ({ __typeof__((ptr)+0) _p = (ptype)(ptr); _p; })
288 #define tal_talloc_typechk_(ptr, ptype) (ptr)
291 void *tal_talloc_(const tal_t *ctx, size_t bytes, bool clear,
293 void *tal_talloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
295 void *tal_talloc_free_(const tal_t *ctx);
296 const char *tal_talloc_name_(const tal_t *ctx);
297 bool tal_talloc_set_name_(tal_t *ctx, const char *name, bool literal);
299 bool tal_talloc_add_destructor_(const tal_t *ctx, void (*destroy)(void *me));
300 bool tal_talloc_del_destructor_(const tal_t *ctx, void (*destroy)(void *me));
302 /* ccan/tal/str uses this, so define it. */
303 #define tal_dup_(ctx, p, size, n, extra, add_count, label) \
304 tal_talloc_dup_((ctx), (p), (size), (n), (extra), (label))
305 void *tal_talloc_dup_(const tal_t *ctx, const void *p, size_t size,
306 size_t n, size_t extra, const char *label);
308 bool tal_talloc_resize_(tal_t **ctxp, size_t size, size_t count);
309 bool tal_talloc_expand_(tal_t **ctxp, const void *src, size_t size, size_t count);
310 bool tal_talloc_check_(const tal_t *ctx, const char *errorstr);
312 void tal_talloc_set_backend_(void *(*alloc_fn)(size_t size),
313 void *(*resize_fn)(void *, size_t size),
314 void (*free_fn)(void *),
315 void (*error_fn)(const char *msg));
317 #endif /* CCAN_TAL_TALLOC_H */