2 * Asterisk -- A telephony toolkit for Linux.
6 * Copyright (C) 2004 - 2005, Digium, Inc.
8 * This program is free software, distributed under the terms of
9 * the GNU General Public License
12 #ifndef _ASTERISK_UTIL_H
13 #define _ASTERISK_UTIL_H
15 #include <netinet/in.h>
18 #include <asterisk/lock.h>
22 It is very important to use only unsigned variables to hold
23 bit flags, as otherwise you can fall prey to the compiler's
24 sign-extension antics if you try to use the top two bits in
27 The flag macros below use a set of compiler tricks to verify
28 that the caller is using an "unsigned int" variable to hold
29 the flags, and nothing else. If the caller uses any other
30 type of variable, a warning message similar to this:
32 warning: comparison of distinct pointer types lacks cast
36 The "dummy" variable below is used to make these comparisons.
38 Also note that at -O2 or above, this type-safety checking
39 does _not_ produce any additional object code at all.
42 extern unsigned int __unsigned_int_flags_dummy;
44 #define ast_test_flag(p,flag) ({ \
45 typeof ((p)->flags) __p = (p)->flags; \
46 typeof (__unsigned_int_flags_dummy) __x = 0; \
47 (void) (&__p == &__x); \
48 ((p)->flags & (flag)); \
51 #define ast_set_flag(p,flag) do { \
52 typeof ((p)->flags) __p = (p)->flags; \
53 typeof (__unsigned_int_flags_dummy) __x = 0; \
54 (void) (&__p == &__x); \
55 ((p)->flags |= (flag)); \
58 #define ast_clear_flag(p,flag) do { \
59 typeof ((p)->flags) __p = (p)->flags; \
60 typeof (__unsigned_int_flags_dummy) __x = 0; \
61 (void) (&__p == &__x); \
62 ((p)->flags &= ~(flag)); \
65 #define ast_copy_flags(dest,src,flagz) do { \
66 typeof ((dest)->flags) __d = (dest)->flags; \
67 typeof ((src)->flags) __s = (src)->flags; \
68 typeof (__unsigned_int_flags_dummy) __x = 0; \
69 (void) (&__d == &__x); \
70 (void) (&__s == &__x); \
71 (dest)->flags &= ~(flagz); \
72 (dest)->flags |= ((src)->flags & (flagz)); \
75 #define ast_set2_flag(p,value,flag) do { \
76 typeof ((p)->flags) __p = (p)->flags; \
77 typeof (__unsigned_int_flags_dummy) __x = 0; \
78 (void) (&__p == &__x); \
80 (p)->flags |= (flag); \
82 (p)->flags &= ~(flag); \
85 /* Non-type checking variations for non-unsigned int flags. You
86 should only use non-unsigned int flags where required by
87 protocol etc and if you know what you're doing :) */
88 #define ast_test_flag_nonstd(p,flag) ({ \
89 ((p)->flags & (flag)); \
92 #define ast_set_flag_nonstd(p,flag) do { \
93 ((p)->flags |= (flag)); \
96 #define ast_clear_flag_nonstd(p,flag) do { \
97 ((p)->flags &= ~(flag)); \
100 #define ast_copy_flags_nonstd(dest,src,flagz) do { \
101 (dest)->flags &= ~(flagz); \
102 (dest)->flags |= ((src)->flags & (flagz)); \
105 #define ast_set2_flag_nonstd(p,value,flag) do { \
107 (p)->flags |= (flag); \
109 (p)->flags &= ~(flag); \
112 #define AST_FLAGS_ALL UINT_MAX
118 static inline int ast_strlen_zero(const char *s)
128 extern char *ast_strip(char *buf);
129 extern struct hostent *ast_gethostbyname(const char *host, struct ast_hostent *hp);
130 /* ast_md5_hash: Produces MD5 hash based on input string */
131 extern void ast_md5_hash(char *output, char *input);
132 extern int ast_base64encode(char *dst, unsigned char *src, int srclen, int max);
133 extern int ast_base64decode(unsigned char *dst, char *src, int max);
135 extern int test_for_thread_safety(void);
137 extern const char *ast_inet_ntoa(char *buf, int bufsiz, struct in_addr ia);
138 extern int ast_utils_init(void);
140 /* The realloca lets us ast_restrdupa(), but you can't mix any other ast_strdup calls! */
142 struct ast_realloca {
147 #define ast_restrdupa(ra, s) \
149 if ((ra)->ptr && strlen(s) + 1 < (ra)->alloclen) { \
150 strcpy((ra)->ptr, s); \
152 (ra)->ptr = alloca(strlen(s) + 1 - (ra)->alloclen); \
153 if ((ra)->ptr) (ra)->alloclen = strlen(s) + 1; \
161 #define inet_ntoa __dont__use__inet_ntoa__use__ast_inet_ntoa__instead__
163 #define AST_STACK_SIZE 128 * 1024
166 #define ast_pthread_create pthread_create
167 #define ast_strcasestr strcasestr
169 /* Linux threads have a default 2MB stack size. */
170 #ifndef PTHREAD_ATTR_STACKSIZE
171 #define PTHREAD_ATTR_STACKSIZE 2097152
172 #endif /* PTHREAD_ATTR_STACKSIZE */
173 extern int ast_pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*start_routine)(void *), void *data);
174 extern char *ast_strcasestr(const char *, const char *);
175 #endif /* __linux__ */