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