dect
/
asterisk
Archived
13
0
Fork 0

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
This commit is contained in:
tilghman 2009-03-11 00:29:59 +00:00
parent 78ae846440
commit a914fe0511
8 changed files with 214 additions and 21 deletions

View File

@ -19,6 +19,10 @@
#include "asterisk/compat.h"
#ifdef MALLOC_DEBUG
#define REF_DEBUG
#endif
/*! \file
* \ref AstObj2
*

View File

@ -291,7 +291,12 @@ void ast_hashtab_destroy( struct ast_hashtab *tab, void (*objdestroyfunc)(void *
* \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 @@ int ast_hashtab_insert_immediate(struct ast_hashtab *tab, const void *obj);
* \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 @@ 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.
*/
#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_size( struct ast_hashtab *tab);
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 @@ void *ast_hashtab_remove_this_object(struct ast_hashtab *tab, void *obj);
/* ------------------ */
/*! \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);

View File

@ -97,8 +97,14 @@ typedef int (*ast_heap_cmp_fn)(void *elm1, void *elm2);
* \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 @@ struct ast_heap *ast_heap_destroy(struct ast_heap *h);
* \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

View File

@ -372,6 +372,26 @@ struct ast_str {
* \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),
{
@ -388,6 +408,7 @@ struct ast_str * attribute_malloc ast_str_create(size_t init_len),
return buf;
}
)
#endif
/*! \brief Reset the content of a dynamic string.
* Useful before a series of ast_str_append.
@ -665,8 +686,14 @@ enum {
* 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);

View File

@ -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);

View File

@ -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;

View File

@ -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;
}

View File

@ -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;
}