The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Data Structures | Functions | Variables
talloc.c File Reference

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>
+ Include dependency graph for talloc.c:

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_CTXtalloc_child_ctx_alloc (TALLOC_CHILD_CTX *parent)
 Allocate a TALLOC_CHILD_CTX from a parent. More...
 
TALLOC_CHILD_CTXtalloc_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_ttalloc_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_ttalloc_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
 

Detailed Description

Functions which we wish were included in the standard talloc distribution.

Definition in file talloc.c.


Data Structure Documentation

◆ talloc_child_ctx_s

struct talloc_child_ctx_s

Definition at line 1004 of file talloc.c.

+ Collaboration diagram for talloc_child_ctx_s:
Data Fields
struct talloc_child_ctx_s * next

Function Documentation

◆ _autofree_global_destructor()

static int _autofree_global_destructor ( TALLOC_CTX *  af)
static

Ensures in the autofree ctx is manually freed, things don't explode atexit.

Definition at line 952 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _autofree_on_exit()

static int _autofree_on_exit ( void *  af)
static

Callback to free the autofree ctx on global exit.

Definition at line 943 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _autofree_thread_local_destructor()

static int _autofree_thread_local_destructor ( TALLOC_CTX *  af)
static

Ensures in the autofree ctx is manually freed, things don't explode atexit.

Definition at line 976 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _child_ctx_free()

static int _child_ctx_free ( TALLOC_CHILD_CTX list)
static

Definition at line 1008 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _talloc_destructor_disarm()

static int _talloc_destructor_disarm ( fr_talloc_destructor_disarm_t ds)
static

Called when the disarm_ctx ctx is freed.

Definition at line 81 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _talloc_destructor_fire()

static int _talloc_destructor_fire ( fr_talloc_destructor_t d)
static

Called with the fire_ctx is freed.

Definition at line 68 of file talloc.c.

+ Here is the caller graph for this function:

◆ _talloc_hdr_size()

static void _talloc_hdr_size ( void  )
static

Calculate the size of talloc chunk headers, once, on startup.

Definition at line 232 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _talloc_link_ctx_free()

static int _talloc_link_ctx_free ( UNUSED void *  parent,
void *  child 
)
static

Definition at line 153 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_aligned_array()

TALLOC_CTX* talloc_aligned_array ( TALLOC_CTX *  ctx,
void **  start,
size_t  alignment,
size_t  size 
)

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.

Parameters
[in]ctxto allocate array memory in.
[out]startThe first aligned address in the array.
[in]alignmentWhat alignment the memory chunk should have.
[in]sizeHow 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
Returns
  • A talloc chunk on success.
  • NULL on failure.

Definition at line 200 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_array_concat()

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.

Parameters
[out]outWhere to write the resulting string.
[in]arrayof strings to concat.
[in]septo insert between elements. May be NULL.
Returns
  • >= 0 on success - length of the string created.
  • <0 on failure. How many bytes we would need.

Definition at line 914 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_array_null_strip()

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).

Parameters
[in]arrayto null strip. Will be invalidated (realloced).
Returns
  • NULL if array is NULL, if terminating element is not NULL, or reallocation fails.
  • A realloced version of array without the terminating NULL element.

Definition at line 882 of file talloc.c.

◆ talloc_array_null_terminate()

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.

Parameters
[in]arrayto null terminate. Will be invalidated (realloced).
Returns
  • NULL if array is NULL, or if reallocation fails.
  • A realloced version of array with an additional NULL element.

Definition at line 852 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_autofree_context_global()

TALLOC_CTX* talloc_autofree_context_global ( void  )

Definition at line 957 of file talloc.c.

+ Here is the call graph for this function:

◆ talloc_autofree_context_thread_local()

TALLOC_CTX* talloc_autofree_context_thread_local ( void  )

Get a thread-safe autofreed ctx that will be freed when the thread or process exits.

Note
This should be used in place of talloc_autofree_context (which is now deprecated)
Will not be thread-safe if NULL tracking is enabled.
Returns
A talloc ctx which will be freed when the thread or process exits.

Definition at line 988 of file talloc.c.

+ Here is the call graph for this function:

◆ talloc_bstr_append()

char* talloc_bstr_append ( TALLOC_CTX *  ctx,
char *  to,
char const *  from,
size_t  from_len 
)

Append a bstr to a bstr.

Parameters
[in]ctxto allocated.
[in]tostring to append to.
[in]fromstring to append from.
[in]from_lenLength of from.
Returns
  • Realloced buffer containing both to and from.
  • NULL on failure. To will still be valid.

Definition at line 592 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_bstr_realloc()

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.

Parameters
[in]ctxto realloc buffer into.
[in]instring to trim. Will be invalid after this function returns. If NULL a new zero terminated buffer of inlen bytes will be allocated.
[in]inlenLength to trim string to.
Returns
  • The realloced string on success. in then points to invalid memory.
  • NULL on failure. In will still be valid.

Definition at line 628 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_bstrdup()

char* talloc_bstrdup ( TALLOC_CTX *  ctx,
char const *  in 
)

Binary safe strdup function.

Parameters
[in]ctxthe talloc context to allocate new buffer in.
[in]inString to dup, may contain embedded '\0'.
Returns
duped string.

Definition at line 536 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_bstrndup()

char* talloc_bstrndup ( TALLOC_CTX *  ctx,
char const *  in,
size_t  inlen 
)

Binary safe strndup function.

Parameters
[in]ctxhe talloc context to allocate new buffer in.
[in]inString to dup, may contain embedded '\0'.
[in]inlenNumber of bytes to dup.
Returns
duped string.

Definition at line 564 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_buffer_append_buffer()

