aboutsummaryrefslogtreecommitdiffstats
path: root/main
diff options
context:
space:
mode:
authortilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2009-03-11 00:29:59 +0000
committertilghman <tilghman@f38db490-d61c-443f-a65b-d21fe96a405b>2009-03-11 00:29:59 +0000
commita914fe05112d518c6abfbc042ae593386ff3d219 (patch)
treed9ce0c66f8edace25beb5e6d7b7032155aa9d5fc /main
parent78ae84644006651127e72a2b8a8e79a2b85a61db (diff)
Add MALLOC_DEBUG to various utility APIs, so that memory leaks can be tracked back to their source.
(related to issue #14636) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@181028 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'main')
-rw-r--r--main/astobj2.c14
-rw-r--r--main/hashtab.c91
-rw-r--r--main/heap.c45
-rw-r--r--main/strings.c13
4 files changed, 142 insertions, 21 deletions
diff --git a/main/astobj2.c b/main/astobj2.c
index 8e33f94ae..7dd4c7ec1 100644
--- a/main/astobj2.c
+++ b/main/astobj2.c
@@ -138,7 +138,7 @@ static int __ao2_ref(void *user_data, const int delta);
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 @@ struct bucket_list {
* 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 @@ static struct bucket_list *__ao2_link(struct ao2_container *c, void *user_data)
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_debug(struct ao2_container *c, void *user_data, char *tag, char
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);
diff --git a/main/hashtab.c b/main/hashtab.c
index 29e5ef856..78878260f 100644
--- a/main/hashtab.c
+++ b/main/hashtab.c
@@ -38,7 +38,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#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 @@ ast_hashtab_create
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 @@ struct ast_hashtab *ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_fun
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 @@ struct ast_hashtab *ast_hashtab_dup(struct ast_hashtab *tab, void *(*obj_dup_fun
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 @@ void ast_hashtab_destroy(struct ast_hashtab *tab, void (*objdestroyfunc)(void *o
}
}
+#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 @@ int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj)
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 @@ int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj)
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 @@ int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj
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 @@ int ast_hashtab_insert_immediate_bucket(struct ast_hashtab *tab, const void *obj
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 @@ int ast_hashtab_insert_safe(struct ast_hashtab *tab, const void *obj)
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 @@ int ast_hashtab_capacity( struct ast_hashtab *tab)
/* 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 @@ static void ast_hashtab_resize( struct ast_hashtab *tab)
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 @@ static void ast_hashtab_resize( struct ast_hashtab *tab)
}
}
+#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 @@ struct ast_hashtab_iter *ast_hashtab_start_traversal(struct ast_hashtab *tab)
}
/* 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;
diff --git a/main/heap.c b/main/heap.c
index 5e8a2baef..e3932d72f 100644
--- a/main/heap.c
+++ b/main/heap.c
@@ -107,8 +107,13 @@ int ast_heap_verify(struct ast_heap *h)
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 @@ struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_f
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 @@ struct ast_heap *ast_heap_create(unsigned int init_height, ast_heap_cmp_fn cmp_f
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 @@ struct ast_heap *ast_heap_destroy(struct ast_heap *h)
/*!
* \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 @@ static inline void max_heapify(struct ast_heap *h, int i)
}
}
+#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;
}
diff --git a/main/strings.c b/main/strings.c
index 5547e54a9..88c240ff5 100644
--- a/main/strings.c
+++ b/main/strings.c
@@ -48,8 +48,13 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
* 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 @@ int __ast_str_helper(struct ast_str **buf, size_t max_len,
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;
}