aboutsummaryrefslogtreecommitdiffstats
path: root/include/asterisk/utils.h
diff options
context:
space:
mode:
authorrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-01-10 23:51:42 +0000
committerrussell <russell@f38db490-d61c-443f-a65b-d21fe96a405b>2006-01-10 23:51:42 +0000
commit1eb7a651e24fdd3d56a76538f1c32140c66dd01c (patch)
treee7d6aff893ce1a4324b44e7f533df6451b7040a9 /include/asterisk/utils.h
parentdd867393028e4fa923e46c6b75033c7a8ca16c2f (diff)
Add wrappers for commonly used memory allocation functions. These wrappers
add an automatically generated Asterisk log message if the allocation fails for some reason. Otherwise, they are functionally the same, with the exception of ast_strdup and ast_strndup. These functions have the added ability to accept a NULL argument without error, which will just be ignored without generating an error. The coding guidelines have also been updated to reflect all of this information. (issue #4996) git-svn-id: http://svn.digium.com/svn/asterisk/trunk@7952 f38db490-d61c-443f-a65b-d21fe96a405b
Diffstat (limited to 'include/asterisk/utils.h')
-rw-r--r--include/asterisk/utils.h149
1 files changed, 149 insertions, 0 deletions
diff --git a/include/asterisk/utils.h b/include/asterisk/utils.h
index 8fd42445b..ed842e1b7 100644
--- a/include/asterisk/utils.h
+++ b/include/asterisk/utils.h
@@ -25,6 +25,7 @@
#include "asterisk/compat.h"
+#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h> /* we want to override inet_ntoa */
#include <netdb.h>
@@ -33,6 +34,7 @@
#include "asterisk/lock.h"
#include "asterisk/time.h"
#include "asterisk/strings.h"
+#include "asterisk/logger.h"
/*! \note
\verbatim
@@ -241,4 +243,151 @@ int getloadavg(double *list, int nelem);
long int ast_random(void);
#endif
+/*!
+ \brief A wrapper for malloc()
+
+ ast_malloc() is a wrapper for malloc() that will generate an Asterisk log
+ message in the case that the allocation fails.
+
+ The argument and return value are the same as malloc()
+*/
+#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;
+}
+)
+
+/*!
+ \brief A wrapper for calloc()
+
+ ast_calloc() is a wrapper for calloc() that will generate an Asterisk log
+ message in the case that the allocation fails.
+
+ The arguments and return value are the same as calloc()
+*/
+#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;
+}
+)
+
+/*!
+ \brief A wrapper for realloc()
+
+ ast_realloc() is a wrapper for realloc() that will generate an Asterisk log
+ message in the case that the allocation fails.
+
+ The arguments and return value are the same as realloc()
+*/
+#define ast_realloc(p, len) \
+ ({ \
+ (_ast_realloc((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;
+}
+)
+
+/*!
+ \brief A wrapper for strdup()
+
+ ast_strdup() is a wrapper for strdup() that will generate an Asterisk log
+ message in the case that the allocation fails.
+
+ ast_strdup(), unlike strdup(), can safely accept a NULL argument. If a NULL
+ argument is provided, ast_strdup will return NULL without generating any
+ kind of error log message.
+
+ The argument and return value are the same as strdup()
+*/
+#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;
+}
+)
+
+/*!
+ \brief A wrapper for strndup()
+
+ ast_strndup() is a wrapper for strndup() that will generate an Asterisk log
+ message in the case that the allocation fails.
+
+ ast_strndup(), unlike strndup(), can safely accept a NULL argument for the
+ string to duplicate. If a NULL argument is provided, ast_strdup will return
+ NULL without generating any kind of error log message.
+
+ The arguments and return value are the same as strndup()
+*/
+#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 */