]> git.ozlabs.org Git - ccan/blob - ccan/tal/talloc/talloc.h
718f12c4095f7c26f4d4d50b40e67d13fe26350a
[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 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.
194  */
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, "[]")))
199
200
201 /**
202  * tal_set_backend - set the allocation or error functions to use
203  * @alloc_fn: NULL
204  * @resize_fn: NULL
205  * @free_fn: NULL
206  * @error_fn: called on errors or NULL (default is abort)
207  *
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.
211  */
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))
214
215 /**
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.
220  *
221  * Note that *@a1 and @a2 should be the same type.  tal_count(@a1) will
222  * be increased by @num2.
223  *
224  * Example:
225  *      int *arr1 = tal_arrz(NULL, int, 2);
226  *      int arr2[2] = { 1, 3 };
227  *
228  *      tal_expand(&arr1, arr2, 2);
229  *      assert(tal_count(arr1) == 4);
230  *      assert(arr1[2] == 1);
231  *      assert(arr1[3] == 3);
232  */
233 #define tal_expand(a1p, a2, num2)                               \
234         tal_talloc_expand_((void **)(a1p), (a2), sizeof**(a1p), \
235                            (num2) + 0*sizeof(*(a1p) == (a2)))
236
237
238 /**
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.
242  *
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.
246  */
247 #define tal_check(ctx, errorstr) \
248         tal_talloc_check_((ctx), (errorstr))
249
250
251 /* Internal support functions */
252 #ifndef TAL_TALLOC_LABEL
253 #ifdef CCAN_TAL_NO_LABELS
254 #define TAL_LABEL(type, arr) NULL
255 #else
256 #ifdef CCAN_TAL_DEBUG
257 #define TAL_LABEL(type, arr) \
258         __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
259 #else
260 #define TAL_LABEL(type, arr) stringify(type) arr
261 #endif /* CCAN_TAL_DEBUG */
262 #endif
263 #endif
264
265 #if HAVE_BUILTIN_CONSTANT_P
266 #define TAL_TALLOC_IS_LITERAL(str) __builtin_constant_p(str)
267 #else
268 #define TAL_TALLOC_IS_LITERAL(str) false
269 #endif
270
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; })
275 #else
276 #define tal_talloc_typechk_(ptr, ptype) (ptr)
277 #endif
278
279 void *tal_talloc_(const tal_t *ctx, size_t bytes, bool clear,
280                   const char *label);
281 void *tal_talloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
282                       const char *label);
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);
286
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));
289
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);
295
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);
299
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));
304
305 #endif /* CCAN_TAL_TALLOC_H */