1 /* Licensed under BSD-MIT - see LICENSE file for details */
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>
14 * tal_t - convenient alias for void to mark tal pointers.
16 * Since any pointer can be a tal-allocated pointer, it's often
17 * useful to use this typedef to mark them explicitly.
22 * tal - basic allocator function
23 * @ctx: NULL, or tal allocated object to be parent.
24 * @type: the type to allocate.
26 * Allocates a specific type, with a given parent context. The name
27 * of the object is a string of the type, but if CCAN_TAL_DEBUG is
28 * defined it also contains the file and line which allocated it.
31 * int *p = tal(NULL, int);
34 #define tal(ctx, type) \
35 ((type *)tal_alloc_((ctx), sizeof(type), false, TAL_LABEL(type, "")))
38 * talz - zeroing allocator function
39 * @ctx: NULL, or tal allocated object to be parent.
40 * @type: the type to allocate.
42 * Equivalent to tal() followed by memset() to zero.
45 * p = talz(NULL, int);
48 #define talz(ctx, type) \
49 ((type *)tal_alloc_((ctx), sizeof(type), true, TAL_LABEL(type, "")))
52 * tal_free - free a tal-allocated pointer.
53 * @p: NULL, or tal allocated object to free.
55 * This calls the destructors for p (if any), then does the same for all its
56 * children (recursively) before finally freeing the memory. It returns
57 * NULL, for convenience.
59 * Note: errno is preserved by this call.
64 void *tal_free(const tal_t *p);
67 * tal_arr - allocate an array of objects.
68 * @ctx: NULL, or tal allocated object to be parent.
69 * @type: the type to allocate.
70 * @count: the number to allocate.
73 * p = tal_arr(NULL, int, 2);
77 #define tal_arr(ctx, type, count) \
78 ((type *)tal_alloc_((ctx), tal_sizeof_(sizeof(type), (count)), false, \
79 TAL_LABEL(type, "[]")))
82 * tal_arrz - allocate an array of zeroed objects.
83 * @ctx: NULL, or tal allocated object to be parent.
84 * @type: the type to allocate.
85 * @count: the number to allocate.
88 * p = tal_arrz(NULL, int, 2);
89 * assert(p[0] == 0 && p[1] == 0);
91 #define tal_arrz(ctx, type, count) \
92 ((type *)tal_alloc_((ctx), tal_sizeof_(sizeof(type), (count)), true, \
93 TAL_LABEL(type, "[]")))
96 * tal_resize - enlarge or reduce a tal_arr[z].
97 * @p: A pointer to the tal allocated array to resize.
98 * @count: the number to allocate.
100 * This returns true on success (and may move *@p), or false on failure.
103 * tal_resize(&p, 100);
105 #define tal_resize(p, count) \
106 tal_resize_((void **)(p), tal_sizeof_(sizeof**(p), (count)))
109 * tal_steal - change the parent of a tal-allocated pointer.
110 * @ctx: The new parent.
111 * @ptr: The tal allocated object to move.
113 * This may need to perform an allocation, in which case it may fail; thus
114 * it can return NULL, otherwise returns @ptr.
116 #if HAVE_STATEMENT_EXPR
117 /* Weird macro avoids gcc's 'warning: value computed is not used'. */
118 #define tal_steal(ctx, ptr) \
119 ({ (tal_typeof(ptr) tal_steal_((ctx),(ptr))); })
121 #define tal_steal(ctx, ptr) \
122 (tal_typeof(ptr) tal_steal_((ctx),(ptr)))
126 * tal_add_destructor - add a callback function when this context is destroyed.
127 * @ptr: The tal allocated object.
128 * @function: the function to call before it's freed.
130 #define tal_add_destructor(ptr, function) \
131 tal_add_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr)))
134 * tal_set_name - attach a name to a tal pointer.
135 * @ptr: The tal allocated object.
136 * @name: The name to use.
138 * The name is copied, unless we're certain it's a string literal.
140 #define tal_set_name(ptr, name) \
141 tal_set_name_((ptr), (name), TAL_IS_LITERAL(name))
144 * tal_name - get the name for a tal pointer.
145 * @ptr: The tal allocated object.
147 * Returns NULL if no name has been set.
149 const char *tal_name(const tal_t *ptr);
152 * tal_first - get the first tal object child.
153 * @root: The tal allocated object to start with, or NULL.
155 * Returns NULL if there are no children.
157 tal_t *tal_first(const tal_t *root);
160 * tal_next - get the next tal object child.
161 * @root: The tal allocated object to start with, or NULL.
162 * @prev: The return value from tal_first or tal_next.
164 * Returns NULL if there are no more children. This should be safe to
165 * call on an altering tree unless @prev is no longer a descendent of
168 tal_t *tal_next(const tal_t *root, const tal_t *prev);
171 * tal_parent - get the parent of a tal object.
172 * @ctx: The tal allocated object.
174 * Returns the parent, which may be NULL. Returns NULL if @ctx is NULL.
176 tal_t *tal_parent(const tal_t *ctx);
179 * tal_dup - duplicate an array.
180 * @ctx: The tal allocated object to be parent of the result (may be NULL).
181 * @type: the type (should match type of @p!)
182 * @p: the array to copy (or resized & reparented if take())
183 * @n: the number of sizeof(type) entries to copy.
184 * @extra: the number of extra sizeof(type) entries to allocate.
186 #define tal_dup(ctx, type, p, n, extra) \
187 ((type *)tal_dup_((ctx), tal_typechk_(p, type *), \
188 tal_sizeof_(sizeof(type), (n)), \
189 tal_sizeof_(sizeof(type), (extra)), \
190 TAL_LABEL(type, "[]")))
193 * tal_strdup - duplicate a string
194 * @ctx: NULL, or tal allocated object to be parent.
195 * @p: the string to copy (can be take()).
197 char *tal_strdup(const tal_t *ctx, const char *p);
200 * tal_strndup - duplicate a limited amount of a string.
201 * @ctx: NULL, or tal allocated object to be parent.
202 * @p: the string to copy (can be take()).
203 * @n: the maximum length to copy.
205 * Always gives a nul-terminated string, with strlen() <= @n.
207 char *tal_strndup(const tal_t *ctx, const char *p, size_t n);
210 * tal_asprintf - allocate a formatted string
211 * @ctx: NULL, or tal allocated object to be parent.
212 * @fmt: the printf-style format (can be take()).
214 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...) PRINTF_FMT(2,3);
217 * tal_vasprintf - allocate a formatted string (va_list version)
218 * @ctx: NULL, or tal allocated object to be parent.
219 * @fmt: the printf-style format (can be take()).
220 * @va: the va_list containing the format args.
222 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
227 * tal_set_backend - set the allocation or error functions to use
228 * @alloc_fn: allocator or NULL (default is malloc)
229 * @resize_fn: re-allocator or NULL (default is realloc)
230 * @free_fn: free function or NULL (default is free)
231 * @error_fn: called on errors or NULL (default is abort)
233 * The defaults are set up so tal functions never return NULL, but you
234 * can override erorr_fn to change that. error_fn can return, and is
235 * called if alloc_fn or resize_fn fail.
237 * If any parameter is NULL, that function is unchanged.
239 void tal_set_backend(void *(*alloc_fn)(size_t size),
240 void *(*resize_fn)(void *, size_t size),
241 void (*free_fn)(void *),
242 void (*error_fn)(const char *msg));
246 * tal_check - set the allocation or error functions to use
247 * @ctx: a tal context, or NULL.
248 * @errorstr: a string to prepend calls to error_fn, or NULL.
250 * This sanity-checks a tal tree (unless NDEBUG is defined, in which case
251 * it simply returns true). If errorstr is not null, error_fn is called
252 * when a problem is found, otherwise it is not.
254 bool tal_check(const tal_t *ctx, const char *errorstr);
256 #ifdef CCAN_TAL_DEBUG
258 * tal_dump - dump entire tal tree.
260 * This is a helper for debugging tal itself, which dumps all the tal internal
266 /* Internal support functions */
268 #ifdef CCAN_TAL_NO_LABELS
269 #define TAL_LABEL(type, arr) NULL
271 #ifdef CCAN_TAL_DEBUG
272 #define TAL_LABEL(type, arr) \
273 __FILE__ ":" stringify(__LINE__) ":" stringify(type) arr
275 #define TAL_LABEL(type, arr) stringify(type) arr
276 #endif /* CCAN_TAL_DEBUG */
280 #if HAVE_BUILTIN_CONSTANT_P
281 #define TAL_IS_LITERAL(str) __builtin_constant_p(str)
283 #define TAL_IS_LITERAL(str) (sizeof(&*(str)) != sizeof(char *))
286 bool tal_set_name_(tal_t *ctx, const char *name, bool literal);
288 static inline size_t tal_sizeof_(size_t size, size_t count)
290 /* Multiplication wrap */
291 if (count && unlikely(size * count / size != count))
292 return (size_t)-1024;
296 /* Make sure we don't wrap adding header. */
297 if (size > (size_t)-1024)
298 return (size_t)-1024;
304 #define tal_typeof(ptr) (__typeof__(ptr))
305 #if HAVE_STATEMENT_EXPR
306 /* Careful: ptr can be const foo *, ptype is foo *. Also, ptr could
307 * be an array, eg "hello". */
308 #define tal_typechk_(ptr, ptype) ({ __typeof__((ptr)+0) _p = (ptype)(ptr); _p; })
310 #define tal_typechk_(ptr, ptype) (ptr)
312 #else /* !HAVE_TYPEOF */
313 #define tal_typeof(ptr)
314 #define tal_typechk_(ptr, ptype) (ptr)
317 void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear, const char *label);
319 void *tal_dup_(const tal_t *ctx, const void *p, size_t n, size_t extra,
322 tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t);
324 bool tal_resize_(tal_t **ctxp, size_t size);
326 bool tal_add_destructor_(tal_t *ctx, void (*destroy)(void *me));
328 #endif /* CCAN_TAL_H */