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