2016-06-28 12:21:56 +02:00
|
|
|
/* Simple Plugin API
|
|
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Copyright © 2018 Wim Taymans
|
2016-06-28 12:21:56 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
2016-06-28 12:21:56 +02:00
|
|
|
*
|
2018-11-05 17:48:52 +01:00
|
|
|
* The above copyright notice and this permission notice (including the next
|
|
|
|
|
* paragraph) shall be included in all copies or substantial portions of the
|
|
|
|
|
* Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
2016-06-28 12:21:56 +02:00
|
|
|
*/
|
|
|
|
|
|
2019-01-14 12:58:23 +01:00
|
|
|
#ifndef SPA_UTILS_DEFS_H
|
|
|
|
|
#define SPA_UTILS_DEFS_H
|
2016-06-28 12:21:56 +02:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
2022-07-30 13:10:19 -04:00
|
|
|
# if __cplusplus >= 201103L
|
|
|
|
|
# define SPA_STATIC_ASSERT static_assert
|
|
|
|
|
# endif
|
2016-06-28 12:21:56 +02:00
|
|
|
#else
|
2022-07-30 13:10:19 -04:00
|
|
|
# include <stdbool.h>
|
|
|
|
|
# if __STDC_VERSION__ >= 201112L
|
|
|
|
|
# define SPA_STATIC_ASSERT _Static_assert
|
|
|
|
|
# endif
|
|
|
|
|
#endif
|
|
|
|
|
#ifndef SPA_STATIC_ASSERT
|
|
|
|
|
#define SPA_STATIC_ASSERT(a, b) \
|
|
|
|
|
((void)sizeof(struct { int spa_static_assertion_failed : 2 * !!(a) - 1; }))
|
2016-06-28 12:21:56 +02:00
|
|
|
#endif
|
|
|
|
|
#include <inttypes.h>
|
2021-06-07 20:53:16 +03:00
|
|
|
#include <signal.h>
|
2016-06-28 12:21:56 +02:00
|
|
|
#include <stdlib.h>
|
2016-11-18 17:46:01 +01:00
|
|
|
#include <string.h>
|
2016-08-03 15:59:17 +02:00
|
|
|
#include <stddef.h>
|
2018-03-20 11:37:11 +01:00
|
|
|
#include <stdio.h>
|
2016-06-28 12:21:56 +02:00
|
|
|
|
2021-10-02 20:55:53 +03:00
|
|
|
/**
|
|
|
|
|
* \defgroup spa_utils_defs Miscellaneous
|
2021-10-04 00:00:41 +03:00
|
|
|
* Helper macros and functions
|
2021-05-21 14:03:07 +10:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-02 20:55:53 +03:00
|
|
|
* \addtogroup spa_utils_defs
|
2021-05-21 14:03:07 +10:00
|
|
|
* \{
|
|
|
|
|
*/
|
|
|
|
|
|
2020-10-13 09:58:43 +02:00
|
|
|
/**
|
|
|
|
|
* SPA_FALLTHROUGH is an annotation to suppress compiler warnings about switch
|
|
|
|
|
* cases that fall through without a break or return statement. SPA_FALLTHROUGH
|
|
|
|
|
* is only needed on cases that have code:
|
|
|
|
|
*
|
|
|
|
|
* switch (foo) {
|
|
|
|
|
* case 1: // These cases have no code. No fallthrough annotations are needed.
|
|
|
|
|
* case 2:
|
|
|
|
|
* case 3:
|
|
|
|
|
* foo = 4; // This case has code, so a fallthrough annotation is needed:
|
|
|
|
|
* SPA_FALLTHROUGH;
|
|
|
|
|
* default:
|
|
|
|
|
* return foo;
|
|
|
|
|
* }
|
|
|
|
|
*/
|
2021-08-03 12:59:26 +01:00
|
|
|
#if defined(__clang__) && defined(__cplusplus) && __cplusplus >= 201103L
|
2020-10-13 09:58:43 +02:00
|
|
|
/* clang's fallthrough annotations are only available starting in C++11. */
|
|
|
|
|
# define SPA_FALLTHROUGH [[clang::fallthrough]];
|
2020-11-27 22:25:03 +01:00
|
|
|
#elif __GNUC__ >= 7 || __clang_major__ >= 10
|
|
|
|
|
# define SPA_FALLTHROUGH __attribute__ ((fallthrough));
|
2020-10-13 09:58:43 +02:00
|
|
|
#else
|
|
|
|
|
# define SPA_FALLTHROUGH /* FALLTHROUGH */
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-09-28 17:47:37 +02:00
|
|
|
#define SPA_FLAG_MASK(field,mask,flag) (((field) & (mask)) == (flag))
|
2022-09-30 16:18:32 +02:00
|
|
|
#define SPA_FLAG_IS_SET(field,flag) SPA_FLAG_MASK(field, flag, flag)
|
|
|
|
|
|
2018-01-18 15:39:03 +01:00
|
|
|
#define SPA_FLAG_SET(field,flag) ((field) |= (flag))
|
2022-07-30 13:10:19 -04:00
|
|
|
#define SPA_FLAG_CLEAR(field, flag) \
|
|
|
|
|
({ \
|
|
|
|
|
SPA_STATIC_ASSERT(__builtin_constant_p(flag) ? \
|
|
|
|
|
(__typeof__(flag))(__typeof__(field))(__typeof__(flag))(flag) == (flag) : \
|
|
|
|
|
sizeof(field) >= sizeof(flag), \
|
|
|
|
|
"truncation problem when masking " #field \
|
|
|
|
|
" with ~" #flag); \
|
|
|
|
|
((field) &= ~(__typeof__(field))(flag)); \
|
|
|
|
|
})
|
|
|
|
|
#define SPA_FLAG_UPDATE(field,flag,val) ((val) ? SPA_FLAG_SET((field),(flag)) : SPA_FLAG_CLEAR((field),(flag)))
|
2018-01-18 15:39:03 +01:00
|
|
|
|
2017-05-25 13:28:15 +02:00
|
|
|
enum spa_direction {
|
2017-05-26 08:05:01 +02:00
|
|
|
SPA_DIRECTION_INPUT = 0,
|
|
|
|
|
SPA_DIRECTION_OUTPUT = 1,
|
2017-05-25 13:28:15 +02:00
|
|
|
};
|
2016-10-03 19:43:42 +02:00
|
|
|
|
2018-04-02 10:34:16 +02:00
|
|
|
#define SPA_DIRECTION_REVERSE(d) ((d) ^ 1)
|
|
|
|
|
|
2022-09-01 17:02:44 -04:00
|
|
|
#define SPA_RECTANGLE(width,height) ((struct spa_rectangle){ (width), (height) })
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_rectangle {
|
2017-05-26 08:05:01 +02:00
|
|
|
uint32_t width;
|
|
|
|
|
uint32_t height;
|
2017-05-25 13:28:15 +02:00
|
|
|
};
|
2017-02-22 13:12:32 +01:00
|
|
|
|
2022-09-01 17:02:44 -04:00
|
|
|
#define SPA_POINT(x,y) ((struct spa_point){ (x), (y) })
|
2018-07-09 12:07:30 +02:00
|
|
|
struct spa_point {
|
|
|
|
|
int32_t x;
|
|
|
|
|
int32_t y;
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-01 17:02:44 -04:00
|
|
|
#define SPA_REGION(x,y,width,height) ((struct spa_region){ SPA_POINT(x,y), SPA_RECTANGLE(width,height) })
|
2018-07-09 12:07:30 +02:00
|
|
|
struct spa_region {
|
|
|
|
|
struct spa_point position;
|
|
|
|
|
struct spa_rectangle size;
|
|
|
|
|
};
|
|
|
|
|
|
2022-09-01 17:02:44 -04:00
|
|
|
#define SPA_FRACTION(num,denom) ((struct spa_fraction){ (num), (denom) })
|
2017-05-25 13:28:15 +02:00
|
|
|
struct spa_fraction {
|
2017-05-26 08:05:01 +02:00
|
|
|
uint32_t num;
|
|
|
|
|
uint32_t denom;
|
2017-05-25 13:28:15 +02:00
|
|
|
};
|
2016-06-28 12:21:56 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
#define SPA_N_ELEMENTS(arr) (sizeof(arr) / sizeof((arr)[0]))
|
2021-06-03 15:23:29 +10:00
|
|
|
/**
|
|
|
|
|
* Array iterator macro. Usage:
|
|
|
|
|
* ```c
|
2021-06-25 19:30:24 +02:00
|
|
|
* struct foo array[16];
|
2021-06-03 15:23:29 +10:00
|
|
|
* struct foo *f;
|
|
|
|
|
* SPA_FOR_EACH_ELEMENT(array, f) {
|
|
|
|
|
* f->bar = baz;
|
|
|
|
|
* }
|
|
|
|
|
* ```
|
|
|
|
|
*/
|
|
|
|
|
#define SPA_FOR_EACH_ELEMENT(arr, ptr) \
|
2022-09-01 17:02:44 -04:00
|
|
|
for ((ptr) = arr; (void*)(ptr) < SPA_PTROFF(arr, sizeof(arr), void); (ptr)++)
|
2017-11-16 16:31:03 +01:00
|
|
|
|
2022-09-30 16:21:28 +02:00
|
|
|
#define SPA_FOR_EACH_ELEMENT_VAR(arr, var) \
|
2023-02-05 10:27:12 +01:00
|
|
|
for (__typeof__((arr)[0])* var = arr; (void*)(var) < SPA_PTROFF(arr, sizeof(arr), void); (var)++)
|
2022-09-30 16:21:28 +02:00
|
|
|
|
2021-09-21 12:16:11 +02:00
|
|
|
#define SPA_ABS(a) \
|
|
|
|
|
({ \
|
|
|
|
|
__typeof__(a) _a = (a); \
|
|
|
|
|
SPA_LIKELY(_a >= 0) ? _a : -_a; \
|
|
|
|
|
})
|
2022-09-30 16:18:32 +02:00
|
|
|
#define SPA_MIN(a,b) \
|
|
|
|
|
({ \
|
|
|
|
|
__typeof__(a) _min_a = (a); \
|
|
|
|
|
__typeof__(b) _min_b = (b); \
|
2022-07-09 20:35:47 +02:00
|
|
|
SPA_LIKELY(_min_a <= _min_b) ? _min_a : _min_b; \
|
2017-11-16 16:31:03 +01:00
|
|
|
})
|
2022-09-30 16:18:32 +02:00
|
|
|
#define SPA_MAX(a,b) \
|
|
|
|
|
({ \
|
|
|
|
|
__typeof__(a) _max_a = (a); \
|
|
|
|
|
__typeof__(b) _max_b = (b); \
|
2022-07-09 20:35:47 +02:00
|
|
|
SPA_LIKELY(_max_a >= _max_b) ? _max_a : _max_b; \
|
2017-11-16 16:31:03 +01:00
|
|
|
})
|
|
|
|
|
#define SPA_CLAMP(v,low,high) \
|
|
|
|
|
({ \
|
2018-09-06 12:38:44 +02:00
|
|
|
__typeof__(v) _v = (v); \
|
|
|
|
|
__typeof__(low) _low = (low); \
|
|
|
|
|
__typeof__(high) _high = (high); \
|
2019-01-07 13:39:57 +01:00
|
|
|
SPA_MIN(SPA_MAX(_v, _low), _high); \
|
2017-11-16 16:31:03 +01:00
|
|
|
})
|
2016-06-28 12:21:56 +02:00
|
|
|
|
2022-07-19 17:59:14 +02:00
|
|
|
#define SPA_CLAMPF(v,low,high) \
|
|
|
|
|
({ \
|
|
|
|
|
fminf(fmaxf(v, low), high); \
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
2019-03-14 12:04:20 +01:00
|
|
|
#define SPA_SWAP(a,b) \
|
|
|
|
|
({ \
|
|
|
|
|
__typeof__(a) _t = (a); \
|
2022-09-30 16:18:32 +02:00
|
|
|
(a) = b; (b) = _t; \
|
2019-03-14 12:04:20 +01:00
|
|
|
})
|
|
|
|
|
|
2019-01-25 16:17:29 +01:00
|
|
|
#define SPA_TYPECHECK(type,x) \
|
2019-01-25 16:52:32 +01:00
|
|
|
({ type _dummy; \
|
|
|
|
|
typeof(x) _dummy2; \
|
|
|
|
|
(void)(&_dummy == &_dummy2); \
|
2019-01-25 16:17:29 +01:00
|
|
|
x; \
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-06 13:41:44 +10:00
|
|
|
/**
|
|
|
|
|
* Return the address (buffer + offset) as pointer of \a type
|
|
|
|
|
*/
|
2021-11-01 21:01:18 +01:00
|
|
|
#define SPA_PTROFF(ptr_,offset_,type_) ((type_*)((uintptr_t)(ptr_) + (ptrdiff_t)(offset_)))
|
2021-05-06 13:41:44 +10:00
|
|
|
#define SPA_PTROFF_ALIGN(ptr_,offset_,alignment_,type_) \
|
|
|
|
|
SPA_PTR_ALIGN(SPA_PTROFF(ptr_,offset_,type_),alignment_,type_)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deprecated, use SPA_PTROFF and SPA_PTROFF_ALIGN instead
|
|
|
|
|
*/
|
|
|
|
|
#define SPA_MEMBER(b,o,t) SPA_PTROFF(b,o,t)
|
|
|
|
|
#define SPA_MEMBER_ALIGN(b,o,a,t) SPA_PTROFF_ALIGN(b,o,a,t)
|
2016-10-07 17:10:46 +02:00
|
|
|
|
2022-09-01 17:02:44 -04:00
|
|
|
#define SPA_CONTAINER_OF(p,t,m) ((t*)((uintptr_t)(p) - offsetof(t,m)))
|
2016-10-07 17:10:46 +02:00
|
|
|
|
2021-11-01 21:01:18 +01:00
|
|
|
#define SPA_PTRDIFF(p1,p2) ((intptr_t)(p1) - (intptr_t)(p2))
|
2016-08-03 15:59:17 +02:00
|
|
|
|
|
|
|
|
#define SPA_PTR_TO_INT(p) ((int) ((intptr_t) (p)))
|
|
|
|
|
#define SPA_INT_TO_PTR(u) ((void*) ((intptr_t) (u)))
|
|
|
|
|
|
|
|
|
|
#define SPA_PTR_TO_UINT32(p) ((uint32_t) ((uintptr_t) (p)))
|
|
|
|
|
#define SPA_UINT32_TO_PTR(u) ((void*) ((uintptr_t) (u)))
|
|
|
|
|
|
2016-09-20 11:20:43 +02:00
|
|
|
#define SPA_TIME_INVALID ((int64_t)INT64_MIN)
|
2016-09-07 13:43:14 +02:00
|
|
|
#define SPA_IDX_INVALID ((unsigned int)-1)
|
2018-08-27 15:03:11 +02:00
|
|
|
#define SPA_ID_INVALID ((uint32_t)0xffffffff)
|
2016-08-02 16:34:44 +02:00
|
|
|
|
2022-09-01 17:02:44 -04:00
|
|
|
#define SPA_NSEC_PER_SEC (1000000000LL)
|
2017-04-17 16:32:25 +02:00
|
|
|
#define SPA_NSEC_PER_MSEC (1000000ll)
|
|
|
|
|
#define SPA_NSEC_PER_USEC (1000ll)
|
|
|
|
|
#define SPA_USEC_PER_SEC (1000000ll)
|
|
|
|
|
#define SPA_USEC_PER_MSEC (1000ll)
|
|
|
|
|
#define SPA_MSEC_PER_SEC (1000ll)
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2018-11-07 09:56:24 +01:00
|
|
|
#define SPA_TIMESPEC_TO_NSEC(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
|
|
|
|
|
#define SPA_TIMESPEC_TO_USEC(ts) ((ts)->tv_sec * SPA_USEC_PER_SEC + (ts)->tv_nsec / SPA_NSEC_PER_USEC)
|
|
|
|
|
#define SPA_TIMEVAL_TO_NSEC(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * SPA_NSEC_PER_USEC)
|
|
|
|
|
#define SPA_TIMEVAL_TO_USEC(tv) ((tv)->tv_sec * SPA_USEC_PER_SEC + (tv)->tv_usec)
|
2016-09-20 11:20:43 +02:00
|
|
|
|
2016-10-05 18:34:36 +02:00
|
|
|
#ifdef __GNUC__
|
2017-05-26 08:05:01 +02:00
|
|
|
#define SPA_PRINTF_FUNC(fmt, arg1) __attribute__((format(printf, fmt, arg1)))
|
2021-09-02 13:44:24 +10:00
|
|
|
#define SPA_FORMAT_ARG_FUNC(arg1) __attribute__((format_arg(arg1)))
|
2017-05-26 08:05:01 +02:00
|
|
|
#define SPA_ALIGNED(align) __attribute__((aligned(align)))
|
2017-05-30 20:33:32 +02:00
|
|
|
#define SPA_DEPRECATED __attribute__ ((deprecated))
|
2019-07-09 21:13:26 +02:00
|
|
|
#define SPA_EXPORT __attribute__((visibility("default")))
|
|
|
|
|
#define SPA_SENTINEL __attribute__((__sentinel__))
|
2020-04-01 12:45:11 +02:00
|
|
|
#define SPA_UNUSED __attribute__ ((unused))
|
2021-05-27 10:05:19 +10:00
|
|
|
#define SPA_NORETURN __attribute__ ((noreturn))
|
2022-08-19 08:19:35 +02:00
|
|
|
#define SPA_WARN_UNUSED_RESULT __attribute__ ((warn_unused_result))
|
2016-10-05 18:34:36 +02:00
|
|
|
#else
|
2017-05-26 08:05:01 +02:00
|
|
|
#define SPA_PRINTF_FUNC(fmt, arg1)
|
2021-09-02 13:44:24 +10:00
|
|
|
#define SPA_FORMAT_ARG_FUNC(arg1)
|
2017-05-26 08:05:01 +02:00
|
|
|
#define SPA_ALIGNED(align)
|
2017-05-30 20:33:32 +02:00
|
|
|
#define SPA_DEPRECATED
|
2019-02-06 09:21:16 +01:00
|
|
|
#define SPA_EXPORT
|
2019-07-09 21:13:26 +02:00
|
|
|
#define SPA_SENTINEL
|
2020-04-01 12:45:11 +02:00
|
|
|
#define SPA_UNUSED
|
2021-05-27 10:05:19 +10:00
|
|
|
#define SPA_NORETURN
|
2022-08-19 08:19:35 +02:00
|
|
|
#define SPA_WARN_UNUSED_RESULT
|
2016-10-05 18:34:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
2019-03-20 13:04:44 +01:00
|
|
|
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
|
|
|
|
|
#define SPA_RESTRICT restrict
|
|
|
|
|
#elif defined(__GNUC__) && __GNUC__ >= 4
|
|
|
|
|
#define SPA_RESTRICT __restrict__
|
|
|
|
|
#else
|
|
|
|
|
#define SPA_RESTRICT
|
|
|
|
|
#endif
|
|
|
|
|
|
2022-09-30 16:18:32 +02:00
|
|
|
#define SPA_ROUND_DOWN(num,value) \
|
|
|
|
|
({ \
|
|
|
|
|
__typeof__(num) _num = (num); \
|
|
|
|
|
((_num) - ((_num) % (value))); \
|
|
|
|
|
})
|
|
|
|
|
#define SPA_ROUND_UP(num,value) \
|
|
|
|
|
({ \
|
|
|
|
|
__typeof__(value) _v = (value); \
|
|
|
|
|
((((num) + (_v) - 1) / (_v)) * (_v)); \
|
2022-07-30 13:10:19 -04:00
|
|
|
})
|
|
|
|
|
|
2022-09-30 16:18:32 +02:00
|
|
|
#define SPA_ROUND_MASK(num,mask) ((__typeof__(num))((mask)-1))
|
|
|
|
|
|
|
|
|
|
#define SPA_ROUND_DOWN_N(num,align) ((num) & ~SPA_ROUND_MASK(num, align))
|
|
|
|
|
#define SPA_ROUND_UP_N(num,align) ((((num)-1) | SPA_ROUND_MASK(num, align))+1)
|
2016-10-28 16:56:33 +02:00
|
|
|
|
2023-01-16 18:28:31 +01:00
|
|
|
#define SPA_SCALE32_UP(val,num,denom) \
|
|
|
|
|
({ \
|
|
|
|
|
uint64_t _val = (val); \
|
|
|
|
|
uint64_t _denom = (denom); \
|
|
|
|
|
(uint32_t)(((_val) * (num) + (_denom)-1) / (_denom)); \
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
2019-03-26 12:58:26 +01:00
|
|
|
#define SPA_PTR_ALIGNMENT(p,align) ((intptr_t)(p) & ((align)-1))
|
|
|
|
|
#define SPA_IS_ALIGNED(p,align) (SPA_PTR_ALIGNMENT(p,align) == 0)
|
2022-04-17 02:09:43 +02:00
|
|
|
#define SPA_PTR_ALIGN(p,align,type) ((type*)SPA_ROUND_UP_N((intptr_t)(p), (intptr_t)(align)))
|
2019-01-24 18:28:52 +01:00
|
|
|
|
2016-10-12 17:27:29 +02:00
|
|
|
#ifndef SPA_LIKELY
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
|
#define SPA_LIKELY(x) (__builtin_expect(!!(x),1))
|
|
|
|
|
#define SPA_UNLIKELY(x) (__builtin_expect(!!(x),0))
|
|
|
|
|
#else
|
|
|
|
|
#define SPA_LIKELY(x) (x)
|
|
|
|
|
#define SPA_UNLIKELY(x) (x)
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-03-17 11:37:56 +01:00
|
|
|
#define SPA_STRINGIFY_1(...) #__VA_ARGS__
|
|
|
|
|
#define SPA_STRINGIFY(...) SPA_STRINGIFY_1(__VA_ARGS__)
|
2017-07-11 20:38:48 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
#define spa_return_if_fail(expr) \
|
|
|
|
|
do { \
|
2020-02-25 14:25:00 +01:00
|
|
|
if (SPA_UNLIKELY(!(expr))) { \
|
|
|
|
|
fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
|
|
|
|
|
#expr , __FILE__, __LINE__, __func__); \
|
2017-05-26 08:05:01 +02:00
|
|
|
return; \
|
2020-02-25 14:25:00 +01:00
|
|
|
} \
|
2017-05-26 08:05:01 +02:00
|
|
|
} while(false)
|
2016-10-12 17:27:29 +02:00
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
#define spa_return_val_if_fail(expr, val) \
|
|
|
|
|
do { \
|
2020-02-25 14:25:00 +01:00
|
|
|
if (SPA_UNLIKELY(!(expr))) { \
|
|
|
|
|
fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
|
|
|
|
|
#expr , __FILE__, __LINE__, __func__); \
|
2017-05-26 08:05:01 +02:00
|
|
|
return (val); \
|
2020-02-25 14:25:00 +01:00
|
|
|
} \
|
2017-05-26 08:05:01 +02:00
|
|
|
} while(false)
|
2016-10-12 17:27:29 +02:00
|
|
|
|
|
|
|
|
/* spa_assert_se() is an assert which guarantees side effects of x,
|
|
|
|
|
* i.e. is never optimized away, regardless of NDEBUG or FASTPATH. */
|
2021-06-11 20:05:25 +02:00
|
|
|
#ifndef __COVERITY__
|
2017-05-26 08:05:01 +02:00
|
|
|
#define spa_assert_se(expr) \
|
|
|
|
|
do { \
|
2020-05-18 18:58:15 +02:00
|
|
|
if (SPA_UNLIKELY(!(expr))) { \
|
2020-02-25 14:25:00 +01:00
|
|
|
fprintf(stderr, "'%s' failed at %s:%u %s()\n", \
|
2018-03-20 11:37:11 +01:00
|
|
|
#expr , __FILE__, __LINE__, __func__); \
|
2017-05-26 08:05:01 +02:00
|
|
|
abort(); \
|
2020-05-18 18:58:15 +02:00
|
|
|
} \
|
2017-05-26 08:05:01 +02:00
|
|
|
} while (false)
|
2021-06-11 20:05:25 +02:00
|
|
|
#else
|
|
|
|
|
#define spa_assert_se(expr) \
|
2018-03-20 11:37:11 +01:00
|
|
|
do { \
|
2021-06-11 20:05:25 +02:00
|
|
|
int _unique_var = (expr); \
|
|
|
|
|
if (!_unique_var) \
|
2018-03-20 11:37:11 +01:00
|
|
|
abort(); \
|
2021-06-11 20:05:25 +02:00
|
|
|
} while (false)
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* Does exactly nothing */
|
|
|
|
|
#define spa_nop() do {} while (false)
|
|
|
|
|
|
|
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#define spa_assert(expr) spa_nop()
|
|
|
|
|
#elif defined (FASTPATH)
|
|
|
|
|
#define spa_assert(expr) spa_assert_se(expr)
|
|
|
|
|
#else
|
|
|
|
|
#define spa_assert(expr) spa_assert_se(expr)
|
|
|
|
|
#endif
|
2018-03-20 11:37:11 +01:00
|
|
|
|
2021-06-11 20:05:25 +02:00
|
|
|
#ifdef NDEBUG
|
|
|
|
|
#define spa_assert_not_reached() abort()
|
|
|
|
|
#else
|
2018-06-01 11:08:25 +02:00
|
|
|
#define spa_assert_not_reached() \
|
|
|
|
|
do { \
|
2020-02-25 14:25:00 +01:00
|
|
|
fprintf(stderr, "Code should not be reached at %s:%u %s()\n", \
|
2018-06-01 11:08:25 +02:00
|
|
|
__FILE__, __LINE__, __func__); \
|
|
|
|
|
abort(); \
|
|
|
|
|
} while (false)
|
2021-06-11 20:05:25 +02:00
|
|
|
#endif
|
2016-10-12 17:27:29 +02:00
|
|
|
|
2016-10-28 16:56:33 +02:00
|
|
|
#define spa_memzero(x,l) (memset((x), 0, (l)))
|
|
|
|
|
#define spa_zero(x) (spa_memzero(&(x), sizeof(x)))
|
2016-08-02 16:34:44 +02:00
|
|
|
|
2019-03-20 19:27:46 +01:00
|
|
|
#ifdef SPA_DEBUG_MEMCPY
|
|
|
|
|
#define spa_memcpy(d,s,n) \
|
|
|
|
|
({ \
|
|
|
|
|
fprintf(stderr, "%s:%u %s() memcpy(%p, %p, %zd)\n", \
|
|
|
|
|
__FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
|
|
|
|
|
memcpy(d,s,n); \
|
|
|
|
|
})
|
2019-10-02 18:02:07 +02:00
|
|
|
#define spa_memmove(d,s,n) \
|
|
|
|
|
({ \
|
|
|
|
|
fprintf(stderr, "%s:%u %s() memmove(%p, %p, %zd)\n", \
|
|
|
|
|
__FILE__, __LINE__, __func__, (d), (s), (size_t)(n)); \
|
|
|
|
|
memmove(d,s,n); \
|
|
|
|
|
})
|
2019-03-20 19:27:46 +01:00
|
|
|
#else
|
|
|
|
|
#define spa_memcpy(d,s,n) memcpy(d,s,n)
|
2019-10-02 18:02:07 +02:00
|
|
|
#define spa_memmove(d,s,n) memmove(d,s,n)
|
2019-03-20 19:27:46 +01:00
|
|
|
#endif
|
|
|
|
|
|
2020-01-27 12:19:21 +01:00
|
|
|
#define spa_aprintf(_fmt, ...) \
|
|
|
|
|
({ \
|
|
|
|
|
char *_strp; \
|
|
|
|
|
if (asprintf(&(_strp), (_fmt), ## __VA_ARGS__ ) == -1) \
|
|
|
|
|
_strp = NULL; \
|
|
|
|
|
_strp; \
|
|
|
|
|
})
|
|
|
|
|
|
2021-05-21 14:03:07 +10:00
|
|
|
/**
|
|
|
|
|
* \}
|
|
|
|
|
*/
|
|
|
|
|
|
2016-06-28 12:21:56 +02:00
|
|
|
#ifdef __cplusplus
|
2017-05-26 08:05:01 +02:00
|
|
|
} /* extern "C" */
|
2016-06-28 12:21:56 +02:00
|
|
|
#endif
|
|
|
|
|
|
2019-01-14 12:58:23 +01:00
|
|
|
#endif /* SPA_UTILS_DEFS_H */
|