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>
15 * tal_t - convenient alias for void to mark tal pointers.
17 * Since any pointer can be a tal-allocated pointer, it's often
18 * useful to use this typedef to mark them explicitly.
20 typedef TALLOC_CTX tal_t;
23 * tal - basic allocator function
24 * @ctx: NULL, or tal allocated object to be parent.
25 * @type: the type to allocate.
27 * Allocates a specific type, with a given parent context. The name
28 * of the object is a string of the type, but if CCAN_TAL_DEBUG is
29 * defined it also contains the file and line which allocated it.
32 * int *p = tal(NULL, int);
35 #define tal(ctx, type) \
36 ((type *)tal_talloc_((ctx), sizeof(type), false, \
40 * talz - zeroing allocator function
41 * @ctx: NULL, or tal allocated object to be parent.
42 * @type: the type to allocate.
44 * Equivalent to tal() followed by memset() to zero.
47 * p = talz(NULL, int);
50 #define talz(ctx, type) \
51 ((type *)tal_talloc_((ctx), sizeof(type), true, \
55 * tal_free - free a tal-allocated pointer.
56 * @p: NULL, or tal allocated object to free.
58 * This calls the destructors for p (if any), then does the same for all its
59 * children (recursively) before finally freeing the memory. It returns
60 * NULL, for convenience.
62 * Note: errno is preserved by this call.
67 #define tal_free(p) tal_talloc_free_(p)
70 * tal_arr - allocate an array of objects.
71 * @ctx: NULL, or tal allocated object to be parent.
72 * @type: the type to allocate.
73 * @count: the number to allocate.
75 * Note that an object allocated with tal_arr() has a length property;
79 * p = tal_arr(NULL, int, 2);
83 #define tal_arr(ctx, type, count) \
84 ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), false, \
85 TAL_LABEL(type, "[]")))
88 * tal_arrz - allocate an array of zeroed objects.
89 * @ctx: NULL, or tal allocated object to be parent.
90 * @type: the type to allocate.
91 * @count: the number to allocate.
93 * Note that an object allocated with tal_arrz() has a length property;
97 * p = tal_arrz(NULL, int, 2);
98 * assert(p[0] == 0 && p[1] == 0);
100 #define tal_arrz(ctx, type, count) \
101 ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), true, \
102 TAL_LABEL(type, "[]")))
105 * tal_resize - enlarge or reduce a tal_arr[z].
106 * @p: A pointer to the tal allocated array to resize.
107 * @count: the number to allocate.
109 * This returns true on success (and may move *@p), or false on failure.
110 * If @p has a length property, it is updated on success.
113 * tal_resize(&p, 100);
115 #define tal_resize(p, count) \
116 tal_talloc_resize_((void **)(p), sizeof**(p), (count))
119 * tal_steal - change the parent of a tal-allocated pointer.
120 * @ctx: The new parent.
121 * @ptr: The tal allocated object to move.
123 * This may need to perform an allocation, in which case it may fail; thus
124 * it can return NULL, otherwise returns @ptr.
126 #define tal_steal(ctx, ptr) talloc_steal((ctx), (ptr))
129 * tal_add_destructor - add a callback function when this context is destroyed.
130 * @ptr: The tal allocated object.
131 * @function: the function to call before it's freed.
133 * This is a more convenient form of tal_add_notifier(@ptr,
134 * TAL_NOTIFY_FREE, ...), in that the function prototype takes only @ptr.
136 #define tal_add_destructor(ptr, function) \
137 tal_talloc_add_destructor_((ptr), typesafe_cb(void, void *, \
141 * tal_del_destructor - remove a destructor callback function.
142 * @ptr: The tal allocated object.
143 * @function: the function to call before it's freed.
145 * If @function has not been successfully added as a destructor, this returns
148 * Note: you can't add more than one destructor with the talloc backend!
150 #define tal_del_destructor(ptr, function) \
151 tal_talloc_del_destructor_((ptr), typesafe_cb(void, void *, \
155 * tal_set_name - attach a name to a tal pointer.
156 * @ptr: The tal allocated object.
157 * @name: The name to use.
159 * The name is copied, unless we're certain it's a string literal.
161 #define tal_set_name(ptr, name) \
162 tal_talloc_set_name_((ptr), (name), TAL_TALLOC_IS_LITERAL(name))
165 * tal_name - get the name for a tal pointer.
166 * @ptr: The tal allocated object.
168 * Returns NULL if no name has been set.
170 #define tal_name(ptr) \
171 tal_talloc_name_(ptr)
174 * tal_count - get the count of objects in a tal_arr.
175 * @ptr: The tal allocated object array.
177 #define tal_count(ptr) talloc_array_length(ptr)
180 * tal_parent - get the parent of a tal object.
181 * @ctx: The tal allocated object.
183 * Returns the parent, which may be NULL. Returns NULL if @ctx is NULL.
185 #define tal_parent(ctx) talloc_parent(ctx)
188 * tal_dup - duplicate an array.
189 * @ctx: The tal allocated object to be parent of the result (may be NULL).
190 * @type: the type (should match type of @p!)
191 * @p: the array to copy (or resized & reparented if take())
192 * @n: the number of sizeof(type) entries to copy.
193 * @extra: the number of extra sizeof(type) entries to allocate.
195 #define tal_dup(ctx, type, p, n, extra) \
196 ((type *)tal_talloc_dup_((ctx), tal_talloc_typechk_(p, type *), \
197 sizeof(type), (n), (extra), \
198 TAL_LABEL(type, "[]")))
202 * tal_set_backend - set the allocation or error functions to use
206 * @error_fn: called on errors or NULL (default is abort)
208 * The defaults are set up so tal functions never return NULL, but you
209 * can override error_fn to change that. error_fn can return, and is
210 * called if malloc or realloc fail.
212 #define tal_set_backend(alloc_fn, resize_fn, free_fn, error_fn) \
213 tal_talloc_set_backend_((alloc_fn), (resize_fn), (free_fn), (error_fn))
216 * tal_expand - expand a tal array with contents.
217 * @a1p: a pointer to the tal array to expand.
218 * @a2: the second array (can be take()).
219 * @num2: the number of elements in the second array.
221 * Note that *@a1 and @a2 should be the same type. tal_count(@a1) will
222 * be increased by @num2.
225 * int *arr1 = tal_arrz(NULL, int, 2);
226 * int arr2[2] = { 1, 3 };
228 * tal_expand(&arr1, arr2, 2);
229 * assert(tal_count(arr1) == 4);
230 * assert(arr1[2] == 1);
231 * assert(arr1[3] == 3);
233 #define tal_expand(a1p, a2, num2) \
234 tal_talloc_expand_((void **)(a1p), (a2), sizeof**(a1p), \
235 (num2) + 0*sizeof(*(a1p) == (a2)))
239 * tal_check - set the allocation or error functions to use
240 * @ctx: a tal context, or NULL.
241 * @errorstr: a string to prepend calls to error_fn, or NULL.
243 * This sanity-checks a tal tree (unless NDEBUG is defined, in which case
244 * it simply returns true). If errorstr is not null, error_fn is called
245 * when a problem is found, otherwise it is not.
247 #define tal_check(ctx, errorstr) \
248 tal_talloc_check_((ctx), (errorstr))
251 /* Internal support functions */
252 #ifndef TAL_TALLOC_LABEL
253 #ifdef CCAN_TAL_NO_LABELS
254 #define TAL_LABEL(type, arr) NULL
256 #ifdef CCAN_TAL_DEBUG
257 #define TAL_LABEL(type, arr) \
258 __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
260 #define TAL_LABEL(type, arr) stringify(type) arr
261 #endif /* CCAN_TAL_DEBUG */
265 #if HAVE_BUILTIN_CONSTANT_P
266 #define TAL_TALLOC_IS_LITERAL(str) __builtin_constant_p(str)
268 #define TAL_TALLOC_IS_LITERAL(str) false
271 #if HAVE_TYPEOF && HAVE_STATEMENT_EXPR
272 /* Careful: ptr can be const foo *, ptype is foo *. Also, ptr could
273 * be an array, eg "hello". */
274 #define tal_talloc_typechk_(ptr, ptype) ({ __typeof__((ptr)+0) _p = (ptype)(ptr); _p; })
276 #define tal_talloc_typechk_(ptr, ptype) (ptr)
279 void *tal_talloc_(const tal_t *ctx, size_t bytes, bool clear,
281 void *tal_talloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
283 void *tal_talloc_free_(const tal_t *ctx);
284 const char *tal_talloc_name_(const tal_t *ctx);
285 bool tal_talloc_set_name_(tal_t *ctx, const char *name, bool literal);
287 bool tal_talloc_add_destructor_(const tal_t *ctx, void (*destroy)(void *me));
288 bool tal_talloc_del_destructor_(const tal_t *ctx, void (*destroy)(void *me));
290 /* ccan/tal/str uses this, so define it. */
291 #define tal_dup_(ctx, p, size, n, extra, add_count, label) \
292 tal_talloc_dup_((ctx), (p), (size), (n), (extra), (label))
293 void *tal_talloc_dup_(const tal_t *ctx, const void *p, size_t size,
294 size_t n, size_t extra, const char *label);
296 bool tal_talloc_resize_(tal_t **ctxp, size_t size, size_t count);
297 bool tal_talloc_expand_(tal_t **ctxp, const void *src, size_t size, size_t count);
298 bool tal_talloc_check_(const tal_t *ctx, const char *errorstr);
300 void tal_talloc_set_backend_(void *(*alloc_fn)(size_t size),
301 void *(*resize_fn)(void *, size_t size),
302 void (*free_fn)(void *),
303 void (*error_fn)(const char *msg));
305 #endif /* CCAN_TAL_TALLOC_H */