char* talloc_buffer_append_buffer ( TALLOC_CTX *  ctx,
char *  to,
char const *  from 
)

Concatenate to + from.

Parameters
[in]ctxto allocate realloced buffer in.
[in]totalloc string buffer to append to.
[in]fromtalloc string buffer to append.
Returns
  • NULL if to or from are NULL or if the realloc fails. Note: You'll still need to free to if this function returns NULL.
  • The concatenation of to + from. After this function returns to may point to invalid memory and should not be used.

Definition at line 660 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_buffer_append_variadic_buffer()

char* talloc_buffer_append_variadic_buffer ( TALLOC_CTX *  ctx,
char *  to,
int  argc,
  ... 
)

Concatenate to + ...

Parameters
[in]ctxto allocate realloced buffer in.
[in]totalloc string buffer to append to.
[in]argchow many variadic arguments were passed.
[in]...talloc string buffer(s) to append. Arguments can be NULL to simplify calling logic.
Returns
  • NULL if to or from are NULL or if the realloc fails. Note: You'll still need to free to if this function returns NULL.
  • The concatenation of to + from. After this function returns to may point to invalid memory and should not be used.

Definition at line 696 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_child_ctx_alloc()

TALLOC_CHILD_CTX* talloc_child_ctx_alloc ( TALLOC_CHILD_CTX parent)

Allocate a TALLOC_CHILD_CTX from a parent.

Definition at line 1052 of file talloc.c.

◆ talloc_child_ctx_init()

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.

+ Here is the call graph for this function:

◆ talloc_decrease_ref_count()

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.

Parameters
ptrto decrement ref count for.
Returns
  • 0 The memory was freed.
  • >0 How many references remain.

Definition at line 820 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_destructor_add()

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.

Parameters
[in]fire_ctxWhen this ctx is freed the destructor function will be called.
[in]disarm_ctxWhen this ctx is freed the destructor will be disarmed. May be NULL. talloc_destructor_disarm may be used to disarm the destructor too.
[in]functo call when the fire_ctx is freed.
[in]uctxdata to pass to the above function.
Returns
  • A handle to access the destructor on success.
  • NULL on failure.

Definition at line 100 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_destructor_disarm()

void talloc_destructor_disarm ( fr_talloc_destructor_t d)

Disarm a destructor and free all memory allocated in the trigger ctxs.

Definition at line 142 of file talloc.c.

+ Here is the call graph for this function:

◆ talloc_free_data()

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 at line 41 of file talloc.c.

+ Here is the call graph for this function:

◆ talloc_hdr_size()

ssize_t talloc_hdr_size ( void  )

Calculate the size of the talloc chunk header.

Returns
  • >0 the size of the talloc chunk header.
  • -1 on error.

Definition at line 275 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_link_ctx()

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.

Note
This is not thread safe. Do not free parent before threads are joined, do not call from a child thread.
Parameters
parentwho's fate the child should share.
childbound to parent's lifecycle.
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 171 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_memcmp_array()

int talloc_memcmp_array ( uint8_t const *  a,
uint8_t const *  b 
)

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.

Parameters
aPointer to first array.
bPointer to second array.
Returns
  • 0 if the arrays match.
  • a positive or negative integer otherwise.

Definition at line 772 of file talloc.c.

◆ talloc_memcmp_bstr()

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.

Parameters
aPointer to first array.
bPointer to second array.
Returns
  • 0 if the arrays match.
  • a positive or negative integer otherwise.

Definition at line 796 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_null_ctx()

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...

Returns
the current talloc NULL context or NULL if memory tracking is not enabled.

Definition at line 53 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_page_aligned_pool()

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.

Parameters
[in]ctxto allocate pool memory in.
[out]startA page aligned address within the pool. This can be passed to mprotect().
[out]end_lenhow many bytes to protect.
[in]headershow much room we should allocate for talloc headers. This value should usually be >= 1.
[in]sizeHow 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.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_typed_asprintf()

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.

Parameters
[in]ctxThe talloc context to hang the result off.
[in]fmtThe format string.
Returns
  • Formatted string.
  • NULL on error.

Definition at line 492 of file talloc.c.

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ talloc_typed_memdup()

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.

Parameters
[in]ctxThe talloc context to hang the result off.
[in]inThe data you want to duplicate.
[in]inlenthe length of the data to be duplicated.
Returns
  • Duplicated data.
  • NULL on error.

Definition at line 420 of file talloc.c.

◆ talloc_typed_strdup()

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.

Parameters
[in]ctxThe talloc context to hang the result off.
[in]pThe string you want to duplicate.
Returns
  • Duplicated string.
  • NULL on error.

Definition at line 445 of file talloc.c.

◆ talloc_typed_strdup_buffer()

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.

Parameters
[in]ctxThe talloc context to hang the result off.
[in]pThe string you want to duplicate.
Returns
  • Duplicated string.
  • NULL on error.

Definition at line 469 of file talloc.c.

+ Here is the caller graph for this function:

◆ talloc_typed_vasprintf()

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.

Parameters
[in]ctxThe talloc context to hang the result off.
[in]fmtThe format string.
[in]apvaradic arguments.
Returns
  • Formatted string.
  • NULL on error.

Definition at line 519 of file talloc.c.

+ Here is the caller graph for this function:

Variable Documentation

◆ global_ctx

TALLOC_CTX* global_ctx
static

Definition at line 36 of file talloc.c.

◆ t_hdr_size

size_t t_hdr_size
static

How big the chunk header is.

Non-zero portion will always fit in a uint8_t, so we don't need to worry about atomicity.

Definition at line 227 of file talloc.c.

◆ thread_local_ctx

_Thread_local TALLOC_CTX* thread_local_ctx
static

Definition at line 37 of file talloc.c.