Index: include/asterisk/astobj2.h =================================================================== --- include/asterisk/astobj2.h (revision 182959) +++ include/asterisk/astobj2.h (working copy) @@ -405,6 +405,11 @@ #define ao2_t_alloc(arg1, arg2, arg3) __ao2_alloc_debug((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__) #define ao2_alloc(arg1, arg2) __ao2_alloc_debug((arg1), (arg2), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) +#elif defined(MALLOC_DEBUG) && !defined(STANDALONE) + +#define ao2_t_alloc(arg1, arg2, arg3) __ao2_alloc_MALLOC_DEBUG((arg1), (arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define ao2_alloc(arg1, arg2) __ao2_alloc_MALLOC_DEBUG((arg1), (arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) + #else #define ao2_t_alloc(arg1,arg2,arg3) __ao2_alloc((arg1), (arg2)) @@ -412,6 +417,7 @@ #endif void *__ao2_alloc_debug(const size_t data_size, ao2_destructor_fn destructor_fn, char *tag, char *file, int line, const char *funcname); +void *__ao2_alloc_MALLOC_DEBUG(const size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int line, const char *funcname); void *__ao2_alloc(const size_t data_size, ao2_destructor_fn destructor_fn); @@ -444,6 +450,7 @@ #define ao2_ref(arg1,arg2) __ao2_ref((arg1), (arg2)) #endif int __ao2_ref_debug(void *o, int delta, char *tag, char *file, int line, const char *funcname); +int __ao2_ref_MALLOC_DEBUG(void *o, int delta, const char *file, int line, const char *funcname); int __ao2_ref(void *o, int delta); /*! \brief @@ -688,12 +695,18 @@ #ifdef REF_DEBUG #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc_debug((arg1), (arg2), (arg3), (arg4), __FILE__, __LINE__, __PRETTY_FUNCTION__) #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc_debug((arg1), (arg2), (arg3), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) +#elif defined(MALLOC_DEBUG) && !defined(STANDALONE) +#define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc_MALLOC_DEBUG((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc_MALLOC_DEBUG((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__) #else #define ao2_t_container_alloc(arg1,arg2,arg3,arg4) __ao2_container_alloc((arg1), (arg2), (arg3)) #define ao2_container_alloc(arg1,arg2,arg3) __ao2_container_alloc((arg1), (arg2), (arg3)) #endif struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn); +struct ao2_container *__ao2_container_alloc_MALLOC_DEBUG(const unsigned int n_buckets, + ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn, + const char *file, int line, const char *funcname); struct ao2_container *__ao2_container_alloc_debug(const unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn, char *tag, char *file, int line, const char *funcname); @@ -732,11 +745,15 @@ #define ao2_t_link(arg1, arg2, arg3) __ao2_link_debug((arg1), (arg2), (arg3), __FILE__, __LINE__, __PRETTY_FUNCTION__) #define ao2_link(arg1, arg2) __ao2_link_debug((arg1), (arg2), "", __FILE__, __LINE__, __PRETTY_FUNCTION__) +#elif defined(MALLOC_DEBUG) && !defined(STANDALONE) +#define ao2_t_link(arg1, arg2, arg3) __ao2_link_MALLOC_DEBUG((arg1), (arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) +#define ao2_link(arg1, arg2) __ao2_link_MALLOC_DEBUG((arg1), (arg2), __FILE__, __LINE__, __PRETTY_FUNCTION__) #else #define ao2_t_link(arg1, arg2, arg3) __ao2_link((arg1), (arg2)) #define ao2_link(arg1, arg2) __ao2_link((arg1), (arg2)) #endif void *__ao2_link_debug(struct ao2_container *c, void *new_obj, char *tag, char *file, int line, const char *funcname); +void *__ao2_link_MALLOC_DEBUG(struct ao2_container *c, void *new_obj, const char *file, int line, const char *funcname); void *__ao2_link(struct ao2_container *c, void *newobj); /*! Index: main/astobj2.c =================================================================== --- main/astobj2.c (revision 182959) +++ main/astobj2.c (working copy) @@ -135,7 +135,7 @@ /* the underlying functions common to debug and non-debug versions */ static int internal_ao2_ref(void *user_data, const int delta); -static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn); +static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int lineno, const char *func); static struct ao2_container *internal_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 *internal_ao2_link(struct ao2_container *c, void *user_data, const char *file, int line, const char *func); @@ -303,7 +303,7 @@ * We always alloc at least the size of a void *, * for debugging purposes. */ -static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn) +static void *internal_ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int lineno, const char *func) { /* allocation */ struct astobj2 *obj; @@ -311,7 +311,11 @@ if (data_size < sizeof(void *)) data_size = sizeof(void *); +#if defined(MALLOC_DEBUG) && !defined(STANDALONE) + obj = __ast_calloc(1, sizeof(*obj) + data_size, file, lineno, func); +#else obj = ast_calloc(1, sizeof(*obj) + data_size); +#endif if (obj == NULL) return NULL; @@ -338,11 +342,11 @@ void *obj; FILE *refo = fopen(REF_FILE,"a"); - obj = internal_ao2_alloc(data_size, destructor_fn); + obj = internal_ao2_alloc(data_size, destructor_fn, file, line, funcname); if (obj == NULL) return NULL; - + if (refo) { fprintf(refo, "%p =1 %s:%d:%s (%s)\n", obj, file, line, funcname, tag); fclose(refo); @@ -354,10 +358,15 @@ void *__ao2_alloc(size_t data_size, ao2_destructor_fn destructor_fn) { - return internal_ao2_alloc(data_size, destructor_fn); + return internal_ao2_alloc(data_size, destructor_fn, __FILE__, __LINE__, __PRETTY_FUNCTION__); } +void *__ao2_alloc_MALLOC_DEBUG(size_t data_size, ao2_destructor_fn destructor_fn, const char *file, int lineno, const char *func) +{ + return internal_ao2_alloc(data_size, destructor_fn, file, lineno, func); +} + /* internal callback to destroy a container. */ static void container_destruct(void *c); @@ -426,7 +435,7 @@ if (!c) return NULL; - + c->version = 1; /* 0 is a reserved value here */ c->n_buckets = n_buckets; c->hash_fn = hash_fn ? hash_fn : hash_zero; @@ -450,6 +459,18 @@ return internal_ao2_container_alloc(c, n_buckets, hash_fn, cmp_fn); } +struct ao2_container *__ao2_container_alloc_MALLOC_DEBUG(const unsigned int n_buckets, ao2_hash_fn *hash_fn, + ao2_callback_fn *cmp_fn, const char *file, int lineno, const char *funcname) +{ + /* XXX maybe consistency check on arguments ? */ + /* compute the container size */ + + size_t container_size = sizeof(struct ao2_container) + n_buckets * sizeof(struct bucket); + struct ao2_container *c = __ao2_alloc_MALLOC_DEBUG(container_size, container_destruct, file, lineno, funcname); + + return internal_ao2_container_alloc(c, n_buckets, hash_fn, cmp_fn); +} + struct ao2_container *__ao2_container_alloc(const unsigned int n_buckets, ao2_hash_fn *hash_fn, ao2_callback_fn *cmp_fn) { @@ -526,6 +547,17 @@ return p; } +void *__ao2_link_MALLOC_DEBUG(struct ao2_container *c, void *user_data, const char *file, int line, const char *funcname) +{ + struct bucket_list *p = internal_ao2_link(c, user_data, file, line, funcname); + + if (p) { + __ao2_ref(user_data, +1); + ao2_unlock(c); + } + return p; +} + void *__ao2_link(struct ao2_container *c, void *user_data) { struct bucket_list *p = internal_ao2_link(c, user_data, __FILE__, __LINE__, __PRETTY_FUNCTION__);