PipeWire: spa/utils/defs.h Source File

PipeWire 1.0.7

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