summaryrefslogtreecommitdiffstats
path: root/src/pulsecore/macro.h
blob: 309f1a0de82dc7ceb88de7e75b50aa08abb8b880 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#ifndef foopulsemacrohfoo
#define foopulsemacrohfoo

/***
  This file is part of PulseAudio.

  Copyright 2004-2006 Lennart Poettering

  PulseAudio is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License,
  or (at your option) any later version.

  PulseAudio is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with PulseAudio; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#include <sys/types.h>
#include <unistd.h>
#include <assert.h>
#include <limits.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

#include <pulse/gccmacro.h>

#ifndef PACKAGE
#error "Please include config.h before including this file!"
#endif

#ifndef PA_LIKELY
#ifdef __GNUC__
#define PA_LIKELY(x) (__builtin_expect(!!(x),1))
#define PA_UNLIKELY(x) (__builtin_expect(!!(x),0))
#else
#define PA_LIKELY(x) (x)
#define PA_UNLIKELY(x) (x)
#endif
#endif

#if defined(PAGE_SIZE)
#define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
#elif defined(PAGESIZE)
#define PA_PAGE_SIZE ((size_t) PAGESIZE)
#elif defined(HAVE_SYSCONF)
#define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
#else
/* Let's hope it's like x86. */
#define PA_PAGE_SIZE ((size_t) 4096)
#endif

static inline size_t pa_align(size_t l) {
    return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
}
#define PA_ALIGN(x) (pa_align(x))

static inline void* pa_page_align_ptr(const void *p) {
    return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
}
#define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))

static inline size_t pa_page_align(size_t l) {
    return l & ~(PA_PAGE_SIZE-1);
}
#define PA_PAGE_ALIGN(x) (pa_page_align(x))

#define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))

/* The users of PA_MIN and PA_MAX should be aware that these macros on
 * non-GCC executed code with side effects twice. It is thus
 * considered misuse to use code with side effects as arguments to MIN
 * and MAX. */

#ifdef __GNUC__
#define PA_MAX(a,b)                             \
    __extension__ ({ typeof(a) _a = (a);        \
            typeof(b) _b = (b);                 \
            _a > _b ? _a : _b;                  \
        })
#else
#define PA_MAX(a, b) ((a) > (b) ? (a) : (b))
#endif

#ifdef __GNUC__
#define PA_MIN(a,b)                             \
    __extension__ ({ typeof(a) _a = (a);        \
            typeof(b) _b = (b);                 \
            _a < _b ? _a : _b;                  \
        })
#else
#define PA_MIN(a, b) ((a) < (b) ? (a) : (b))
#endif

#ifdef __GNUC__
#define PA_CLAMP(x, low, high)                                          \
    __extension__ ({ typeof(x) _x = (x);                                \
            typeof(low) _low = (low);                                   \
            typeof(high) _high = (high);                                \
            ((_x > _high) ? _high : ((_x < _low) ? _low : _x));         \
        })
#else
#define PA_CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
#endif

#ifdef __GNUC__
#define PA_CLAMP_UNLIKELY(x, low, high)                                 \
    __extension__ ({ typeof(x) _x = (x);                                \
            typeof(low) _low = (low);                                   \
            typeof(high) _high = (high);                                \
            (PA_UNLIKELY(_x > _high) ? _high : (PA_UNLIKELY(_x < _low) ? _low : _x)); \
        })
#else
#define PA_CLAMP_UNLIKELY(x, low, high) (PA_UNLIKELY((x) > (high)) ? (high) : (PA_UNLIKELY((x) < (low)) ? (low) : (x)))
#endif

/* We don't define a PA_CLAMP_LIKELY here, because it doesn't really
 * make sense: we cannot know if it is more likely that the values is
 * lower or greater than the boundaries.*/

/* This type is not intended to be used in exported APIs! Use classic "int" there! */
#ifdef HAVE_STD_BOOL
typedef _Bool pa_bool_t;
#else
typedef int pa_bool_t;
#endif

#ifndef FALSE
#define FALSE ((pa_bool_t) 0)
#endif

#ifndef TRUE
#define TRUE (!FALSE)
#endif

#ifdef __GNUC__
#define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
#else
#define PA_PRETTY_FUNCTION ""
#endif

#define pa_return_if_fail(expr)                                         \
    do {                                                                \
        if (PA_UNLIKELY(!(expr))) {                                     \
            pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
            return;                                                     \
        }                                                               \
    } while(FALSE)

#define pa_return_val_if_fail(expr, val)                                \
    do {                                                                \
        if (PA_UNLIKELY(!(expr))) {                                     \
            pa_log_debug("Assertion '%s' failed at %s:%u, function %s.\n", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
            return (val);                                               \
        }                                                               \
    } while(FALSE)

#define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)

/* pa_assert_se() is an assert which guarantees side effects of x,
 * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
#define pa_assert_se(expr)                                              \
    do {                                                                \
        if (PA_UNLIKELY(!(expr))) {                                     \
            pa_log_error("Assertion '%s' failed at %s:%u, function %s(). Aborting.", #expr , __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
            abort();                                                    \
        }                                                               \
    } while (FALSE)

/* Does exactly nothing */
#define pa_nop() do {} while (FALSE)

/* pa_assert() is an assert that may be optimized away by defining
 * NDEBUG. pa_assert_fp() is an assert that may be optimized away by
 * defining FASTPATH. It is supposed to be used in inner loops. It's
 * there for extra paranoia checking and should probably be removed in
 * production builds. */
#ifdef NDEBUG
#define pa_assert(expr) pa_nop()
#define pa_assert_fp(expr) pa_nop()
#elif defined (FASTPATH)
#define pa_assert(expr) pa_assert_se(expr)
#define pa_assert_fp(expr) pa_nop()
#else
#define pa_assert(expr) pa_assert_se(expr)
#define pa_assert_fp(expr) pa_assert_se(expr)
#endif

#define pa_assert_not_reached()                                         \
    do {                                                                \
        pa_log_error("Code should not be reached at %s:%u, function %s(). Aborting.", __FILE__, __LINE__, PA_PRETTY_FUNCTION); \
        abort();                                                        \
    } while (FALSE)

#define PA_PTR_TO_UINT(p) ((unsigned int) ((uintptr_t) (p)))
#define PA_UINT_TO_PTR(u) ((void*) ((uintptr_t) (u)))

#define PA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
#define PA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))

#define PA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
#define PA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))

#define PA_PTR_TO_INT32(p) ((int32_t) ((intptr_t) (p)))
#define PA_INT32_TO_PTR(u) ((void*) ((intptr_t) (u)))

#ifdef OS_IS_WIN32
#define PA_PATH_SEP "\\"
#define PA_PATH_SEP_CHAR '\\'
#else
#define PA_PATH_SEP "/"
#define PA_PATH_SEP_CHAR '/'
#endif

#if defined(__GNUC__) && defined(__ELF__)

#define PA_WARN_REFERENCE(sym, msg)                  \
    __asm__(".section .gnu.warning." #sym);          \
    __asm__(".asciz \"" msg "\"");                   \
    __asm__(".previous")

#else

#define PA_WARN_REFERENCE(sym, msg)

#endif

#if defined(__i386__) || defined(__x86_64__)
#define PA_DEBUG_TRAP __asm__("int $3")
#else
#define PA_DEBUG_TRAP raise(SIGTRAP)
#endif

/* We include this at the very last place */
#include <pulsecore/log.h>

#endif