• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef foopulsemacrohfoo
2 #define foopulsemacrohfoo
3 
4 /***
5   This file is part of PulseAudio.
6 
7   Copyright 2004-2006 Lennart Poettering
8 
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as published
11   by the Free Software Foundation; either version 2.1 of the License,
12   or (at your option) any later version.
13 
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public License
20   along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
21 ***/
22 
23 #include <sys/types.h>
24 #include <unistd.h>
25 #include <assert.h>
26 #include <limits.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdbool.h>
32 
33 #ifndef PACKAGE
34 #error "Please include config.h before including this file!"
35 #endif
36 
37 /* Rounds down */
PA_ALIGN_PTR(const void * p)38 static inline void* PA_ALIGN_PTR(const void *p) {
39     return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
40 }
41 
42 /* Rounds up */
PA_ALIGN(size_t l)43 static inline size_t PA_ALIGN(size_t l) {
44     return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
45 }
46 
47 #if defined(__GNUC__)
48     #define PA_UNUSED __attribute__ ((unused))
49 #else
50     #define PA_UNUSED
51 #endif
52 
53 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
54 
55 #if defined(__GNUC__)
56     #define PA_DECLARE_ALIGNED(n,t,v)      t v __attribute__ ((aligned (n)))
57 #else
58     #define PA_DECLARE_ALIGNED(n,t,v)      t v
59 #endif
60 
61 #ifdef __GNUC__
62 #define typeof __typeof__
63 #endif
64 
65 /* The users of PA_MIN and PA_MAX, PA_CLAMP, PA_ROUND_UP should be
66  * aware that these macros on non-GCC executed code with side effects
67  * twice. It is thus considered misuse to use code with side effects
68  * as arguments to MIN and MAX. */
69 
70 #ifdef __GNUC__
71 #define PA_MAX(a,b)                             \
72     __extension__ ({                            \
73             typeof(a) _a = (a);                 \
74             typeof(b) _b = (b);                 \
75             _a > _b ? _a : _b;                  \
76         })
77 #else
78 #define PA_MAX(a, b) ((a) > (b) ? (a) : (b))
79 #endif
80 
81 #ifdef __GNUC__
82 #define PA_MIN(a,b)                             \
83     __extension__ ({                            \
84             typeof(a) _a = (a);                 \
85             typeof(b) _b = (b);                 \
86             _a < _b ? _a : _b;                  \
87         })
88 #else
89 #define PA_MIN(a, b) ((a) < (b) ? (a) : (b))
90 #endif
91 
92 #ifdef __GNUC__
93 #define PA_ROUND_UP(a, b)                       \
94     __extension__ ({                            \
95             typeof(a) _a = (a);                 \
96             typeof(b) _b = (b);                 \
97             ((_a + _b - 1) / _b) * _b;          \
98         })
99 #else
100 #define PA_ROUND_UP(a, b) ((((a) + (b) - 1) / (b)) * (b))
101 #endif
102 
103 #ifdef __GNUC__
104 #define PA_ROUND_DOWN(a, b)                     \
105     __extension__ ({                            \
106             typeof(a) _a = (a);                 \
107             typeof(b) _b = (b);                 \
108             (_a / _b) * _b;                     \
109         })
110 #else
111 #define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b))
112 #endif
113 
114 #ifdef __GNUC__
115 #define PA_CLIP_SUB(a, b)                       \
116     __extension__ ({                            \
117             typeof(a) _a = (a);                 \
118             typeof(b) _b = (b);                 \
119             _a > _b ? _a - _b : 0;              \
120         })
121 #else
122 #define PA_CLIP_SUB(a, b) ((a) > (b) ? (a) - (b) : 0)
123 #endif
124 
125 #ifdef __GNUC__
126 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
127 #else
128 #define PA_PRETTY_FUNCTION ""
129 #endif
130 
131 #define pa_return_if_fail(expr)                                         \
132     do {                                                                \
133         if (PA_UNLIKELY(!(expr))) {                                     \
134             pa_log_debug("Assertion '%s' failed at %s:%u, function %s.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
135             return;                                                     \
136         }                                                               \
137     } while(false)
138 
139 #define pa_return_val_if_fail(expr, val)                                \
140     do {                                                                \
141         if (PA_UNLIKELY(!(expr))) {                                     \
142             pa_log_debug("Assertion '%s' failed at %s:%u, function %s.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
143             return (val);                                               \
144         }                                                               \
145     } while(false)
146 
147 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
148 
149 /* pa_assert_se() is an assert which guarantees side effects of x,
150  * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
151 #ifndef __COVERITY__
152 #define pa_assert_se(expr)                                              \
153     do {                                                                \
154         if (PA_UNLIKELY(!(expr))) {                                     \
155             pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
156             abort();                                                    \
157         }                                                               \
158     } while (false)
159 #else
160 #define pa_assert_se(expr)                                              \
161     do {                                                                \
162         int _unique_var = (expr);                                       \
163         if (!_unique_var)                                               \
164             abort();                                                    \
165     } while (false)
166 #endif
167 
168 /* Does exactly nothing */
169 #define pa_nop() do {} while (false)
170 
171 /* pa_assert() is an assert that may be optimized away by defining
172  * NDEBUG. pa_assert_fp() is an assert that may be optimized away by
173  * defining FASTPATH. It is supposed to be used in inner loops. It's
174  * there for extra paranoia checking and should probably be removed in
175  * production builds. */
176 #ifdef NDEBUG
177 #define pa_assert(expr) pa_nop()
178 #define pa_assert_fp(expr) pa_nop()
179 #elif defined (FASTPATH)
180 #define pa_assert(expr) pa_assert_se(expr)
181 #define pa_assert_fp(expr) pa_nop()
182 #else
183 #define pa_assert(expr) pa_assert_se(expr)
184 #define pa_assert_fp(expr) pa_assert_se(expr)
185 #endif
186 
187 #ifdef NDEBUG
188 #define pa_assert_not_reached() abort()
189 #else
190 #define pa_assert_not_reached()                                         \
191     do {                                                                \
192         pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
193         abort();                                                        \
194     } while (false)
195 #endif
196 
197 /* A compile time assertion */
198 #define pa_assert_cc(expr)                         \
199     do {                                           \
200         switch (0) {                               \
201             case 0:                                \
202             case !!(expr):                         \
203                 ;                                  \
204         }                                          \
205     } while (false)
206 
207 #define PA_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
208 #define PA_UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
209 
210 #define PA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
211 #define PA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
212 
213 #define PA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
214 #define PA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
215 
216 #define PA_PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
217 #define PA_INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
218 
219 #ifdef OS_IS_WIN32
220 #define PA_PATH_SEP "\\"
221 #define PA_PATH_SEP_CHAR '\\'
222 #else
223 #define PA_PATH_SEP "/"
224 #define PA_PATH_SEP_CHAR '/'
225 #endif
226 
227 #if defined(__GNUC__) && defined(__ELF__)
228 
229 #define PA_WARN_REFERENCE(sym, msg)                  \
230     __asm__(".section .gnu.warning." #sym);          \
231     __asm__(".asciz \"" msg "\"");                   \
232     __asm__(".previous")
233 
234 #else
235 
236 #define PA_WARN_REFERENCE(sym, msg)
237 
238 #endif
239 
240 #if defined(__i386__) || defined(__x86_64__)
241 #define PA_DEBUG_TRAP __asm__("int $3")
242 #else
243 #define PA_DEBUG_TRAP raise(SIGTRAP)
244 #endif
245 
246 #define pa_memzero(x,l) (memset((x), 0, (l)))
247 #define pa_zero(x) (pa_memzero(&(x), sizeof(x)))
248 
249 #define PA_INT_TYPE_SIGNED(type) (!!((type) 0 > (type) -1))
250 
251 #define PA_INT_TYPE_HALF(type) ((type) 1 << (sizeof(type)*8 - 2))
252 
253 #define PA_INT_TYPE_MAX(type)                                          \
254     ((type) (PA_INT_TYPE_SIGNED(type)                                  \
255              ? (PA_INT_TYPE_HALF(type) - 1 + PA_INT_TYPE_HALF(type))   \
256              : (type) -1))
257 
258 #define PA_INT_TYPE_MIN(type)                                          \
259     ((type) (PA_INT_TYPE_SIGNED(type)                                  \
260              ? (-1 - PA_INT_TYPE_MAX(type))                            \
261              : (type) 0))
262 
263 /* The '#' preprocessor operator doesn't expand any macros that are in the
264  * parameter, which is why we need a separate macro for those cases where the
265  * parameter contains a macro that needs expanding. */
266 #define PA_STRINGIZE(x) #x
267 #define PA_EXPAND_AND_STRINGIZE(x) PA_STRINGIZE(x)
268 
269 /* We include this at the very last place */
270 #include <pulsecore/log.h>
271 
272 #endif
273