From c711e4076a6913e18e27a694bcb3fb721822fb58 Mon Sep 17 00:00:00 2001 From: Richard Mudgett Date: Mon, 19 Feb 2018 19:55:50 -0600 Subject: core: Remove ABI effects of MALLOC_DEBUG. This allows asterisk to be compiled with MALLOC_DEBUG to load modules built without MALLOC_DEBUG. Now pre-compiled third-party modules will still work regardless of MALLOC_DEBUG being enabled or not. Change-Id: Ic07ad80b2c2df894db984cf27b16a69383ce0e10 --- include/asterisk.h | 2 +- include/asterisk/astmm.h | 67 ++++++++++++------------ include/asterisk/chanvars.h | 8 ++- include/asterisk/compat.h | 6 +-- include/asterisk/config.h | 5 +- include/asterisk/hashtab.h | 67 ++++++++---------------- include/asterisk/heap.h | 15 ++---- include/asterisk/stringfields.h | 4 +- include/asterisk/strings.h | 69 +++++-------------------- include/asterisk/utils.h | 111 ++++++++++++++++++++++++++++++++++------ 10 files changed, 176 insertions(+), 178 deletions(-) (limited to 'include') diff --git a/include/asterisk.h b/include/asterisk.h index 371860278..27d66b776 100644 --- a/include/asterisk.h +++ b/include/asterisk.h @@ -21,7 +21,7 @@ #include "asterisk/autoconfig.h" #include "asterisk/compat.h" -#if !defined(NO_MALLOC_DEBUG) && !defined(STANDALONE) && !defined(STANDALONE2) && defined(MALLOC_DEBUG) +#if !defined(NO_MALLOC_DEBUG) && !defined(STANDALONE) && !defined(STANDALONE2) #include "asterisk/astmm.h" #endif diff --git a/include/asterisk/astmm.h b/include/asterisk/astmm.h index 06300c861..4e4a65b31 100644 --- a/include/asterisk/astmm.h +++ b/include/asterisk/astmm.h @@ -32,27 +32,30 @@ extern "C" { #define _ASTERISK_ASTMM_H /* IWYU pragma: private, include "asterisk.h" */ +#if defined(MALLOC_DEBUG) #define __AST_DEBUG_MALLOC +void __ast_mm_init_phase_1(void); +void __ast_mm_init_phase_2(void); +#endif + void *ast_std_malloc(size_t size); void *ast_std_calloc(size_t nmemb, size_t size); void *ast_std_realloc(void *ptr, size_t size); void ast_std_free(void *ptr); void ast_free_ptr(void *ptr); -void *__ast_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func); -void *__ast_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func); -void *__ast_malloc(size_t size, const char *file, int lineno, const char *func); +void *__ast_repl_calloc(size_t nmemb, size_t size, const char *file, int lineno, const char *func); +void *__ast_repl_calloc_cache(size_t nmemb, size_t size, const char *file, int lineno, const char *func); +void *__ast_repl_malloc(size_t size, const char *file, int lineno, const char *func); void __ast_free(void *ptr, const char *file, int lineno, const char *func); -void *__ast_realloc(void *ptr, size_t size, const char *file, int lineno, const char *func); -char *__ast_strdup(const char *s, const char *file, int lineno, const char *func); -char *__ast_strndup(const char *s, size_t n, const char *file, int lineno, const char *func); -int __ast_asprintf(const char *file, int lineno, const char *func, char **strp, const char *format, ...) +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); +char *__ast_repl_strndup(const char *s, size_t n, 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, ...) __attribute__((format(printf, 5, 6))); -int __ast_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func) +int __ast_repl_vasprintf(char **strp, const char *format, va_list ap, const char *file, int lineno, const char *func) __attribute__((format(printf, 2, 0))); -void __ast_mm_init_phase_1(void); -void __ast_mm_init_phase_2(void); /*! * \brief ASTMM_LIBC can be defined to control the meaning of standard allocators. @@ -120,42 +123,42 @@ void __ast_mm_init_phase_2(void); #if ASTMM_LIBC == ASTMM_REDIRECT /* Redefine libc functions to our own versions */ -#define calloc(a,b) \ - __ast_calloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__) +#define calloc(a, b) \ + __ast_repl_calloc(a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__) #define malloc(a) \ - __ast_malloc(a,__FILE__, __LINE__, __PRETTY_FUNCTION__) + __ast_repl_malloc(a, __FILE__, __LINE__, __PRETTY_FUNCTION__) #define free(a) \ - __ast_free(a,__FILE__, __LINE__, __PRETTY_FUNCTION__) -#define realloc(a,b) \ - __ast_realloc(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__) + __ast_free(a, __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define realloc(a, b) \ + __ast_repl_realloc(a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__) #define strdup(a) \ - __ast_strdup(a,__FILE__, __LINE__, __PRETTY_FUNCTION__) -#define strndup(a,b) \ - __ast_strndup(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__) + __ast_repl_strdup(a, __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define strndup(a, b) \ + __ast_repl_strndup(a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__) #define asprintf(a, b, c...) \ - __ast_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, b, c) -#define vasprintf(a,b,c) \ - __ast_vasprintf(a,b,c,__FILE__, __LINE__, __PRETTY_FUNCTION__) + __ast_repl_asprintf(__FILE__, __LINE__, __PRETTY_FUNCTION__, a, b, c) +#define vasprintf(a, b, c) \ + __ast_repl_vasprintf(a, b, c, __FILE__, __LINE__, __PRETTY_FUNCTION__) #elif ASTMM_LIBC == ASTMM_BLOCK /* Redefine libc functions to cause compile errors */ -#define calloc(a,b) \ - Do_not_use_calloc__use_ast_calloc->fail(a,b) +#define calloc(a, b) \ + Do_not_use_calloc__use_ast_calloc->fail(a, b) #define malloc(a) \ Do_not_use_malloc__use_ast_malloc->fail(a) #define free(a) \ Do_not_use_free__use_ast_free_or_ast_std_free_for_remotely_allocated_memory->fail(a) -#define realloc(a,b) \ - Do_not_use_realloc__use_ast_realloc->fail(a,b) +#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 strndup(a, b) \ + Do_not_use_strndup__use_ast_strndup->fail(a, b) #define asprintf(a, b, c...) \ - Do_not_use_asprintf__use_ast_asprintf->fail(a,b,c) -#define vasprintf(a,b,c) \ - Do_not_use_vasprintf__use_ast_vasprintf->fail(a,b,c) + Do_not_use_asprintf__use_ast_asprintf->fail(a, b, c) +#define vasprintf(a, b, c) \ + Do_not_use_vasprintf__use_ast_vasprintf->fail(a, b, c) #else #error "Unacceptable value for the macro ASTMM_LIBC" @@ -166,7 +169,7 @@ void __ast_mm_init_phase_2(void); /* Provide our own definition for ast_free */ #define ast_free(a) \ - __ast_free(a,__FILE__, __LINE__, __PRETTY_FUNCTION__) + __ast_free(a, __FILE__, __LINE__, __PRETTY_FUNCTION__) #else #error "NEVER INCLUDE astmm.h DIRECTLY!!" diff --git a/include/asterisk/chanvars.h b/include/asterisk/chanvars.h index 2040c7b65..1a303c5f7 100644 --- a/include/asterisk/chanvars.h +++ b/include/asterisk/chanvars.h @@ -35,12 +35,10 @@ AST_LIST_HEAD_NOLOCK(varshead, ast_var_t); struct varshead *ast_var_list_create(void); void ast_var_list_destroy(struct varshead *head); -#ifdef __AST_DEBUG_MALLOC + struct ast_var_t *_ast_var_assign(const char *name, const char *value, const char *file, int lineno, const char *function); -#define ast_var_assign(a,b) _ast_var_assign(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -struct ast_var_t *ast_var_assign(const char *name, const char *value); -#endif +#define ast_var_assign(name, value) _ast_var_assign(name, value, __FILE__, __LINE__, __PRETTY_FUNCTION__) + void ast_var_delete(struct ast_var_t *var); const char *ast_var_name(const struct ast_var_t *var); const char *ast_var_full_name(const struct ast_var_t *var); diff --git a/include/asterisk/compat.h b/include/asterisk/compat.h index 2e89a39eb..85472837c 100644 --- a/include/asterisk/compat.h +++ b/include/asterisk/compat.h @@ -64,7 +64,7 @@ void closefrom(int lowfd); #endif -#if !defined(HAVE_ASPRINTF) && !defined(__AST_DEBUG_MALLOC) +#if !defined(HAVE_ASPRINTF) int __attribute__((format(printf, 2, 3))) asprintf(char **str, const char *fmt, ...); #endif @@ -96,7 +96,7 @@ int setenv(const char *name, const char *value, int overwrite); char *strcasestr(const char *, const char *); #endif -#if !defined(HAVE_STRNDUP) && !defined(__AST_DEBUG_MALLOC) +#if !defined(HAVE_STRNDUP) char *strndup(const char *, size_t); #endif @@ -116,7 +116,7 @@ uint64_t strtoq(const char *nptr, char **endptr, int base); int unsetenv(const char *name); #endif -#if !defined(HAVE_VASPRINTF) && !defined(__AST_DEBUG_MALLOC) +#if !defined(HAVE_VASPRINTF) int __attribute__((format(printf, 2, 0))) vasprintf(char **strp, const char *fmt, va_list ap); #endif diff --git a/include/asterisk/config.h b/include/asterisk/config.h index fd9641e99..891284039 100644 --- a/include/asterisk/config.h +++ b/include/asterisk/config.h @@ -919,12 +919,9 @@ void ast_category_destroy(struct ast_category *cat); struct ast_variable *ast_category_detach_variables(struct ast_category *cat); void ast_category_rename(struct ast_category *cat, const char *name); -#ifdef __AST_DEBUG_MALLOC struct ast_variable *_ast_variable_new(const char *name, const char *value, const char *filename, const char *file, const char *function, int lineno); #define ast_variable_new(name, value, filename) _ast_variable_new(name, value, filename, __FILE__, __PRETTY_FUNCTION__, __LINE__) -#else -struct ast_variable *ast_variable_new(const char *name, const char *value, const char *filename); -#endif + struct ast_config_include *ast_include_new(struct ast_config *conf, const char *from_file, const char *included_file, int is_exec, const char *exec_file, int from_lineno, char *real_included_file_name, int real_included_file_name_size); struct ast_config_include *ast_include_find(struct ast_config *conf, const char *included_file); void ast_include_rename(struct ast_config *conf, const char *from_file, const char *to_file); diff --git a/include/asterisk/hashtab.h b/include/asterisk/hashtab.h index cfe035b28..64ed1bf6f 100644 --- a/include/asterisk/hashtab.h +++ b/include/asterisk/hashtab.h @@ -251,22 +251,15 @@ unsigned int ast_hashtab_hash_short(const short num); * \param hash a func ptr to do the hashing * \param do_locking use locks to guarantee safety of iterators/insertion/deletion -- real simpleminded right now */ -#ifdef __AST_DEBUG_MALLOC -struct ast_hashtab * _ast_hashtab_create(int initial_buckets, - int (*compare)(const void *a, const void *b), - int (*resize)(struct ast_hashtab *), - int (*newsize)(struct ast_hashtab *tab), - unsigned int (*hash)(const void *obj), - int do_locking, const char *file, int lineno, const char *function); -#define ast_hashtab_create(a,b,c,d,e,f) _ast_hashtab_create(a,b,c,d,e,f,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -struct ast_hashtab * ast_hashtab_create(int initial_buckets, - int (*compare)(const void *a, const void *b), - int (*resize)(struct ast_hashtab *), - int (*newsize)(struct ast_hashtab *tab), - unsigned int (*hash)(const void *obj), - int do_locking ); -#endif +struct ast_hashtab *_ast_hashtab_create(int initial_buckets, + int (*compare)(const void *a, const void *b), + int (*resize)(struct ast_hashtab *), + int (*newsize)(struct ast_hashtab *tab), + unsigned int (*hash)(const void *obj), + int do_locking, + const char *file, int lineno, const char *function); +#define ast_hashtab_create(initial_buckets, compare, resize, newsize, hash, do_locking) \ + _ast_hashtab_create(initial_buckets, compare, resize, newsize, hash, do_locking, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief This func will free the hash table and all its memory. @@ -294,12 +287,9 @@ void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void * * \retval 1 on success * \retval 0 if there's a problem */ -#ifdef __AST_DEBUG_MALLOC int _ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func); -#define ast_hashtab_insert_immediate(a,b) _ast_hashtab_insert_immediate(a, b, __FILE__, __LINE__, __PRETTY_FUNCTION__) -#else -int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj); -#endif +#define ast_hashtab_insert_immediate(tab, obj) \ + _ast_hashtab_insert_immediate(tab, obj, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief Insert without checking, hashing or locking @@ -311,12 +301,9 @@ int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj); * \retval 1 on success * \retval 0 if there's a problem */ -#ifdef __AST_DEBUG_MALLOC int _ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h, const char *file, int lineno, const char *func); -#define ast_hashtab_insert_immediate_bucket(a,b,c) _ast_hashtab_insert_immediate_bucket(a, b, c, __FILE__, __LINE__, __PRETTY_FUNCTION__) -#else -int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h); -#endif +#define ast_hashtab_insert_immediate_bucket(tab, obj, h) \ + _ast_hashtab_insert_immediate_bucket(tab, obj, h, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief Check and insert new object only if it is not there. @@ -324,12 +311,9 @@ int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj * \retval 1 on success * \retval 0 if there's a problem, or it's already there. */ -#ifdef __AST_DEBUG_MALLOC int _ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func); -#define ast_hashtab_insert_safe(a,b) _ast_hashtab_insert_safe(a,b,__FILE__, __LINE__, __PRETTY_FUNCTION__) -#else -int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj); -#endif +#define ast_hashtab_insert_safe(tab, obj) \ + _ast_hashtab_insert_safe(tab, obj, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief Lookup this object in the hash table. @@ -362,20 +346,14 @@ int ast_hashtab_size( struct ast_hashtab *tab); int ast_hashtab_capacity( struct ast_hashtab *tab); /*! \brief Return a copy of the hash table */ -#ifdef __AST_DEBUG_MALLOC struct ast_hashtab *_ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj), const char *file, int lineno, const char *func); -#define ast_hashtab_dup(a,b) _ast_hashtab_dup(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -struct ast_hashtab *ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj)); -#endif +#define ast_hashtab_dup(tab, obj_dup_func) \ + _ast_hashtab_dup(tab, obj_dup_func, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! \brief Gives an iterator to hastable */ -#ifdef __AST_DEBUG_MALLOC struct ast_hashtab_iter *_ast_hashtab_start_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func); -#define ast_hashtab_start_traversal(a) _ast_hashtab_start_traversal(a,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab); -#endif +#define ast_hashtab_start_traversal(tab) \ + _ast_hashtab_start_traversal(tab, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! \brief end the traversal, free the iterator, unlock if necc. */ void ast_hashtab_end_traversal(struct ast_hashtab_iter *it); @@ -395,12 +373,9 @@ void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj); /* ------------------ */ /*! \brief Gives an iterator to hastable */ -#ifdef __AST_DEBUG_MALLOC struct ast_hashtab_iter *_ast_hashtab_start_write_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func); -#define ast_hashtab_start_write_traversal(a) _ast_hashtab_start_write_traversal(a,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -struct ast_hashtab_iter *ast_hashtab_start_write_traversal(struct ast_hashtab *tab); -#endif +#define ast_hashtab_start_write_traversal(tab) \ + _ast_hashtab_start_write_traversal(tab, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! \brief Looks up the object, removes the corresponding bucket */ void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void *obj); diff --git a/include/asterisk/heap.h b/include/asterisk/heap.h index 728327936..83c69a8d6 100644 --- a/include/asterisk/heap.h +++ b/include/asterisk/heap.h @@ -97,14 +97,10 @@ typedef int (*ast_heap_cmp_fn)(void *elm1, void *elm2); * \return An instance of a max heap * \since 1.6.1 */ -#ifdef __AST_DEBUG_MALLOC struct ast_heap *_ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn, ssize_t index_offset, const char *file, int lineno, const char *func); -#define ast_heap_create(a,b,c) _ast_heap_create(a,b,c,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn, - ssize_t index_offset); -#endif +#define ast_heap_create(init_height, cmp_fn, index_offset) \ + _ast_heap_create(init_height, cmp_fn, index_offset, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief Destroy a max heap @@ -126,12 +122,9 @@ struct ast_heap *ast_heap_destroy(struct ast_heap *h); * \retval non-zero failure * \since 1.6.1 */ -#ifdef __AST_DEBUG_MALLOC int _ast_heap_push(struct ast_heap *h, void *elm, const char *file, int lineno, const char *func); -#define ast_heap_push(a,b) _ast_heap_push(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -int ast_heap_push(struct ast_heap *h, void *elm); -#endif +#define ast_heap_push(h, elm) \ + _ast_heap_push(h, elm, __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief Pop the max element off of the heap diff --git a/include/asterisk/stringfields.h b/include/asterisk/stringfields.h index 69f2fcca4..5ac5e0973 100644 --- a/include/asterisk/stringfields.h +++ b/include/asterisk/stringfields.h @@ -228,11 +228,11 @@ struct ast_string_field_mgr { ast_string_field last_alloc; /*!< the last field allocated */ struct ast_string_field_pool *embedded_pool; /*!< pointer to the embedded pool, if any */ struct ast_string_field_vector string_fields; /*!< field vector for compare and copy */ -#if defined(__AST_DEBUG_MALLOC) + /* v-- MALLOC_DEBUG information */ const char *owner_file; /*!< filename of owner */ const char *owner_func; /*!< function name of owner */ int owner_line; /*!< line number of owner */ -#endif + /* ^-- MALLOC_DEBUG information */ }; /*! diff --git a/include/asterisk/strings.h b/include/asterisk/strings.h index 85393b4f9..e4bef5f0f 100644 --- a/include/asterisk/strings.h +++ b/include/asterisk/strings.h @@ -617,8 +617,8 @@ int ast_regex_string_to_regex_pattern(const char *regex_string, struct ast_str * * \note The result of this function is dynamically allocated memory, and must * be free()'d after it is no longer needed. */ -#ifdef __AST_DEBUG_MALLOC -#define ast_str_create(a) _ast_str_create(a,__FILE__,__LINE__,__PRETTY_FUNCTION__) +#define ast_str_create(init_len) \ + _ast_str_create(init_len, __FILE__, __LINE__, __PRETTY_FUNCTION__) AST_INLINE_API( struct ast_str * attribute_malloc _ast_str_create(size_t init_len, const char *file, int lineno, const char *func), @@ -636,24 +636,6 @@ struct ast_str * attribute_malloc _ast_str_create(size_t init_len, return buf; } ) -#else -AST_INLINE_API( -struct ast_str * attribute_malloc ast_str_create(size_t init_len), -{ - struct ast_str *buf; - - buf = (struct ast_str *)ast_calloc(1, sizeof(*buf) + init_len); - if (buf == NULL) - return NULL; - - buf->__AST_STR_LEN = init_len; - buf->__AST_STR_USED = 0; - buf->__AST_STR_TS = DS_MALLOC; - - return buf; -} -) -#endif /*! \brief Reset the content of a dynamic string. * Useful before a series of ast_str_append. @@ -772,7 +754,6 @@ char *ast_str_truncate(struct ast_str *buf, ssize_t len), /*! * Make space in a new string (e.g. to read in data from a file) */ -#ifdef __AST_DEBUG_MALLOC AST_INLINE_API( int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, int lineno, const char *function), { @@ -796,32 +777,8 @@ int _ast_str_make_space(struct ast_str **buf, size_t new_len, const char *file, return 0; } ) -#define ast_str_make_space(a,b) _ast_str_make_space(a,b,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -AST_INLINE_API( -int ast_str_make_space(struct ast_str **buf, size_t new_len), -{ - struct ast_str *old_buf = *buf; - - if (new_len <= (*buf)->__AST_STR_LEN) - return 0; /* success */ - if ((*buf)->__AST_STR_TS == DS_ALLOCA || (*buf)->__AST_STR_TS == DS_STATIC) - return -1; /* cannot extend */ - *buf = (struct ast_str *)ast_realloc(*buf, new_len + sizeof(struct ast_str)); - if (*buf == NULL) { - *buf = old_buf; - return -1; - } - if ((*buf)->__AST_STR_TS != DS_MALLOC) { - pthread_setspecific((*buf)->__AST_STR_TS->key, *buf); - _DB1(__ast_threadstorage_object_replace(old_buf, *buf, new_len + sizeof(struct ast_str));) - } - - (*buf)->__AST_STR_LEN = new_len; - return 0; -} -) -#endif +#define ast_str_make_space(buf, new_len) \ + _ast_str_make_space(buf, new_len, __FILE__, __LINE__, __PRETTY_FUNCTION__) AST_INLINE_API( int ast_str_copy_string(struct ast_str **dst, struct ast_str *src), @@ -965,14 +922,12 @@ enum { * through calling one of the other functions or macros defined in this * file. */ -#ifdef __AST_DEBUG_MALLOC -int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, ssize_t max_len, - int append, const char *fmt, va_list ap, const char *file, int lineno, const char *func); -#define __ast_str_helper(a,b,c,d,e) __ast_debug_str_helper(a,b,c,d,e,__FILE__,__LINE__,__PRETTY_FUNCTION__) -#else -int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, ssize_t max_len, - int append, const char *fmt, va_list ap); -#endif +int __attribute__((format(printf, 4, 0))) __ast_str_helper(struct ast_str **buf, + ssize_t max_len, int append, const char *fmt, va_list ap, + const char *file, int lineno, const char *func); +#define _ast_str_helper(buf, max_len, append, fmt, ap) \ + __ast_str_helper(buf, max_len, append, fmt, ap, __FILE__, __LINE__, __PRETTY_FUNCTION__) + char *__ast_str_helper2(struct ast_str **buf, ssize_t max_len, const char *src, size_t maxsrc, int append, int escapecommas); @@ -1022,7 +977,7 @@ char *__ast_str_helper2(struct ast_str **buf, ssize_t max_len, */ AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_set_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap), { - return __ast_str_helper(buf, max_len, 0, fmt, ap); + return _ast_str_helper(buf, max_len, 0, fmt, ap); } ) @@ -1040,7 +995,7 @@ AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_set_va(struct a */ AST_INLINE_API(int __attribute__((format(printf, 3, 0))) ast_str_append_va(struct ast_str **buf, ssize_t max_len, const char *fmt, va_list ap), { - return __ast_str_helper(buf, max_len, 1, fmt, ap); + return _ast_str_helper(buf, max_len, 1, fmt, ap); } ) diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h index c6c34074e..c00310d39 100644 --- a/include/asterisk/utils.h +++ b/include/asterisk/utils.h @@ -509,11 +509,11 @@ long int ast_random(void); #endif -#ifndef __AST_DEBUG_MALLOC -#define ast_std_malloc malloc -#define ast_std_calloc calloc -#define ast_std_realloc realloc -#define ast_std_free free +#if !defined(NO_MALLOC_DEBUG) && !defined(STANDALONE) && !defined(STANDALONE2) +void *ast_std_malloc(size_t size); +void *ast_std_calloc(size_t nmemb, size_t size); +void *ast_std_realloc(void *ptr, size_t size); +void ast_std_free(void *ptr); /*! * \brief free() wrapper @@ -521,8 +521,44 @@ long int ast_random(void); * ast_free_ptr should be used when a function pointer for free() needs to be passed * as the argument to a function. Otherwise, astmm will cause seg faults. */ +void ast_free_ptr(void *ptr); +void __ast_free(void *ptr, const char *file, int lineno, const char *func); + +#else + +/* + * Need to defeat the MALLOC_DEBUG API when building the standalone utilities. + */ + +#define ast_std_malloc malloc +#define ast_std_calloc calloc +#define ast_std_realloc realloc +#define ast_std_free free + +#define ast_free_ptr free #define ast_free free -#define ast_free_ptr ast_free + +#define __ast_repl_calloc(nmemb, size, file, lineno, func) \ + calloc(nmemb, size) + +#define __ast_repl_calloc_cache(nmemb, size, file, lineno, func) \ + calloc(nmemb, size) + +#define __ast_repl_malloc(size, file, lineno, func) \ + malloc(size) + +#define __ast_repl_realloc(ptr, size, file, lineno, func) \ + realloc(ptr, size) + +#define __ast_repl_strdup(s, file, lineno, func) \ + strdup(s) + +#define __ast_repl_strndup(s, n, file, lineno, func) \ + strndup(s, n) + +#define __ast_repl_vasprintf(strp, format, ap, file, lineno, func) \ + vasprintf(strp, format, ap) +#endif #if defined(AST_IN_CORE) #define MALLOC_FAILURE_MSG \ @@ -539,7 +575,8 @@ void * attribute_malloc __ast_malloc(size_t len, const char *file, int lineno, c DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL); - if (!(p = malloc(len))) { + p = __ast_repl_malloc(len, file, lineno, func); + if (!p) { MALLOC_FAILURE_MSG; } @@ -554,7 +591,24 @@ void * attribute_malloc __ast_calloc(size_t num, size_t len, const char *file, i DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL); - if (!(p = calloc(num, len))) { + p = __ast_repl_calloc(num, len, file, lineno, func); + if (!p) { + MALLOC_FAILURE_MSG; + } + + return p; +} +) + +AST_INLINE_API( +void * attribute_malloc __ast_calloc_cache(size_t num, size_t len, const char *file, int lineno, const char *func), +{ + void *p; + + DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL); + + p = __ast_repl_calloc_cache(num, len, file, lineno, func); + if (!p) { MALLOC_FAILURE_MSG; } @@ -569,7 +623,8 @@ void * attribute_malloc __ast_realloc(void *p, size_t len, const char *file, int DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL); - if (!(newp = realloc(p, len))) { + newp = __ast_repl_realloc(p, len, file, lineno, func); + if (!newp) { MALLOC_FAILURE_MSG; } @@ -585,7 +640,8 @@ char * attribute_malloc __ast_strdup(const char *str, const char *file, int line DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL); if (str) { - if (!(newstr = strdup(str))) { + newstr = __ast_repl_strdup(str, file, lineno, func); + if (!newstr) { MALLOC_FAILURE_MSG; } } @@ -602,7 +658,8 @@ char * attribute_malloc __ast_strndup(const char *str, size_t len, const char *f DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, NULL); if (str) { - if (!(newstr = strndup(str, len))) { + newstr = __ast_repl_strndup(str, len, file, lineno, func); + if (!newstr) { MALLOC_FAILURE_MSG; } } @@ -611,8 +668,30 @@ char * attribute_malloc __ast_strndup(const char *str, size_t len, const char *f } ) -int __attribute__((format(printf, 5, 6))) - __ast_asprintf(const char *file, int lineno, const char *func, char **ret, const char *fmt, ...); +AST_INLINE_API( +__attribute__((format(printf, 5, 6))) +int __ast_asprintf(const char *file, int lineno, const char *func, char **ret, const char *fmt, ...), +{ + int res; + va_list ap; + + DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, -1); + + va_start(ap, fmt); + res = __ast_repl_vasprintf(ret, fmt, ap, file, lineno, func); + if (res < 0) { + /* + * *ret is undefined so set to NULL to ensure it is + * initialized to something useful. + */ + *ret = NULL; + MALLOC_FAILURE_MSG; + } + va_end(ap); + + return res; +} +) AST_INLINE_API( __attribute__((format(printf, 2, 0))) @@ -622,7 +701,7 @@ int __ast_vasprintf(char **ret, const char *fmt, va_list ap, const char *file, i DEBUG_CHAOS_RETURN(DEBUG_CHAOS_ALLOC_CHANCE, -1); - res = vasprintf(ret, fmt, ap); + res = __ast_repl_vasprintf(ret, fmt, ap, file, lineno, func); if (res < 0) { /* * *ret is undefined so set to NULL to ensure it is @@ -636,8 +715,6 @@ int __ast_vasprintf(char **ret, const char *fmt, va_list ap, const char *file, i } ) -#endif /* AST_DEBUG_MALLOC */ - /*! * \brief A wrapper for malloc() * @@ -671,7 +748,7 @@ int __ast_vasprintf(char **ret, const char *fmt, va_list ap, const char *file, i * The arguments and return value are the same as calloc() */ #define ast_calloc_cache(num, len) \ - __ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__) + __ast_calloc_cache((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__) /*! * \brief A wrapper for realloc() -- cgit v1.2.3