+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 468 lines · 12.7 KB · SPDX: MIT
main
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 #include <inttypes.h>
9 #include <signal.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <stddef.h>
13 #include <stdio.h>
14  
15 #ifdef __cplusplus
16 extern "C" {
17 # if __cplusplus >= 201103L
18 # define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) static_assert(expr, msg)
19 # define SPA_ALIGNOF alignof
20 # endif
21 #elif __STDC_VERSION__ >= 202311L
22 # define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) static_assert(expr, msg)
23 # define SPA_ALIGNOF alignof
24 #else
25 # include <stdbool.h>
26 # if __STDC_VERSION__ >= 201112L
27 # define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) _Static_assert(expr, msg)
28 # define SPA_ALIGNOF _Alignof
29 # endif
30 #endif
31 #ifndef SPA_STATIC_ASSERT_IMPL
32 #define SPA_STATIC_ASSERT_IMPL(expr, ...) \
33 ((void)sizeof(struct { int spa_static_assertion_failed : 2 * !!(expr) - 1; }))
34 #endif
35 #ifndef SPA_ALIGNOF
36 #define SPA_ALIGNOF __alignof__
37 #endif
38  
39 #define SPA_STATIC_ASSERT(expr, ...) SPA_STATIC_ASSERT_IMPL(expr, ## __VA_ARGS__, "`" #expr "` evaluated to false")
40  
41 #define SPA_CONCAT_NOEXPAND(a, b) a ## b
42 #define SPA_CONCAT(a, b) SPA_CONCAT_NOEXPAND(a, b)
43  
44 /**
45 * \defgroup spa_utils_defs Miscellaneous
46 * Helper macros and functions
47 */
48  
49 /**
50 * \addtogroup spa_utils_defs
51 * \{
52 */
53  
54 /**
55 * SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch
56 * cases that fall through without a break or return statement. SPA_FALLTHROUGH
57 * is only needed on cases that have code:
58 *
59 * switch (foo) {
60 * case 1: // These cases have no code. No fallthrough annotations are needed.
61 * case 2:
62 * case 3:
63 * foo = 4; // This case has code, so a fallthrough annotation is needed:
64 * SPA_FALLTHROUGH;
65 * default:
66 * return foo;
67 * }
68 */
69 #if defined(__clang__) && defined(__cplusplus) && __cplusplus >= 201103L
70 /* clang's fallthrough annotations are only available starting in C++11. */
71 # define SPA_FALLTHROUGH [[clang::fallthrough]];
72 #elif __GNUC__ >= 7 || __clang_major__ >= 10
73 # define SPA_FALLTHROUGH __attribute__ ((fallthrough));
74 #else
75 # define SPA_FALLTHROUGH /* FALLTHROUGH */
76 #endif
77  
78 #define SPA_FLAG_MASK(field,mask,flag) (((field) & (mask)) == (flag))
79 #define SPA_FLAG_IS_SET(field,flag) SPA_FLAG_MASK(field, flag, flag)
80  
81 #define SPA_FLAG_SET(field,flag) ((field) |= (flag))
82 #define SPA_FLAG_CLEAR(field, flag) \
83 ({ \
84 SPA_STATIC_ASSERT(__builtin_constant_p(flag) ? \
85 (__typeof__(flag))(__typeof__(field))(__typeof__(flag))(flag) == (flag) : \
86 sizeof(field) >= sizeof(flag), \
87 "truncation problem when masking " #field \
88 " with ~" #flag); \
89 ((field) &= ~(__typeof__(field))(flag)); \
90 })
91 #define SPA_FLAG_UPDATE(field,flag,val) ((val) ? SPA_FLAG_SET((field),(flag)) : SPA_FLAG_CLEAR((field),(flag)))
92  
93 enum spa_direction {
94 SPA_DIRECTION_INPUT = 0,
95 SPA_DIRECTION_OUTPUT = 1,
96 };
97  
98 #define SPA_DIRECTION_REVERSE(d) ((d) ^ 1)
99  
100 #define SPA_RECTANGLE(width,height) ((struct spa_rectangle){ (width), (height) })
101 struct spa_rectangle {
102 uint32_t width;
103 uint32_t height;
104 };
105  
106 #define SPA_POINT(x,y) ((struct spa_point){ (x), (y) })
107 struct spa_point {
108 int32_t x;
109 int32_t y;
110 };
111  
112 #define SPA_REGION(x,y,width,height) ((struct spa_region){ SPA_POINT(x,y), SPA_RECTANGLE(width,height) })
113 struct spa_region {
114 struct spa_point position;
115 struct spa_rectangle size;
116 };
117  
118 #define SPA_FRACTION(num,denom) ((struct spa_fraction){ (num), (denom) })
119 struct spa_fraction {
120 uint32_t num;
121 uint32_t denom;
122 };
123  
124 #define SPA_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
125 /**
126 * Array iterator macro. Usage:
127 * ```c
128 * struct foo array[16];
129 * struct foo *f;
130 * SPA_FOR_EACH_ELEMENT(array, f) {
131 * f->bar = baz;
132 * }
133 * ```
134 */
135 #define SPA_FOR_EACH_ELEMENT(arr, ptr) \
136 for ((ptr) = arr; (ptr) < (arr) + SPA_N_ELEMENTS(arr); (ptr)++)
137  
138 #define SPA_FOR_EACH_ELEMENT_VAR(arr, var) \
139 for (__typeof__((arr)[0])* var = arr; (var) < (arr) + SPA_N_ELEMENTS(arr); (var)++)
140  
141 #define SPA_ABS(a) \
142 ({ \
143 __typeof__(a) _a = (a); \
144 SPA_LIKELY(_a >= 0) ? _a : -_a; \
145 })
146 #define SPA_MIN(a,b) \
147 ({ \
148 __typeof__(a) _min_a = (a); \
149 __typeof__(b) _min_b = (b); \
150 SPA_LIKELY(_min_a <= _min_b) ? _min_a : _min_b; \
151 })
152 #define SPA_MAX(a,b) \
153 ({ \
154 __typeof__(a) _max_a = (a); \
155 __typeof__(b) _max_b = (b); \
156 SPA_LIKELY(_max_a >= _max_b) ? _max_a : _max_b; \
157 })
158 #define SPA_CLAMP(v,low,high) \
159 ({ \
160 __typeof__(v) _v = (v); \
161 __typeof__(low) _low = (low); \
162 __typeof__(high) _high = (high); \
163 SPA_MIN(SPA_MAX(_v, _low), _high); \
164 })
165  
166 #define SPA_CLAMPF(v,low,high) \
167 ({ \
168 fminf(fmaxf(v, low), high); \
169 })
170 #define SPA_CLAMPD(v,low,high) \
171 ({ \
172 fmin(fmax(v, low), high); \
173 })
174  
175  
176 #define SPA_SWAP(a,b) \
177 ({ \
178 __typeof__(a) _t = (a); \
179 (a) = b; (b) = _t; \
180 })
181  
182 #define SPA_TYPECHECK(type,x) \
183 ({ type _dummy; \
184 typeof(x) _dummy2; \
185 (void)(&_dummy == &_dummy2); \
186 x; \
187 })
188  
189 /** 3-way comparison. NaN > NaN and NaN > finite numbers */
190 #define SPA_CMP(a, b) \
191 ({ \
192 __typeof__(a) _a = (a); \
193 __typeof__(b) _b = (b); \
194 (_a > _b) ? 1 : (_a == _b) ? 0 : (_a < _b) ? -1 \
195 : (_a == _a) ? -1 : (_b == _b) ? 1 \
196 : 1; \
197 })
198  
199 /**
200 * Return the address (buffer + offset) as pointer of \a type
201 */
202 #define SPA_PTROFF(ptr_,offset_,type_) ((type_*)((uintptr_t)(ptr_) + (ptrdiff_t)(offset_)))
203 #define SPA_PTROFF_ALIGN(ptr_,offset_,alignment_,type_) \
204 SPA_PTR_ALIGN(SPA_PTROFF(ptr_,offset_,type_),alignment_,type_)
205  
206 /**
207 * Deprecated, use SPA_PTROFF and SPA_PTROFF_ALIGN instead
208 */
209 #define SPA_MEMBER(b,o,t) SPA_PTROFF(b,o,t)
210 #define SPA_MEMBER_ALIGN(b,o,a,t) SPA_PTROFF_ALIGN(b,o,a,t)
211  
212 #define SPA_CONTAINER_OF(p,t,m) ((t*)((uintptr_t)(p) - offsetof(t,m)))
213  
214 #define SPA_PTRDIFF(p1,p2) ((intptr_t)(p1) - (intptr_t)(p2))
215  
216 #define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
217 #define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
218  
219 #define SPA_TIME_INVALID ((int64_t)INT64_MIN)
220 #define SPA_IDX_INVALID ((unsigned int)-1)
221 #define SPA_ID_INVALID ((uint32_t)0xffffffff)
222  
223 #define SPA_NSEC_PER_SEC (1000000000LL)
224 #define SPA_NSEC_PER_MSEC (1000000ll)
225 #define SPA_NSEC_PER_USEC (1000ll)
226 #define SPA_USEC_PER_SEC (1000000ll)
227 #define SPA_USEC_PER_MSEC (1000ll)
228 #define SPA_MSEC_PER_SEC (1000ll)
229  
230 #define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
231 #define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
232 #define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
233 #define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
234  
235 #ifdef __GNUC__
236 #define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
237 #define SPA_FORMAT_ARG_FUNC(arg1) __attribute__((format_arg(arg1)))
238 #define SPA_ALIGNED(align) __attribute__((aligned(align)))
239 #define SPA_DEPRECATED __attribute__ ((deprecated))
240 #define SPA_EXPORT __attribute__((visibility("default")))
241 #define SPA_SENTINEL __attribute__((__sentinel__))
242 #define SPA_UNUSED __attribute__ ((unused))
243 #define SPA_NORETURN __attribute__ ((noreturn))
244 #define SPA_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
245 #define SPA_BARRIER __asm__ __volatile__("": : :"memory")
246 #else
247 #define SPA_PRINTF_FUNC(fmt, arg1)
248 #define SPA_FORMAT_ARG_FUNC(arg1)
249 #define SPA_ALIGNED(align)
250 #define SPA_DEPRECATED
251 #define SPA_EXPORT
252 #define SPA_SENTINEL
253 #define SPA_UNUSED
254 #define SPA_NORETURN
255 #define SPA_WARN_UNUSED_RESULT
256 #define SPA_BARRIER
257 #endif
258  
259 #ifndef SPA_API_IMPL
260 #define SPA_API_PROTO static inline
261 #define SPA_API_IMPL static inline
262 #endif
263  
264 #ifndef SPA_API_UTILS_DEFS
265 #ifdef SPA_API_IMPL
266 #define SPA_API_UTILS_DEFS SPA_API_IMPL
267 #else
268 #define SPA_API_UTILS_DEFS static inline
269 #endif
270 #endif
271  
272  
273 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
274 #define SPA_RESTRICT restrict
275 #elif defined(__GNUC__) && __GNUC__ >= 4
276 #define SPA_RESTRICT __restrict__
277 #else
278 #define SPA_RESTRICT
279 #endif
280  
281 #define SPA_ROUND_DOWN(num,value) \
282 ({ \
283 __typeof__(num) _num = (num); \
284 ((_num) - ((_num) % (value))); \
285 })
286 #define SPA_ROUND_UP(num,value) \
287 ({ \
288 __typeof__(value) _v = (value); \
289 ((((num) + (_v) - 1) / (_v)) * (_v)); \
290 })
291  
292 #define SPA_ROUND_MASK(num,mask) ((__typeof__(num))((mask)-1))
293  
294 #define SPA_ROUND_DOWN_N(num,align) ((num) & ~SPA_ROUND_MASK(num, align))
295 #define SPA_ROUND_UP_N(num,align) ((((num)-1) | SPA_ROUND_MASK(num, align))+1)
296  
297 #define SPA_SCALE32(val,num,denom) \
298 ({ \
299 uint64_t _val = (val); \
300 uint64_t _denom = (denom); \
301 (uint32_t)(((_val) * (num)) / (_denom)); \
302 })
303  
304 #define SPA_SCALE32_UP(val,num,denom) \
305 ({ \
306 uint64_t _val = (val); \
307 uint64_t _denom = (denom); \
308 (uint32_t)(((_val) * (num) + (_denom)-1) / (_denom)); \
309 })
310  
311  
312 #define SPA_PTR_ALIGNMENT(p,align) ((uintptr_t)(p) & ((align)-1))
313 #define SPA_IS_ALIGNED(p,align) (SPA_PTR_ALIGNMENT(p,align) == 0)
314 #define SPA_PTR_ALIGN(p,align,type) ((type*)SPA_ROUND_UP_N((intptr_t)(p), (intptr_t)(align)))
315  
316 #ifndef SPA_LIKELY
317 #ifdef __GNUC__
318 #define SPA_LIKELY(x) (__builtin_expect(!!(x),1))
319 #define SPA_UNLIKELY(x) (__builtin_expect(!!(x),0))
320 #else
321 #define SPA_LIKELY(x) (x)
322 #define SPA_UNLIKELY(x) (x)
323 #endif
324 #endif
325  
326 SPA_API_UTILS_DEFS bool spa_ptrinside(const void *p1, size_t s1, const void *p2, size_t s2,
327 size_t *remaining)
328 {
329 if (SPA_LIKELY((uintptr_t)p1 <= (uintptr_t)p2 && s2 <= s1 &&
330 (uintptr_t)p2 - (uintptr_t)p1 <= s1 - s2)) {
331 if (remaining != NULL)
332 *remaining = ((uintptr_t)p1 + s1) - ((uintptr_t)p2 + s2);
333 return true;
334 } else {
335 if (remaining != NULL)
336 *remaining = 0;
337 return false;
338 }
339 }
340  
341 SPA_API_UTILS_DEFS bool spa_ptr_inside_and_aligned(const void *p1, size_t s1,
342 const void *p2, size_t s2, size_t align,
343 size_t *remaining)
344 {
345 if (SPA_IS_ALIGNED(p2, align)) {
346 return spa_ptrinside(p1, s1, p2, s2, remaining);
347 } else {
348 if (remaining != NULL)
349 *remaining = 0;
350 return false;
351 }
352 }
353  
354 #define spa_ptr_type_inside(p1, s1, p2, type, remaining) \
355 spa_ptr_inside_and_aligned(p1, s1, p2, sizeof(type), SPA_ALIGNOF(type), remaining)
356  
357 #define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
358 #define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
359  
360 #define SPA_STRINGIFY_1(...) #__VA_ARGS__
361 #define SPA_STRINGIFY(...) SPA_STRINGIFY_1(__VA_ARGS__)
362  
363 struct spa_error_location {
364 int line;
365 int col;
366 size_t len;
367 const char *location;
368 const char *reason;
369 };
370  
371 #define spa_return_if_fail(expr) \
372 do { \
373 if (SPA_UNLIKELY(!(expr))) { \
374 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
375 #expr , __FILE__, __LINE__, __func__); \
376 return; \
377 } \
378 } while(false)
379  
380 #define spa_return_val_if_fail(expr, val) \
381 do { \
382 if (SPA_UNLIKELY(!(expr))) { \
383 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
384 #expr , __FILE__, __LINE__, __func__); \
385 return (val); \
386 } \
387 } while(false)
388  
389 /* spa_assert_se() is an assert which guarantees side effects of x,
390 * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
391 #ifndef __COVERITY__
392 #define spa_assert_se(expr) \
393 do { \
394 if (SPA_UNLIKELY(!(expr))) { \
395 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
396 #expr , __FILE__, __LINE__, __func__); \
397 abort(); \
398 } \
399 } while (false)
400 #else
401 #define spa_assert_se(expr) \
402 do { \
403 int _unique_var = (expr); \
404 if (!_unique_var) \
405 abort(); \
406 } while (false)
407 #endif
408  
409 /* Does exactly nothing */
410 #define spa_nop() do {} while (false)
411  
412 #ifdef NDEBUG
413 #define spa_assert(expr) spa_nop()
414 #elif defined (FASTPATH)
415 #define spa_assert(expr) spa_assert_se(expr)
416 #else
417 #define spa_assert(expr) spa_assert_se(expr)
418 #endif
419  
420 #ifdef NDEBUG
421 #define spa_assert_not_reached() abort()
422 #else
423 #define spa_assert_not_reached() \
424 do { \
425 fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
426 __FILE__, __LINE__, __func__); \
427 abort(); \
428 } while (false)
429 #endif
430  
431 #define spa_memzero(x,l) (memset((x), 0, (l)))
432 #define spa_zero(x) (spa_memzero(&(x), sizeof(x)))
433  
434 #ifdef SPA_DEBUG_MEMCPY
435 #define spa_memcpy(d,s,n) \
436 ({ \
437 fprintf(stderr, "%s:%u %s() memcpy(%p, %p, %zd)\n", \
438 __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
439 memcpy(d,s,n); \
440 })
441 #define spa_memmove(d,s,n) \
442 ({ \
443 fprintf(stderr, "%s:%u %s() memmove(%p, %p, %zd)\n", \
444 __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
445 memmove(d,s,n); \
446 })
447 #else
448 #define spa_memcpy(d,s,n) memcpy(d,s,n)
449 #define spa_memmove(d,s,n) memmove(d,s,n)
450 #endif
451  
452 #define spa_aprintf(_fmt, ...) \
453 ({ \
454 char *_strp; \
455 if (asprintf(&(_strp), (_fmt), ## __VA_ARGS__ ) == -1) \
456 _strp = NULL; \
457 _strp; \
458 })
459  
460 /**
461 * \}
462 */
463  
464 #ifdef __cplusplus
465 } /* extern "C" */
466 #endif
467  
468 #endif /* SPA_UTILS_DEFS_H */