]> code.delx.au - pulseaudio/blob - src/pulsecore/macro.h
merge 'lennart' branch back into trunk.
[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
35 #ifndef PACKAGE
36 #error "Please include config.h before including this file!"
37 #endif
38
39 #if defined(PAGE_SIZE)
40 #define PA_PAGE_SIZE ((size_t) PAGE_SIZE)
41 #elif defined(PAGESIZE)
42 #define PA_PAGE_SIZE ((size_t) PAGESIZE)
43 #elif defined(HAVE_SYSCONF)
44 #define PA_PAGE_SIZE ((size_t) (sysconf(_SC_PAGE_SIZE)))
45 #else
46 /* Let's hope it's like x86. */
47 #define PA_PAGE_SIZE ((size_t) 4096)
48 #endif
49
50 static inline size_t pa_align(size_t l) {
51 return (((l + sizeof(void*) - 1) / sizeof(void*)) * sizeof(void*));
52 }
53 #define PA_ALIGN(x) (pa_align(x))
54
55 static inline void* pa_page_align_ptr(const void *p) {
56 return (void*) (((size_t) p) & ~(PA_PAGE_SIZE-1));
57 }
58 #define PA_PAGE_ALIGN_PTR(x) (pa_page_align_ptr(x))
59
60 static inline size_t pa_page_align(size_t l) {
61 return l & ~(PA_PAGE_SIZE-1);
62 }
63 #define PA_PAGE_ALIGN(x) (pa_page_align(x))
64
65 #define PA_ELEMENTSOF(x) (sizeof(x)/sizeof((x)[0]))
66
67 #ifndef MAX
68 #define MAX(a, b) ((a) > (b) ? (a) : (b))
69 #endif
70
71 #ifndef MIN
72 #define MIN(a, b) ((a) < (b) ? (a) : (b))
73 #endif
74
75 #ifndef CLAMP
76 #define CLAMP(x, low, high) (((x) > (high)) ? (high) : (((x) < (low)) ? (low) : (x)))
77 #endif
78
79 /* This type is not intended to be used in exported APIs! Use classic "int" there! */
80 #ifdef HAVE_STD_BOOL
81 typedef _Bool pa_bool_t;
82 #else
83 typedef int pa_bool_t;
84 #endif
85
86 #ifndef FALSE
87 #define FALSE ((pa_bool_t) 0)
88 #endif
89
90 #ifndef TRUE
91 #define TRUE (!FALSE)
92 #endif
93
94 #ifdef __GNUC__
95 #define PA_PRETTY_FUNCTION __PRETTY_FUNCTION__
96 #else
97 #define PA_PRETTY_FUNCTION ""
98 #endif
99
100 #define pa_return_if_fail(expr) \
101 do { \
102 if (!(expr)) { \
103 pa_log_debug("%s: Assertion <%s> failed.\n", PA_PRETTY_FUNCTION, #expr ); \
104 return; \
105 } \
106 } while(0)
107
108 #define pa_return_val_if_fail(expr, val) \
109 do { \
110 if (!(expr)) { \
111 pa_log_debug("%s: Assertion <%s> failed.\n", PA_PRETTY_FUNCTION, #expr ); \
112 return (val); \
113 } \
114 } while(0)
115
116 #define pa_return_null_if_fail(expr) pa_return_val_if_fail(expr, NULL)
117
118 #define pa_assert assert
119
120 #define pa_assert_not_reached() pa_assert(!"Should not be reached.")
121
122 /* An assert which guarantees side effects of x */
123 #ifdef NDEBUG
124 #define pa_assert_se(x) x
125 #else
126 #define pa_assert_se(x) pa_assert(x)
127 #endif
128
129 #define PA_PTR_TO_UINT(p) ((unsigned int) (unsigned long) (p))
130 #define PA_UINT_TO_PTR(u) ((void*) (unsigned long) (u))
131
132 #define PA_PTR_TO_UINT32(p) ((uint32_t) PA_PTR_TO_UINT(p))
133 #define PA_UINT32_TO_PTR(u) PA_UINT_TO_PTR((uint32_t) u)
134
135 #define PA_PTR_TO_INT(p) ((int) PA_PTR_TO_UINT(p))
136 #define PA_INT_TO_PTR(u) PA_UINT_TO_PTR((int) u)
137
138 #define PA_PTR_TO_INT32(p) ((int32_t) PA_PTR_TO_UINT(p))
139 #define PA_INT32_TO_PTR(u) PA_UINT_TO_PTR((int32_t) u)
140
141 #ifdef OS_IS_WIN32
142 #define PA_PATH_SEP "\\"
143 #define PA_PATH_SEP_CHAR '\\'
144 #else
145 #define PA_PATH_SEP "/"
146 #define PA_PATH_SEP_CHAR '/'
147 #endif
148
149 #endif