]> code.delx.au - pulseaudio/blob - src/pulsecore/macro.h
beefup proplist handling for sound events
[pulseaudio] / src / pulsecore / macro.h
1 #ifndef foopulsemacrohfoo
2 #define foopulsemacrohfoo
3
4 /* $Id$ */
5
6 /***
7 This file is part of PulseAudio.
8
9 Copyright 2004-2006 Lennart Poettering
10
11 PulseAudio is free software; you can redistribute it and/or modify
12 it under the terms of the GNU Lesser General Public License as published
13 by the Free Software Foundation; either version 2 of the License,
14 or (at your option) any later version.
15
16 PulseAudio is distributed in the hope that it will be useful, but
17 WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 General Public License for more details.
20
21 You should have received a copy of the GNU Lesser General Public License
22 along with PulseAudio; if not, write to the Free Software
23 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 USA.
25 ***/
26
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include <assert.h>
30 #include <limits.h>
31 #include <unistd.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include <pulsecore/log.h>
36 #include <pulse/gccmacro.h>
37
38 #ifndef PACKAGE
39 #error "Please include config.h before including this file!"
40 #endif
41
42 #ifndef PA_LIKELY
43 #ifdef __GNUC__
44 #define PA_LIKELY(x) (__builtin_expect(!!(x),1))
45 #define PA_UNLIKELY(x) (__builtin_expect((x),0))
46 #else
47 #define PA_LIKELY(x) (x)
48 #define PA_UNLIKELY(x) (x)
49 #endif
50 #endif
51
52 #if defined(PAGE_SIZE)
53 #define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
54 #elif defined(PAGESIZE)
55 #define PA_PAGE_SIZE ((size_t) PAGESIZE)
56 #elif defined(HAVE_SYSCONF)
57 #define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
58 #else
59 /* Let's hope it's like x86. */
60 #define PA_PAGE_SIZE ((size_t) 4096)
61 #endif
62
63 static inline size_t pa_align(size_t l) {
64 return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
65 }
66 #define PA_ALIGN(x) (pa_align(x))
67
68 static inline void* pa_page_align_ptr(const void *p) {
69 return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
70 }
71 #define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
72
73 static inline size_t pa_page_align(size_t l) {
74 return l & ~(PA_PAGE_SIZE-1);
75 }
76 #define PA_PAGE_ALIGN(x) (pa_page_align(x))
77
78 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
79
80 /* The users of PA_MIN and PA_MAX should be aware that these macros on
81 * non-GCC executed code with side effects twice. It is thus
82 * considered misuse to use code with side effects as arguments to MIN
83 * and MAX. */
84
85 #ifdef __GNUC__
86 #define PA_MAX(a,b) \
87 __extension__ ({ typeof(a) _a = (a); \
88 typeof(b) _b = (b); \
89 _a > _b ? _a : _b; \
90 })
91 #else
92 #define PA_MAX(a, b) ((a) > (b) ? (a) : (b))
93 #endif
94
95 #ifdef __GNUC__
96 #define PA_MIN(a,b) \
97 __extension__ ({ typeof(a) _a = (a); \
98 typeof(b) _b = (b); \
99 _a < _b ? _a : _b; \
100 })
101 #else
102 #define PA_MIN(a, b) ((a) < (b) ? (a) : (b))
103 #endif
104
105 #ifdef __GNUC__
106 #define PA_CLAMP(x, low, high) \
107 __extension__ ({ typeof(x) _x = (x); \
108 typeof(low) _low = (low); \
109 typeof(high) _high = (high); \
110 ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
111 })
112 #else
113 #define PA_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
114 #endif
115
116 #ifdef __GNUC__
117 #define PA_CLAMP_UNLIKELY(x, low, high) \
118 __extension__ ({ typeof(x) _x = (x); \
119 typeof(low) _low = (low); \
120 typeof(high) _high = (high); \
121 (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
122 })
123 #else
124 #define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
125 #endif
126
127 /* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
128 * make sense: we cannot know if it is more likely that the values is
129 * lower or greater than the boundaries.*/
130
131 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
132 #ifdef HAVE_STD_BOOL
133 typedef _Bool pa_bool_t;
134 #else
135 typedef int pa_bool_t;
136 #endif
137
138 #ifndef FALSE
139 #define FALSE ((pa_bool_t) 0)
140 #endif
141
142 #ifndef TRUE
143 #define TRUE (!FALSE)
144 #endif
145
146 #ifdef __GNUC__
147 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
148 #else
149 #define PA_PRETTY_FUNCTION ""
150 #endif
151
152 #define pa_return_if_fail(expr) \
153 do { \
154 if (PA_UNLIKELY(!(expr))) { \
155 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
156 return; \
157 } \
158 } while(FALSE)
159
160 #define pa_return_val_if_fail(expr, val) \
161 do { \
162 if (PA_UNLIKELY(!(expr))) { \
163 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
164 return (val); \
165 } \
166 } while(FALSE)
167
168 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
169
170 /* An assert which guarantees side effects of x, i.e. is never
171 * optimized away */
172 #define pa_assert_se(expr) \
173 do { \
174 if (PA_UNLIKELY(!(expr))) { \
175 pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
176 abort(); \
177 } \
178 } while (FALSE)
179
180 /* An assert that may be optimized away by defining NDEBUG */
181 #ifdef NDEBUG
182 #define pa_assert(expr) do {} while (FALSE)
183 #else
184 #define pa_assert(expr) pa_assert_se(expr)
185 #endif
186
187 #define pa_assert_not_reached() \
188 do { \
189 pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
190 abort(); \
191 } while (FALSE)
192
193 #define PA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
194 #define PA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
195
196 #define PA_PTR_TO_UINT32(p) ((uint32_t) PA_PTR_TO_UINT(p))
197 #define PA_UINT32_TO_PTR(u) PA_UINT_TO_PTR((uint32_t) u)
198
199 #define PA_PTR_TO_INT(p) ((int) PA_PTR_TO_UINT(p))
200 #define PA_INT_TO_PTR(u) PA_UINT_TO_PTR((int) u)
201
202 #define PA_PTR_TO_INT32(p) ((int32_t) PA_PTR_TO_UINT(p))
203 #define PA_INT32_TO_PTR(u) PA_UINT_TO_PTR((int32_t) u)
204
205 #ifdef OS_IS_WIN32
206 #define PA_PATH_SEP "\\"
207 #define PA_PATH_SEP_CHAR '\\'
208 #else
209 #define PA_PATH_SEP "/"
210 #define PA_PATH_SEP_CHAR '/'
211 #endif
212
213 #ifdef __GNUC__
214
215 #define PA_WARN_REFERENCE(sym, msg) \
216 __asm__(".section .gnu.warning." #sym); \
217 __asm__(".asciz \"" msg "\""); \
218 __asm__(".previous")
219
220 #else
221
222 #define PA_WARN_REFERENCE(sym, msg)
223
224 #endif
225
226 #endif