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