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