The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
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: 31f64bf1676026dd83fa7f9469e17590b4f0ede0 $")
26
27#ifdef __cplusplus
28extern "C" {
29#endif
30
31#include <ctype.h>
32
33#ifdef HAVE_WDOCUMENTATION
34DIAG_OFF(documentation)
35#endif
36#include <talloc.h>
37#ifdef HAVE_WDOCUMENTATION
38DIAG_ON(documentation)
39#endif
40
41#include <freeradius-devel/autoconf.h> /* Very easy to miss including in special builds */
42#include <freeradius-devel/build.h>
43#include <freeradius-devel/missing.h>
44
45#undef talloc_autofree_context
46/** The original function is deprecated, so replace it with our version
47 */
48#define talloc_autofree_context talloc_autofree_context_global
49
50/** Iterate over a talloced array of elements
51 *
52@verbatim
53talloc_foreach(vpt_m, vpt) {
54 tmpl_debug(vpt);
55}
56@endverbatim
57 *
58 * There seems to be a limitation in for loop initialiser arguments where they all
59 * must be the same type, though we can control the number of layers of pointer
60 * indirection on a per variable basis.
61 *
62 * We declare _p to be a pointer of the specified _type, and initialise it to the
63 * start of the array. We declare _end to be a pointer of the specified type and
64 * initialise it to point to the end of the array using talloc_array_length().
65 *
66 * _iter is only updated in the condition to avoid de-referencing invalid memory.
67 *
68 * @param[in] _array to iterate over. May contain zero elements.
69 * @param[in] _iter Name of iteration variable.
70 * Will be declared in the scope of the loop.
71 */
72#define talloc_foreach(_array, _iter) \
73 for (__typeof__(_array[0]) _iter, *_p = (void *)(_array), *_end = _array ? (void *)((_array) + talloc_array_length(_array)) : NULL; \
74 (_p < _end) && (_iter = *((void **)(uintptr_t)(_p))); \
75 _p = (__typeof__(_p))((__typeof__(_array))_p) + 1)
76
77typedef int(* fr_talloc_free_func_t)(void *fire_ctx, void *uctx);
78
81
82/** Structure to record a destructor operation on a specific talloc chunk
83 *
84 * Provided here so that additional memory can be allocated with talloc pool.
85 */
87 void *fire; //!< Parent chunk.
88
89 fr_talloc_free_func_t func; //!< Free function.
90 void *uctx; //!< uctx to pass to free function.
91 fr_talloc_destructor_disarm_t *ds; //!< Chunk to free.
92};
93
94/** Structure to record a destructor to disarm if a child talloc chunk is freed
95 *
96 * Provided here so that additional memory can be allocated with talloc pool.
97 */
99 fr_talloc_destructor_t *d; //!< Destructor to disarm.
100};
101
102/*
103 * talloc portability issues. 'const' is not part of the talloc
104 * type, but it is part of the pointer type. But only if
105 * talloc_get_type_abort() is just a cast.
106 */
107#ifdef TALLOC_GET_TYPE_ABORT_NOOP
108# define talloc_get_type_abort_const(ptr, type) (const type *)(ptr)
109#else
110# define talloc_get_type_abort_const talloc_get_type_abort
111#endif
112
113/** Allocate a top level chunk with a constant name
114 *
115 * @param[in] name Must be a string literal.
116 * @return
117 * - NULL on allocation error.
118 * - A new talloc chunk on success.
119 */
120static inline TALLOC_CTX *talloc_init_const(char const *name)
121{
122 TALLOC_CTX *ctx;
123
124 ctx = talloc_new(NULL);
125 if (unlikely(!ctx)) return NULL;
126
127 talloc_set_name_const(ctx, name);
128
129 return ctx;
130}
131
132/** Returns the length of a talloc array containing a string
133 *
134 * @param[in] s to return the length of.
135 */
136static inline size_t talloc_strlen(char const *s)
137{
138// char const *our_s = talloc_get_type_abort_const(s, char);
139 char const *our_s = s;
140 return talloc_array_length(our_s) - 1;
141}
142#define talloc_strdup(_ctx, _str) talloc_typed_strdup((TALLOC_CTX *) (_ctx), _str)
143#define talloc_strndup(_ctx, _str, _len) talloc_typed_strndup((TALLOC_CTX *) (_ctx), _str, _len)
144#define talloc_asprintf talloc_typed_asprintf
145
146/** Convert a talloced string to lowercase
147 *
148 * @param[in] str to convert.
149 */
150static inline void talloc_bstr_tolower(char *str)
151{
152 char *p, *end;
153
154 end = str + talloc_strlen(str);
155
156 for (p = str; p < end; p++) *p = tolower((uint8_t) *p);
157}
158
159void talloc_free_data(void *data);
160
161void *talloc_null_ctx(void);
162
163fr_talloc_destructor_t *talloc_destructor_add(TALLOC_CTX *fire_ctx, TALLOC_CTX *disarm_ctx,
164 fr_talloc_free_func_t func, void const *uctx);
165
167
168int talloc_link_ctx(TALLOC_CTX *parent, TALLOC_CTX *child);
169
171TALLOC_CTX *talloc_page_aligned_pool(TALLOC_CTX *ctx, void **start, size_t *end_len, unsigned int headers, size_t size);
172TALLOC_CTX *talloc_aligned_array(TALLOC_CTX *ctx, void **start, size_t alignment, size_t size);
173
174/*
175 * Add variant that zeroes out newly allocated memory
176 */
177#if defined(HAVE__TALLOC_POOLED_OBJECT) && defined(talloc_pooled_object)
178# define HAVE_TALLOC_ZERO_POOLED_OBJECT 1
179# define HAVE_TALLOC_POOLED_OBJECT 1
180
181# define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
182 (_type *)_talloc_zero_pooled_object((_ctx), sizeof(_type), #_type, \
183 (_num_subobjects), (_total_subobjects_size))
184
185static inline TALLOC_CTX *_talloc_zero_pooled_object(const void *ctx,
186 size_t type_size,
187 const char *type_name,
188 unsigned num_subobjects,
189 size_t total_subobjects_size)
190{
191 TALLOC_CTX *new;
192 new = _talloc_pooled_object(ctx, type_size, type_name, num_subobjects, total_subobjects_size);
193 if (unlikely(!new)) return NULL;
194 memset(new, 0, type_size);
195 return new;
196}
197/*
198 * Fall back to non-pooled variants
199 */
200#else
201# define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
202 talloc_zero(_ctx, _type)
203#undef talloc_pooled_object
204# define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
205 talloc(_ctx, _type)
206#endif
207
208void *_talloc_realloc_zero(const void *ctx, void *ptr, size_t elem_size, unsigned count, const char *name);
209
210#undef talloc_realloc_zero
211#define talloc_realloc_zero(_ctx, _ptr, _type, _count) \
212 (_type *)_talloc_realloc_zero((_ctx), (_ptr), sizeof(_type), _count, #_type)
213
214/** @hidecallergraph */
215char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p);
216
217char *talloc_typed_strdup_buffer(TALLOC_CTX *ctx, char const *p);
218
219char *talloc_typed_strndup(TALLOC_CTX *ctx, char const *p, size_t len);
220
221char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
222
223char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0)) CC_HINT(nonnull (2));
224
225uint8_t *talloc_typed_memdup(TALLOC_CTX *ctx, uint8_t const *in, size_t inlen);
226
227char *talloc_bstrdup(TALLOC_CTX *ctx, char const *in);
228
229char *talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen);
230
231char *talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len);
232
233char *talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen);
234
235char *talloc_buffer_append_buffer(TALLOC_CTX *ctx, char *to, char const *from);
236
237char *talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc, ...);
238
239int talloc_memcmp_array(uint8_t const *a, uint8_t const *b);
240
241int talloc_memcmp_bstr(char const *a, char const *b);
242
243int talloc_decrease_ref_count(void const *ptr);
244
245void **talloc_array_null_terminate(void **array);
246
247void **talloc_array_null_strip(void **array);
248
249/** Free const'd memory
250 *
251 * @param[in] ptr to free.
252 */
253static inline int talloc_const_free(void const *ptr)
254{
255 if (!ptr) return 0;
256
257 return talloc_free(UNCONST(void *, ptr));
258}
259
260TALLOC_CTX *talloc_autofree_context_global(void);
262
264
267
268#ifdef __cplusplus
269}
270#endif
static int const char * fmt
Definition acutest.h:573
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define DIAG_ON(_x)
Definition build.h:481
#define RCSIDH(h, id)
Definition build.h:507
#define unlikely(_x)
Definition build.h:402
#define DIAG_OFF(_x)
Definition build.h:480
static fr_slen_t in
Definition dict.h:882
talloc_free(hp)
long int ssize_t
unsigned char uint8_t
static char const * name
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:906
char * talloc_bstrdup(TALLOC_CTX *ctx, char const *in)
Binary safe strdup function.
Definition talloc.c:589
TALLOC_CHILD_CTX * talloc_child_ctx_alloc(TALLOC_CHILD_CTX *parent)
Allocate a TALLOC_CHILD_CTX from a parent.
Definition talloc.c:1073
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:253
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...))
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:545
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:96
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:495
ssize_t talloc_hdr_size(void)
Calculate the size of the talloc chunk header.
Definition talloc.c:271
TALLOC_CTX * talloc_autofree_context_global(void)
Definition talloc.c:976
TALLOC_CHILD_CTX * talloc_child_ctx_init(TALLOC_CTX *ctx)
Allocate and initialize a TALLOC_CHILD_CTX.
Definition talloc.c:1059
int talloc_memcmp_array(uint8_t const *a, uint8_t const *b)
Compares two talloced uint8_t arrays with memcmp.
Definition talloc.c:826
char * talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc,...)
Concatenate to + ...
Definition talloc.c:750
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:418
char * talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen)
Trim a bstr (char) buffer.
Definition talloc.c:681
fr_talloc_free_func_t func
Free function.
Definition talloc.h:89
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:120
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:196
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:167
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:445
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:617
void * talloc_null_ctx(void)
Retrieve the current talloc NULL ctx.
Definition talloc.c:49
fr_talloc_destructor_t * d
Destructor to disarm.
Definition talloc.h:99
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:308
char * talloc_typed_strdup(TALLOC_CTX *ctx, char const *p)
Call talloc_strdup, setting the type on the new chunk correctly.
Definition talloc.c:470
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:37
int talloc_decrease_ref_count(void const *ptr)
Decrease the reference count on a ptr.
Definition talloc.c:874
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:521
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:1008
void * uctx
uctx to pass to free function.
Definition talloc.h:90
char * talloc_buffer_append_buffer(TALLOC_CTX *ctx, char *to, char const *from)
Concatenate to + from.
Definition talloc.c:714
void ** talloc_array_null_strip(void **array)
Remove a NULL termination pointer from an array of pointers.
Definition talloc.c:936
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:645
void * fire
Parent chunk.
Definition talloc.h:87
void talloc_destructor_disarm(fr_talloc_destructor_t *d)
Disarm a destructor and free all memory allocated in the trigger ctxs.
Definition talloc.c:138
int(* fr_talloc_free_func_t)(void *fire_ctx, void *uctx)
Definition talloc.h:77
static void talloc_bstr_tolower(char *str)
Convert a talloced string to lowercase.
Definition talloc.h:150
fr_talloc_destructor_disarm_t * ds
Chunk to free.
Definition talloc.h:91
static size_t talloc_strlen(char const *s)
Returns the length of a talloc array containing a string.
Definition talloc.h:136
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:572
int talloc_memcmp_bstr(char const *a, char const *b)
Compares two talloced char arrays with memcmp.
Definition talloc.c:850
Structure to record a destructor to disarm if a child talloc chunk is freed.
Definition talloc.h:98
Structure to record a destructor operation on a specific talloc chunk.
Definition talloc.h:86
static fr_slen_t parent
Definition pair.h:858
static fr_slen_t data
Definition value.h:1340
static size_t char fr_sbuff_t size_t inlen
Definition value.h:1030
int nonnull(2, 5))