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