The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
talloc.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/** Functions which we wish were included in the standard talloc distribution
19 *
20 * @file src/lib/util/talloc.h
21 *
22 * @copyright 2017 The FreeRADIUS server project
23 * @copyright 2017 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
24 */
25RCSIDH(talloc_h, "$Id: 4f68ad9a26a6a20988ab2903dc7af9948d61a9f9 $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <ctype.h>
32
33#include <freeradius-devel/autoconf.h> /* Very easy to miss including in special builds */
34#include <freeradius-devel/build.h>
35#include <freeradius-devel/missing.h>
36
37#ifdef TALLOC_EXTENSIONS
38#include TALLOC_EXTENSIONS
39#else
40
41/*
42 * The default talloc headers have doxygen complaints.
43 */
44#ifdef HAVE_WDOCUMENTATION
45DIAG_OFF(documentation)
46#endif
47#include <talloc.h>
48#ifdef HAVE_WDOCUMENTATION
49DIAG_ON(documentation)
50#endif
51
52#undef talloc_autofree_context
53/** The original function is deprecated, so replace it with our version
54 */
55#define talloc_autofree_context talloc_autofree_context_global
56
57/** Iterate over a talloced array of elements
58 *
59@verbatim
60talloc_foreach(vpt_m, vpt) {
61 tmpl_debug(vpt);
62}
63@endverbatim
64 *
65 * There seems to be a limitation in for loop initialiser arguments where they all
66 * must be the same type, though we can control the number of layers of pointer
67 * indirection on a per variable basis.
68 *
69 * We declare _p to be a pointer of the specified _type, and initialise it to the
70 * start of the array. We declare _end to be a pointer of the specified type and
71 * initialise it to point to the end of the array using talloc_array_length().
72 *
73 * _iter is only updated in the condition to avoid de-referencing invalid memory.
74 *
75 * @param[in] _array to iterate over. May contain zero elements.
76 * @param[in] _iter Name of iteration variable.
77 * Will be declared in the scope of the loop.
78 */
79#define talloc_foreach(_array, _iter) \
80 for (__typeof__(_array[0]) _iter, *_p = (void *)(_array), *_end = _array ? (void *)((_array) + talloc_array_length(_array)) : NULL; \
81 (_p < _end) && (_iter = *((void **)(uintptr_t)(_p))); \
82 _p = (__typeof__(_p))((__typeof__(_array))_p) + 1)
83
84typedef int(* fr_talloc_free_func_t)(void *fire_ctx, void *uctx);
85
88
89/** Structure to record a destructor operation on a specific talloc chunk
90 *
91 * Provided here so that additional memory can be allocated with talloc pool.
92 */
94 void *fire; //!< Parent chunk.
95
96 fr_talloc_free_func_t func; //!< Free function.
97 void *uctx; //!< uctx to pass to free function.
98 fr_talloc_destructor_disarm_t *ds; //!< Chunk to free.
99};
100
101/** Structure to record a destructor to disarm if a child talloc chunk is freed
102 *
103 * Provided here so that additional memory can be allocated with talloc pool.
104 */
106 fr_talloc_destructor_t *d; //!< Destructor to disarm.
107};
108
109/*
110 * talloc portability issues. 'const' is not part of the talloc
111 * type, but it is part of the pointer type. But only if
112 * talloc_get_type_abort() is just a cast.
113 */
114#ifdef TALLOC_GET_TYPE_ABORT_NOOP
115# define talloc_get_type_abort_const(ptr, type) (const type *)(ptr)
116#else
117# define talloc_get_type_abort_const talloc_get_type_abort
118#endif
119
120/** Allocate a top level chunk with a constant name
121 *
122 * @param[in] name Must be a string literal.
123 * @return
124 * - NULL on allocation error.
125 * - A new talloc chunk on success.
126 */
127static inline TALLOC_CTX *talloc_init_const(char const *name)
128{
129 TALLOC_CTX *ctx;
130
131 ctx = talloc_new(NULL);
132 if (unlikely(!ctx)) return NULL;
133
134 talloc_set_name_const(ctx, name);
135
136 return ctx;
137}
138
139/** Returns the length of a talloc array containing a string
140 *
141 * @param[in] s to return the length of.
142 */
143static inline size_t talloc_strlen(char const *s)
144{
145// char const *our_s = talloc_get_type_abort_const(s, char);
146 char const *our_s = s;
147 return talloc_array_length(our_s) - 1;
148}
149#define talloc_strdup(_ctx, _str) talloc_typed_strdup((TALLOC_CTX *) (_ctx), _str)
150#define talloc_strndup(_ctx, _str, _len) talloc_typed_strndup((TALLOC_CTX *) (_ctx), _str, _len)
151#define talloc_asprintf talloc_typed_asprintf
152
153/** Convert a talloced string to lowercase
154 *
155 * @param[in] str to convert.
156 */
157static inline void talloc_bstr_tolower(char *str)
158{
159 char *p, *end;
160
161 end = str + talloc_strlen(str);
162
163 for (p = str; p < end; p++) *p = tolower((uint8_t) *p);
164}
165
166void talloc_free_data(void *data);
167
168void *talloc_null_ctx(void);
169
170fr_talloc_destructor_t *talloc_destructor_add(TALLOC_CTX *fire_ctx, TALLOC_CTX *disarm_ctx,
171 fr_talloc_free_func_t func, void const *uctx);
172
174
175int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child);
176
178TALLOC_CTX *talloc_page_aligned_pool(TALLOC_CTX *ctx, void **start, size_t *end_len, unsigned int headers, size_t size);
179size_t talloc_pool_used(bool *overflowed, size_t *live, TALLOC_CTX const *pool, size_t pool_size) CC_HINT(nonnull);
180TALLOC_CTX *talloc_aligned_array(TALLOC_CTX *ctx, void **start, size_t alignment, size_t size);
181
182/*
183 * Add variant that zeroes out newly allocated memory
184 */
185#if defined(HAVE__TALLOC_POOLED_OBJECT) && defined(talloc_pooled_object)
186# define HAVE_TALLOC_ZERO_POOLED_OBJECT 1
187# define HAVE_TALLOC_POOLED_OBJECT 1
188
189# define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
190 (_type *)_talloc_zero_pooled_object((_ctx), sizeof(_type), #_type, \
191 (_num_subobjects), (_total_subobjects_size))
192
193static inline TALLOC_CTX *_talloc_zero_pooled_object(const void *ctx,
194 size_t type_size,
195 const char *type_name,
196 unsigned num_subobjects,
197 size_t total_subobjects_size)
198{
199 TALLOC_CTX *new;
200 new = _talloc_pooled_object(ctx, type_size, type_name, num_subobjects, total_subobjects_size);
201 if (unlikely(!new)) return NULL;
202 memset(new, 0, type_size);
203 return new;
204}
205/*
206 * Fall back to non-pooled variants
207 */
208#else
209# define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
210 talloc_zero(_ctx, _type)
211#undef talloc_pooled_object
212# define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
213 talloc(_ctx, _type)
214#endif
215
216void *_talloc_realloc_zero(const void *ctx, void *ptr, size_t elem_size, unsigned count, const char *name);
217
218#undef talloc_realloc_zero
219#define talloc_realloc_zero(_ctx, _ptr, _type, _count) \
220 (_type *)_talloc_realloc_zero((_ctx), (_ptr), sizeof(_type), _count, #_type)
221
222/** @hidecallergraph */
223char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p);
224
225char *talloc_typed_strdup_buffer(TALLOC_CTX *ctx, char const *p);
226
227char *talloc_typed_strndup(TALLOC_CTX *ctx, char const *p, size_t len);
228
229char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
230
231char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0)) CC_HINT(nonnull (2));
232
233uint8_t *talloc_typed_memdup(TALLOC_CTX *ctx, uint8_t const *in, size_t inlen);
234
235char *talloc_bstrdup(TALLOC_CTX *ctx, char const *in);
236
237char *talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen);
238
239char *talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len);
240
241char *talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen);
242
243char *talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc, ...);
244
245int talloc_memcmp_bstr(char const *a, char const *b);
246
247int talloc_decrease_ref_count(void const *ptr);
248
249void **talloc_array_null_terminate(void **array);
250
251/** A NULL terminated array of strings with an append cursor
252 *
253 */
254typedef struct {
255 char const **strings; //!< NULL terminated array of strings.
256 char const **p; //!< Where the next appended string is written.
257 char const **end; //!< Final element, reserved for the NULL terminator.
259
260talloc_str_list_t *talloc_str_list_alloc(TALLOC_CTX *ctx, size_t num, size_t strings_len);
261
262int talloc_str_list_realloc(talloc_str_list_t *list, size_t extra);
263
264char const *talloc_str_list_append(talloc_str_list_t *list, char const *str, size_t len);
265
266/** Return the number of strings in a string list
267 *
268 */
269static inline size_t talloc_str_list_num(talloc_str_list_t const *list)
270{
271 return (size_t)(list->p - list->strings);
272}
273
274/** Return the number of strings a NULL terminated string array was sized for
275 *
276 * Reads the talloc array length of the strings array, excluding the
277 * slot reserved for the NULL terminator. Slots not yet filled by
278 * talloc_str_list_append are NULL.
279 */
280static inline size_t talloc_str_array_len(char const * const *strings)
281{
282 return talloc_array_length(strings) - 1;
283}
284
285/** Free const'd memory
286 *
287 * @param[in] ptr to free.
288 */
289static inline int talloc_const_free(void const *ptr)
290{
291 if (!ptr) return 0;
292
293 return talloc_free(UNCONST(void *, ptr));
294}
295
296TALLOC_CTX *talloc_autofree_context_global(void);
298#endif
299
300#ifdef __cplusplus
301}
302#endif
static int const char * fmt
Definition acutest.h:638
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:203
#define DIAG_ON(_x)
Definition build.h:576
#define RCSIDH(h, id)
Definition build.h:604
#define unlikely(_x)
Definition build.h:486
#define DIAG_OFF(_x)
Definition build.h:575
talloc_free(hp)
long int ssize_t
unsigned char uint8_t
static char const * name
PRIVATE void strings()
return count
Definition module.c:155
Functions which we wish were included in the standard talloc distribution.
void ** talloc_array_null_terminate(void **array)
Add a NULL pointer to an array of pointers.
Definition talloc.c:926
char * talloc_bstrdup(TALLOC_CTX *ctx, char const *in)
Binary safe strdup function.
Definition talloc.c:665
int talloc_str_list_realloc(talloc_str_list_t *list, size_t extra)
Extend a string list to hold additional strings.
Definition talloc.c:998
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:289
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...))
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:621
fr_talloc_destructor_t * talloc_destructor_add(TALLOC_CTX *fire_ctx, TALLOC_CTX *disarm_ctx, fr_talloc_free_func_t func, void const *uctx)
Add an additional destructor to a talloc chunk.
Definition talloc.c:97
char * talloc_typed_strdup_buffer(TALLOC_CTX *ctx, char const *p)
Call talloc_strndup, setting the type on the new chunk correctly.
Definition talloc.c:571
char const ** strings
NULL terminated array of strings.
Definition talloc.h:255
ssize_t talloc_hdr_size(void)
Calculate the size of the talloc chunk header.
Definition talloc.c:272
TALLOC_CTX * talloc_autofree_context_global(void)
Definition talloc.c:1056
static size_t talloc_str_array_len(char const *const *strings)
Return the number of strings a NULL terminated string array was sized for.
Definition talloc.h:280
char * talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc,...)
Concatenate to + ...
Definition talloc.c:793
void * _talloc_realloc_zero(const void *ctx, void *ptr, size_t elem_size, unsigned count, const char *name)
Version of talloc_realloc which zeroes out freshly allocated memory.
Definition talloc.c:494
char * talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen)
Trim a bstr (char) buffer.
Definition talloc.c:757
static size_t talloc_str_list_num(talloc_str_list_t const *list)
Return the number of strings in a string list.
Definition talloc.h:269
fr_talloc_free_func_t func
Free function.
Definition talloc.h:96
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
size_t talloc_pool_used(bool *overflowed, size_t *live, TALLOC_CTX const *pool, size_t pool_size)
Measure how many bytes of a talloc pool are used and live, and whether the pool overflowed.
Definition talloc.c:461
talloc_str_list_t * talloc_str_list_alloc(TALLOC_CTX *ctx, size_t num, size_t strings_len)
Allocate a list to hold num strings of strings_len total length.
Definition talloc.c:963
TALLOC_CTX * talloc_aligned_array(TALLOC_CTX *ctx, void **start, size_t alignment, size_t size)
Return a page aligned talloc memory array.
Definition talloc.c:197
int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child)
Link two different parent and child contexts, so the child is freed before the parent.
Definition talloc.c:168
uint8_t * talloc_typed_memdup(TALLOC_CTX *ctx, uint8_t const *in, size_t inlen)
Call talloc_memdup, setting the type on the new chunk correctly.
Definition talloc.c:521
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:693
char const * talloc_str_list_append(talloc_str_list_t *list, char const *str, size_t len)
Append a copy of a string to a string list.
Definition talloc.c:1029
void * talloc_null_ctx(void)
Retrieve the current talloc NULL ctx.
Definition talloc.c:50
fr_talloc_destructor_t * d
Destructor to disarm.
Definition talloc.h:106
TALLOC_CTX * talloc_page_aligned_pool(TALLOC_CTX *ctx, void **start, size_t *end_len, unsigned int headers, size_t size)
Return a page aligned talloc memory pool.
Definition talloc.c:309
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:546
void talloc_free_data(void *data)
A wrapper that can be passed to tree or hash alloc functions that take a fr_free_t.
Definition talloc.c:38
int talloc_decrease_ref_count(void const *ptr)
Decrease the reference count on a ptr.
Definition talloc.c:893
char * talloc_typed_strndup(TALLOC_CTX *ctx, char const *p, size_t len)
Call talloc_strndup, setting the type on the new chunk correctly.
Definition talloc.c:597
TALLOC_CTX * talloc_autofree_context_thread_local(void)
Get a thread-safe autofreed ctx that will be freed when the thread or process exits.
Definition talloc.c:1088
void * uctx
uctx to pass to free function.
Definition talloc.h:97
char * talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len)
Append a bstr to a bstr.
Definition talloc.c:721
void * fire
Parent chunk.
Definition talloc.h:94
void talloc_destructor_disarm(fr_talloc_destructor_t *d)
Disarm a destructor and free all memory allocated in the trigger ctxs.
Definition talloc.c:139
int(* fr_talloc_free_func_t)(void *fire_ctx, void *uctx)
Definition talloc.h:84
static void talloc_bstr_tolower(char *str)
Convert a talloced string to lowercase.
Definition talloc.h:157
fr_talloc_destructor_disarm_t * ds
Chunk to free.
Definition talloc.h:98
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:143
char const ** p
Where the next appended string is written.
Definition talloc.h:256
char * talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap))
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:648
int talloc_memcmp_bstr(char const *a, char const *b)
Compares two talloced char arrays with memcmp.
Definition talloc.c:869
char const ** end
Final element, reserved for the NULL terminator.
Definition talloc.h:257
Structure to record a destructor to disarm if a child talloc chunk is freed.
Definition talloc.h:105
Structure to record a destructor operation on a specific talloc chunk.
Definition talloc.h:93
A NULL terminated array of strings with an append cursor.
Definition talloc.h:254
static fr_slen_t parent
Definition pair.h:864
static fr_sbuff_err_t char size_t fr_sbuff_t * in
Definition value.h:1061
static fr_slen_t data
Definition value.h:1366
static fr_sbuff_err_t char size_t * len
Definition value.h:1061
int nonnull(2, 5))