Asterisk - The Open Source Telephony Project GIT-master-85241bd
Macros | Functions
astmm.h File Reference

Asterisk memory management routines. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define _ASTERISK_ASTMM_H
 
#define asprintf(a, b, c...)    Do_not_use_asprintf__use_ast_asprintf->fail(a, b, c)
 
#define ast_alloca(size)   __builtin_alloca(size)
 call __builtin_alloca to ensure we get gcc builtin semantics More...
 
#define ast_asprintf(ret, fmt, ...)    __ast_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, (ret), (fmt), __VA_ARGS__)
 A wrapper for asprintf() More...
 
#define ast_calloc(num, len)    __ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for calloc() More...
 
#define ast_calloc_cache(num, len)    __ast_calloc_cache((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for calloc() for use in cache pools. More...
 
#define ast_free(a)    __ast_free(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
 
#define ast_malloc(len)    __ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for malloc() More...
 
#define ast_realloc(p, len)    __ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for realloc() More...
 
#define ast_strdup(str)    __ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for strdup() More...
 
#define ast_strdupa(s)
 duplicate a string in memory from the stack More...
 
#define ast_strndup(str, len)    __ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for strndup() More...
 
#define ast_vasprintf(ret, fmt, ap)    __ast_vasprintf((ret), (fmt), (ap), __FILE__, __LINE__, __PRETTY_FUNCTION__)
 A wrapper for vasprintf() More...
 
#define ASTMM_LIBC   ASTMM_BLOCK
 
#define calloc(a, b)    Do_not_use_calloc__use_ast_calloc->fail(a, b)
 
#define free(a)    Do_not_use_free__use_ast_free_or_ast_std_free_for_remotely_allocated_memory->fail(a)
 
#define malloc(a)    Do_not_use_malloc__use_ast_malloc->fail(a)
 
#define realloc(a, b)    Do_not_use_realloc__use_ast_realloc->fail(a, b)
 
#define strdup(a)    Do_not_use_strdup__use_ast_strdup->fail(a)
 
#define strndup(a, b)    Do_not_use_strndup__use_ast_strndup->fail(a, b)
 
#define vasprintf(a, b, c)    Do_not_use_vasprintf__use_ast_vasprintf->fail(a, b, c)
 
#define ASTMM_BLOCK   0
 ASTMM_LIBC can be defined to control the meaning of standard allocators. More...
 
#define ASTMM_IGNORE   2
 Standard allocators are used directly. More...
 
#define ASTMM_REDIRECT   1
 Redirect standard allocators to use Asterisk functions. More...
 

Functions

int __ast_asprintf (const char *file, int lineno, const char *func, char **strp, const char *format,...)
 
