default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / macro.h
blob1207a108215bb4be096d46718066f9268b30a7be
1 #ifndef foopulsemacrohfoo
2 #define foopulsemacrohfoo
4 /***
5 This file is part of PulseAudio.
7 Copyright 2004-2006 Lennart Poettering
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.
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.
19 You should have received a copy of the GNU Lesser General Public License
20 along with PulseAudio; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include <assert.h>
28 #include <limits.h>
29 #include <unistd.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
34 #ifndef PACKAGE
35 #error "Please include config.h before including this file!"
36 #endif
38 #ifndef PA_LIKELY
39 #ifdef __GNUC__
40 #define PA_LIKELY(x) (__builtin_expect(!!(x),1))
41 #define PA_UNLIKELY(x) (__builtin_expect(!!(x),0))
42 #else
43 #define PA_LIKELY(x) (x)
44 #define PA_UNLIKELY(x) (x)
45 #endif
46 #endif
48 #if defined(PAGE_SIZE)
49 #define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
50 #elif defined(PAGESIZE)
51 #define PA_PAGE_SIZE ((size_t) PAGESIZE)
52 #elif defined(HAVE_SYSCONF)
53 #define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
54 #else
55 /* Let's hope it's like x86. */
56 #define PA_PAGE_SIZE ((size_t) 4096)
57 #endif
59 /* Rounds down */
60 static inline void* PA_ALIGN_PTR(const void *p) {
61 return (void*) (((size_t) p) & ~(sizeof(void*) - 1));
64 /* Rounds up */
65 static inline size_t PA_ALIGN(size_t l) {
66 return ((l + sizeof(void*) - 1) & ~(sizeof(void*) - 1));
69 /* Rounds down */
70 static inline void* PA_PAGE_ALIGN_PTR(const void *p) {
71 return (void*) (((size_t) p) & ~(PA_PAGE_SIZE - 1));
74 /* Rounds up */
75 static inline size_t PA_PAGE_ALIGN(size_t l) {
76 return (l + PA_PAGE_SIZE - 1) & ~(PA_PAGE_SIZE - 1);
79 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
81 #if defined(__GNUC__)
82 #define PA_DECLARE_ALIGNED(n,t,v) t v __attribute__ ((aligned (n)))
83 #else
84 #define PA_DECLARE_ALIGNED(n,t,v) t v
85 #endif
87 /* The users of PA_MIN and PA_MAX, PA_CLAMP, PA_ROUND_UP should be
88 * aware that these macros on non-GCC executed code with side effects
89 * twice. It is thus considered misuse to use code with side effects
90 * as arguments to MIN and MAX. */
92 #ifdef __GNUC__
93 #define PA_MAX(a,b) \
94 __extension__ ({ \
95 typeof(a) _a = (a); \
96 typeof(b) _b = (b); \
97 _a > _b ? _a : _b; \
99 #else
100 #define PA_MAX(a, b) ((a) > (b) ? (a) : (b))
101 #endif
103 #ifdef __GNUC__
104 #define PA_MIN(a,b) \
105 __extension__ ({ \
106 typeof(a) _a = (a); \
107 typeof(b) _b = (b); \
108 _a < _b ? _a : _b; \
110 #else
111 #define PA_MIN(a, b) ((a) < (b) ? (a) : (b))
112 #endif
114 #ifdef __GNUC__
115 #define PA_CLAMP(x, low, high) \
116 __extension__ ({ \
117 typeof(x) _x = (x); \
118 typeof(low) _low = (low); \
119 typeof(high) _high = (high); \
120 ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
122 #else
123 #define PA_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
124 #endif
126 #ifdef __GNUC__
127 #define PA_CLAMP_UNLIKELY(x, low, high) \
128 __extension__ ({ \
129 typeof(x) _x = (x); \
130 typeof(low) _low = (low); \
131 typeof(high) _high = (high); \
132 (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
134 #else
135 #define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
136 #endif
138 /* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
139 * make sense: we cannot know if it is more likely that the values is
140 * lower or greater than the boundaries.*/
142 #ifdef __GNUC__
143 #define PA_ROUND_UP(a, b) \
144 __extension__ ({ \
145 typeof(a) _a = (a); \
146 typeof(b) _b = (b); \
147 ((_a + _b - 1) / _b) * _b; \
149 #else
150 #define PA_ROUND_UP(a, b) ((((a) + (b) - 1) / (b)) * (b))
151 #endif
153 #ifdef __GNUC__
154 #define PA_ROUND_DOWN(a, b) \
155 __extension__ ({ \
156 typeof(a) _a = (a); \
157 typeof(b) _b = (b); \
158 (_a / _b) * _b; \
160 #else
161 #define PA_ROUND_DOWN(a, b) (((a) / (b)) * (b))
162 #endif
164 #ifdef __GNUC__
165 #define PA_CLIP_SUB(a, b) \
166 __extension__ ({ \
167 typeof(a) _a = (a); \
168 typeof(b) _b = (b); \
169 _a > _b ? _a - _b : 0; \
171 #else
172 #define PA_CLIP_SUB(a, b) ((a) > (b) ? (a) - (b) : 0)
173 #endif
175 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
176 #ifdef HAVE_STD_BOOL
177 typedef _Bool pa_bool_t;
178 #else
179 typedef int pa_bool_t;
180 #endif
182 #ifndef FALSE
183 #define FALSE ((pa_bool_t) 0)
184 #endif
186 #ifndef TRUE
187 #define TRUE (!FALSE)
188 #endif
190 #ifdef __GNUC__
191 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
192 #else
193 #define PA_PRETTY_FUNCTION ""
194 #endif
196 #define pa_return_if_fail(expr) \
197 do { \
198 if (PA_UNLIKELY(!(expr))) { \
199 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
200 return; \
202 } while(FALSE)
204 #define pa_return_val_if_fail(expr, val) \
205 do { \
206 if (PA_UNLIKELY(!(expr))) { \
207 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
208 return (val); \
210 } while(FALSE)
212 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
214 /* pa_assert_se() is an assert which guarantees side effects of x,
215 * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
216 #define pa_assert_se(expr) \
217 do { \
218 if (PA_UNLIKELY(!(expr))) { \
219 pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
220 abort(); \
222 } while (FALSE)
224 /* Does exactly nothing */
225 #define pa_nop() do {} while (FALSE)
227 /* pa_assert() is an assert that may be optimized away by defining
228 * NDEBUG. pa_assert_fp() is an assert that may be optimized away by
229 * defining FASTPATH. It is supposed to be used in inner loops. It's
230 * there for extra paranoia checking and should probably be removed in
231 * production builds. */
232 #ifdef NDEBUG
233 #define pa_assert(expr) pa_nop()
234 #define pa_assert_fp(expr) pa_nop()
235 #elif defined (FASTPATH)
236 #define pa_assert(expr) pa_assert_se(expr)
237 #define pa_assert_fp(expr) pa_nop()
238 #else
239 #define pa_assert(expr) pa_assert_se(expr)
240 #define pa_assert_fp(expr) pa_assert_se(expr)
241 #endif
243 #ifdef NDEBUG
244 #define pa_assert_not_reached() pa_nop()
245 #else
246 #define pa_assert_not_reached() \
247 do { \
248 pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
249 abort(); \
250 } while (FALSE)
251 #endif
253 /* A compile time assertion */
254 #define pa_assert_cc(expr) \
255 do { \
256 switch (0) { \
257 case 0: \
258 case !!(expr): \
261 } while (FALSE)
263 #define PA_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
264 #define PA_UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
266 #define PA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
267 #define PA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
269 #define PA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
270 #define PA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
272 #define PA_PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
273 #define PA_INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
275 #ifdef OS_IS_WIN32
276 #define PA_PATH_SEP "\\"
277 #define PA_PATH_SEP_CHAR '\\'
278 #else
279 #define PA_PATH_SEP "/"
280 #define PA_PATH_SEP_CHAR '/'
281 #endif
283 #if defined(__GNUC__) && defined(__ELF__)
285 #define PA_WARN_REFERENCE(sym, msg) \
286 __asm__(".section .gnu.warning." #sym); \
287 __asm__(".asciz \"" msg "\""); \
288 __asm__(".previous")
290 #else
292 #define PA_WARN_REFERENCE(sym, msg)
294 #endif
296 #if defined(__i386__) || defined(__x86_64__)
297 #define PA_DEBUG_TRAP __asm__("int $3")
298 #else
299 #define PA_DEBUG_TRAP raise(SIGTRAP)
300 #endif
302 #define pa_memzero(x,l) (memset((x), 0, (l)))
303 #define pa_zero(x) (pa_memzero(&(x), sizeof(x)))
305 #define PA_INT_TYPE_SIGNED(type) (!!((type) 0 > (type) -1))
307 #define PA_INT_TYPE_MAX(type) \
308 ((type) (PA_INT_TYPE_SIGNED(type) \
309 ? ~(~(type) 0 << (8*sizeof(type)-1)) \
310 : (type) -1))
312 #define PA_INT_TYPE_MIN(type) \
313 ((type) (PA_INT_TYPE_SIGNED(type) \
314 ? (~(type) 0 << (8*sizeof(type)-1)) \
315 : (type) 0))
317 /* We include this at the very last place */
318 #include <pulsecore/log.h>
320 #endif