25 #ifndef SPA_UTILS_DEFS_H
26 #define SPA_UTILS_DEFS_H
65 #if defined(__clang__) && defined(__cplusplus) && __cplusplus >= 201103L
67 # define SPA_FALLTHROUGH [[clang::fallthrough]];
68 #elif __GNUC__ >= 7 || __clang_major__ >= 10
69 # define SPA_FALLTHROUGH __attribute__ ((fallthrough));
71 # define SPA_FALLTHROUGH
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))
85 #define SPA_DIRECTION_REVERSE(d) ((d) ^ 1)
87 #define SPA_RECTANGLE(width,height) (struct spa_rectangle){ width, height }
93 #define SPA_POINT(x,y) (struct spa_point){ x, y }
99 #define SPA_REGION(x,y,width,height) (struct spa_region){ SPA_POINT(x,y), SPA_RECTANGLE(width,height) }
105 #define SPA_FRACTION(num,denom) (struct spa_fraction){ num, denom }
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++)
127 __typeof__(a) _a = (a); \
128 SPA_LIKELY(_a >= 0) ? _a : -_a; \
130 #define SPA_MIN(a,b) \
132 __typeof__(a) _min_a = (a); \
133 __typeof__(b) _min_b = (b); \
134 SPA_LIKELY(_min_a <= _min_b) ? _min_a : _min_b; \
136 #define SPA_MAX(a,b) \
138 __typeof__(a) _max_a = (a); \
139 __typeof__(b) _max_b = (b); \
140 SPA_LIKELY(_max_a >= _max_b) ? _max_a : _max_b; \
142 #define SPA_CLAMP(v,low,high) \
144 __typeof__(v) _v = (v); \
145 __typeof__(low) _low = (low); \
146 __typeof__(high) _high = (high); \
147 SPA_MIN(SPA_MAX(_v, _low), _high); \
150 #define SPA_CLAMPF(v,low,high) \
152 fminf(fmaxf(v, low), high); \
156 #define SPA_SWAP(a,b) \
158 __typeof__(a) _t = (a); \
162 #define SPA_TYPECHECK(type,x) \
165 (void)(&_dummy == &_dummy2); \
172 #define SPA_PTROFF(ptr_,offset_,type_) ((type_*)((uintptr_t)(ptr_) + (ptrdiff_t)(offset_)))
173 #define SPA_PTROFF_ALIGN(ptr_,offset_,alignment_,type_) \
174 SPA_PTR_ALIGN(SPA_PTROFF(ptr_,offset_,type_),alignment_,type_)
180 #define SPA_MEMBER(b,o,t) SPA_PTROFF(b,o,t)
181 #define SPA_MEMBER_ALIGN(b,o,a,t) SPA_PTROFF_ALIGN(b,o,a,t)
183 #define SPA_CONTAINER_OF(p,t,m) ((t*)((uintptr_t)p - offsetof(t,m)))
185 #define SPA_PTRDIFF(p1,p2) ((intptr_t)(p1) - (intptr_t)(p2))
187 #define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
188 #define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
190 #define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
191 #define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
193 #define SPA_TIME_INVALID ((int64_t)INT64_MIN)
194 #define SPA_IDX_INVALID ((unsigned int)-1)
195 #define SPA_ID_INVALID ((uint32_t)0xffffffff)
197 #define SPA_NSEC_PER_SEC (1000000000ll)
198 #define SPA_NSEC_PER_MSEC (1000000ll)
199 #define SPA_NSEC_PER_USEC (1000ll)
200 #define SPA_USEC_PER_SEC (1000000ll)
201 #define SPA_USEC_PER_MSEC (1000ll)
202 #define SPA_MSEC_PER_SEC (1000ll)
204 #define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
205 #define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
206 #define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
207 #define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
210 #define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
211 #define SPA_FORMAT_ARG_FUNC(arg1) __attribute__((format_arg(arg1)))
212 #define SPA_ALIGNED(align) __attribute__((aligned(align)))
213 #define SPA_DEPRECATED __attribute__ ((deprecated))
214 #define SPA_EXPORT __attribute__((visibility("default")))
215 #define SPA_SENTINEL __attribute__((__sentinel__))
216 #define SPA_UNUSED __attribute__ ((unused))
217 #define SPA_NORETURN __attribute__ ((noreturn))
219 #define SPA_PRINTF_FUNC(fmt, arg1)
220 #define SPA_FORMAT_ARG_FUNC(arg1)
221 #define SPA_ALIGNED(align)
222 #define SPA_DEPRECATED
229 #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
230 #define SPA_RESTRICT restrict
231 #elif defined(__GNUC__) && __GNUC__ >= 4
232 #define SPA_RESTRICT __restrict__
237 #define SPA_ROUND_DOWN(num,value) ((num) - ((num) % (value)))
238 #define SPA_ROUND_UP(num,value) ((((num) + (value) - 1) / (value)) * (value))
240 #define SPA_ROUND_DOWN_N(num,align) ((num) & ~((align) - 1))
241 #define SPA_ROUND_UP_N(num,align) SPA_ROUND_DOWN_N((num) + ((align) - 1),align)
243 #define SPA_PTR_ALIGNMENT(p,align) ((intptr_t)(p) & ((align)-1))
244 #define SPA_IS_ALIGNED(p,align) (SPA_PTR_ALIGNMENT(p,align) == 0)
245 #define SPA_PTR_ALIGN(p,align,type) ((type*)SPA_ROUND_UP_N((intptr_t)(p), (intptr_t)(align)))
249 #define SPA_LIKELY(x) (__builtin_expect(!!(x),1))
250 #define SPA_UNLIKELY(x) (__builtin_expect(!!(x),0))
252 #define SPA_LIKELY(x) (x)
253 #define SPA_UNLIKELY(x) (x)
257 #define SPA_STRINGIFY_1(...) #__VA_ARGS__
258 #define SPA_STRINGIFY(...) SPA_STRINGIFY_1(__VA_ARGS__)
260 #define spa_return_if_fail(expr) \
262 if (SPA_UNLIKELY(!(expr))) { \
263 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
264 #expr , __FILE__, __LINE__, __func__); \
269 #define spa_return_val_if_fail(expr, val) \
271 if (SPA_UNLIKELY(!(expr))) { \
272 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
273 #expr , __FILE__, __LINE__, __func__); \
281 #define spa_assert_se(expr) \
283 if (SPA_UNLIKELY(!(expr))) { \
284 fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
285 #expr , __FILE__, __LINE__, __func__); \
290 #define spa_assert_se(expr) \
292 int _unique_var = (expr); \
299 #define spa_nop() do {} while (false)
302 #define spa_assert(expr) spa_nop()
303 #elif defined (FASTPATH)
304 #define spa_assert(expr) spa_assert_se(expr)
306 #define spa_assert(expr) spa_assert_se(expr)
310 #define spa_assert_not_reached() abort()
312 #define spa_assert_not_reached() \
314 fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
315 __FILE__, __LINE__, __func__); \
320 #define spa_memzero(x,l) (memset((x), 0, (l)))
321 #define spa_zero(x) (spa_memzero(&(x), sizeof(x)))
323 #ifdef SPA_DEBUG_MEMCPY
324 #define spa_memcpy(d,s,n) \
326 fprintf(stderr, "%s:%u %s() memcpy(%p, %p, %zd)\n", \
327 __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
330 #define spa_memmove(d,s,n) \
332 fprintf(stderr, "%s:%u %s() memmove(%p, %p, %zd)\n", \
333 __FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
337 #define spa_memcpy(d,s,n) memcpy(d,s,n)
338 #define spa_memmove(d,s,n) memmove(d,s,n)
341 #define spa_aprintf(_fmt, ...) \
344 if (asprintf(&(_strp), (_fmt), ## __VA_ARGS__ ) == -1) \
spa_direction
Definition: defs.h:90
@ SPA_DIRECTION_INPUT
Definition: defs.h:91
@ SPA_DIRECTION_OUTPUT
Definition: defs.h:92
uint32_t num
Definition: defs.h:122
uint32_t denom
Definition: defs.h:123
int32_t y
Definition: defs.h:109
int32_t x
Definition: defs.h:108
uint32_t width
Definition: defs.h:101
uint32_t height
Definition: defs.h:102
struct spa_point position
Definition: defs.h:115
struct spa_rectangle size
Definition: defs.h:116