void * __ast_calloc (size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
 
void * __ast_calloc_cache (size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
 
void __ast_free (void *ptr, const char *file, int lineno, const char *func)
 
void * __ast_malloc (size_t size, const char *file, int lineno, const char *func) attribute_malloc
 
void * __ast_realloc (void *ptr, size_t size, const char *file, int lineno, const char *func)
 
int __ast_repl_asprintf (const char *file, int lineno, const char *func, char **strp, const char *format,...)
 
void * __ast_repl_calloc (size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
 
void * __ast_repl_malloc (size_t size, const char *file, int lineno, const char *func) attribute_malloc
 
void * __ast_repl_realloc (void *ptr, size_t size, const char *file, int lineno, const char *func)
 
char * __ast_repl_strdup (const char *s, const char *file, int lineno, const char *func) attribute_malloc
 
char * __ast_repl_strndup (const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc
 
int __ast_repl_vasprintf (char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
 
char * __ast_strdup (const char *s, const char *file, int lineno, const char *func) attribute_malloc
 
char * __ast_strndup (const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc
 
int __ast_vasprintf (char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
 
void ast_free_ptr (void *ptr)
 free() wrapper More...
 
void * ast_std_calloc (size_t nmemb, size_t size) attribute_malloc
 
void ast_std_free (void *ptr)
 
void * ast_std_malloc (size_t size) attribute_malloc
 
void * ast_std_realloc (void *ptr, size_t size)
 

Detailed Description

Asterisk memory management routines.

This file should never be #included directly, it is included by asterisk.h.

Definition in file astmm.h.

Macro Definition Documentation

◆ _ASTERISK_ASTMM_H

#define _ASTERISK_ASTMM_H

Definition at line 31 of file astmm.h.

◆ asprintf

#define asprintf (   a,
  b,
  c... 
)     Do_not_use_asprintf__use_ast_asprintf->fail(a, b, c)

Definition at line 167 of file astmm.h.

◆ ast_alloca

#define ast_alloca (   size)    __builtin_alloca(size)

call __builtin_alloca to ensure we get gcc builtin semantics

Parameters
sizeThe size of the buffer we want allocated

This macro will attempt to allocate memory from the stack. If it fails you won't get a NULL returned, but a SEGFAULT if you're lucky.

Definition at line 288 of file astmm.h.

◆ ast_asprintf

#define ast_asprintf (   ret,
  fmt,
  ... 
)     __ast_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, (ret), (fmt), __VA_ARGS__)

A wrapper for asprintf()

ast_asprintf() is a wrapper for asprintf() that will generate an Asterisk log message in the case that the allocation fails.

The arguments and return value are the same as asprintf()

Definition at line 267 of file astmm.h.

◆ ast_calloc

#define ast_calloc (   num,
  len 
)     __ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for calloc()

ast_calloc() is a wrapper for calloc() that will generate an Asterisk log message in the case that the allocation fails.

The arguments and return value are the same as calloc()

Definition at line 202 of file astmm.h.

◆ ast_calloc_cache

#define ast_calloc_cache (   num,
  len 
)     __ast_calloc_cache((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for calloc() for use in cache pools.

ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log message in the case that the allocation fails. When memory debugging is in use, the memory allocated by this function will be marked as 'cache' so it can be distinguished from normal memory allocations.

The arguments and return value are the same as calloc()

Definition at line 215 of file astmm.h.

◆ ast_free

#define ast_free (   a)     __ast_free(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)

Definition at line 180 of file astmm.h.

◆ ast_malloc

#define ast_malloc (   len)     __ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for malloc()

ast_malloc() is a wrapper for malloc() that will generate an Asterisk log message in the case that the allocation fails.

The argument and return value are the same as malloc()

Definition at line 191 of file astmm.h.

◆ ast_realloc

#define ast_realloc (   p,
  len 
)     __ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for realloc()

ast_realloc() is a wrapper for realloc() that will generate an Asterisk log message in the case that the allocation fails.

The arguments and return value are the same as realloc()

Definition at line 226 of file astmm.h.

◆ ast_strdup

#define ast_strdup (   str)     __ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for strdup()

ast_strdup() is a wrapper for strdup() that will generate an Asterisk log message in the case that the allocation fails.

ast_strdup(), unlike strdup(), can safely accept a NULL argument. If a NULL argument is provided, ast_strdup will return NULL without generating any kind of error log message.

The argument and return value are the same as strdup()

Definition at line 241 of file astmm.h.

◆ ast_strdupa

#define ast_strdupa (   s)

duplicate a string in memory from the stack

Parameters
sThe string to duplicate

This macro will duplicate the given string. It returns a pointer to the stack allocated memory for the new string.

Examples
app_skel.c.

Definition at line 298 of file astmm.h.

◆ ast_strndup

#define ast_strndup (   str,
  len 
)     __ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for strndup()

ast_strndup() is a wrapper for strndup() that will generate an Asterisk log message in the case that the allocation fails.

ast_strndup(), unlike strndup(), can safely accept a NULL argument for the string to duplicate. If a NULL argument is provided, ast_strdup will return NULL without generating any kind of error log message.

The arguments and return value are the same as strndup()

Definition at line 256 of file astmm.h.

◆ ast_vasprintf

#define ast_vasprintf (   ret,
  fmt,
  ap 
)     __ast_vasprintf((ret), (fmt), (ap), __FILE__, __LINE__, __PRETTY_FUNCTION__)

A wrapper for vasprintf()

ast_vasprintf() is a wrapper for vasprintf() that will generate an Asterisk log message in the case that the allocation fails.

The arguments and return value are the same as vasprintf()

Definition at line 278 of file astmm.h.

◆ ASTMM_BLOCK

#define ASTMM_BLOCK   0

ASTMM_LIBC can be defined to control the meaning of standard allocators.

Note
The standard allocators effected by this compiler define are: malloc, calloc, realloc, strdup, strndup, asprintf, vasprintf and free.

Produce compiler errors if standard allocators are used.

Note
This is the default option, and in most cases the correct option. Any use of standard allocators will cause an error, even if those uses are in unused static inline header functions.

Definition at line 87 of file astmm.h.

◆ ASTMM_IGNORE

#define ASTMM_IGNORE   2

Standard allocators are used directly.

Note
This option is needed when including 3rd party headers with calls to standard allocators from inline functions. Using ASTMM_REDIRECT in this situation could result in an object being allocated by malloc and freed by ast_free, or the reverse.

Definition at line 109 of file astmm.h.

◆ ASTMM_LIBC

#define ASTMM_LIBC   ASTMM_BLOCK

Definition at line 115 of file astmm.h.

◆ ASTMM_REDIRECT

#define ASTMM_REDIRECT   1

Redirect standard allocators to use Asterisk functions.

Note
This option is used in some cases instead of changing the existing source to use Asterisk functions. New code should generally avoid this option, except where it's needed to work with situations where switching the code is unreasonable, such as output from code generators that are hard coded to use standard functions.

Definition at line 99 of file astmm.h.

◆ calloc

#define calloc (   a,
  b 
)     Do_not_use_calloc__use_ast_calloc->fail(a, b)

