2016-06-28 12:21:56 +02:00
|
|
|
/* Simple Plugin API
|
|
|
|
|
* Copyright (C) 2016 Wim Taymans <wim.taymans@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
* Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-11-10 16:11:53 +01:00
|
|
|
#ifndef __SPA_UTILS_DEFS_H__
|
|
|
|
|
#define __SPA_UTILS_DEFS_H__
|
2016-06-28 12:21:56 +02:00
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
extern "C" {
|
|
|
|
|
#else
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#endif
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
|
2017-05-26 15:55:23 +02:00
|
|
|
#define SPA_ASYNC_BIT (1 << 30)
|
2017-05-26 09:09:31 +02:00
|
|
|
#define SPA_ASYNC_MASK (3 << 30)
|
2017-05-26 15:55:23 +02:00
|
|
|
#define SPA_ASYNC_SEQ_MASK (SPA_ASYNC_BIT - 1)
|
2016-09-27 16:59:45 +02:00
|
|
|
|
2017-05-26 09:09:31 +02:00
|
|
|
#define SPA_RESULT_IS_OK(res) ((res) >= 0)
|
|
|
|
|
#define SPA_RESULT_IS_ERROR(res) ((res) < 0)
|
2017-05-26 15:55:23 +02:00
|
|
|
#define SPA_RESULT_IS_ASYNC(res) (((res) & SPA_ASYNC_MASK) == SPA_ASYNC_BIT)
|
2016-09-22 08:55:30 +02:00
|
|
|
|
2017-05-26 09:09:31 +02:00
|
|
|
#define SPA_RESULT_ASYNC_SEQ(res) ((res) & SPA_ASYNC_SEQ_MASK)
|
2017-05-26 15:55:23 +02:00
|
|
|
#define SPA_RESULT_RETURN_ASYNC(seq) (SPA_ASYNC_BIT | ((seq) & SPA_ASYNC_SEQ_MASK))
|
2016-09-22 08:55:30 +02:00
|
|
|
|
2018-01-18 15:39:03 +01:00
|
|
|
#define SPA_FLAG_CHECK(field,flag) (((field) & (flag)) == (flag))
|
|
|
|
|
#define SPA_FLAG_SET(field,flag) ((field) |= (flag))
|
|
|
|
|
#define SPA_FLAG_UNSET(field,flag) ((field) &= ~(flag))
|
|
|
|
|
|
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)
|
|
|
|
|
|
2017-09-21 18:57:41 +02: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
|
|
|
|
2018-07-09 12:07:30 +02:00
|
|
|
#define SPA_POINT(x,y) (struct spa_point){ x, y }
|
|
|
|
|
struct spa_point {
|
|
|
|
|
int32_t x;
|
|
|
|
|
int32_t y;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#define SPA_REGION(x,y,width,height) (struct spa_region){ SPA_POINT(x,y), SPA_RECTANGLE(width,height) }
|
|
|
|
|
struct spa_region {
|
|
|
|
|
struct spa_point position;
|
|
|
|
|
struct spa_rectangle size;
|
|
|
|
|
};
|
|
|
|
|
|
2017-09-21 18:57:41 +02: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]))
|
2017-11-16 16:31:03 +01:00
|
|
|
|
|
|
|
|
#define SPA_MIN(a,b) \
|
|
|
|
|
({ \
|
|
|
|
|
typeof(a) _a = (a); \
|
|
|
|
|
typeof(b) _b = (b); \
|
|
|
|
|
_a < _b ? _a : _b; \
|
|
|
|
|
})
|
|
|
|
|
#define SPA_MAX(a,b) \
|
|
|
|
|
({ \
|
|
|
|
|
typeof(a) _a = (a); \
|
|
|
|
|
typeof(b) _b = (b); \
|
|
|
|
|
_a > _b ? _a : _b; \
|
|
|
|
|
})
|
|
|
|
|
#define SPA_CLAMP(v,low,high) \
|
|
|
|
|
({ \
|
|
|
|
|
typeof(v) _v = (v); \
|
|
|
|
|
typeof(low) _low = (low); \
|
|
|
|
|
typeof(high) _high = (high); \
|
|
|
|
|
_v > _high ? _high : ( _v < _low ? _low : _v); \
|
|
|
|
|
})
|
2016-06-28 12:21:56 +02:00
|
|
|
|
2016-08-03 15:59:17 +02:00
|
|
|
#define SPA_MEMBER(b,o,t) ((t*)((uint8_t*)(b) + (o)))
|
2016-10-07 17:10:46 +02:00
|
|
|
|
|
|
|
|
#define SPA_CONTAINER_OF(p,t,m) (t*)((uint8_t*)p - offsetof (t,m))
|
|
|
|
|
|
2016-08-04 17:33:49 +02:00
|
|
|
#define SPA_PTRDIFF(p1,p2) ((uint8_t*)(p1) - (uint8_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
|
|
|
|
2017-04-17 16:32:25 +02:00
|
|
|
#define SPA_NSEC_PER_SEC (1000000000ll)
|
|
|
|
|
#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
|
|
|
|
|
|
|
|
#define SPA_TIMESPEC_TO_TIME(ts) ((ts)->tv_sec * SPA_NSEC_PER_SEC + (ts)->tv_nsec)
|
|
|
|
|
#define SPA_TIMEVAL_TO_TIME(tv) ((tv)->tv_sec * SPA_NSEC_PER_SEC + (tv)->tv_usec * 1000ll)
|
|
|
|
|
|
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)))
|
|
|
|
|
#define SPA_ALIGNED(align) __attribute__((aligned(align)))
|
2017-05-30 20:33:32 +02:00
|
|
|
#define SPA_DEPRECATED __attribute__ ((deprecated))
|
2016-10-05 18:34:36 +02:00
|
|
|
#else
|
2017-05-26 08:05:01 +02:00
|
|
|
#define SPA_PRINTF_FUNC(fmt, arg1)
|
|
|
|
|
#define SPA_ALIGNED(align)
|
2017-05-30 20:33:32 +02:00
|
|
|
#define SPA_DEPRECATED
|
2016-10-05 18:34:36 +02:00
|
|
|
#endif
|
|
|
|
|
|
2017-12-01 12:40:32 +01:00
|
|
|
#define SPA_ROUND_DOWN_N(num,align) ((num) & ~((align) - 1))
|
|
|
|
|
#define SPA_ROUND_UP_N(num,align) SPA_ROUND_DOWN_N((num) + ((align) - 1),align)
|
2016-10-28 16:56:33 +02: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
|
|
|
|
|
|
2017-07-11 20:38:48 +02:00
|
|
|
#define SPA_STRINGIFY_1(x...) #x
|
|
|
|
|
#define SPA_STRINGIFY(x...) SPA_STRINGIFY_1(x)
|
|
|
|
|
|
2017-05-26 08:05:01 +02:00
|
|
|
#define spa_return_if_fail(expr) \
|
|
|
|
|
do { \
|
|
|
|
|
if (SPA_UNLIKELY(!(expr))) \
|
|
|
|
|
return; \
|
|
|
|
|
} 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 { \
|
|
|
|
|
if (SPA_UNLIKELY(!(expr))) \
|
|
|
|
|
return (val); \
|
|
|
|
|
} 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. */
|
2017-05-26 08:05:01 +02:00
|
|
|
#define spa_assert_se(expr) \
|
|
|
|
|
do { \
|
|
|
|
|
if (SPA_UNLIKELY(!(expr))) \
|
2018-03-20 11:37:11 +01:00
|
|
|
fprintf(stderr, "'%s' failed at %s:%u %s()", \
|
|
|
|
|
#expr , __FILE__, __LINE__, __func__); \
|
2017-05-26 08:05:01 +02:00
|
|
|
abort(); \
|
|
|
|
|
} while (false)
|
2016-10-12 17:27:29 +02:00
|
|
|
|
2018-03-20 11:37:11 +01:00
|
|
|
#define spa_assert(expr) \
|
|
|
|
|
do { \
|
|
|
|
|
if (SPA_UNLIKELY(!(expr))) { \
|
|
|
|
|
fprintf(stderr, "'%s' failed at %s:%u %s()", \
|
|
|
|
|
#expr , __FILE__, __LINE__, __func__); \
|
|
|
|
|
abort(); \
|
|
|
|
|
} \
|
|
|
|
|
} while (false)
|
|
|
|
|
|
2018-06-01 11:08:25 +02:00
|
|
|
#define spa_assert_not_reached() \
|
|
|
|
|
do { \
|
|
|
|
|
fprintf(stderr, "Code should not be reached at %s:%u %s()", \
|
|
|
|
|
__FILE__, __LINE__, __func__); \
|
|
|
|
|
abort(); \
|
|
|
|
|
} while (false)
|
2018-03-20 11:37:11 +01:00
|
|
|
|
2016-10-12 17:27:29 +02:00
|
|
|
/* Does exactly nothing */
|
|
|
|
|
#define spa_nop() do {} while (false)
|
|
|
|
|
|
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
|
|
|
|
2017-11-14 16:40:58 +01:00
|
|
|
#define spa_strerror(err) \
|
|
|
|
|
({ \
|
|
|
|
|
int __err = -err; \
|
|
|
|
|
if (SPA_RESULT_IS_ASYNC(err)) \
|
|
|
|
|
__err = EINPROGRESS; \
|
|
|
|
|
strerror(__err); \
|
|
|
|
|
})
|
2017-11-13 09:41:41 +01: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
|
|
|
|
|
|
2017-11-10 16:11:53 +01:00
|
|
|
#endif /* __SPA_UTILS_DEFS_H__ */
|