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