Definition at line 155 of file astmm.h.

◆ free

#define free (   a)     Do_not_use_free__use_ast_free_or_ast_std_free_for_remotely_allocated_memory->fail(a)

Definition at line 159 of file astmm.h.

◆ malloc

#define malloc (   a)     Do_not_use_malloc__use_ast_malloc->fail(a)

Definition at line 157 of file astmm.h.

◆ realloc

#define realloc (   a,
  b 
)     Do_not_use_realloc__use_ast_realloc->fail(a, b)

Definition at line 161 of file astmm.h.

◆ strdup

#define strdup (   a)     Do_not_use_strdup__use_ast_strdup->fail(a)

Definition at line 163 of file astmm.h.

◆ strndup

#define strndup (   a,
  b 
)     Do_not_use_strndup__use_ast_strndup->fail(a, b)

Definition at line 165 of file astmm.h.

◆ vasprintf

#define vasprintf (   a,
  b,
  c 
)     Do_not_use_vasprintf__use_ast_vasprintf->fail(a, b, c)

Definition at line 169 of file astmm.h.

Function Documentation

◆ __ast_asprintf()

int __ast_asprintf ( const char *  file,
int  lineno,
const char *  func,
char **  strp,
const char *  format,
  ... 
)

Definition at line 1680 of file astmm.c.

1681{
1682 int res;
1683 va_list ap;
1684
1685 va_start(ap, format);
1686 res = __ast_repl_vasprintf(strp, format, ap, file, lineno, func);
1687 if (res < 0) {
1688 /*
1689 * *strp is undefined so set to NULL to ensure it is
1690 * initialized to something useful.
1691 */
1692 *strp = NULL;
1693
1695 }
1696 va_end(ap);
1697
1698 return res;
1699}
int __ast_repl_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
Definition: astmm.c:1594
#define MALLOC_FAILURE_MSG
Definition: astmm.c:70
#define NULL
Definition: resample.c:96

References __ast_repl_vasprintf(), make_ari_stubs::file, MALLOC_FAILURE_MSG, and NULL.

◆ __ast_calloc()

