]> git.ozlabs.org Git - ccan/blob - ccan/tal/talloc/talloc.h
base64: fix for unsigned chars (e.g. ARM).
[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 <ccan/take/take.h>
10 #include <talloc.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 #include <stdarg.h>
14
15 /**
16  * tal_t - convenient alias for void to mark tal pointers.
17  *
18  * Since any pointer can be a tal-allocated pointer, it's often
19  * useful to use this typedef to mark them explicitly.
20  */
21 typedef TALLOC_CTX tal_t;
22
23 /**
24  * tal - basic allocator function
25  * @ctx: NULL, or tal allocated object to be parent.
26  * @type: the type to allocate.
27  *
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.
31  *
32  * Example:
33  *      int *p = tal(NULL, int);
34  *      *p = 1;
35  */
36 #define tal(ctx, type)                                                  \
37         ((type *)tal_talloc_((ctx), sizeof(type), false,                \
38                              TAL_LABEL(type, "")))
39
40 /**
41  * talz - zeroing allocator function
42  * @ctx: NULL, or tal allocated object to be parent.
43  * @type: the type to allocate.
44  *
45  * Equivalent to tal() followed by memset() to zero.
46  *
47  * Example:
48  *      p = talz(NULL, int);
49  *      assert(*p == 0);
50  */
51 #define talz(ctx, type)                                         \
52         ((type *)tal_talloc_((ctx), sizeof(type), true,         \
53                              TAL_LABEL(type, "")))
54
55 /**
56  * tal_free - free a tal-allocated pointer.
57  * @p: NULL, or tal allocated object to free.
58  *
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.
62  *
63  * Note: errno is preserved by this call.
64  *
65  * Example:
66  *      p = tal_free(p);
67  */
68 #define tal_free(p) tal_talloc_free_(p)
69
70 /**
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.
75  *
76  * Note that an object allocated with tal_arr() has a length property;
77  * see tal_count().
78  *
79  * Example:
80  *      p = tal_arr(NULL, int, 2);
81  *      p[0] = 0;
82  *      p[1] = 1;
83  */
84 #define tal_arr(ctx, type, count)                                       \
85         ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), false,   \
86                                  TAL_LABEL(type, "[]")))
87
88 /**
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.
93  *
94  * Note that an object allocated with tal_arrz() has a length property;
95  * see tal_count().
96  *
97  * Example:
98  *      p = tal_arrz(NULL, int, 2);
99  *      assert(p[0] == 0 && p[1] == 0);
100  */
101 #define tal_arrz(ctx, type, count) \
102         ((type *)tal_talloc_arr_((ctx), sizeof(type), (count), true,    \
103                                  TAL_LABEL(type, "[]")))
104
105 /**
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.
109  *
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.
112  *
113  * Example:
114  *      tal_resize(&p, 100);
115  */
116 #define tal_resize(p, count) \
117         tal_talloc_resize_((void **)(p), sizeof**(p), (count))
118
119 /**
120  * tal_steal - change the parent of a tal-allocated pointer.
121  * @ctx: The new parent.
122  * @ptr: The tal allocated object to move.
123  *
124  * This may need to perform an allocation, in which case it may fail; thus
125  * it can return NULL, otherwise returns @ptr.
126  */
127 #define tal_steal(ctx, ptr) talloc_steal((ctx), (ptr))
128
129 /**
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.
133  *
134  * This is a more convenient form of tal_add_notifier(@ptr,
135  * TAL_NOTIFY_FREE, ...), in that the function prototype takes only @ptr.
136  */
137 #define tal_add_destructor(ptr, function)                               \
138         tal_talloc_add_destructor_((ptr), typesafe_cb(void, void *,     \
139                                                       (function), (ptr)))
140
141 /**
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.
145  *
146  * If @function has not been successfully added as a destructor, this returns
147  * false.
148  *
149  * Note: you can't add more than one destructor with the talloc backend!
150  */
151 #define tal_del_destructor(ptr, function)                                     \
152         tal_talloc_del_destructor_((ptr), typesafe_cb(void, void *,     \
153                                                       (function), (ptr)))
154
155 /**
156  * tal_set_name - attach a name to a tal pointer.
157  * @ptr: The tal allocated object.
158  * @name: The name to use.
159  *
160  * The name is copied, unless we're certain it's a string literal.
161  */
162 #define tal_set_name(ptr, name)                               \
163         tal_talloc_set_name_((ptr), (name), TAL_TALLOC_IS_LITERAL(name))
164
165 /**
166  * tal_name - get the name for a tal pointer.
167  * @ptr: The tal allocated object.
168  *
169  * Returns NULL if no name has been set.
170  */
171 #define tal_name(ptr) \
172         tal_talloc_name_(ptr)
173
174 /**
175  * tal_count - get the count of objects in a tal_arr.
176  * @ptr: The tal allocated object array.
177  */
178 #define tal_count(ptr) talloc_array_length(ptr)
179
180 /**
181  * tal_parent - get the parent of a tal object.
182  * @ctx: The tal allocated object.
183  *
184  * Returns the parent, which may be NULL.  Returns NULL if @ctx is NULL.
185  */
186 #define tal_parent(ctx) talloc_parent(ctx)
187
188 /**
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())
193  */
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, "")))
198
199 /**
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.
206  */
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, "[]")))
211
212
213 /**
214  * tal_set_backend - set the allocation or error functions to use
215  * @alloc_fn: NULL
216  * @resize_fn: NULL
217  * @free_fn: NULL
218  * @error_fn: called on errors or NULL (default is abort)
219  *
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.
223  */
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))
226
227 /**
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.
232  *
233  * Note that *@a1 and @a2 should be the same type.  tal_count(@a1) will
234  * be increased by @num2.
235  *
236  * Example:
237  *      int *arr1 = tal_arrz(NULL, int, 2);
238  *      int arr2[2] = { 1, 3 };
239  *
240  *      tal_expand(&arr1, arr2, 2);
241  *      assert(tal_count(arr1) == 4);
242  *      assert(arr1[2] == 1);
243  *      assert(arr1[3] == 3);
244  */
245 #define tal_expand(a1p, a2, num2)                               \
246         tal_talloc_expand_((void **)(a1p), (a2), sizeof**(a1p), \
247                            (num2) + 0*sizeof(*(a1p) == (a2)))
248
249
250 /**
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.
254  *
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.
258  */
259 #define tal_check(ctx, errorstr) \
260         tal_talloc_check_((ctx), (errorstr))
261
262
263 /* Internal support functions */
264 #ifndef TAL_TALLOC_LABEL
265 #ifdef CCAN_TAL_NO_LABELS
266 #define TAL_LABEL(type, arr) NULL
267 #else
268 #ifdef CCAN_TAL_DEBUG
269 #define TAL_LABEL(type, arr) \
270         __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
271 #else
272 #define TAL_LABEL(type, arr) stringify(type) arr
273 #endif /* CCAN_TAL_DEBUG */
274 #endif
275 #endif
276
277 #if HAVE_BUILTIN_CONSTANT_P
278 #define TAL_TALLOC_IS_LITERAL(str) __builtin_constant_p(str)
279 #else
280 #define TAL_TALLOC_IS_LITERAL(str) false
281 #endif
282
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; })
287 #else
288 #define tal_talloc_typechk_(ptr, ptype) (ptr)
289 #endif
290
291 void *tal_talloc_(const tal_t *ctx, size_t bytes, bool clear,
292                   const char *label);
293 void *tal_talloc_arr_(const tal_t *ctx, size_t bytes, size_t count, bool clear,
294                       const char *label);
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);
298
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));
301
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);
307
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);
311
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));
316
317 #endif /* CCAN_TAL_TALLOC_H */