PipeWire  0.3.30
defs.h
Go to the documentation of this file.
1 /* Simple Plugin API
2  *
3  * Copyright © 2018 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #ifndef SPA_UTILS_DEFS_H
26 #define SPA_UTILS_DEFS_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #else
31 #include <stdbool.h>
32 #endif
33 #include <inttypes.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 
63 #if defined(__clang__) && __cplusplus >= 201103L
64  /* clang's fallthrough annotations are only available starting in C++11. */
65 # define SPA_FALLTHROUGH [[clang::fallthrough]];
66 #elif __GNUC__ >= 7 || __clang_major__ >= 10
67 # define SPA_FALLTHROUGH __attribute__ ((fallthrough));
68 #else
69 # define SPA_FALLTHROUGH /* FALLTHROUGH */
70 #endif
71 
72 #define SPA_FLAG_MASK(field,mask,flag) (((field) & (mask)) == (flag))
73 #define SPA_FLAG_IS_SET(field,flag) SPA_FLAG_MASK(field,flag,flag)
74 #define SPA_FLAG_SET(field,flag) ((field) |= (flag))
75 #define SPA_FLAG_CLEAR(field,flag) ((field) &= ~(flag))
76 #define SPA_FLAG_UPDATE(field,flag,val) ((val) ? SPA_FLAG_SET(field,flag) : SPA_FLAG_CLEAR(field,flag))
77 
81 };
82 
83 #define SPA_DIRECTION_REVERSE(d) ((d) ^ 1)
84 
85 #define SPA_RECTANGLE(width,height) (struct spa_rectangle){ width, height }
86 struct spa_rectangle {
87  uint32_t width;
88  uint32_t height;
89 };
90 
91 #define SPA_POINT(x,y) (struct spa_point){ x, y }
92 struct spa_point {
93  int32_t x;
94  int32_t y;
95 };
96 
97 #define SPA_REGION(x,y,width,height) (struct spa_region){ SPA_POINT(x,y), SPA_RECTANGLE(width,height) }
98 struct spa_region {
101 };
102 
103 #define SPA_FRACTION(num,denom) (struct spa_fraction){ num, denom }
104 struct spa_fraction {
105  uint32_t num;
106  uint32_t denom;
107 };
108 
109 #define SPA_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
110 
120 #define SPA_FOR_EACH_ELEMENT(arr, ptr) \
121  for (ptr = arr; (void*)ptr < SPA_PTROFF(arr, sizeof(arr), void); ptr++)
122 
123 #define SPA_MIN(a,b) \
124 ({ \
125  __typeof__(a) _min_a = (a); \
126  __typeof__(b) _min_b = (b); \
127  SPA_LIKELY(_min_a < _min_b) ? _min_a : _min_b; \
128 })
129 #define SPA_MAX(a,b) \
130 ({ \
131  __typeof__(a) _max_a = (a); \
132  __typeof__(b) _max_b = (b); \
133  SPA_LIKELY(_max_a > _max_b) ? _max_a : _max_b; \
134 })
135 #define SPA_CLAMP(v,low,high) \
136 ({ \
137  __typeof__(v) _v = (v); \
138  __typeof__(low) _low = (low); \
139  __typeof__(high) _high = (high); \
140  SPA_MIN(SPA_MAX(_v, _low), _high); \
141 })
142 
143 #define SPA_SWAP(a,b) \
144 ({ \
145  __typeof__(a) _t = (a); \
146  a = b; b = _t; \
147 })
148 
149 #define SPA_TYPECHECK(type,x) \
150 ({ type _dummy; \
151  typeof(x) _dummy2; \
152  (void)(&_dummy == &_dummy2); \
153  x; \
154 })
155 
159 #define SPA_PTROFF(ptr_,offset_,type_) ((type_*)((uint8_t*)(ptr_) + (int)(offset_)))
160 #define SPA_PTROFF_ALIGN(ptr_,offset_,alignment_,type_) \
161  SPA_PTR_ALIGN(SPA_PTROFF(ptr_,offset_,type_),alignment_,type_)
162 
163 
167 #define SPA_MEMBER(b,o,t) SPA_PTROFF(b,o,t)
168 #define SPA_MEMBER_ALIGN(b,o,a,t) SPA_PTROFF_ALIGN(b,o,a,t)
169 
170 #define SPA_CONTAINER_OF(p,t,m) (t*)((uint8_t*)p - offsetof (t,m))
171 
172 #define SPA_PTRDIFF(p1,p2) ((uint8_t*)(p1) - (uint8_t*)(p2))
173 
174 #define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
175 #define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
176 
177 #define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
178 #define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
179 
180 #define SPA_TIME_INVALID ((int64_t)INT64_MIN)
181 #define SPA_IDX_INVALID ((unsigned int)-1)
182 #define SPA_ID_INVALID ((uint32_t)0xffffffff)
183 
184 #define SPA_NSEC_PER_SEC (1000000000ll)
185 #define SPA_NSEC_PER_MSEC (1000000ll)
186 #define SPA_NSEC_PER_USEC (1000ll)
187 #define SPA_USEC_PER_SEC (1000000ll)
188 #define SPA_USEC_PER_MSEC (1000ll)
189 #define SPA_MSEC_PER_SEC (1000ll)
190 
191 #define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
192 #define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
193 #define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
194 #define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
195 
196 #ifdef __GNUC__
197 #define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
198 #define SPA_ALIGNED(align) __attribute__((aligned(align)))
199 #define SPA_DEPRECATED __attribute__ ((deprecated))
200 #define SPA_EXPORT __attribute__((visibility("default")))
201 #define SPA_SENTINEL __attribute__((__sentinel__))
202 #define SPA_UNUSED __attribute__ ((unused))
203 #else
204 #define SPA_PRINTF_FUNC(fmt, arg1)
205 #define SPA_ALIGNED(align)
206 #define SPA_DEPRECATED
207 #define SPA_EXPORT
208 #define SPA_SENTINEL
209 #define SPA_UNUSED
210 #endif
211 
212 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
213 #define SPA_RESTRICT restrict
214 #elif defined(__GNUC__) && __GNUC__ >= 4
215 #define SPA_RESTRICT __restrict__
216 #else
217 #define SPA_RESTRICT
218 #endif
219 
220 #define SPA_ROUND_DOWN_N(num,align) ((num) & ~((align) - 1))
221 #define SPA_ROUND_UP_N(num,align) SPA_ROUND_DOWN_N((num) + ((align) - 1),align)
222 
223 #define SPA_PTR_ALIGNMENT(p,align) ((intptr_t)(p) & ((align)-1))
224 #define SPA_IS_ALIGNED(p,align) (SPA_PTR_ALIGNMENT(p,align) == 0)
225 #define SPA_PTR_ALIGN(p,align,type) (type*)SPA_ROUND_UP_N((intptr_t)(p), (intptr_t)(align))
226 
227 #ifndef SPA_LIKELY
228 #ifdef __GNUC__
229 #define SPA_LIKELY(x) (__builtin_expect(!!(x),1))
230 #define SPA_UNLIKELY(x) (__builtin_expect(!!(x),0))
231 #else
232 #define SPA_LIKELY(x) (x)
233 #define SPA_UNLIKELY(x) (x)
234 #endif
235 #endif
236 
237 #define SPA_STRINGIFY_1(...) #__VA_ARGS__
238 #define SPA_STRINGIFY(...) SPA_STRINGIFY_1(__VA_ARGS__)
239 
240 #define spa_return_if_fail(expr) \
241  do { \
242  if (SPA_UNLIKELY(!(expr))) { \
243  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
244  #expr , __FILE__, __LINE__, __func__); \
245  return; \
246  } \
247  } while(false)
248 
249 #define spa_return_val_if_fail(expr, val) \
250  do { \
251  if (SPA_UNLIKELY(!(expr))) { \
252  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
253  #expr , __FILE__, __LINE__, __func__); \
254  return (val); \
255  } \
256  } while(false)
257 
258 /* spa_assert_se() is an assert which guarantees side effects of x,
259  * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
260 #define spa_assert_se(expr) \
261  do { \
262  if (SPA_UNLIKELY(!(expr))) { \
263  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
264  #expr , __FILE__, __LINE__, __func__); \
265  abort(); \
266  } \
267  } while (false)
268 
269 #define spa_assert(expr) \
270  do { \
271  if (SPA_UNLIKELY(!(expr))) { \
272  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
273  #expr , __FILE__, __LINE__, __func__); \
274  abort(); \
275  } \
276  } while (false)
277 
278 #define spa_assert_not_reached() \
279  do { \
280  fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
281  __FILE__, __LINE__, __func__); \
282  abort(); \
283  } while (false)
284 
285 /* Does exactly nothing */
286 #define spa_nop() do {} while (false)
287 
288 #define spa_memzero(x,l) (memset((x), 0, (l)))
289 #define spa_zero(x) (spa_memzero(&(x), sizeof(x)))
290 
291 #ifdef SPA_DEBUG_MEMCPY
292 #define spa_memcpy(d,s,n) \
293 ({ \
294  fprintf(stderr, "%s:%u %s() memcpy(%p, %p, %zd)\n", \
295  __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
296  memcpy(d,s,n); \
297 })
298 #define spa_memmove(d,s,n) \
299 ({ \
300  fprintf(stderr, "%s:%u %s() memmove(%p, %p, %zd)\n", \
301  __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
302  memmove(d,s,n); \
303 })
304 #else
305 #define spa_memcpy(d,s,n) memcpy(d,s,n)
306 #define spa_memmove(d,s,n) memmove(d,s,n)
307 #endif
308 
309 #define spa_aprintf(_fmt, ...) \
310 ({ \
311  char *_strp; \
312  if (asprintf(&(_strp), (_fmt), ## __VA_ARGS__ ) == -1) \
313  _strp = NULL; \
314  _strp; \
315 })
316 
321 #ifdef __cplusplus
322 } /* extern "C" */
323 #endif
324 
325 #endif /* SPA_UTILS_DEFS_H */
spa_rectangle::width
uint32_t width
Definition: defs.h:87
spa_direction
spa_direction
Definition: defs.h:78
spa_point::x
int32_t x
Definition: defs.h:93
string.h
spa_region::size
struct spa_rectangle size
Definition: defs.h:100
SPA_DIRECTION_INPUT
@ SPA_DIRECTION_INPUT
Definition: defs.h:79
spa_rectangle::height
uint32_t height
Definition: defs.h:88
spa_fraction
Definition: defs.h:104
spa_point
Definition: defs.h:92
spa_rectangle
Definition: defs.h:86
SPA_DIRECTION_OUTPUT
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:80
spa_fraction::denom
uint32_t denom
Definition: defs.h:106
spa_region
Definition: defs.h:98
spa_fraction::num
uint32_t num
Definition: defs.h:105
spa_point::y
int32_t y
Definition: defs.h:94
spa_region::position
struct spa_point position
Definition: defs.h:99