PipeWire  0.3.59
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 # if __cplusplus >= 201103L
31 # define SPA_STATIC_ASSERT static_assert
32 # endif
33 #else
34 # include <stdbool.h>
35 # if __STDC_VERSION__ >= 201112L
36 # define SPA_STATIC_ASSERT _Static_assert
37 # endif
38 #endif
39 #ifndef SPA_STATIC_ASSERT
40 #define SPA_STATIC_ASSERT(a, b) \
41  ((void)sizeof(struct { int spa_static_assertion_failed : 2 * !!(a) - 1; }))
42 #endif
43 #include <inttypes.h>
44 #include <signal.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stddef.h>
48 #include <stdio.h>
49 
75 #if defined(__clang__) && defined(__cplusplus) && __cplusplus >= 201103L
76  /* clang's fallthrough annotations are only available starting in C++11. */
77 # define SPA_FALLTHROUGH [[clang::fallthrough]];
78 #elif __GNUC__ >= 7 || __clang_major__ >= 10
79 # define SPA_FALLTHROUGH __attribute__ ((fallthrough));
80 #else
81 # define SPA_FALLTHROUGH /* FALLTHROUGH */
82 #endif
83 
84 #define SPA_FLAG_MASK(field,mask,flag) (((field) & (mask)) == (flag))
85 #define SPA_FLAG_IS_SET(field,flag) SPA_FLAG_MASK(field, flag, flag)
86 
87 #define SPA_FLAG_SET(field,flag) ((field) |= (flag))
88 #define SPA_FLAG_CLEAR(field, flag) \
89 ({ \
90  SPA_STATIC_ASSERT(__builtin_constant_p(flag) ? \
91  (__typeof__(flag))(__typeof__(field))(__typeof__(flag))(flag) == (flag) : \
92  sizeof(field) >= sizeof(flag), \
93  "truncation problem when masking " #field \
94  " with ~" #flag); \
95  ((field) &= ~(__typeof__(field))(flag)); \
96 })
97 #define SPA_FLAG_UPDATE(field,flag,val) ((val) ? SPA_FLAG_SET((field),(flag)) : SPA_FLAG_CLEAR((field),(flag)))
98 
99 enum spa_direction {
102 };
103 
104 #define SPA_DIRECTION_REVERSE(d) ((d) ^ 1)
105 
106 #define SPA_RECTANGLE(width,height) ((struct spa_rectangle){ (width), (height) })
107 struct spa_rectangle {
108  uint32_t width;
109  uint32_t height;
110 };
111 
112 #define SPA_POINT(x,y) ((struct spa_point){ (x), (y) })
113 struct spa_point {
114  int32_t x;
115  int32_t y;
116 };
117 
118 #define SPA_REGION(x,y,width,height) ((struct spa_region){ SPA_POINT(x,y), SPA_RECTANGLE(width,height) })
119 struct spa_region {
121  struct spa_rectangle size;
122 };
123 
124 #define SPA_FRACTION(num,denom) ((struct spa_fraction){ (num), (denom) })
125 struct spa_fraction {
126  uint32_t num;
127  uint32_t denom;
128 };
129 
130 #define SPA_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
141 #define SPA_FOR_EACH_ELEMENT(arr, ptr) \
142  for ((ptr) = arr; (void*)(ptr) < SPA_PTROFF(arr, sizeof(arr), void); (ptr)++)
143 
144 #define SPA_FOR_EACH_ELEMENT_VAR(arr, var) \
145  for (__typeof__((arr)[0])* (var) = arr; (void*)(var) < SPA_PTROFF(arr, sizeof(arr), void); (var)++)
146 
147 #define SPA_ABS(a) \
148 ({ \
149  __typeof__(a) _a = (a); \
150  SPA_LIKELY(_a >= 0) ? _a : -_a; \
151 })
152 #define SPA_MIN(a,b) \
153 ({ \
154  __typeof__(a) _min_a = (a); \
155  __typeof__(b) _min_b = (b); \
156  SPA_LIKELY(_min_a <= _min_b) ? _min_a : _min_b; \
157 })
158 #define SPA_MAX(a,b) \
159 ({ \
160  __typeof__(a) _max_a = (a); \
161  __typeof__(b) _max_b = (b); \
162  SPA_LIKELY(_max_a >= _max_b) ? _max_a : _max_b; \
163 })
164 #define SPA_CLAMP(v,low,high) \
165 ({ \
166  __typeof__(v) _v = (v); \
167  __typeof__(low) _low = (low); \
168  __typeof__(high) _high = (high); \
169  SPA_MIN(SPA_MAX(_v, _low), _high); \
170 })
171 
172 #define SPA_CLAMPF(v,low,high) \
173 ({ \
174  fminf(fmaxf(v, low), high); \
175 })
176 
177 
178 #define SPA_SWAP(a,b) \
179 ({ \
180  __typeof__(a) _t = (a); \
181  (a) = b; (b) = _t; \
182 })
183 
184 #define SPA_TYPECHECK(type,x) \
185 ({ type _dummy; \
186  typeof(x) _dummy2; \
187  (void)(&_dummy == &_dummy2); \
188  x; \
189 })
190 
194 #define SPA_PTROFF(ptr_,offset_,type_) ((type_*)((uintptr_t)(ptr_) + (ptrdiff_t)(offset_)))
195 #define SPA_PTROFF_ALIGN(ptr_,offset_,alignment_,type_) \
196  SPA_PTR_ALIGN(SPA_PTROFF(ptr_,offset_,type_),alignment_,type_)
197 
198 
202 #define SPA_MEMBER(b,o,t) SPA_PTROFF(b,o,t)
203 #define SPA_MEMBER_ALIGN(b,o,a,t) SPA_PTROFF_ALIGN(b,o,a,t)
204 
205 #define SPA_CONTAINER_OF(p,t,m) ((t*)((uintptr_t)(p) - offsetof(t,m)))
206 
207 #define SPA_PTRDIFF(p1,p2) ((intptr_t)(p1) - (intptr_t)(p2))
208 
209 #define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
210 #define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
211 
212 #define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
213 #define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
214 
215 #define SPA_TIME_INVALID ((int64_t)INT64_MIN)
216 #define SPA_IDX_INVALID ((unsigned int)-1)
217 #define SPA_ID_INVALID ((uint32_t)0xffffffff)
218 
219 #define SPA_NSEC_PER_SEC (1000000000LL)
220 #define SPA_NSEC_PER_MSEC (1000000ll)
221 #define SPA_NSEC_PER_USEC (1000ll)
222 #define SPA_USEC_PER_SEC (1000000ll)
223 #define SPA_USEC_PER_MSEC (1000ll)
224 #define SPA_MSEC_PER_SEC (1000ll)
225 
226 #define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
227 #define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
228 #define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
229 #define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
230 
231 #ifdef __GNUC__
232 #define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
233 #define SPA_FORMAT_ARG_FUNC(arg1) __attribute__((format_arg(arg1)))
234 #define SPA_ALIGNED(align) __attribute__((aligned(align)))
235 #define SPA_DEPRECATED __attribute__ ((deprecated))
236 #define SPA_EXPORT __attribute__((visibility("default")))
237 #define SPA_SENTINEL __attribute__((__sentinel__))
238 #define SPA_UNUSED __attribute__ ((unused))
239 #define SPA_NORETURN __attribute__ ((noreturn))
240 #define SPA_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
241 #else
242 #define SPA_PRINTF_FUNC(fmt, arg1)
243 #define SPA_FORMAT_ARG_FUNC(arg1)
244 #define SPA_ALIGNED(align)
245 #define SPA_DEPRECATED
246 #define SPA_EXPORT
247 #define SPA_SENTINEL
248 #define SPA_UNUSED
249 #define SPA_NORETURN
250 #define SPA_WARN_UNUSED_RESULT
251 #endif
252 
253 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
254 #define SPA_RESTRICT restrict
255 #elif defined(__GNUC__) && __GNUC__ >= 4
256 #define SPA_RESTRICT __restrict__
257 #else
258 #define SPA_RESTRICT
259 #endif
260 
261 #define SPA_ROUND_DOWN(num,value) \
262 ({ \
263  __typeof__(num) _num = (num); \
264  ((_num) - ((_num) % (value))); \
265 })
266 #define SPA_ROUND_UP(num,value) \
267 ({ \
268  __typeof__(value) _v = (value); \
269  ((((num) + (_v) - 1) / (_v)) * (_v)); \
270 })
271 
272 #define SPA_ROUND_MASK(num,mask) ((__typeof__(num))((mask)-1))
273 
274 #define SPA_ROUND_DOWN_N(num,align) ((num) & ~SPA_ROUND_MASK(num, align))
275 #define SPA_ROUND_UP_N(num,align) ((((num)-1) | SPA_ROUND_MASK(num, align))+1)
276 
277 #define SPA_PTR_ALIGNMENT(p,align) ((intptr_t)(p) & ((align)-1))
278 #define SPA_IS_ALIGNED(p,align) (SPA_PTR_ALIGNMENT(p,align) == 0)
279 #define SPA_PTR_ALIGN(p,align,type) ((type*)SPA_ROUND_UP_N((intptr_t)(p), (intptr_t)(align)))
280 
281 #ifndef SPA_LIKELY
282 #ifdef __GNUC__
283 #define SPA_LIKELY(x) (__builtin_expect(!!(x),1))
284 #define SPA_UNLIKELY(x) (__builtin_expect(!!(x),0))
285 #else
286 #define SPA_LIKELY(x) (x)
287 #define SPA_UNLIKELY(x) (x)
288 #endif
289 #endif
290 
291 #define SPA_STRINGIFY_1(...) #__VA_ARGS__
292 #define SPA_STRINGIFY(...) SPA_STRINGIFY_1(__VA_ARGS__)
293 
294 #define spa_return_if_fail(expr) \
295  do { \
296  if (SPA_UNLIKELY(!(expr))) { \
297  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
298  #expr , __FILE__, __LINE__, __func__); \
299  return; \
300  } \
301  } while(false)
302 
303 #define spa_return_val_if_fail(expr, val) \
304  do { \
305  if (SPA_UNLIKELY(!(expr))) { \
306  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
307  #expr , __FILE__, __LINE__, __func__); \
308  return (val); \
309  } \
310  } while(false)
311 
312 /* spa_assert_se() is an assert which guarantees side effects of x,
313  * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
314 #ifndef __COVERITY__
315 #define spa_assert_se(expr) \
316  do { \
317  if (SPA_UNLIKELY(!(expr))) { \
318  fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
319  #expr , __FILE__, __LINE__, __func__); \
320  abort(); \
321  } \
322  } while (false)
323 #else
324 #define spa_assert_se(expr) \
325  do { \
326  int _unique_var = (expr); \
327  if (!_unique_var) \
328  abort(); \
329  } while (false)
330 #endif
331 
332 /* Does exactly nothing */
333 #define spa_nop() do {} while (false)
334 
335 #ifdef NDEBUG
336 #define spa_assert(expr) spa_nop()
337 #elif defined (FASTPATH)
338 #define spa_assert(expr) spa_assert_se(expr)
339 #else
340 #define spa_assert(expr) spa_assert_se(expr)
341 #endif
342 
343 #ifdef NDEBUG
344 #define spa_assert_not_reached() abort()
345 #else
346 #define spa_assert_not_reached() \
347  do { \
348  fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
349  __FILE__, __LINE__, __func__); \
350  abort(); \
351  } while (false)
352 #endif
353 
354 #define spa_memzero(x,l) (memset((x), 0, (l)))
355 #define spa_zero(x) (spa_memzero(&(x), sizeof(x)))
356 
357 #ifdef SPA_DEBUG_MEMCPY
358 #define spa_memcpy(d,s,n) \
359 ({ \
360  fprintf(stderr, "%s:%u %s() memcpy(%p, %p, %zd)\n", \
361  __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
362  memcpy(d,s,n); \
363 })
364 #define spa_memmove(d,s,n) \
365 ({ \
366  fprintf(stderr, "%s:%u %s() memmove(%p, %p, %zd)\n", \
367  __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
368  memmove(d,s,n); \
369 })
370 #else
371 #define spa_memcpy(d,s,n) memcpy(d,s,n)
372 #define spa_memmove(d,s,n) memmove(d,s,n)
373 #endif
374 
375 #define spa_aprintf(_fmt, ...) \
376 ({ \
377  char *_strp; \
378  if (asprintf(&(_strp), (_fmt), ## __VA_ARGS__ ) == -1) \
379  _strp = NULL; \
380  _strp; \
381 })
382 
387 #ifdef __cplusplus
388 } /* extern "C" */
389 #endif
390 
391 #endif /* SPA_UTILS_DEFS_H */
spa_direction
Definition: defs.h:108
@ SPA_DIRECTION_INPUT
Definition: defs.h:109
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:110
spa/utils/string.h
Definition: defs.h:139
uint32_t num
Definition: defs.h:140
uint32_t denom
Definition: defs.h:141
Definition: defs.h:125
int32_t y
Definition: defs.h:127
int32_t x
Definition: defs.h:126
Definition: defs.h:118
uint32_t width
Definition: defs.h:119
uint32_t height
Definition: defs.h:120
Definition: defs.h:132
struct spa_point position
Definition: defs.h:133
struct spa_rectangle size
Definition: defs.h:134