Index: include/asterisk/utils.h =================================================================== RCS file: /usr/cvsroot/asterisk/include/asterisk/utils.h,v retrieving revision 1.51 diff -u -r1.51 utils.h --- include/asterisk/utils.h 8 Nov 2005 04:13:19 -0000 1.51 +++ include/asterisk/utils.h 26 Nov 2005 16:43:14 -0000 @@ -25,6 +25,7 @@ #include "asterisk/compat.h" +#include #include #include /* we want to override inet_ntoa */ #include @@ -33,6 +34,7 @@ #include "asterisk/lock.h" #include "asterisk/time.h" #include "asterisk/strings.h" +#include "asterisk/logger.h" /*! \note \verbatim @@ -235,4 +237,103 @@ int getloadavg(double *list, int nelem); #endif +#define ast_malloc(len) \ + ({ \ + (_ast_malloc((len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \ + }) + +AST_INLINE_API( +void *_ast_malloc(size_t len, const char *file, int lineno, const char *func), +{ + void *p; + + p = malloc(len); + + if (!p) + ast_log(LOG_ERROR, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int)len, func, lineno, file); + + return p; +} +) + +#define ast_calloc(num, len) \ + ({ \ + (_ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \ + }) + +AST_INLINE_API( +void *_ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func), +{ + void *p; + + p = calloc(num, len); + + if (!p) + ast_log(LOG_ERROR, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int)len, func, lineno, file); + + return p; +} +) + +#define ast_realloc(p, len) \ + ({ \ + (_ast_calloc((p), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \ + }) + +AST_INLINE_API( +void *_ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func), +{ + void *newp; + + newp = realloc(p, len); + + if (!newp) + ast_log(LOG_ERROR, "Memory Allocation Failure - '%d' bytes in function %s at line %d of %s\n", (int)len, func, lineno, file); + + return newp; +} +) + +#define ast_strdup(str) \ + ({ \ + (_ast_strdup((str), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \ + }) + +AST_INLINE_API( +void *_ast_strdup(const char *str, const char *file, int lineno, const char *func), +{ + char *newstr = NULL; + + if (str) { + newstr = strdup(str); + + if (!newstr) + ast_log(LOG_ERROR, "Memory Allocation Failure - Could not duplicate '%s' in function %s at line %d of %s\n", str, func, lineno, file); + } + + return newstr; +} +) + +#define ast_strndup(str, len) \ + ({ \ + (_ast_strndup((str), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)); \ + }) + +AST_INLINE_API( +void *_ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func), +{ + char *newstr = NULL; + + if (str) { + newstr = strndup(str, len); + + if (!newstr) + ast_log(LOG_ERROR, "Memory Allocation Failure - Could not duplicate '%d' bytes of '%s' in function %s at line %d of %s\n", (int)len, str, func, lineno, file); + } + + return newstr; +} +) + #endif /* _ASTERISK_UTILS_H */