📄 utils.h
字号:
/* * Thread management support (should be moved to lock.h or a different header) */ #define AST_STACKSIZE 240 * 1024#if defined(LOW_MEMORY)#define AST_BACKGROUND_STACKSIZE 48 * 1024#else#define AST_BACKGROUND_STACKSIZE 240 * 1024#endifvoid ast_register_thread(char *name);void ast_unregister_thread(void *id);int ast_pthread_create_stack(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data, size_t stacksize, const char *file, const char *caller, int line, const char *start_fn);int ast_pthread_create_detached_stack(pthread_t *thread, pthread_attr_t *attr, void*(*start_routine)(void *), void *data, size_t stacksize, const char *file, const char *caller, int line, const char *start_fn);#define ast_pthread_create(a, b, c, d) \ ast_pthread_create_stack(a, b, c, d, \ 0, __FILE__, __FUNCTION__, __LINE__, #c)#define ast_pthread_create_detached(a, b, c, d) \ ast_pthread_create_detached_stack(a, b, c, d, \ 0, __FILE__, __FUNCTION__, __LINE__, #c)#define ast_pthread_create_background(a, b, c, d) \ ast_pthread_create_stack(a, b, c, d, \ AST_BACKGROUND_STACKSIZE, \ __FILE__, __FUNCTION__, __LINE__, #c)#define ast_pthread_create_detached_background(a, b, c, d) \ ast_pthread_create_detached_stack(a, b, c, d, \ AST_BACKGROUND_STACKSIZE, \ __FILE__, __FUNCTION__, __LINE__, #c)/* End of thread management support *//*! \brief Process a string to find and replace characters \param start The string to analyze \param find The character to find \param replace_with The character that will replace the one we are looking for*/char *ast_process_quotes_and_slashes(char *start, char find, char replace_with);long int ast_random(void);#define ast_free free/*! * \brief free() wrapper * * ast_free_ptr should be used when a function pointer for free() needs to be passed * as the argument to a function. Otherwise, astmm will cause seg faults. */#ifdef __AST_DEBUG_MALLOCstatic void ast_free_ptr(void *ptr) attribute_unused;static void ast_free_ptr(void *ptr){ ast_free(ptr);}#else#define ast_free_ptr ast_free#endif#ifndef __AST_DEBUG_MALLOC#define MALLOC_FAILURE_MSG \ ast_log(LOG_ERROR, "Memory Allocation Failure in function %s at line %d of %s\n", func, lineno, file);/*! * \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 * attribute_malloc _ast_malloc(size_t len, const char *file, int lineno, const char *func),{ void *p; if (!(p = malloc(len))) MALLOC_FAILURE_MSG; 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 * attribute_malloc _ast_calloc(size_t num, size_t len, const char *file, int lineno, const char *func),{ void *p; if (!(p = calloc(num, len))) MALLOC_FAILURE_MSG; return p;})/*! * \brief A wrapper for calloc() for use in cache pools * * ast_calloc_cache() is a wrapper for calloc() that will generate an Asterisk log * message in the case that the allocation fails. When memory debugging is in use, * the memory allocated by this function will be marked as 'cache' so it can be * distinguished from normal memory allocations. * * The arguments and return value are the same as calloc() */#define ast_calloc_cache(num, len) \ _ast_calloc((num), (len), __FILE__, __LINE__, __PRETTY_FUNCTION__)/*! * \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 * attribute_malloc _ast_realloc(void *p, size_t len, const char *file, int lineno, const char *func),{ void *newp; if (!(newp = realloc(p, len))) MALLOC_FAILURE_MSG; 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(char * attribute_malloc _ast_strdup(const char *str, const char *file, int lineno, const char *func),{ char *newstr = NULL; if (str) { if (!(newstr = strdup(str))) MALLOC_FAILURE_MSG; } 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(char * attribute_malloc _ast_strndup(const char *str, size_t len, const char *file, int lineno, const char *func),{ char *newstr = NULL; if (str) { if (!(newstr = strndup(str, len))) MALLOC_FAILURE_MSG; } return newstr;})/*! * \brief A wrapper for asprintf() * * ast_asprintf() is a wrapper for asprintf() that will generate an Asterisk log * message in the case that the allocation fails. * * The arguments and return value are the same as asprintf() */#define ast_asprintf(ret, fmt, ...) \ _ast_asprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, fmt, __VA_ARGS__)int __attribute__((format(printf, 5, 6))) _ast_asprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, ...);/*! * \brief A wrapper for vasprintf() * * ast_vasprintf() is a wrapper for vasprintf() that will generate an Asterisk log * message in the case that the allocation fails. * * The arguments and return value are the same as vasprintf() */#define ast_vasprintf(ret, fmt, ap) \ _ast_vasprintf((ret), __FILE__, __LINE__, __PRETTY_FUNCTION__, (fmt), (ap))AST_INLINE_API(__attribute__((format(printf, 5, 0)))int _ast_vasprintf(char **ret, const char *file, int lineno, const char *func, const char *fmt, va_list ap),{ int res; if ((res = vasprintf(ret, fmt, ap)) == -1) MALLOC_FAILURE_MSG; return res;})#else/* If astmm is in use, let it handle these. Otherwise, it will report that all allocations are coming from this header file */#define ast_malloc(a) malloc(a)#define ast_calloc(a,b) calloc(a,b)#define ast_realloc(a,b) realloc(a,b)#define ast_strdup(a) strdup(a)#define ast_strndup(a,b) strndup(a,b)#define ast_asprintf(a,b,...) asprintf(a,b,__VA_ARGS__)#define ast_vasprintf(a,b,c) vasprintf(a,b,c)#endif /* AST_DEBUG_MALLOC */#if !defined(ast_strdupa) && defined(__GNUC__)/*! \brief duplicate a string in memory from the stack \param s The string to duplicate This macro will duplicate the given string. It returns a pointer to the stack allocatted memory for the new string.*/#define ast_strdupa(s) \ (__extension__ \ ({ \ const char *__old = (s); \ size_t __len = strlen(__old) + 1; \ char *__new = __builtin_alloca(__len); \ memcpy (__new, __old, __len); \ __new; \ }))#endif/*! \brief Disable PMTU discovery on a socket \param sock The socket to manipulate \return Nothing On Linux, UDP sockets default to sending packets with the Dont Fragment (DF) bit set. This is supposedly done to allow the application to do PMTU discovery, but Asterisk does not do this. Because of this, UDP packets sent by Asterisk that are larger than the MTU of any hop in the path will be lost. This function can be called on a socket to ensure that the DF bit will not be set. */void ast_enable_packet_fragmentation(int sock);/*! \brief Recursively create directory path \param path The directory path to create \param mode The permissions with which to try to create the directory \return 0 on success or an error code otherwise Creates a directory path, creating parent directories as needed. */int ast_mkdir(const char *path, int mode);#define ARRAY_LEN(a) (sizeof(a) / sizeof(0[a]))#ifdef AST_DEVMODE#define ast_assert(a) _ast_assert(a, # a, __FILE__, __LINE__, __PRETTY_FUNCTION__)static void force_inline _ast_assert(int condition, const char *condition_str, const char *file, int line, const char *function){ if (__builtin_expect(!condition, 1)) { /* Attempt to put it into the logger, but hope that at least someone saw the * message on stderr ... */ ast_log(__LOG_ERROR, file, line, function, "FRACK!, Failed assertion %s (%d)\n", condition_str, condition); fprintf(stderr, "FRACK!, Failed assertion %s (%d) at line %d in %s of %s\n", condition_str, condition, line, function, file); /* Give the logger a chance to get the message out, just in case we abort(), or * Asterisk crashes due to whatever problem just happened after we exit ast_assert(). */ usleep(1);#ifdef DO_CRASH abort(); /* Just in case abort() doesn't work or something else super silly, * and for Qwell's amusement. */ *((int*)0)=0;#endif }}#else#define ast_assert(a)#endif#include "asterisk/strings.h"#endif /* _ASTERISK_UTILS_H */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -