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