The FreeRADIUS server
$Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
|
Functions which we wish were included in the standard talloc distribution. More...
#include <freeradius-devel/util/atexit.h>
#include <freeradius-devel/util/debug.h>
#include <freeradius-devel/util/dlist.h>
#include <freeradius-devel/util/sbuff.h>
#include <freeradius-devel/util/strerror.h>
#include <freeradius-devel/util/syserror.h>
#include <talloc.h>
#include <stdatomic.h>
Go to the source code of this file.
Data Structures | |
struct | talloc_child_ctx_s |
Functions | |
static int | _autofree_global_destructor (TALLOC_CTX *af) |
Ensures in the autofree ctx is manually freed, things don't explode atexit. More... | |
static int | _autofree_on_exit (void *af) |
Callback to free the autofree ctx on global exit. More... | |
static int | _autofree_thread_local_destructor (TALLOC_CTX *af) |
Ensures in the autofree ctx is manually freed, things don't explode atexit. More... | |
static int | _child_ctx_free (TALLOC_CHILD_CTX *list) |
static int | _talloc_destructor_disarm (fr_talloc_destructor_disarm_t *ds) |
Called when the disarm_ctx ctx is freed. More... | |
static int | _talloc_destructor_fire (fr_talloc_destructor_t *d) |
Called with the fire_ctx is freed. More... | |
static void | _talloc_hdr_size (void) |
Calculate the size of talloc chunk headers, once, on startup. More... | |
static int | _talloc_link_ctx_free (UNUSED void *parent, void *child) |
TALLOC_CTX * | talloc_aligned_array (TALLOC_CTX *ctx, void **start, size_t alignment, size_t size) |
Return a page aligned talloc memory array. More... | |
fr_slen_t | talloc_array_concat (fr_sbuff_t *out, char const *const *array, char const *sep) |
Concat an array of strings (not NULL terminated), with a string separator. More... | |
void ** | talloc_array_null_strip (void **array) |
Remove a NULL termination pointer from an array of pointers. More... | |
void ** | talloc_array_null_terminate (void **array) |
Add a NULL pointer to an array of pointers. More... | |
TALLOC_CTX * | talloc_autofree_context_global (void) |
TALLOC_CTX * | talloc_autofree_context_thread_local (void) |
Get a thread-safe autofreed ctx that will be freed when the thread or process exits. More... | |
char * | talloc_bstr_append (TALLOC_CTX *ctx, char *to, char const *from, size_t from_len) |
Append a bstr to a bstr. More... | |
char * | talloc_bstr_realloc (TALLOC_CTX *ctx, char *in, size_t inlen) |
Trim a bstr (char) buffer. More... | |
char * | talloc_bstrdup (TALLOC_CTX *ctx, char const *in) |
Binary safe strdup function. More... | |
char * | talloc_bstrndup (TALLOC_CTX *ctx, char const *in, size_t inlen) |
Binary safe strndup function. More... | |
char * | talloc_buffer_append_buffer (TALLOC_CTX *ctx, char *to, char const *from) |
Concatenate to + from. More... | |
char * | talloc_buffer_append_variadic_buffer (TALLOC_CTX *ctx, char *to, int argc,...) |
Concatenate to + ... More... | |
TALLOC_CHILD_CTX * | talloc_child_ctx_alloc (TALLOC_CHILD_CTX *parent) |
Allocate a TALLOC_CHILD_CTX from a parent. More... | |
TALLOC_CHILD_CTX * | talloc_child_ctx_init (TALLOC_CTX *ctx) |
Allocate and initialize a TALLOC_CHILD_CTX. More... | |
int | talloc_decrease_ref_count (void const *ptr) |
Decrease the reference count on a ptr. More... | |
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. More... | |
void | talloc_destructor_disarm (fr_talloc_destructor_t *d) |
Disarm a destructor and free all memory allocated in the trigger ctxs. More... | |
void | talloc_free_data (void *data) |
A wrapper that can be passed to tree or hash alloc functions that take a fr_free_t. More... | |
ssize_t | talloc_hdr_size (void) |
Calculate the size of the talloc chunk header. More... | |
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. More... | |
int | talloc_memcmp_array (uint8_t const *a, uint8_t const *b) |
Compares two talloced uint8_t arrays with memcmp. More... | |
int | talloc_memcmp_bstr (char const *a, char const *b) |
Compares two talloced char arrays with memcmp. More... | |
void * | talloc_null_ctx (void) |
Retrieve the current talloc NULL ctx. More... | |
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. More... | |
char * | talloc_typed_asprintf (TALLOC_CTX *ctx, char const *fmt,...) |
Call talloc vasprintf, setting the type on the new chunk correctly. More... | |
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. More... | |
char * | talloc_typed_strdup (TALLOC_CTX *ctx, char const *p) |
Call talloc_strdup, setting the type on the new chunk correctly. More... | |
char * | talloc_typed_strdup_buffer (TALLOC_CTX *ctx, char const *p) |
Call talloc_strndup, setting the type on the new chunk correctly. More... | |
char * | talloc_typed_vasprintf (TALLOC_CTX *ctx, char const *fmt, va_list ap) |
Call talloc vasprintf, setting the type on the new chunk correctly. More... | |
Variables | |
static TALLOC_CTX * | global_ctx |
static size_t | t_hdr_size |
How big the chunk header is. More... | |
static _Thread_local TALLOC_CTX * | thread_local_ctx |
Functions which we wish were included in the standard talloc distribution.
Definition in file talloc.c.
struct talloc_child_ctx_s |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
|
static |
Return a page aligned talloc memory array.
Because we can't intercept talloc's malloc() calls, we need to do some tricks in order to get the first allocation in the array page aligned, and to limit the size of the array to a multiple of the page size.
The reason for wanting a page aligned talloc array, is it allows us to mprotect() the pages that belong to the array.
Talloc chunks appear to be allocated within the protected region, so this should catch frees too.
[in] | ctx | to allocate array memory in. |
[out] | start | The first aligned address in the array. |
[in] | alignment | What alignment the memory chunk should have. |
[in] | size | How big to make the array. Will be corrected to a multiple of the page size. The actual array size will be size rounded to a multiple of the (page_size), + page_size |
Definition at line 200 of file talloc.c.
fr_slen_t talloc_array_concat | ( | fr_sbuff_t * | out, |
char const *const * | array, | ||
char const * | sep | ||
) |
Concat an array of strings (not NULL terminated), with a string separator.
[out] | out | Where to write the resulting string. |
[in] | array | of strings to concat. |
[in] | sep | to insert between elements. May be NULL. |
Definition at line 914 of file talloc.c.
void** talloc_array_null_strip | ( | void ** | array | ) |
Remove a NULL termination pointer from an array of pointers.
If the end of the array is not NULL, NULL will be returned (error).
[in] | array | to null strip. Will be invalidated (realloced). |
void** talloc_array_null_terminate | ( | void ** | array | ) |
Add a NULL pointer to an array of pointers.
This is needed by some 3rd party libraries which take NULL terminated arrays for arguments.
If allocation fails, NULL will be returned and the original array will not be touched.
[in] | array | to null terminate. Will be invalidated (realloced). |
Definition at line 852 of file talloc.c.
TALLOC_CTX* talloc_autofree_context_global | ( | void | ) |
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 at line 988 of file talloc.c.
char* talloc_bstr_append | ( | TALLOC_CTX * | ctx, |
char * | to, | ||
char const * | from, | ||
size_t | from_len | ||
) |
Append a bstr to a bstr.
[in] | ctx | to allocated. |
[in] | to | string to append to. |
[in] | from | string to append from. |
[in] | from_len | Length of from. |
Definition at line 592 of file talloc.c.
char* talloc_bstr_realloc | ( | TALLOC_CTX * | ctx, |
char * | in, | ||
size_t | inlen | ||
) |
Trim a bstr (char) buffer.
Reallocs to inlen + 1 and '\0' terminates the string buffer.
[in] | ctx | to realloc buffer into. |
[in] | in | string to trim. Will be invalid after this function returns. If NULL a new zero terminated buffer of inlen bytes will be allocated. |
[in] | inlen | Length to trim string to. |
Definition at line 628 of file talloc.c.
char* talloc_bstrdup | ( | TALLOC_CTX * | ctx, |
char const * | in | ||
) |
char* talloc_bstrndup | ( | TALLOC_CTX * | ctx, |
char const * | in, | ||
size_t | inlen | ||
) |
char* talloc_buffer_append_buffer | ( | TALLOC_CTX * | ctx, |
char * | to, | ||
char const * | from | ||
) |
Concatenate to + from.
[in] | ctx | to allocate realloced buffer in. |
[in] | to | talloc string buffer to append to. |
[in] | from | talloc string buffer to append. |
Definition at line 660 of file talloc.c.
char* talloc_buffer_append_variadic_buffer | ( | TALLOC_CTX * | ctx, |
char * | to, | ||
int | argc, | ||
... | |||
) |
Concatenate to + ...
[in] | ctx | to allocate realloced buffer in. |
[in] | to | talloc string buffer to append to. |
[in] | argc | how many variadic arguments were passed. |
[in] | ... | talloc string buffer(s) to append. Arguments can be NULL to simplify calling logic. |
Definition at line 696 of file talloc.c.
TALLOC_CHILD_CTX* talloc_child_ctx_alloc | ( | TALLOC_CHILD_CTX * | parent | ) |
TALLOC_CHILD_CTX* talloc_child_ctx_init | ( | TALLOC_CTX * | ctx | ) |
Allocate and initialize a TALLOC_CHILD_CTX.
The TALLOC_CHILD_CTX ensures ordering for allocators and destructors. When a TALLOC_CHILD_CTX is created, it is added to parent, in LIFO order. In contrast, the basic talloc operations do not guarantee any kind of order.
When the TALLOC_CHILD_CTX is freed, the children are freed in FILO order. That process ensures that the children are freed before the parent, and that the younger siblings are freed before the older siblings.
The idea is that if we have an initializer for A, which in turn initializes B and C. When the memory is freed, we should do the operations in the reverse order.
Definition at line 1038 of file talloc.c.
int talloc_decrease_ref_count | ( | void const * | ptr | ) |
Decrease the reference count on a ptr.
Ptr will be freed if count reaches zero.
This is equivalent to talloc 1.0 behaviour of talloc_free.
ptr | to decrement ref count for. |
Definition at line 820 of file talloc.c.
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.
[in] | fire_ctx | When this ctx is freed the destructor function will be called. |
[in] | disarm_ctx | When this ctx is freed the destructor will be disarmed. May be NULL. talloc_destructor_disarm may be used to disarm the destructor too. |
[in] | func | to call when the fire_ctx is freed. |
[in] | uctx | data to pass to the above function. |
Definition at line 100 of file talloc.c.
void talloc_destructor_disarm | ( | fr_talloc_destructor_t * | d | ) |
void talloc_free_data | ( | void * | data | ) |
ssize_t talloc_hdr_size | ( | void | ) |
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.
parent | who's fate the child should share. |
child | bound to parent's lifecycle. |
Definition at line 171 of file talloc.c.
Compares two talloced uint8_t arrays with memcmp.
Talloc arrays carry their length as part of the structure, so can be passed to a generic comparison function.
a | Pointer to first array. |
b | Pointer to second array. |
int talloc_memcmp_bstr | ( | char const * | a, |
char const * | b | ||
) |
Compares two talloced char arrays with memcmp.
Talloc arrays carry their length as part of the structure, so can be passed to a generic comparison function.
a | Pointer to first array. |
b | Pointer to second array. |
Definition at line 796 of file talloc.c.
void* talloc_null_ctx | ( | void | ) |
Retrieve the current talloc NULL ctx.
Talloc doesn't provide a function to retrieve the top level memory tracking context. This function does that...
Definition at line 53 of file talloc.c.
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.
Because we can't intercept talloc's malloc() calls, we need to do some tricks in order to get the first allocation in the pool page aligned, and to limit the size of the pool to a multiple of the page size.
The reason for wanting a page aligned talloc pool, is it allows us to mprotect() the pages that belong to the pool.
Talloc chunks appear to be allocated within the protected region, so this should catch frees too.
[in] | ctx | to allocate pool memory in. |
[out] | start | A page aligned address within the pool. This can be passed to mprotect(). |
[out] | end_len | how many bytes to protect. |
[in] | headers | how much room we should allocate for talloc headers. This value should usually be >= 1. |
[in] | size | How big to make the pool. Will be corrected to a multiple of the page size. The actual pool size will be size rounded to a multiple of the (page_size), + page_size |
Definition at line 312 of file talloc.c.
char* talloc_typed_asprintf | ( | TALLOC_CTX * | ctx, |
char const * | fmt, | ||
... | |||
) |
Call talloc vasprintf, setting the type on the new chunk correctly.
For some bizarre reason the talloc string functions don't set the memory chunk type to char, which causes all kinds of issues with verifying fr_pair_ts.
[in] | ctx | The talloc context to hang the result off. |
[in] | fmt | The format string. |
Definition at line 492 of file talloc.c.
char* talloc_typed_strdup | ( | TALLOC_CTX * | ctx, |
char const * | p | ||
) |
Call talloc_strdup, setting the type on the new chunk correctly.
For some bizarre reason the talloc string functions don't set the memory chunk type to char, which causes all kinds of issues with verifying fr_pair_ts.
[in] | ctx | The talloc context to hang the result off. |
[in] | p | The string you want to duplicate. |
char* talloc_typed_strdup_buffer | ( | TALLOC_CTX * | ctx, |
char const * | p | ||
) |
Call talloc_strndup, setting the type on the new chunk correctly.
This function is similar to talloc_typed_strdup but gets the chunk length using talloc functions.
[in] | ctx | The talloc context to hang the result off. |
[in] | p | The string you want to duplicate. |
Definition at line 469 of file talloc.c.
char* talloc_typed_vasprintf | ( | TALLOC_CTX * | ctx, |
char const * | fmt, | ||
va_list | ap | ||
) |
Call talloc vasprintf, setting the type on the new chunk correctly.
For some bizarre reason the talloc string functions don't set the memory chunk type to char, which causes all kinds of issues with verifying fr_pair_ts.
[in] | ctx | The talloc context to hang the result off. |
[in] | fmt | The format string. |
[in] | ap | varadic arguments. |
Definition at line 519 of file talloc.c.
|
static |