PipeWire 1.1.0
Loading...
Searching...
No Matches
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
9extern "C" {
10# if __cplusplus >= 201103L
11# define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) static_assert(expr, msg)
12# define SPA_ALIGNOF alignof
13# endif
14#elif __STDC_VERSION__ >= 202311L
15# define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) static_assert(expr, msg)
16# define SPA_ALIGNOF alignof
17#else
18# include <stdbool.h>
19# if __STDC_VERSION__ >= 201112L
20# define SPA_STATIC_ASSERT_IMPL(expr, msg, ...) _Static_assert(expr, msg)
21# define SPA_ALIGNOF _Alignof
22# endif
23#endif
24#ifndef SPA_STATIC_ASSERT_IMPL
25#define SPA_STATIC_ASSERT_IMPL(expr, ...) \
26 ((void)sizeof(struct { int spa_static_assertion_failed : 2 * !!(expr) - 1; }))
27#endif
28#ifndef SPA_ALIGNOF
29#define SPA_ALIGNOF __alignof__
30#endif
31
32#define SPA_STATIC_ASSERT(expr, ...) SPA_STATIC_ASSERT_IMPL(expr, ## __VA_ARGS__, "`" #expr "` evaluated to false")
33
34#define SPA_CONCAT_NOEXPAND(a, b) a ## b
35#define SPA_CONCAT(a, b) SPA_CONCAT_NOEXPAND(a, b)
36
37#include <inttypes.h>
38#include <signal.h>
39#include <stdlib.h>
40#include <string.h>
41#include <stddef.h>
42#include <stdio.h>
43
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
96};
97
98#define SPA_DIRECTION_REVERSE(d) ((d) ^ 1)
99
100#define SPA_RECTANGLE(width,height) ((struct spa_rectangle){ (width), (height) })
101struct spa_rectangle {
102 uint32_t width;
103 uint32_t height;
105
106#define SPA_POINT(x,y) ((struct spa_point){ (x), (y) })
107struct 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) })
113struct spa_region {
114 struct spa_point position;
118#define SPA_FRACTION(num,denom) ((struct spa_fraction){ (num), (denom) })
119struct spa_fraction {
120 uint32_t num;
121 uint32_t denom;
124#define SPA_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
135#define SPA_FOR_EACH_ELEMENT(arr, ptr) \
136 for ((ptr) = arr; (void*)(ptr) < SPA_PTROFF(arr, sizeof(arr), void); (ptr)++)
138#define SPA_FOR_EACH_ELEMENT_VAR(arr, var) \
139 for (__typeof__((arr)[0])* var = arr; (void*)(var) < SPA_PTROFF(arr, sizeof(arr), void); (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; \
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})
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
192#define SPA_PTROFF(ptr_,offset_,type_) ((type_*)((uintptr_t)(ptr_) + (ptrdiff_t)(offset_)))
193#define SPA_PTROFF_ALIGN(ptr_,offset_,alignment_,type_) \
194 SPA_PTR_ALIGN(SPA_PTROFF(ptr_,offset_,type_),alignment_,type_)
199#define SPA_MEMBER(b,o,t) SPA_PTROFF(b,o,t)
200#define SPA_MEMBER_ALIGN(b,o,a,t) SPA_PTROFF_ALIGN(b,o,a,t)
202#define SPA_CONTAINER_OF(p,t,m) ((t*)((uintptr_t)(p) - offsetof(t,m)))
203
204#define SPA_PTRDIFF(p1,p2) ((intptr_t)(p1) - (intptr_t)(p2))
205
206#define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
207#define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
208
209#define SPA_TIME_INVALID ((int64_t)INT64_MIN)
210#define SPA_IDX_INVALID ((unsigned int)-1)
211#define SPA_ID_INVALID ((uint32_t)0xffffffff)
213#define SPA_NSEC_PER_SEC (1000000000LL)
214#define SPA_NSEC_PER_MSEC (1000000ll)
215#define SPA_NSEC_PER_USEC (1000ll)
216#define SPA_USEC_PER_SEC (1000000ll)
217#define SPA_USEC_PER_MSEC (1000ll)
218#define SPA_MSEC_PER_SEC (1000ll)
219
220#define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
221#define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
222#define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
223#define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
224
225#ifdef __GNUC__
226#define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
227#define SPA_FORMAT_ARG_FUNC(arg1) __attribute__((format_arg(arg1)))
228#define SPA_ALIGNED(align) __attribute__((aligned(align)))
229#define SPA_DEPRECATED __attribute__ ((deprecated))
230#define SPA_EXPORT __attribute__((visibility("default")))
231#define SPA_SENTINEL __attribute__((__sentinel__))
232#define SPA_UNUSED __attribute__ ((unused))
233#define SPA_NORETURN __attribute__ ((noreturn))
234#define SPA_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
235#else
236#define SPA_PRINTF_FUNC(fmt, arg1)
237#define SPA_FORMAT_ARG_FUNC(arg1)
238#define SPA_ALIGNED(align)
239#define SPA_DEPRECATED
240#define SPA_EXPORT
241#define SPA_SENTINEL
242#define SPA_UNUSED
243#define SPA_NORETURN
244#define SPA_WARN_UNUSED_RESULT
245#endif
246
247#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
248#define SPA_RESTRICT restrict
249#elif defined(__GNUC__) && __GNUC__ >= 4
250#define SPA_RESTRICT __restrict__
251#else
252#define SPA_RESTRICT
253#endif
254
255#define SPA_ROUND_DOWN(num,value) \
256({ \
257 __typeof__(num) _num = (num); \
258 ((_num) - ((_num) % (value))); \
259})
260#define SPA_ROUND_UP(num,value) \
261({ \
262 __typeof__(value) _v = (value); \
263 ((((num) + (_v) - 1) / (_v)) * (_v)); \
264})
265
266#define SPA_ROUND_MASK(num,mask) ((__typeof__(num))((mask)-1))
267
268#define SPA_ROUND_DOWN_N(num,align) ((num) & ~SPA_ROUND_MASK(num, align))
269#define SPA_ROUND_UP_N(num,align) ((((num)-1) | SPA_ROUND_MASK(num, align))+1)
270
271#define SPA_SCALE32_UP(val,num,denom) \
272({ \
273 uint64_t _val = (val); \
274 uint64_t _denom = (denom); \
275 (uint32_t)(((_val) * (num) + (_denom)-1) / (_denom)); \
276})
277
278
279#define SPA_PTR_ALIGNMENT(p,align) ((uintptr_t)(p) & ((align)-1))
280#define SPA_IS_ALIGNED(p,align) (SPA_PTR_ALIGNMENT(p,align) == 0)
281#define SPA_PTR_ALIGN(p,align,type) ((type*)SPA_ROUND_UP_N((intptr_t)(p), (intptr_t)(align)))
282
283#ifndef SPA_LIKELY
284#ifdef __GNUC__
285#define SPA_LIKELY(x) (__builtin_expect(!!(x),1))
286#define SPA_UNLIKELY(x) (__builtin_expect(!!(x),0))
287#else
288#define SPA_LIKELY(x) (x)
289#define SPA_UNLIKELY(x) (x)
290#endif
291#endif
292
293static inline bool spa_ptrinside(const void *p1, size_t s1, const void *p2, size_t s2,
294 size_t *remaining)
296 if (SPA_LIKELY((uintptr_t)p1 <= (uintptr_t)p2 && s2 <= s1 &&
297 (uintptr_t)p2 - (uintptr_t)p1 <= s1 - s2)) {
298 if (remaining != NULL)
299 *remaining = ((uintptr_t)p1 + s1) - ((uintptr_t)p2 + s2);
300 return true;
301 } else {
302 if (remaining != NULL)
303 *remaining = 0;
304 return false;
305 }
306}
307
308static inline bool spa_ptr_inside_and_aligned(const void *p1, size_t s1,
309 const void *p2, size_t s2, size_t align,
310 size_t *remaining)
311{
312 if (SPA_IS_ALIGNED(p2, align)) {
313 return spa_ptrinside(p1, s1, p2, s2, remaining);
314 } else {
315 if (remaining != NULL)
316 *remaining = 0;
317 return false;
318 }
319}
321#define spa_ptr_type_inside(p1, s1, p2, type, remaining) \
322 spa_ptr_inside_and_aligned(p1, s1, p2, sizeof(type), SPA_ALIGNOF(type), remaining)
323
324#define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
325#define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
326
327#define SPA_STRINGIFY_1(...) #__VA_ARGS__
328#define SPA_STRINGIFY(...) SPA_STRINGIFY_1(__VA_ARGS__)
329
330#define spa_return_if_fail(expr) \
331 do { \
332 if (SPA_UNLIKELY(!(expr))) { \
333 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
334 #expr , __FILE__, __LINE__, __func__); \
335 return; \
336 } \
337 } while(false)
338
339#define spa_return_val_if_fail(expr, val) \
340 do { \
341 if (SPA_UNLIKELY(!(expr))) { \
342 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
343 #expr , __FILE__, __LINE__, __func__); \
344 return (val); \
345 } \
346 } while(false)
348/* spa_assert_se() is an assert which guarantees side effects of x,
349 * i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
350#ifndef __COVERITY__
351#define spa_assert_se(expr) \
352 do { \
353 if (SPA_UNLIKELY(!(expr))) { \
354 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
355 #expr , __FILE__, __LINE__, __func__); \
356 abort(); \
357 } \
358 } while (false)
359#else
360#define spa_assert_se(expr) \
361 do { \
362 int _unique_var = (expr); \
363 if (!_unique_var) \
364 abort(); \
365 } while (false)
366#endif
367
368/* Does exactly nothing */
369#define spa_nop() do {} while (false)
370
371#ifdef NDEBUG
372#define spa_assert(expr) spa_nop()
373#elif defined (FASTPATH)
374#define spa_assert(expr) spa_assert_se(expr)
375#else
376#define spa_assert(expr) spa_assert_se(expr)
377#endif
379#ifdef NDEBUG
380#define spa_assert_not_reached() abort()
381#else
382#define spa_assert_not_reached() \
383 do { \
384 fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
385 __FILE__, __LINE__, __func__); \
386 abort(); \
387 } while (false)
388#endif
389
390#define spa_memzero(x,l) (memset((x), 0, (l)))
391#define spa_zero(x) (spa_memzero(&(x), sizeof(x)))
392
393#ifdef SPA_DEBUG_MEMCPY
394#define spa_memcpy(d,s,n) \
395({ \
396 fprintf(stderr, "%s:%u %s() memcpy(%p, %p, %zd)\n", \
397 __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
398 memcpy(d,s,n); \
399})
400#define spa_memmove(d,s,n) \
401({ \
402 fprintf(stderr, "%s:%u %s() memmove(%p, %p, %zd)\n", \
403 __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
404 memmove(d,s,n); \
405})
406#else
407#define spa_memcpy(d,s,n) memcpy(d,s,n)
408#define spa_memmove(d,s,n) memmove(d,s,n)
409#endif
410
411#define spa_aprintf(_fmt, ...) \
412({ \
413 char *_strp; \
414 if (asprintf(&(_strp), (_fmt), ## __VA_ARGS__ ) == -1) \
415 _strp = NULL; \
416 _strp; \
417})
418
423#ifdef __cplusplus
424} /* extern "C" */
425#endif
426
427#endif /* SPA_UTILS_DEFS_H */
static bool spa_ptrinside(const void *p1, size_t s1, const void *p2, size_t s2, size_t *remaining)
Definition defs.h:363
#define SPA_LIKELY(x)
Definition defs.h:357
spa_direction
Definition defs.h:106
static bool spa_ptr_inside_and_aligned(const void *p1, size_t s1, const void *p2, size_t s2, size_t align, size_t *remaining)
Definition defs.h:378
#define SPA_IS_ALIGNED(p, align)
Definition defs.h:345
@ SPA_DIRECTION_INPUT
Definition defs.h:107
@ SPA_DIRECTION_OUTPUT
Definition defs.h:108
spa/utils/string.h
Definition defs.h:137
uint32_t num
Definition defs.h:138
uint32_t denom
Definition defs.h:139
Definition defs.h:123
int32_t y
Definition defs.h:125
int32_t x
Definition defs.h:124
Definition defs.h:116
uint32_t width
Definition defs.h:117
uint32_t height
Definition defs.h:118
Definition defs.h:130
struct spa_point position
Definition defs.h:131
struct spa_rectangle size
Definition defs.h:132