void * __ast_calloc ( size_t  nmemb,
size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1603 of file astmm.c.

1604{
1605 void *p;
1606
1607 p = __ast_repl_calloc(nmemb, size, file, lineno, func);
1608 if (!p) {
1610 }
1611
1612 return p;
1613}
void * __ast_repl_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1533

References __ast_repl_calloc(), make_ari_stubs::file, and MALLOC_FAILURE_MSG.

Referenced by __ast_calloc_with_stringfields(), __ast_datastore_alloc(), __ast_frdup(), _ast_hashtab_create(), _ast_hashtab_dup(), _ast_hashtab_insert_immediate_bucket(), _ast_hashtab_resize(), _ast_hashtab_start_traversal(), _ast_hashtab_start_write_traversal(), _ast_heap_create(), _ast_var_assign(), _ast_variable_new(), add_string_pool(), ast_frame_header_new(), and internal_ao2_alloc().

◆ __ast_calloc_cache()

void * __ast_calloc_cache ( size_t  nmemb,
size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1615 of file astmm.c.

1616{
1617 void *p;
1618
1619 p = __ast_repl_calloc_cache(nmemb, size, file, lineno, func);
1620 if (!p) {
1622 }
1623
1624 return p;
1625
1626}
static void * __ast_repl_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1540

References __ast_repl_calloc_cache(), make_ari_stubs::file, and MALLOC_FAILURE_MSG.

◆ __ast_free()

void __ast_free ( void *  ptr,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1554 of file astmm.c.

1555{
1556 free(ptr);
1557}
void free()

References free().

◆ __ast_malloc()

void * __ast_malloc ( size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1628 of file astmm.c.

1629{
1630 void *p;
1631
1632 p = __ast_repl_malloc(size, file, lineno, func);
1633 if (!p) {
1635 }
1636
1637 return p;
1638}
void * __ast_repl_malloc(size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1547

References __ast_repl_malloc(), make_ari_stubs::file, and MALLOC_FAILURE_MSG.

Referenced by __ast_cc_config_params_init(), and _ast_heap_create().

◆ __ast_realloc()

void * __ast_realloc ( void *  ptr,
size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1640 of file astmm.c.

1641{
1642 void *newp;
1643
1644 newp = __ast_repl_realloc(ptr, size, file, lineno, func);
1645 if (!newp) {
1647 }
1648
1649 return newp;
1650}
void * __ast_repl_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1559

References __ast_repl_realloc(), make_ari_stubs::file, and MALLOC_FAILURE_MSG.

Referenced by grow_heap().

◆ __ast_repl_asprintf()

int __ast_repl_asprintf ( const char *  file,
int  lineno,
const char *  func,
char **  strp,
const char *  format,
  ... 
)

Definition at line 1580 of file astmm.c.

1581{
1582 int res;
1583 va_list ap;
1584
1585 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, -1);
1586
1587 va_start(ap, format);
1588 res = vasprintf(strp, format, ap);
1589 va_end(ap);
1590
1591 return res;
1592}
#define DEBUG_CHAOS_RETURN(c, f)
DEBUG_CHAOS returns failure randomly.
Definition: astmm.c:59
int vasprintf(char **strp, const char *fmt, va_list ap)

References DEBUG_CHAOS_RETURN, and vasprintf().

◆ __ast_repl_calloc()

void * __ast_repl_calloc ( size_t  nmemb,
size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1533 of file astmm.c.

1534{
1535 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
1536
1537 return calloc(nmemb, size);
1538}
#define calloc(a, b)
Definition: astmm.h:155

References calloc, DEBUG_CHAOS_RETURN, and NULL.

Referenced by __ast_calloc().

◆ __ast_repl_malloc()

void * __ast_repl_malloc ( size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1547 of file astmm.c.

1548{
1549 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
1550
1551 return malloc(size);
1552}
char * malloc()

References DEBUG_CHAOS_RETURN, malloc(), and NULL.

Referenced by __ast_malloc().

◆ __ast_repl_realloc()

void * __ast_repl_realloc ( void *  ptr,
size_t  size,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1559 of file astmm.c.

1560{
1561 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
1562
1563 return realloc(ptr, size);
1564}
#define realloc(a, b)
Definition: astmm.h:161

References DEBUG_CHAOS_RETURN, NULL, and realloc.

Referenced by __ast_realloc().

◆ __ast_repl_strdup()

char * __ast_repl_strdup ( const char *  s,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1566 of file astmm.c.

1567{
1568 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
1569
1570 return strdup(s);
1571}
#define strdup(a)
Definition: astmm.h:163

References DEBUG_CHAOS_RETURN, NULL, and strdup.

Referenced by __ast_strdup().

◆ __ast_repl_strndup()

char * __ast_repl_strndup ( const char *  s,
size_t  n,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1573 of file astmm.c.

1574{
1575 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL);
1576
1577 return strndup(s, n);
1578}
char * strndup(const char *, size_t)

References DEBUG_CHAOS_RETURN, NULL, and strndup().

Referenced by __ast_strndup().

◆ __ast_repl_vasprintf()

int __ast_repl_vasprintf ( char **  strp,
const char *  format,
va_list  ap,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1594 of file astmm.c.

1595{
1596 DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, -1);
1597
1598 return vasprintf(strp, format, ap);
1599}

References DEBUG_CHAOS_RETURN, and vasprintf().

Referenced by __ast_asprintf(), and __ast_vasprintf().

◆ __ast_strdup()

char * __ast_strdup ( const char *  s,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1652 of file astmm.c.

1653{
1654 char *newstr = NULL;
1655
1656 if (s) {
1657 newstr = __ast_repl_strdup(s, file, lineno, func);
1658 if (!newstr) {
1660 }
1661 }
1662
1663 return newstr;
1664}
char * __ast_repl_strdup(const char *s, const char *file, int lineno, const char *func)
Definition: astmm.c:1566

References __ast_repl_strdup(), make_ari_stubs::file, MALLOC_FAILURE_MSG, and NULL.

◆ __ast_strndup()

char * __ast_strndup ( const char *  s,
size_t  n,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1666 of file astmm.c.

1667{
1668 char *newstr = NULL;
1669
1670 if (s) {
1671 newstr = __ast_repl_strndup(s, n, file, lineno, func);
1672 if (!newstr) {
1674 }
1675 }
1676
1677 return newstr;
1678}
char * __ast_repl_strndup(const char *s, size_t n, const char *file, int lineno, const char *func)
Definition: astmm.c:1573

References __ast_repl_strndup(), make_ari_stubs::file, MALLOC_FAILURE_MSG, and NULL.

◆ __ast_vasprintf()

int __ast_vasprintf ( char **  strp,
const char *  format,
va_list  ap,
const char *  file,
int  lineno,
const char *  func 
)

Definition at line 1701 of file astmm.c.

1702{
1703 int res;
1704
1705 res = __ast_repl_vasprintf(strp, format, ap, file, lineno, func);
1706 if (res < 0) {
1707 /*
1708 * *strp is undefined so set to NULL to ensure it is
1709 * initialized to something useful.
1710 */
1711 *strp = NULL;
1712
1714 }
1715
1716 return res;
1717}

References __ast_repl_vasprintf(), make_ari_stubs::file, MALLOC_FAILURE_MSG, and NULL.

◆ ast_free_ptr()

void ast_free_ptr ( void *  ptr)

◆ ast_std_calloc()

void * ast_std_calloc ( size_t  nmemb,
size_t  size 
)

Definition at line 1724 of file astmm.c.

1725{
1726 return calloc(nmemb, size);
1727}

References calloc.

Referenced by ast_module_register().

◆ ast_std_free()

void ast_std_free ( void *  ptr)

◆ ast_std_malloc()

void * ast_std_malloc ( size_t  size)

Definition at line 1719 of file astmm.c.

1720{
1721 return malloc(size);
1722}

References malloc().

◆ ast_std_realloc()

void * ast_std_realloc ( void *  ptr,
size_t  size 
)

Definition at line 1729 of file astmm.c.

1730{
1731 return realloc(ptr, size);
1732}

References realloc.