remove PA_CLAMP_LIKELY macro because it doesn't really make sense.
[profile/ivi/pulseaudio-panda.git] / 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 #ifndef MAX
69 #define MAX(a, b) ((a) > (b) ? (a) : (b))
70 #endif
71
72 #ifndef MIN
73 #define MIN(a, b) ((a) < (b) ? (a) : (b))
74 #endif
75
76 #ifndef CLAMP
77 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
78 #endif
79
80 #define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
81 /* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
82  * make sense: we cannot know if it is more likely that the values is
83  * lower or greater than the boundaries.*/
84
85 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
86 #ifdef HAVE_STD_BOOL
87 typedef _Bool pa_bool_t;
88 #else
89 typedef int pa_bool_t;
90 #endif
91
92 #ifndef FALSE
93 #define FALSE ((pa_bool_t) 0)
94 #endif
95
96 #ifndef TRUE
97 #define TRUE (!FALSE)
98 #endif
99
100 #ifdef __GNUC__
101 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
102 #else
103 #define PA_PRETTY_FUNCTION ""
104 #endif
105
106 #define pa_return_if_fail(expr) \
107     do { \
108         if (!(expr)) { \
109             pa_log_debug("%s: Assertion <%s> failed.\n", PA_PRETTY_FUNCTION, #expr ); \
110             return; \
111         } \
112     } while(0)
113
114 #define pa_return_val_if_fail(expr, val) \
115     do { \
116         if (!(expr)) { \
117             pa_log_debug("%s: Assertion <%s> failed.\n", PA_PRETTY_FUNCTION, #expr ); \
118             return (val); \
119         } \
120     } while(0)
121
122 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
123
124 #define pa_assert assert
125
126 #define pa_assert_not_reached() pa_assert(!"Should not be reached.")
127
128 /* An assert which guarantees side effects of x */
129 #ifdef NDEBUG
130 #define pa_assert_se(x) x
131 #else
132 #define pa_assert_se(x) pa_assert(x)
133 #endif
134
135 #define PA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
136 #define PA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
137
138 #define PA_PTR_TO_UINT32(p) ((uint32_t) PA_PTR_TO_UINT(p))
139 #define PA_UINT32_TO_PTR(u) PA_UINT_TO_PTR((uint32_t) u)
140
141 #define PA_PTR_TO_INT(p) ((int) PA_PTR_TO_UINT(p))
142 #define PA_INT_TO_PTR(u) PA_UINT_TO_PTR((int) u)
143
144 #define PA_PTR_TO_INT32(p) ((int32_t) PA_PTR_TO_UINT(p))
145 #define PA_INT32_TO_PTR(u) PA_UINT_TO_PTR((int32_t) u)
146
147 #ifdef OS_IS_WIN32
148 #define PA_PATH_SEP "\\"
149 #define PA_PATH_SEP_CHAR '\\'
150 #else
151 #define PA_PATH_SEP "/"
152 #define PA_PATH_SEP_CHAR '/'
153 #endif
154
155 #endif