]> git.ozlabs.org Git - ccan/blob - ccan/tal/tal.h
d511e88aa7b637c7dc8d98b1cccf3ad923025c92
[ccan] / ccan / tal / tal.h
1 /* Licensed under BSD-MIT - see LICENSE file for details */
2 #ifndef CCAN_TAL_H
3 #define CCAN_TAL_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 <stdlib.h>
9 #include <stdbool.h>
10 #include <stdarg.h>
11
12 /**
13  * tal_t - convenient alias for void to mark tal pointers.
14  *
15  * Since any pointer can be a tal-allocated pointer, it's often
16  * useful to use this typedef to mark them explicitly.
17  */
18 typedef void tal_t;
19
20 /**
21  * tal - basic allocator function
22  * @ctx: NULL, or tal allocated object to be parent.
23  * @type: the type to allocate.
24  *
25  * Allocates a specific type, with a given parent context.
26  */
27 #define tal(ctx, type) tal_arr((ctx), type, 1)
28
29 /**
30  * talz - zeroing allocator function
31  * @ctx: NULL, or tal allocated object to be parent.
32  * @type: the type to allocate.
33  *
34  * Equivalent to tal() followed by memset() to zero.
35  */
36 #define talz(ctx, type) tal_arrz((ctx), type, 1)
37
38 /**
39  * tal_free - free a tal-allocated pointer.
40  * @p: NULL, or tal allocated object to free.
41  *
42  * This calls the destructors for p (if any), then does the same for all its
43  * children (recursively) before finally freeing the memory.
44  */
45 void tal_free(const tal_t *p);
46
47 /**
48  * tal_arr - allocate an array of objects.
49  * @ctx: NULL, or tal allocated object to be parent.
50  * @type: the type to allocate.
51  * @count: the number to allocate.
52  */
53 #define tal_arr(ctx, type, count) \
54         ((type *)tal_alloc_((ctx), tal_sizeof_(sizeof(type), (count)), false))
55
56 /**
57  * tal_arrz - allocate an array of zeroed objects.
58  * @ctx: NULL, or tal allocated object to be parent.
59  * @type: the type to allocate.
60  * @count: the number to allocate.
61  */
62 #define tal_arrz(ctx, type, count) \
63         ((type *)tal_alloc_((ctx), tal_sizeof_(sizeof(type), (count)), true))
64
65 /**
66  * tal_resize - enlarge or reduce a tal_arr(z).
67  * @p: The tal allocated array to resize.
68  * @count: the number to allocate.
69  *
70  * This returns the new pointer, or NULL (and destroys the old one)
71  * on failure.
72  */
73 #define tal_resize(p, count) \
74         ((tal_typeof(p) tal_realloc_((p), tal_sizeof_(sizeof(*p), (count)))))
75
76 /**
77  * tal_steal - change the parent of a tal-allocated pointer.
78  * @ctx: The new parent.
79  * @ptr: The tal allocated object to move.
80  *
81  * This may need to perform an allocation, in which case it may fail; thus
82  * it can return NULL, otherwise returns @ptr.
83  *
84  * Weird macro avoids gcc's 'warning: value computed is not used'.
85  */
86 #if HAVE_STATEMENT_EXPR
87 #define tal_steal(ctx, ptr) \
88         ({ (tal_typeof(ptr) tal_steal_((ctx),(ptr))); })
89 #else
90 #define tal_steal(ctx, ptr) \
91         (tal_typeof(ptr) tal_steal_((ctx),(ptr)))
92 #endif
93
94 /**
95  * tal_add_destructor - add a callback function when this context is destroyed.
96  * @ptr: The tal allocated object.
97  * @function: the function to call before it's freed.
98  */
99 #define tal_add_destructor(ptr, function)                                     \
100         tal_add_destructor_((ptr), typesafe_cb(void, void *, (function), (ptr)))
101
102 /**
103  * tal_set_name - attach a name to a tal pointer.
104  * @ptr: The tal allocated object.
105  * @name: The name to use.
106  *
107  * The name is copied, unless we're certain it's a string literal.
108  */
109 #define tal_set_name(ptr, name)                               \
110     tal_set_name_((ptr), (name), TAL_IS_LITERAL(name))
111
112 /**
113  * tal_name - get the name for a tal pointer.
114  * @ptr: The tal allocated object.
115  *
116  * Returns NULL if no name has been set.
117  */
118 const char *tal_name(const tal_t *ptr);
119
120 /**
121  * tal_first - get the first tal object child.
122  * @root: The tal allocated object to start with, or NULL.
123  *
124  * Returns NULL if there are no children.
125  */
126 tal_t *tal_first(const tal_t *root);
127
128 /**
129  * tal_next - get the next tal object child.
130  * @root: The tal allocated object to start with, or NULL.
131  * @prev: The return value from tal_first or tal_next.
132  *
133  * Returns NULL if there are no more children.  This should be safe to
134  * call on an altering tree unless @prev is no longer a descendent of
135  * @root.
136  */
137 tal_t *tal_next(const tal_t *root, const tal_t *prev);
138
139 /**
140  * tal_parent - get the parent of a tal object.
141  * @ctx: The tal allocated object.
142  *
143  * Returns the parent, which may be NULL.
144  */
145 tal_t *tal_parent(const tal_t *ctx);
146
147 /**
148  * tal_memdup - duplicate memory.
149  * @ctx: NULL, or tal allocated object to be parent.
150  * @p: the memory to copy
151  * @n: the number of bytes.
152  */
153 void *tal_memdup(const tal_t *ctx, const void *p, size_t n);
154
155 /**
156  * tal_strdup - duplicate a string.
157  * @ctx: NULL, or tal allocated object to be parent.
158  * @p: the string to copy
159  */
160 char *tal_strdup(const tal_t *ctx, const char *p);
161
162 /**
163  * tal_strndup - duplicate a limited amount of a string.
164  * @ctx: NULL, or tal allocated object to be parent.
165  * @p: the string to copy
166  * @n: the maximum length to copy.
167  *
168  * Always gives a nul-terminated string, with strlen() <= @n.
169  */
170 char *tal_strndup(const tal_t *ctx, const char *p, size_t n);
171
172 /**
173  * tal_asprintf - allocate a formatted string
174  * @ctx: NULL, or tal allocated object to be parent.
175  * @fmt: the printf-style format.
176  */
177 char *tal_asprintf(const tal_t *ctx, const char *fmt, ...) PRINTF_FMT(2,3);
178
179 /**
180  * tal_vasprintf - allocate a formatted string (va_list version)
181  * @ctx: NULL, or tal allocated object to be parent.
182  * @fmt: the printf-style format.
183  * @va: the va_list containing the format args.
184  */
185 char *tal_vasprintf(const tal_t *ctx, const char *fmt, va_list ap)
186         PRINTF_FMT(2,0);
187
188
189 /**
190  * tal_set_backend - set the allocation or error functions to use
191  * @alloc_fn: allocator or NULL (default is malloc)
192  * @resize_fn: re-allocator or NULL (default is realloc)
193  * @free_fn: free function or NULL (default is free)
194  * @error_fn: called on errors or NULL (default is abort)
195  *
196  * The defaults are set up so tal functions never return NULL, but you
197  * can override erorr_fn to change that.  error_fn can return, and is
198  * called if alloc_fn or resize_fn fail.
199  *
200  * If any parameter is NULL, that function is unchanged.
201  */
202 void tal_set_backend(void *(*alloc_fn)(size_t size),
203                      void *(*resize_fn)(void *, size_t size),
204                      void (*free_fn)(void *),
205                      void (*error_fn)(const char *msg));
206
207
208 /**
209  * tal_check - set the allocation or error functions to use
210  * @ctx: a tal context, or NULL.
211  * @errorstr: a string to prepend calls to error_fn, or NULL.
212  *
213  * This sanity-checks a tal tree (unless NDEBUG is defined, in which case
214  * it simply returns true).  If errorstr is not null, error_fn is called
215  * when a problem is found, otherwise it is not.
216  */
217 bool tal_check(const tal_t *ctx, const char *errorstr);
218
219 #ifdef CCAN_TAL_DEBUG
220 /**
221  * tal_dump - dump entire tal tree.
222  *
223  * This is a helper for debugging tal itself, which dumps all the tal internal
224  * state.
225  */
226 void tal_dump(void);
227 #endif
228
229 /* Internal support functions */
230 #if HAVE_BUILTIN_CONSTANT_P
231 #define TAL_IS_LITERAL(str) __builtin_constant_p(str)
232 #else
233 #define TAL_IS_LITERAL(str) (sizeof(&*(str)) != sizeof(char *))
234 #endif
235
236 bool tal_set_name_(tal_t *ctx, const char *name, bool literal);
237
238 static inline size_t tal_sizeof_(size_t size, size_t count)
239 {
240         /* Multiplication wrap */
241         if (count && unlikely(size * count / size != count))
242                 return (size_t)-1024;
243
244         size *= count;
245
246         /* Make sure we don't wrap adding header. */
247         if (size > (size_t)-1024)
248                 return (size_t)-1024;
249
250         return size;
251 }
252
253 #if HAVE_TYPEOF
254 #define tal_typeof(ptr) (__typeof__(ptr))
255 #else
256 #define tal_typeof(ptr)
257 #endif
258
259 void *tal_alloc_(const tal_t *ctx, size_t bytes, bool clear);
260
261 tal_t *tal_steal_(const tal_t *new_parent, const tal_t *t);
262
263 void *tal_realloc_(tal_t *ctx, size_t size);
264
265 bool tal_add_destructor_(tal_t *ctx, void (*destroy)(void *me));
266
267 #endif /* CCAN_TAL_H */