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