1 #ifndef foopulsemacrohfoo
2 #define foopulsemacrohfoo
7 This file is part of PulseAudio.
9 Copyright 2004-2006 Lennart Poettering
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.
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.
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
27 #include <sys/types.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/gccmacro.h>
39 #error "Please include config.h before including this file!"
42 #if defined(PAGE_SIZE)
43 #define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
44 #elif defined(PAGESIZE)
45 #define PA_PAGE_SIZE ((size_t) PAGESIZE)
46 #elif defined(HAVE_SYSCONF)
47 #define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
49 /* Let's hope it's like x86. */
50 #define PA_PAGE_SIZE ((size_t) 4096)
53 static inline size_t pa_align(size_t l) {
54 return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
56 #define PA_ALIGN(x) (pa_align(x))
58 static inline void* pa_page_align_ptr(const void *p) {
59 return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
61 #define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
63 static inline size_t pa_page_align(size_t l) {
64 return l & ~(PA_PAGE_SIZE-1);
66 #define PA_PAGE_ALIGN(x) (pa_page_align(x))
68 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
70 /* The users of PA_MIN and PA_MAX should be aware that these macros on
71 * non-GCC executed code with side effects twice. It is thus
72 * considered misuse to use code with side effects as arguments to MIN
77 __extension__ ({ typeof(a) _a = (a); \
82 #define PA_MAX(a, b) ((a) > (b) ? (a) : (b))
87 __extension__ ({ typeof(a) _a = (a); \
92 #define PA_MIN(a, b) ((a) < (b) ? (a) : (b))
96 #define PA_CLAMP(x, low, high) \
97 __extension__ ({ typeof(x) _x = (x); \
98 typeof(low) _low = (low); \
99 typeof(high) _high = (high); \
100 ((_x > _high) ? _high : ((_x < _low) ? _low : _x)); \
103 #define PA_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
107 #define PA_CLAMP_UNLIKELY(x, low, high) \
108 __extension__ ({ typeof(x) _x = (x); \
109 typeof(low) _low = (low); \
110 typeof(high) _high = (high); \
111 (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
114 #define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
117 /* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
118 * make sense: we cannot know if it is more likely that the values is
119 * lower or greater than the boundaries.*/
121 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
123 typedef _Bool pa_bool_t;
125 typedef int pa_bool_t;
129 #define FALSE ((pa_bool_t) 0)
133 #define TRUE (!FALSE)
137 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
139 #define PA_PRETTY_FUNCTION ""
142 #define pa_return_if_fail(expr) \
144 if (PA_UNLIKELY(!(expr))) { \
145 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
150 #define pa_return_val_if_fail(expr, val) \
152 if (PA_UNLIKELY(!(expr))) { \
153 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
158 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
160 /* An assert which guarantees side effects of x, i.e. is never
162 #define pa_assert_se(expr) \
164 if (PA_UNLIKELY(!(expr))) { \
165 pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
170 /* An assert that may be optimized away by defining NDEBUG */
172 #define pa_assert(expr) do {} while (FALSE)
174 #define pa_assert(expr) pa_assert_se(expr)
177 #define pa_assert_not_reached() \
179 pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
183 #define PA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
184 #define PA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
186 #define PA_PTR_TO_UINT32(p) ((uint32_t) PA_PTR_TO_UINT(p))
187 #define PA_UINT32_TO_PTR(u) PA_UINT_TO_PTR((uint32_t) u)
189 #define PA_PTR_TO_INT(p) ((int) PA_PTR_TO_UINT(p))
190 #define PA_INT_TO_PTR(u) PA_UINT_TO_PTR((int) u)
192 #define PA_PTR_TO_INT32(p) ((int32_t) PA_PTR_TO_UINT(p))
193 #define PA_INT32_TO_PTR(u) PA_UINT_TO_PTR((int32_t) u)
196 #define PA_PATH_SEP "\\"
197 #define PA_PATH_SEP_CHAR '\\'
199 #define PA_PATH_SEP "/"
200 #define PA_PATH_SEP_CHAR '/'
203 static inline const char *pa_strnull(const char *x) {
204 return x ? x : "(null)";