]> code.delx.au - pulseaudio/blob - src/pulsecore/macro.h
merge r2194 from prepare-0.9.10
[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 <pulsecore/gccmacro.h>
37
38 #ifndef PACKAGE
39 #error "Please include config.h before including this file!"
40 #endif
41
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)))
48 #else
49 /* Let's hope it's like x86. */
50 #define PA_PAGE_SIZE ((size_t) 4096)
51 #endif
52
53 static inline size_t pa_align(size_t l) {
54 return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
55 }
56 #define PA_ALIGN(x) (pa_align(x))
57
58 static inline void* pa_page_align_ptr(const void *p) {
59 return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
60 }
61 #define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
62
63 static inline size_t pa_page_align(size_t l) {
64 return l & ~(PA_PAGE_SIZE-1);
65 }
66 #define PA_PAGE_ALIGN(x) (pa_page_align(x))
67
68 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
69
70 #ifndef MAX
71 #define MAX(a, b) ((a) > (b) ? (a) : (b))
72 #endif
73
74 #ifndef MIN
75 #define MIN(a, b) ((a) < (b) ? (a) : (b))
76 #endif
77
78 #ifndef CLAMP
79 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
80 #endif
81
82 #define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
83 /* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
84 * make sense: we cannot know if it is more likely that the values is
85 * lower or greater than the boundaries.*/
86
87 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
88 #ifdef HAVE_STD_BOOL
89 typedef _Bool pa_bool_t;
90 #else
91 typedef int pa_bool_t;
92 #endif
93
94 #ifndef FALSE
95 #define FALSE ((pa_bool_t) 0)
96 #endif
97
98 #ifndef TRUE
99 #define TRUE (!FALSE)
100 #endif
101
102 #ifdef __GNUC__
103 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
104 #else
105 #define PA_PRETTY_FUNCTION ""
106 #endif
107
108 #define pa_return_if_fail(expr) \
109 do { \
110 if (PA_UNLIKELY(!(expr))) { \
111 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
112 return; \
113 } \
114 } while(FALSE)
115
116 #define pa_return_val_if_fail(expr, val) \
117 do { \
118 if (PA_UNLIKELY(!(expr))) { \
119 pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
120 return (val); \
121 } \
122 } while(FALSE)
123
124 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
125
126 /* An assert which guarantees side effects of x, i.e. is never
127 * optimized away */
128 #define pa_assert_se(expr) \
129 do { \
130 if (PA_UNLIKELY(!(expr))) { \
131 pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
132 abort(); \
133 } \
134 } while (FALSE)
135
136 /* An assert that may be optimized away by defining NDEBUG */
137 #ifdef NDEBUG
138 #define pa_assert(expr) do {} while (FALSE)
139 #else
140 #define pa_assert(expr) pa_assert_se(expr)
141 #endif
142
143 #define pa_assert_not_reached() \
144 do { \
145 pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
146 abort(); \
147 } while (FALSE)
148
149 #define PA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
150 #define PA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
151
152 #define PA_PTR_TO_UINT32(p) ((uint32_t) PA_PTR_TO_UINT(p))
153 #define PA_UINT32_TO_PTR(u) PA_UINT_TO_PTR((uint32_t) u)
154
155 #define PA_PTR_TO_INT(p) ((int) PA_PTR_TO_UINT(p))
156 #define PA_INT_TO_PTR(u) PA_UINT_TO_PTR((int) u)
157
158 #define PA_PTR_TO_INT32(p) ((int32_t) PA_PTR_TO_UINT(p))
159 #define PA_INT32_TO_PTR(u) PA_UINT_TO_PTR((int32_t) u)
160
161 #ifdef OS_IS_WIN32
162 #define PA_PATH_SEP "\\"
163 #define PA_PATH_SEP_CHAR '\\'
164 #else
165 #define PA_PATH_SEP "/"
166 #define PA_PATH_SEP_CHAR '/'
167 #endif
168
169 static inline const char *pa_strnull(const char *x) {
170 return x ? x : "(null)";
171 }
172
173 #endif