Asterisk - The Open Source Telephony Project  GIT-master-b91fb3c
astmm.h
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2006, Digium, Inc.
5  *
6  * Mark Spencer <markster@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18 
19 /*! \file
20  * \brief Asterisk memory management routines
21  *
22  * This file should never be \#included directly, it is included
23  * by asterisk.h.
24  */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #ifndef _ASTERISK_ASTMM_H
31 #define _ASTERISK_ASTMM_H
32 /* IWYU pragma: private, include "asterisk.h" */
33 
34 void *ast_std_malloc(size_t size) attribute_malloc;
35 void *ast_std_calloc(size_t nmemb, size_t size) attribute_malloc;
36 void *ast_std_realloc(void *ptr, size_t size);
37 void ast_std_free(void *ptr);
38 
39 /*!
40  * \brief free() wrapper
41  *
42  * ast_free_ptr should be used when a function pointer for free() needs to be passed
43  * as the argument to a function. Otherwise, astmm will cause seg faults.
44  */
45 void ast_free_ptr(void *ptr);
46 
47 void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc;
48 void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc;
49 void *__ast_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc;
50 void __ast_free(void *ptr, const char *file, int lineno, const char *func);
51 void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
52 char *__ast_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc;
53 char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc;
54 int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...)
55  __attribute__((format(printf, 5, 6)));
56 int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
57  __attribute__((format(printf, 2, 0)));
58 
59 /* The __ast_repl functions should not used from Asterisk sources, they are exposed
60  * for use by ASTMM_REDIRECT and bundled pjproject. */
61 void *__ast_repl_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc;
62 void *__ast_repl_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc;
63 void *__ast_repl_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
64 char *__ast_repl_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc;
65 char *__ast_repl_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc;
66 int __ast_repl_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...)
67  __attribute__((format(printf, 5, 6)));
68 int __ast_repl_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
69  __attribute__((format(printf, 2, 0)));
70 
71 /*!
72  * \brief ASTMM_LIBC can be defined to control the meaning of standard allocators.
73  *
74  * \note The standard allocators effected by this compiler define are:
75  * malloc, calloc, realloc, strdup, strndup, asprintf, vasprintf and free.
76  *
77  * @{
78  */
79 
80 /*!
81  * \brief Produce compiler errors if standard allocators are used.
82  *
83  * \note This is the default option, and in most cases the correct option.
84  * Any use of standard allocators will cause an error, even if those uses
85  * are in unused static inline header functions.
86  */
87 #define ASTMM_BLOCK 0
88 
89 /*!
90  * \brief Redirect standard allocators to use Asterisk functions.
91  *
92  * \note This option is used in some cases instead of changing the
93  * existing source to use Asterisk functions. New code should
94  * generally avoid this option, except where it's needed to work
95  * with situations where switching the code is unreasonable, such
96  * as output from code generators that are hard coded to use
97  * standard functions.
98  */
99 #define ASTMM_REDIRECT 1
100 
101 /*!
102  * \brief Standard allocators are used directly.
103  *
104  * \note This option is needed when including 3rd party headers with calls
105  * to standard allocators from inline functions. Using ASTMM_REDIRECT in
106  * this situation could result in an object being allocated by malloc and
107  * freed by ast_free, or the reverse.
108  */
109 #define ASTMM_IGNORE 2
110 
111 /*!
112  * }@
113  */
114 
115 #if !defined(ASTMM_LIBC)
116 /* BLOCK libc allocators by default. */
117 #define ASTMM_LIBC ASTMM_BLOCK
118 #endif
119 
120 #if ASTMM_LIBC == ASTMM_IGNORE
121 /* Don't touch the libc functions. */
122 #else
123 
124 /* Undefine any macros */
125 #undef malloc
126 #undef calloc
127 #undef realloc
128 #undef strdup
129 #undef strndup
130 #undef asprintf
131 #undef vasprintf
132 #undef free
133 
134 #if ASTMM_LIBC == ASTMM_REDIRECT
135 
136 /* Redefine libc functions to our own versions */
137 #define calloc(nmemb, size) \
138  __ast_repl_calloc(nmemb, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
139 #define malloc(size) \
140  __ast_repl_malloc(size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
141 #define free(ptr) \
142  __ast_free(ptr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
143 #define realloc(ptr, size) \
144  __ast_repl_realloc(ptr, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
145 #define strdup(s) \
146  __ast_repl_strdup(s, __FILE__, __LINE__, __PRETTY_FUNCTION__)
147 #define strndup(s, n) \
148  __ast_repl_strndup(s, n, __FILE__, __LINE__, __PRETTY_FUNCTION__)
149 #define asprintf(strp, format, args...) \
150  __ast_repl_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, strp, format, args)
151 #define vasprintf(strp, format, ap) \
152  __ast_repl_vasprintf(strp, format, ap, __FILE__, __LINE__, __PRETTY_FUNCTION__)
153 
154 #elif ASTMM_LIBC == ASTMM_BLOCK
155 
156 /* Redefine libc functions to cause compile errors */
157 #define calloc(a, b) \
158  Do_not_use_calloc__use_ast_calloc->fail(a, b)
159 #define malloc(a) \
160  Do_not_use_malloc__use_ast_malloc->fail(a)
161 #define free(a) \
162  Do_not_use_free__use_ast_free_or_ast_std_free_for_remotely_allocated_memory->fail(a)
163 #define realloc(a, b) \
164  Do_not_use_realloc__use_ast_realloc->fail(a, b)
165 #define strdup(a) \
166  Do_not_use_strdup__use_ast_strdup->fail(a)
167 #define strndup(a, b) \
168  Do_not_use_strndup__use_ast_strndup->fail(a, b)
169 #define asprintf(a, b, c...) \
170  Do_not_use_asprintf__use_ast_asprintf->fail(a, b, c)
171 #define vasprintf(a, b, c) \
172  Do_not_use_vasprintf__use_ast_vasprintf->fail(a, b, c)
173 
174 #else
175 #error "Unacceptable value for the macro ASTMM_LIBC"
176 #endif
177 
178 #endif
179 
180 /* Provide our own definition for ast_free */
181 
182 #define ast_free(a) \
183  __ast_free(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
184 
185 /*!
186  * \brief A wrapper for malloc()
187  *
188  * ast_malloc() is a wrapper for malloc() that will generate an Asterisk log
189  * message in the case that the allocation fails.
190  *
191  * The argument and return value are the same as malloc()
192  */
193 #define ast_malloc(len) \
194  __ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
195 
196 /*!
197  * \brief A wrapper for calloc()
198  *
199  * ast_calloc() is a wrapper for calloc() that will generate an Asterisk log
200  * message in the case that the allocation fails.
201  *
202  * The arguments and return value are the same as calloc()
203  */
204 #define ast_calloc(num, len) \
205  __ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
206 
207 /*!
208  * \brief A wrapper for calloc() for use in cache pools
209  *
210  * ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log
211  * message in the case that the allocation fails. When memory debugging is in use,
212  * the memory allocated by this function will be marked as 'cache' so it can be
213  * distinguished from normal memory allocations.
214  *
215  * The arguments and return value are the same as calloc()
216  */
217 #define ast_calloc_cache(num, len) \
218  __ast_calloc_cache((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
219 
220 /*!
221  * \brief A wrapper for realloc()
222  *
223  * ast_realloc() is a wrapper for realloc() that will generate an Asterisk log
224  * message in the case that the allocation fails.
225  *
226  * The arguments and return value are the same as realloc()
227  */
228 #define ast_realloc(p, len) \
229  __ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
230 
231 /*!
232  * \brief A wrapper for strdup()
233  *
234  * ast_strdup() is a wrapper for strdup() that will generate an Asterisk log
235  * message in the case that the allocation fails.
236  *
237  * ast_strdup(), unlike strdup(), can safely accept a NULL argument. If a NULL
238  * argument is provided, ast_strdup will return NULL without generating any
239  * kind of error log message.
240  *
241  * The argument and return value are the same as strdup()
242  */
243 #define ast_strdup(str) \
244  __ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
245 
246 /*!
247  * \brief A wrapper for strndup()
248  *
249  * ast_strndup() is a wrapper for strndup() that will generate an Asterisk log
250  * message in the case that the allocation fails.
251  *
252  * ast_strndup(), unlike strndup(), can safely accept a NULL argument for the
253  * string to duplicate. If a NULL argument is provided, ast_strdup will return
254  * NULL without generating any kind of error log message.
255  *
256  * The arguments and return value are the same as strndup()
257  */
258 #define ast_strndup(str, len) \
259  __ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
260 
261 /*!
262  * \brief A wrapper for asprintf()
263  *
264  * ast_asprintf() is a wrapper for asprintf() that will generate an Asterisk log
265  * message in the case that the allocation fails.
266  *
267  * The arguments and return value are the same as asprintf()
268  */
269 #define ast_asprintf(ret, fmt, ...) \
270  __ast_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, (ret), (fmt), __VA_ARGS__)
271 
272 /*!
273  * \brief A wrapper for vasprintf()
274  *
275  * ast_vasprintf() is a wrapper for vasprintf() that will generate an Asterisk log
276  * message in the case that the allocation fails.
277  *
278  * The arguments and return value are the same as vasprintf()
279  */
280 #define ast_vasprintf(ret, fmt, ap) \
281  __ast_vasprintf((ret), (fmt), (ap), __FILE__, __LINE__, __PRETTY_FUNCTION__)
282 
283 /*!
284  \brief call __builtin_alloca to ensure we get gcc builtin semantics
285  \param size The size of the buffer we want allocated
286 
287  This macro will attempt to allocate memory from the stack. If it fails
288  you won't get a NULL returned, but a SEGFAULT if you're lucky.
289 */
290 #define ast_alloca(size) __builtin_alloca(size)
291 
292 #if !defined(ast_strdupa) && defined(__GNUC__)
293 /*!
294  * \brief duplicate a string in memory from the stack
295  * \param s The string to duplicate
296  *
297  * This macro will duplicate the given string. It returns a pointer to the stack
298  * allocatted memory for the new string.
299  */
300 #define ast_strdupa(s) \
301  (__extension__ \
302  ({ \
303  const char *__old = (s); \
304  size_t __len = strlen(__old) + 1; \
305  char *__new = __builtin_alloca(__len); \
306  memcpy (__new, __old, __len); \
307  __new; \
308  }))
309 #endif
310 
311 #else
312 #error "NEVER INCLUDE astmm.h DIRECTLY!!"
313 #endif /* _ASTERISK_ASTMM_H */
314 
315 #ifdef __cplusplus
316 }
317 #endif
void ast_std_free(void *ptr)
Definition: astmm.c:1766
void * __ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1635
void * ast_std_malloc(size_t size) attribute_malloc
Definition: astmm.c:1751
void * __ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1647
char * __ast_repl_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1605
void __ast_free(void *ptr, const char *file, int lineno, const char *func)
Definition: astmm.c:1586
char * __ast_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1684
void * ast_std_realloc(void *ptr, size_t size)
Definition: astmm.c:1761
void * __ast_repl_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1591
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1771
int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
Definition: astmm.c:1733
char * __ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1698
int __ast_repl_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format,...)
Definition: astmm.c:1612
void * __ast_repl_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1579
void * __ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1672
void * ast_std_calloc(size_t nmemb, size_t size) attribute_malloc
Definition: astmm.c:1756
#define attribute_malloc
Definition: compiler.h:59
int __ast_repl_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
Definition: astmm.c:1626
void * __ast_repl_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1565
char * __ast_repl_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1598
static snd_pcm_format_t format
Definition: chan_alsa.c:102
int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format,...)
Definition: astmm.c:1712
void * __ast_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1660