]> code.delx.au - pulseaudio/blob - src/pulsecore/macro.h
Merge commit 'origin/master-tx'
[pulseaudio] / src / pulsecore / macro.h
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, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 USA.
23 ***/
24
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
33 #include <pulse/gccmacro.h>
34
35 #ifndef PACKAGE
36 #error "Please include config.h before including this file!"
37 #endif
38
39 #ifndef PA_LIKELY
40 #ifdef __GNUC__
41 #define PA_LIKELY(x) (__builtin_expect(!!(x),1))
42 #define PA_UNLIKELY(x) (__builtin_expect(!!(x),0))
43 #else
44 #define PA_LIKELY(x) (x)
45 #define PA_UNLIKELY(x) (x)
46 #endif
47 #endif
48
49 #if defined(PAGE_SIZE)
50 #define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
51 #elif defined(PAGESIZE)
52 #define PA_PAGE_SIZE ((size_t) PAGESIZE)
53 #elif defined(HAVE_SYSCONF)
54 #define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
55 #else
56 /* Let's hope it's like x86. */
57 #define PA_PAGE_SIZE ((size_t) 4096)
58 #endif
59
60 static inline size_t pa_align(size_t l) {
61 return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
62 }
63 #define PA_ALIGN(x) (pa_align(x))
64
65 static inline void* pa_page_align_ptr(const void *p) {
66 return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
67 }
68 #define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
69
70 static inline size_t pa_page_align(size_t l) {
71 return l & ~(PA_PAGE_SIZE-1);
72 }
73 #define PA_PAGE_ALIGN(x) (pa_page_align(x))
74
75 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
76
77 /* The users of PA_MIN and PA_MAX should be aware that these macros on
78 * non-GCC executed code with side effects twice. It is thus
79 * considered misuse to use code with side effects as arguments to MIN
80 * and MAX. */
81
82 #ifdef __GNUC__
83 #define PA_MAX(a,b) \
84 __extension__ ({ typeof(a) _a = (a); \
85 typeof(b) _b = (b); \
86 _a > _b ? _a : _b; \
87 })
88 #else
89 #define PA_MAX(a, b) ((a) > (b) ? (a) : (b))
90 #endif
91
92 #ifdef __GNUC__
93 #define PA_MIN(a,b) \
94 __extension__ ({ typeof(a) _a = (a); \
95 typeof(b) _b = (b); \
96 _a < _b ? _a : _b; \
97 })
98 #else
99 #define PA_MIN(a, b) ((a) < (b) ? (a) : (b))
100 #endif
101
102 #ifdef __GNUC__
103 #define PA_CLAMP(x, low, high) \
104 __extension__ ({ typeof(x) _x = (x); \
105 typeof(low) _low = (low); \
106 typeof(high) _high = (high); \
107 ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
108 })
109 #else
110 #define PA_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
111 #endif
112
113 #ifdef __GNUC__
114 #define PA_CLAMP_UNLIKELY(x, low, high) \
115 __extension__ ({ typeof(x) _x = (x); \
116 typeof(low) _low = (low); \
117 typeof(high) _high = (high); \
118 (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
119 })
120 #else
121 #define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
122 #endif
123
124 /* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
125 * make sense: we cannot know if it is more likely that the values is
126 * lower or greater than the boundaries.*/
127
128 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
129 #ifdef HAVE_STD_BOOL
130 typedef _Bool pa_bool_t;
131 #else
132 typedef int pa_bool_t;
133 #endif
134
135 #ifndef FALSE
136 #define FALSE ((pa_bool_t) 0)
137 #endif
138
139 #ifndef TRUE
140 #define TRUE (!FALSE)
141 #endif
142
143 #ifdef __GNUC__
144 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
145 #else
146 #define PA_PRETTY_FUNCTION ""
147 #endif
148
149 #define pa_return_if_fail(expr) \
150 do { \
151 if (PA_UNLIKELY(!(expr))) { \
152 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
153 return; \
154 } \
155 } while(FALSE)
156
157 #define pa_return_val_if_fail(expr, val) \
158 do { \
159 if (PA_UNLIKELY(!(expr))) { \
160 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
161 return (val); \
162 } \
163 } while(FALSE)
164
165 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
166
167 /* pa_assert_se() is an assert which guarantees side effects of x,
168 * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
169 #define pa_assert_se(expr) \
170 do { \
171 if (PA_UNLIKELY(!(expr))) { \
172 pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
173 abort(); \
174 } \
175 } while (FALSE)
176
177 /* Does exactly nothing */
178 #define pa_nop() do {} while (FALSE)
179
180 /* pa_assert() is an assert that may be optimized away by defining
181 * NDEBUG. pa_assert_fp() is an assert that may be optimized away by
182 * defining FASTPATH. It is supposed to be used in inner loops. It's
183 * there for extra paranoia checking and should probably be removed in
184 * production builds. */
185 #ifdef NDEBUG
186 #define pa_assert(expr) pa_nop()
187 #define pa_assert_fp(expr) pa_nop()
188 #elif defined (FASTPATH)
189 #define pa_assert(expr) pa_assert_se(expr)
190 #define pa_assert_fp(expr) pa_nop()
191 #else
192 #define pa_assert(expr) pa_assert_se(expr)
193 #define pa_assert_fp(expr) pa_assert_se(expr)
194 #endif
195
196 #define pa_assert_not_reached() \
197 do { \
198 pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
199 abort(); \
200 } while (FALSE)
201
202 #define PA_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
203 #define PA_UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))
204
205 #define PA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
206 #define PA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
207
208 #define PA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
209 #define PA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
210
211 #define PA_PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
212 #define PA_INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))
213
214 #ifdef OS_IS_WIN32
215 #define PA_PATH_SEP "\\"
216 #define PA_PATH_SEP_CHAR '\\'
217 #else
218 #define PA_PATH_SEP "/"
219 #define PA_PATH_SEP_CHAR '/'
220 #endif
221
222 #if defined(__GNUC__) && defined(__ELF__)
223
224 #define PA_WARN_REFERENCE(sym, msg) \
225 __asm__(".section .gnu.warning." #sym); \
226 __asm__(".asciz \"" msg "\""); \
227 __asm__(".previous")
228
229 #else
230
231 #define PA_WARN_REFERENCE(sym, msg)
232
233 #endif
234
235 #if defined(__i386__) || defined(__x86_64__)
236 #define PA_DEBUG_TRAP __asm__("int $3")
237 #else
238 #define PA_DEBUG_TRAP raise(SIGTRAP)
239 #endif
240
241 /* We include this at the very last place */
242 #include <pulsecore/log.h>
243
244 #endif