Asterisk - The Open Source Telephony Project GIT-master-67613d1
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
27extern "C" {
28#endif
29
30#ifndef _ASTERISK_ASTMM_H
31#define _ASTERISK_ASTMM_H
32/* IWYU pragma: private, include "asterisk.h" */
33
34void *ast_std_malloc(size_t size) attribute_malloc;
35void *ast_std_calloc(size_t nmemb, size_t size) attribute_malloc;
36void *ast_std_realloc(void *ptr, size_t size);
37void 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 */
45void ast_free_ptr(void *ptr);
46
47void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc;
48void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc;
49void *__ast_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc;
50void __ast_free(void *ptr, const char *file, int lineno, const char *func);
51void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
52char *__ast_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc;
53char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc;
54int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...)
55 __attribute__((format(printf, 5, 6)));
56int __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. */
61void *__ast_repl_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc;
62void *__ast_repl_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc;
63void *__ast_repl_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func);
64char *__ast_repl_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc;
65char *__ast_repl_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc;
66int __ast_repl_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...)
67 __attribute__((format(printf, 5, 6)));
68int __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#if !defined(ASTMM_LIBC)
114/* BLOCK libc allocators by default. */
115#define ASTMM_LIBC ASTMM_BLOCK
116#endif
117
118#if ASTMM_LIBC == ASTMM_IGNORE
119/* Don't touch the libc functions. */
120#else
121
122/* Undefine any macros */
123#undef malloc
124#undef calloc
125#undef realloc
126#undef strdup
127#undef strndup
128#undef asprintf
129#undef vasprintf
130#undef free
131
132#if ASTMM_LIBC == ASTMM_REDIRECT
133
134/* Redefine libc functions to our own versions */
135#define calloc(nmemb, size) \
136 __ast_repl_calloc(nmemb, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
137#define malloc(size) \
138 __ast_repl_malloc(size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
139#define free(ptr) \
140 __ast_free(ptr, __FILE__, __LINE__, __PRETTY_FUNCTION__)
141#define realloc(ptr, size) \
142 __ast_repl_realloc(ptr, size, __FILE__, __LINE__, __PRETTY_FUNCTION__)
143#define strdup(s) \
144 __ast_repl_strdup(s, __FILE__, __LINE__, __PRETTY_FUNCTION__)
145#define strndup(s, n) \
146 __ast_repl_strndup(s, n, __FILE__, __LINE__, __PRETTY_FUNCTION__)
147#define asprintf(strp, format, args...) \
148 __ast_repl_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, strp, format, args)
149#define vasprintf(strp, format, ap) \
150 __ast_repl_vasprintf(strp, format, ap, __FILE__, __LINE__, __PRETTY_FUNCTION__)
151
152#elif ASTMM_LIBC == ASTMM_BLOCK
153
154/* Redefine libc functions to cause compile errors */
155#define calloc(a, b) \
156 Do_not_use_calloc__use_ast_calloc->fail(a, b)
157#define malloc(a) \
158 Do_not_use_malloc__use_ast_malloc->fail(a)
159#define free(a) \
160 Do_not_use_free__use_ast_free_or_ast_std_free_for_remotely_allocated_memory->fail(a)
161#define realloc(a, b) \
162 Do_not_use_realloc__use_ast_realloc->fail(a, b)
163#define strdup(a) \
164 Do_not_use_strdup__use_ast_strdup->fail(a)
165#define strndup(a, b) \
166 Do_not_use_strndup__use_ast_strndup->fail(a, b)
167#define asprintf(a, b, c...) \
168 Do_not_use_asprintf__use_ast_asprintf->fail(a, b, c)
169#define vasprintf(a, b, c) \
170 Do_not_use_vasprintf__use_ast_vasprintf->fail(a, b, c)
171
172#else
173#error "Unacceptable value for the macro ASTMM_LIBC"
174#endif
175
176#endif
177
178/* Provide our own definition for ast_free */
179
180#define ast_free(a) \
181 __ast_free(a, __FILE__, __LINE__, __PRETTY_FUNCTION__)
182
183/*!
184 * \brief A wrapper for malloc()
185 *
186 * ast_malloc() is a wrapper for malloc() that will generate an Asterisk log
187 * message in the case that the allocation fails.
188 *
189 * The argument and return value are the same as malloc()
190 */
191#define ast_malloc(len) \
192 __ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
193
194/*!
195 * \brief A wrapper for calloc()
196 *
197 * ast_calloc() is a wrapper for calloc() that will generate an Asterisk log
198 * message in the case that the allocation fails.
199 *
200 * The arguments and return value are the same as calloc()
201 */
202#define ast_calloc(num, len) \
203 __ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
204
205/*!
206 * \brief A wrapper for calloc() for use in cache pools
207 *
208 * ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log
209 * message in the case that the allocation fails. When memory debugging is in use,
210 * the memory allocated by this function will be marked as 'cache' so it can be
211 * distinguished from normal memory allocations.
212 *
213 * The arguments and return value are the same as calloc()
214 */
215#define ast_calloc_cache(num, len) \
216 __ast_calloc_cache((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
217
218/*!
219 * \brief A wrapper for realloc()
220 *
221 * ast_realloc() is a wrapper for realloc() that will generate an Asterisk log
222 * message in the case that the allocation fails.
223 *
224 * The arguments and return value are the same as realloc()
225 */
226#define ast_realloc(p, len) \
227 __ast_realloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
228
229/*!
230 * \brief A wrapper for strdup()
231 *
232 * ast_strdup() is a wrapper for strdup() that will generate an Asterisk log
233 * message in the case that the allocation fails.
234 *
235 * ast_strdup(), unlike strdup(), can safely accept a NULL argument. If a NULL
236 * argument is provided, ast_strdup will return NULL without generating any
237 * kind of error log message.
238 *
239 * The argument and return value are the same as strdup()
240 */
241#define ast_strdup(str) \
242 __ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)
243
244/*!
245 * \brief A wrapper for strndup()
246 *
247 * ast_strndup() is a wrapper for strndup() that will generate an Asterisk log
248 * message in the case that the allocation fails.
249 *
250 * ast_strndup(), unlike strndup(), can safely accept a NULL argument for the
251 * string to duplicate. If a NULL argument is provided, ast_strdup will return
252 * NULL without generating any kind of error log message.
253 *
254 * The arguments and return value are the same as strndup()
255 */
256#define ast_strndup(str, len) \
257 __ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)
258
259/*!
260 * \brief A wrapper for asprintf()
261 *
262 * ast_asprintf() is a wrapper for asprintf() that will generate an Asterisk log
263 * message in the case that the allocation fails.
264 *
265 * The arguments and return value are the same as asprintf()
266 */
267#define ast_asprintf(ret, fmt, ...) \
268 __ast_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, (ret), (fmt), __VA_ARGS__)
269
270/*!
271 * \brief A wrapper for vasprintf()
272 *
273 * ast_vasprintf() is a wrapper for vasprintf() that will generate an Asterisk log
274 * message in the case that the allocation fails.
275 *
276 * The arguments and return value are the same as vasprintf()
277 */
278#define ast_vasprintf(ret, fmt, ap) \
279 __ast_vasprintf((ret), (fmt), (ap), __FILE__, __LINE__, __PRETTY_FUNCTION__)
280
281/*!
282 \brief call __builtin_alloca to ensure we get gcc builtin semantics
283 \param size The size of the buffer we want allocated
284
285 This macro will attempt to allocate memory from the stack. If it fails
286 you won't get a NULL returned, but a SEGFAULT if you're lucky.
287*/
288#define ast_alloca(size) __builtin_alloca(size)
289
290#if !defined(ast_strdupa) && defined(__GNUC__)
291/*!
292 * \brief duplicate a string in memory from the stack
293 * \param s The string to duplicate
294 *
295 * This macro will duplicate the given string. It returns a pointer to the stack
296 * allocated memory for the new string.
297 */
298#define ast_strdupa(s) \
299 (__extension__ \
300 ({ \
301 const char *__old = (s); \
302 size_t __len = strlen(__old) + 1; \
303 char *__new = __builtin_alloca(__len); \
304 memcpy (__new, __old, __len); \
305 __new; \
306 }))
307#endif
308
309#else
310#error "NEVER INCLUDE astmm.h DIRECTLY!!"
311#endif /* _ASTERISK_ASTMM_H */
312
313#ifdef __cplusplus
314}
315#endif
void * __ast_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1628
void ast_std_free(void *ptr)
Definition: astmm.c:1734
void * ast_std_malloc(size_t size) attribute_malloc
Definition: astmm.c:1719
void * __ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1615
int __ast_repl_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
Definition: astmm.c:1594
char * __ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1666
void * ast_std_realloc(void *ptr, size_t size)
Definition: astmm.c:1729
char * __ast_repl_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1566
void * __ast_repl_malloc(size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1547
void * ast_std_calloc(size_t nmemb, size_t size) attribute_malloc
Definition: astmm.c:1724
void * __ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1640
char * __ast_repl_strndup(const char *s, size_t n, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1573
int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func)
Definition: astmm.c:1701
void * __ast_repl_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1533
void * __ast_repl_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func)
Definition: astmm.c:1559
char * __ast_strdup(const char *s, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1652
int __ast_repl_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format,...)
Definition: astmm.c:1580
void ast_free_ptr(void *ptr)
free() wrapper
Definition: astmm.c:1739
void * __ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func) attribute_malloc
Definition: astmm.c:1603
void __ast_free(void *ptr, const char *file, int lineno, const char *func)
Definition: astmm.c:1554
int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format,...)
Definition: astmm.c:1680
#define attribute_malloc
Definition: compiler.h:59