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: b65112d6de797b9f25ae4ed371939c0e5c3fca02 $")
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);
179TALLOC_CTX *talloc_aligned_array(TALLOC_CTX *ctx, void **start, size_t alignment, size_t size);
180
181/*
182 * Add variant that zeroes out newly allocated memory
183 */
184#if defined(HAVE__TALLOC_POOLED_OBJECT) && defined(talloc_pooled_object)
185# define HAVE_TALLOC_ZERO_POOLED_OBJECT 1
186# define HAVE_TALLOC_POOLED_OBJECT 1
187
188# define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
189 (_type *)_talloc_zero_pooled_object((_ctx), sizeof(_type), #_type, \
190 (_num_subobjects), (_total_subobjects_size))
191
192static inline TALLOC_CTX *_talloc_zero_pooled_object(const void *ctx,
193 size_t type_size,
194 const char *type_name,
195 unsigned num_subobjects,
196 size_t total_subobjects_size)
197{
198 TALLOC_CTX *new;
199 new = _talloc_pooled_object(ctx, type_size, type_name, num_subobjects, total_subobjects_size);
200 if (unlikely(!new)) return NULL;
201 memset(new, 0, type_size);
202 return new;
203}
204/*
205 * Fall back to non-pooled variants
206 */
207#else
208# define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
209 talloc_zero(_ctx, _type)
210#undef talloc_pooled_object
211# define talloc_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size) \
212 talloc(_ctx, _type)
213#endif
214
215void *_talloc_realloc_zero(const void *ctx, void *ptr, size_t elem_size, unsigned count, const char *name);
216
217#undef talloc_realloc_zero
218#define talloc_realloc_zero(_ctx, _ptr, _type, _count) \
219 (_type *)_talloc_realloc_zero((_ctx), (_ptr), sizeof(_type), _count, #_type)
220
221/** @hidecallergraph */
222char *talloc_typed_strdup(TALLOC_CTX *ctx, char const *p);
223
224char *talloc_typed_strdup_buffer(TALLOC_CTX *ctx, char const *p);
225
226char *talloc_typed_strndup(TALLOC_CTX *ctx, char const *p, size_t len);
227
228char *talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
229
230char *talloc_typed_vasprintf(TALLOC_CTX *ctx, char const *fmt, va_list ap) CC_HINT(format (printf, 2, 0)) CC_HINT(nonnull (2));
231
232uint8_t *talloc_typed_memdup(TALLOC_CTX *ctx, uint8_t const *in, size_t inlen);
233
234char *talloc_bstrdup(TALLOC_CTX *ctx, char const *in);
235
236char *talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen);
237
238char *talloc_bstr_append(TALLOC_CTX *ctx, char *to, char const *from, size_t from_len);
239
240char *talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen);
241
242char *talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc, ...);
243
244int talloc_memcmp_bstr(char const *a, char const *b);
245
246int talloc_decrease_ref_count(void const *ptr);
247
248void **talloc_array_null_terminate(void **array);
249
250/** Free const'd memory
251 *
252 * @param[in] ptr to free.
253 */
254static inline int talloc_const_free(void const *ptr)
255{
256 if (!ptr) return 0;
257
258 return talloc_free(UNCONST(void *, ptr));
259}
260
261TALLOC_CTX *talloc_autofree_context_global(void);
263#endif
264
265#ifdef __cplusplus
266}
267#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:487
#define RCSIDH(h, id)
Definition build.h:513
#define unlikely(_x)
Definition build.h:407
#define DIAG_OFF(_x)
Definition build.h:486
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:851
char * talloc_bstrdup(TALLOC_CTX *ctx, char const *in)
Binary safe strdup function.
Definition talloc.c:590
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:254
char * talloc_typed_asprintf(TALLOC_CTX *ctx, char const *fmt,...))
Call talloc vasprintf, setting the type on the new chunk correctly.
Definition talloc.c:546
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:496
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:889
char * talloc_buffer_append_variadic_buffer(TALLOC_CTX *ctx, char *to, int argc,...)
Concatenate to + ...
Definition talloc.c:718
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:419
char * talloc_bstr_realloc(TALLOC_CTX *ctx, char *in, size_t inlen)
Trim a bstr (char) buffer.
Definition talloc.c:682
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
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:446
char * talloc_bstrndup(TALLOC_CTX *ctx, char const *in, size_t inlen)
Binary safe strndup function.
Definition talloc.c:618
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:471
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:818
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:522
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:921
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:646
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 * 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:573
int talloc_memcmp_bstr(char const *a, char const *b)
Compares two talloced char arrays with memcmp.
Definition talloc.c:794
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
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))