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