--- include/asterisk/utils.h 17 Oct 2004 22:13:05 -0000 1.10 +++ include/asterisk/utils.h 28 Oct 2004 11:12:06 -0000 @@ -15,7 +15,10 @@ #include #include #include +#include +#include #include +#include static inline int ast_strlen_zero(const char *s) { @@ -54,5 +57,111 @@ #endif /* __linux__ */ extern char *ast_strcasestr(const char *, const char *); + +#define AST_LIKELY(expr) (__builtin_expect(expr, 1)) +#define AST_UNLIKELY(expr) (__builtin_expect(expr, 0)) + +//! Allocate initalised memory +/* + * \param len bytes of memory to allocate + * + * Allocates len bytes memory using malloc(), and initialises it to 0. + * This function abort()s if it fails to allocate the memory. If your + * code can handle not being allocated the memory, use ast_smalloc0() + * instead. + * + * Returns a pointer to the newly allocated memory + * + */ + +#define ast_malloc0(len) _ast_malloc0(len, __FILE__, __LINE__) + +static inline void * +_ast_malloc0(size_t len, const char * file, int line) +{ + void * data = malloc(len); + if (AST_UNLIKELY(data == NULL)) { + ast_log(LOG_ERROR, "Failed to allocate %d bytes of memory at %s:%d\n", len, file, line); + abort(); + } + memset(data, 0, len); + return data; +} + +//! Allocate initalised memory +/* + * \param len bytes of memory to allocate + * + * Allocates len bytes memory using malloc(), and initialises it to 0. + * This function returns NULL if it fails to allocate the memory. If your + * code can't handle not being allocated the memory, use ast_malloc0() + * instead. + * + * Returns a pointer to the newly allocated memory + * + */ + +#define ast_smalloc0(len) _ast_smalloc0(len, __FILE__, __LINE__) + +static inline void * +_ast_smalloc0(size_t len, const char * file, int line) +{ + void * data = malloc(len); + if (AST_UNLIKELY(data == NULL)) { + ast_log(LOG_NOTICE, "Failed to allocate %d bytes of memory at %s:%d\n", len, file, line); + return NULL; + } + memset(data, 0, len); + return data; +} + + +//! Allocate memory elements +/* + * \param e_type type of element + * \param e_count number of elements + * + * Allocates e_count elements of type e_type, allocated using ast_malloc0(). + * + * Returns pointer to the allocated memory, or NULL if allocation failed. + * + */ + +#define ast_new0(type, count) \ + ((type*)ast_malloc0(((size_t)sizeof(type)) * ((size_t)(count)))) + +//! Allocate memory elements +/* + * \param e_type type of element + * \param e_count number of elements + * + * Allocates e_count elements of type e_type, allocated using ast_smalloc0(). + * + * Returns pointer to the allocated memory. + * + */ + +#define ast_snew0(type, count) \ + ((type*)ast_smalloc0(((size_t)sizeof(type)) * ((size_t)(count)))) + + +//! Frees allocated memory +/* + * \param ptr Pointer to allocated memory + * + * Your bog standard free() with a quick check for NULL pointer. + * + */ + +static inline void _ast_free(void * ptr, const char * filename, int line) +{ + if (AST_UNLIKELY(ptr == NULL)) { + ast_log(LOG_ERROR, "Tried to free NULL pointer at %s:%d\n", filename, line); + return; + } + free(ptr); +} + +#define ast_free(ptr) _ast_free(ptr, __FILE__, __LINE__) #endif