Index: include/asterisk/hashtab.h =================================================================== --- include/asterisk/hashtab.h (revision 180844) +++ include/asterisk/hashtab.h (working copy) @@ -291,7 +291,12 @@ * \retval 1 on success * \retval 0 if there's a problem */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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 /*! * \brief Insert without checking, hashing or locking @@ -303,7 +308,12 @@ * \retval 1 on success * \retval 0 if there's a problem */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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 /*! * \brief Check and insert new object only if it is not there. @@ -311,7 +321,12 @@ * \retval 1 on success * \retval 0 if there's a problem, or it's already there. */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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 /*! * \brief Lookup this object in the hash table. @@ -344,10 +359,20 @@ int ast_hashtab_capacity( struct ast_hashtab *tab); /*! \brief Return a copy of the hash table */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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 /*! \brief Gives an iterator to hastable */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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 /*! \brief end the traversal, free the iterator, unlock if necc. */ void ast_hashtab_end_traversal(struct ast_hashtab_iter *it); @@ -367,7 +392,12 @@ /* ------------------ */ /*! \brief Gives an iterator to hastable */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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 /*! \brief Looks up the object, removes the corresponding bucket */ void *ast_hashtab_remove_object_via_lookup_nolock(struct ast_hashtab *tab, void *obj); Index: include/asterisk/astobj2.h =================================================================== --- include/asterisk/astobj2.h (revision 180844) +++ include/asterisk/astobj2.h (working copy) @@ -19,6 +19,10 @@ #include "asterisk/compat.h" +#ifdef MALLOC_DEBUG +#define REF_DEBUG +#endif + /*! \file * \ref AstObj2 * Index: include/asterisk/heap.h =================================================================== --- include/asterisk/heap.h (revision 180844) +++ include/asterisk/heap.h (working copy) @@ -97,8 +97,14 @@ * \return An instance of a max heap * \since 1.6.1 */ +#ifdef MALLOC_DEBUG +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 /*! * \brief Destroy a max heap @@ -120,7 +126,12 @@ * \retval non-zero failure * \since 1.6.1 */ +#ifdef MALLOC_DEBUG +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 /*! * \brief Pop the max element off of the heap Index: include/asterisk/strings.h =================================================================== --- include/asterisk/strings.h (revision 180844) +++ include/asterisk/strings.h (working copy) @@ -372,7 +372,27 @@ * \note The result of this function is dynamically allocated memory, and must * be free()'d after it is no longer needed. */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +#define ast_str_create(a) _ast_str_create(a,__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), +{ + struct ast_str *buf; + + buf = (struct ast_str *)__ast_calloc(1, sizeof(*buf) + init_len, file, lineno, func); + if (buf == NULL) + return NULL; + + buf->__AST_STR_LEN = init_len; + buf->__AST_STR_USED = 0; + buf->__AST_STR_TS = DS_MALLOC; + + return buf; +} +) +#else +AST_INLINE_API( struct ast_str * attribute_malloc ast_str_create(size_t init_len), { struct ast_str *buf; @@ -388,6 +408,7 @@ return buf; } ) +#endif /*! \brief Reset the content of a dynamic string. * Useful before a series of ast_str_append. @@ -665,8 +686,14 @@ * through calling one of the other functions or macros defined in this * file. */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +int __attribute__((format(printf, 4, 0))) __ast_debug_str_helper(struct ast_str **buf, size_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, size_t max_len, int append, const char *fmt, va_list ap); +#endif char *__ast_str_helper2(struct ast_str **buf, size_t max_len, const char *src, size_t maxsrc, int append, int escapecommas); Index: main/hashtab.c =================================================================== --- main/hashtab.c (revision 180844) +++ main/hashtab.c (working copy) @@ -38,7 +38,13 @@ #include "asterisk/linkedlists.h" #include "asterisk/hashtab.h" + +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +static void _ast_hashtab_resize(struct ast_hashtab *tab, const char *file, int lineno, const char *func); +#define ast_hashtab_resize(a) _ast_hashtab_resize(a,__FILE__, __LINE__, __PRETTY_FUNCTION__) +#else static void ast_hashtab_resize(struct ast_hashtab *tab); +#endif static void *ast_hashtab_lookup_internal(struct ast_hashtab *tab, const void *obj, unsigned int h); /* some standard, default routines for general use */ @@ -264,7 +270,11 @@ return ht; } +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +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) +#else struct ast_hashtab *ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_func)(const void *obj)) +#endif { struct ast_hashtab *ht; unsigned int i; @@ -272,7 +282,13 @@ if (!(ht = ast_calloc(1, sizeof(*ht)))) return NULL; - if (!(ht->array = ast_calloc(tab->hash_tab_size, sizeof(*(ht->array))))) { + if (!(ht->array = +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + __ast_calloc(tab->hash_tab_size, sizeof(*(ht->array)), file, lineno, func) +#else + ast_calloc(tab->hash_tab_size, sizeof(*(ht->array))) +#endif + )) { free(ht); return NULL; } @@ -295,7 +311,11 @@ while (b) { void *newobj = (*obj_dup_func)(b->object); if (newobj) +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + _ast_hashtab_insert_immediate_bucket(ht, newobj, i, file, lineno, func); +#else ast_hashtab_insert_immediate_bucket(ht, newobj, i); +#endif b = b->next; } } @@ -402,7 +422,11 @@ } } +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +int _ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func) +#else int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj) +#endif { unsigned int h; int res=0; @@ -415,7 +439,11 @@ h = (*tab->hash)(obj) % tab->hash_tab_size; - res = ast_hashtab_insert_immediate_bucket(tab,obj,h); +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + res = _ast_hashtab_insert_immediate_bucket(tab, obj, h, file, lineno, func); +#else + res = ast_hashtab_insert_immediate_bucket(tab, obj, h); +#endif if (tab->do_locking) ast_rwlock_unlock(&tab->lock); @@ -423,7 +451,11 @@ return res; } +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +int _ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h, const char *file, int lineno, const char *func) +#else int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj, unsigned int h) +#endif { int c; struct ast_hashtab_bucket *b; @@ -437,8 +469,13 @@ if (c + 1 > tab->largest_bucket_size) tab->largest_bucket_size = c + 1; - if (!(b = ast_calloc(1, sizeof(*b)))) - return 0; + if (!(b = +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + __ast_calloc(1, sizeof(*b), file, lineno, func) +#else + ast_calloc(1, sizeof(*b)) +#endif + )) return 0; b->object = obj; b->next = tab->array[h]; @@ -456,7 +493,11 @@ return 1; } +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +int _ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj, const char *file, int lineno, const char *func) +#else int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj) +#endif { /* check to see if the element is already there; insert only if it is not there. */ @@ -468,7 +509,11 @@ ast_rwlock_wrlock(&tab->lock); if (!ast_hashtab_lookup_bucket(tab, obj, &bucket)) { +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + int ret2 = _ast_hashtab_insert_immediate_bucket(tab, obj, bucket, file, lineno, func); +#else int ret2 = ast_hashtab_insert_immediate_bucket(tab, obj, bucket); +#endif if (tab->do_locking) ast_rwlock_unlock(&tab->lock); @@ -587,7 +632,11 @@ /* the insert operation calls this, and is wrlock'd when it does. */ /* if you want to call it, you should set the wrlock yourself */ -static void ast_hashtab_resize( struct ast_hashtab *tab) +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +static void _ast_hashtab_resize(struct ast_hashtab *tab, const char *file, int lineno, const char *func) +#else +static void ast_hashtab_resize(struct ast_hashtab *tab) +#endif { /* this function is called either internally, when the resize func returns 1, or externally by the user to force a resize of the hash table */ @@ -605,7 +654,13 @@ tab->array[i] = 0; /* erase old ptrs */ } free(tab->array); - if (!(tab->array = ast_calloc(newsize, sizeof(*(tab->array))))) + if (!(tab->array = +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + __ast_calloc(newsize, sizeof(*(tab->array)), file, lineno, func) +#else + ast_calloc(newsize, sizeof(*(tab->array))) +#endif + )) return; /* now sort the buckets into their rightful new slots */ @@ -631,12 +686,22 @@ } } +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +struct ast_hashtab_iter *_ast_hashtab_start_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func) +#else struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab) +#endif { /* returns an iterator */ struct ast_hashtab_iter *it; - if (!(it = ast_calloc(1, sizeof(*it)))) + if (!(it = +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + __ast_calloc(1, sizeof(*it), file, lineno, func) +#else + ast_calloc(1, sizeof(*it)) +#endif + )) return NULL; it->next = tab->tlist; @@ -648,12 +713,22 @@ } /* use this function to get a write lock */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +struct ast_hashtab_iter *_ast_hashtab_start_write_traversal(struct ast_hashtab *tab, const char *file, int lineno, const char *func) +#else struct ast_hashtab_iter *ast_hashtab_start_write_traversal(struct ast_hashtab *tab) +#endif { /* returns an iterator */ struct ast_hashtab_iter *it; - if (!(it = ast_calloc(1, sizeof(*it)))) + if (!(it = +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + __ast_calloc(1, sizeof(*it), file, lineno, func) +#else + ast_calloc(1, sizeof(*it)) +#endif + )) return NULL; it->next = tab->tlist; Index: main/astobj2.c =================================================================== --- main/astobj2.c (revision 180844) +++ main/astobj2.c (working copy) @@ -138,7 +138,7 @@ static void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn); static struct ao2_container *__ao2_container_alloc(struct ao2_container *c, const uint n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn); -static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data); +static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func); static void *__ao2_callback(struct ao2_container *c, const enum search_flags flags, void *cb_fn, void *arg, void *data, enum ao2_callback_type type, char *tag, char *file, int line, const char *funcname); @@ -486,13 +486,13 @@ * link an object to a container */ -static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data) +static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func) { int i; /* create a new list entry */ struct bucket_list *p; struct astobj2 *obj = INTERNAL_OBJ(user_data); - + if (!obj) return NULL; @@ -518,8 +518,8 @@ void *_ao2_link_debug(struct ao2_container *c, void *user_data, char *tag, char *file, int line, const char *funcname) { - struct bucket_list *p = __ao2_link(c, user_data); - + struct bucket_list *p = __ao2_link(c, user_data, file, line, funcname); + if (p) { _ao2_ref_debug(user_data, +1, tag, file, line, funcname); ao2_unlock(c); @@ -529,8 +529,8 @@ void *_ao2_link(struct ao2_container *c, void *user_data) { - struct bucket_list *p = __ao2_link(c, user_data); - + struct bucket_list *p = __ao2_link(c, user_data, __FILE__, __LINE__, __PRETTY_FUNCTION__); + if (p) { _ao2_ref(user_data, +1); ao2_unlock(c); Index: main/heap.c =================================================================== --- main/heap.c (revision 180844) +++ main/heap.c (working copy) @@ -107,8 +107,13 @@ return 0; } +#ifdef MALLOC_DEBUG +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) +#else struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_fn, ssize_t index_offset) +#endif { struct ast_heap *h; @@ -121,7 +126,13 @@ init_height = 8; } - if (!(h = ast_calloc(1, sizeof(*h)))) { + if (!(h = +#ifdef MALLOC_DEBUG + __ast_calloc(1, sizeof(*h), file, lineno, func) +#else + ast_calloc(1, sizeof(*h)) +#endif + )) { return NULL; } @@ -129,7 +140,13 @@ h->index_offset = index_offset; h->avail_len = (1 << init_height) - 1; - if (!(h->heap = ast_calloc(1, h->avail_len * sizeof(void *)))) { + if (!(h->heap = +#ifdef MALLOC_DEBUG + __ast_calloc(1, h->avail_len * sizeof(void *), file, lineno, func) +#else + ast_calloc(1, h->avail_len * sizeof(void *)) +#endif + )) { ast_free(h); return NULL; } @@ -154,11 +171,21 @@ /*! * \brief Add a row of additional storage for the heap. */ -static int grow_heap(struct ast_heap *h) +static int grow_heap(struct ast_heap *h +#ifdef MALLOC_DEBUG +, const char *file, int lineno, const char *func +#endif +) { h->avail_len = h->avail_len * 2 + 1; - if (!(h->heap = ast_realloc(h->heap, h->avail_len * sizeof(void *)))) { + if (!(h->heap = +#ifdef MALLOC_DEBUG + __ast_realloc(h->heap, h->avail_len * sizeof(void *), file, lineno, func) +#else + ast_realloc(h->heap, h->avail_len * sizeof(void *)) +#endif + )) { h->cur_len = h->avail_len = 0; return -1; } @@ -202,11 +229,19 @@ } } +#ifdef MALLOC_DEBUG +int _ast_heap_push(struct ast_heap *h, void *elm, const char *file, int lineno, const char *func) +#else int ast_heap_push(struct ast_heap *h, void *elm) +#endif { int i; - if (h->cur_len == h->avail_len && grow_heap(h)) { + if (h->cur_len == h->avail_len && grow_heap(h +#ifdef MALLOC_DEBUG + , file, lineno, func +#endif + )) { return -1; } Index: main/strings.c =================================================================== --- main/strings.c (revision 180844) +++ main/strings.c (working copy) @@ -48,8 +48,13 @@ * ast_str_append_va(...) */ +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) +int __ast_debug_str_helper(struct ast_str **buf, size_t max_len, + int append, const char *fmt, va_list ap, const char *file, int lineno, const char *function) +#else int __ast_str_helper(struct ast_str **buf, size_t max_len, int append, const char *fmt, va_list ap) +#endif { int res, need; int offset = (append && (*buf)->__AST_STR_LEN) ? (*buf)->__AST_STR_USED : 0; @@ -80,7 +85,13 @@ if (0) { /* debugging */ ast_verbose("extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need); } - if (ast_str_make_space(buf, need)) { + if ( +#if (defined(MALLOC_DEBUG) && !defined(STANDALONE)) + _ast_str_make_space(buf, need, file, lineno, function) +#else + ast_str_make_space(buf, need) +#endif + ) { ast_verbose("failed to extend from %d to %d\n", (int)(*buf)->__AST_STR_LEN, need); return AST_DYNSTR_BUILD_FAILED; }