1 #ifndef R2_TYPES_H
2 #define R2_TYPES_H
3 
4 #undef _FILE_OFFSET_BITS
5 #define _FILE_OFFSET_BITS 64
6 
7 // defines like IS_DIGIT, etc'
8 #include "r_util/r_str_util.h"
9 #include <r_userconf.h>
10 #include <stddef.h>
11 #include <stdlib.h>
12 #include <assert.h>
13 
14 // TODO: fix this to make it crosscompile-friendly: R_SYS_OSTYPE ?
15 /* operating system */
16 #undef __BSD__
17 #undef __KFBSD__
18 #undef __UNIX__
19 #undef __WINDOWS__
20 
21 #define R_MODE_PRINT 0x000
22 #define R_MODE_RADARE 0x001
23 #define R_MODE_SET 0x002
24 #define R_MODE_SIMPLE 0x004
25 #define R_MODE_JSON 0x008
26 #define R_MODE_ARRAY 0x010
27 #define R_MODE_SIMPLEST 0x020
28 #define R_MODE_CLASSDUMP 0x040
29 #define R_MODE_EQUAL 0x080
30 
31 #define R_IN /* do not use, implicit */
32 #define R_OUT /* parameter is written, not read */
33 #define R_INOUT /* parameter is read and written */
34 #define R_OWN /* pointer ownership is transferred */
35 #define R_BORROW /* pointer ownership is not transferred, it must not be freed by the receiver */
36 #define R_NONNULL /* pointer can not be null */
37 #define R_NULLABLE /* pointer can be null */
38 #define R_DEPRECATE /* should not be used in new code and should/will be removed in the future */
39 #define R_IFNULL(x) /* default value for the pointer when null */
40 #ifdef __GNUC__
41 #define R_UNUSED __attribute__((__unused__))
42 #else
43 #define R_UNUSED /* unused */
44 #endif
45 
46 #ifdef R_NEW
47 #undef R_NEW
48 #endif
49 
50 #ifdef R_NEW0
51 #undef R_NEW0
52 #endif
53 
54 #ifdef R_FREE
55 #undef R_FREE
56 #endif
57 
58 #ifdef R_NEWCOPY
59 #undef R_NEWCOPY
60 #endif
61 
62 // used in debug, io, bin, anal, ...
63 #define R_PERM_R	4
64 #define R_PERM_W	2
65 #define R_PERM_X	1
66 #define R_PERM_RW	(R_PERM_R|R_PERM_W)
67 #define R_PERM_RX	(R_PERM_R|R_PERM_X)
68 #define R_PERM_RWX	(R_PERM_R|R_PERM_W|R_PERM_X)
69 #define R_PERM_WX	(R_PERM_W|R_PERM_X)
70 #define R_PERM_SHAR	8
71 #define R_PERM_PRIV	16
72 #define R_PERM_ACCESS	32
73 #define R_PERM_CREAT	64
74 
75 
76 // HACK to fix capstone-android-mips build
77 #undef mips
78 #define mips mips
79 
80 #if defined(__powerpc) || defined(__powerpc__)
81 #undef __POWERPC__
82 #define __POWERPC__ 1
83 #endif
84 
85 #if __IPHONE_8_0 && TARGET_OS_IPHONE
86 #define LIBC_HAVE_SYSTEM 0
87 #else
88 #define LIBC_HAVE_SYSTEM 1
89 #endif
90 
91 #if APPLE_SDK_IPHONEOS || APPLE_SDK_APPLETVOS || APPLE_SDK_WATCHOS || APPLE_SDK_APPLETVSIMULATOR || APPLE_SDK_WATCHSIMULATOR
92 #define LIBC_HAVE_PTRACE 0
93 #else
94 #define LIBC_HAVE_PTRACE 1
95 #endif
96 
97 #if HAVE_FORK
98 #define LIBC_HAVE_FORK 1
99 #else
100 #define LIBC_HAVE_FORK 0
101 #endif
102 
103 #if defined(__OpenBSD__)
104 #include <sys/param.h>
105 #undef MAXCOMLEN	/* redefined in zipint.h */
106 #endif
107 
108 /* release >= 5.9 */
109 #if __OpenBSD__ && OpenBSD >= 201605
110 #define LIBC_HAVE_PLEDGE 1
111 #else
112 #define LIBC_HAVE_PLEDGE 0
113 #endif
114 
115 #if __sun
116 #define LIBC_HAVE_PRIV_SET 1
117 #else
118 #define LIBC_HAVE_PRIV_SET 0
119 #endif
120 
121 #ifdef __GNUC__
122 #  define UNUSED_FUNCTION(x) __attribute__((__unused__)) UNUSED_ ## x
123 #else
124 #  define UNUSED_FUNCTION(x) UNUSED_ ## x
125 #endif
126 
127 #ifdef __EMSCRIPTEN__
128 # define __UNIX__ 1
129 #endif
130 
131 #ifdef __HAIKU__
132 # define __UNIX__ 1
133 #endif
134 
135 #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
136 #define __KFBSD__ 1
137 #else
138 #define __KFBSD__ 0
139 #endif
140 
141 #ifdef _MSC_VER
142   #define restrict
143   #define strcasecmp stricmp
144   #define strncasecmp strnicmp
145   #define __WINDOWS__ 1
146 
147   #include <time.h>
gmtime_r(const time_t * t,struct tm * r)148   static inline struct tm *gmtime_r(const time_t *t, struct tm *r) { return (gmtime_s(r, t))? NULL : r; }
149 #endif
150 
151 #if defined(EMSCRIPTEN) || defined(__linux__) || defined(__APPLE__) || defined(__GNU__) || defined(__ANDROID__) || defined(__QNX__) || defined(__sun) || defined(__HAIKU__)
152   #define __BSD__ 0
153   #define __UNIX__ 1
154 #endif
155 #if __KFBSD__ || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__DragonFly__)
156   #define __BSD__ 1
157   #define __UNIX__ 1
158 #endif
159 #if __WINDOWS__ || _WIN32
160   #ifdef _MSC_VER
161   /* Must be included before windows.h */
162   #include <winsock2.h>
163   #include <ws2tcpip.h>
164   #ifndef WIN32_LEAN_AND_MEAN
165   #define WIN32_LEAN_AND_MEAN
166   #endif
167   #endif
168   typedef int socklen_t;
169   #undef USE_SOCKETS
170   #define __WINDOWS__ 1
171   #undef __UNIX__
172   #undef __BSD__
173 #endif
174 #if __WINDOWS__ || _WIN32
175   #define __addr_t_defined
176   #include <windows.h>
177 #endif
178 
179 #if defined(__APPLE__) && (__arm__ || __arm64__ || __aarch64__)
180 #define TARGET_OS_IPHONE 1
181 #else
182 #define TARGET_OS_IPHONE 0
183 #endif
184 
185 #ifdef __GNUC__
186   #define FUNC_ATTR_MALLOC __attribute__((malloc))
187   #define FUNC_ATTR_ALLOC_SIZE(x) __attribute__((alloc_size(x)))
188   #define FUNC_ATTR_ALLOC_SIZE_PROD(x,y) __attribute__((alloc_size(x,y)))
189   #define FUNC_ATTR_ALLOC_ALIGN(x) __attribute__((alloc_align(x)))
190   #define FUNC_ATTR_PURE __attribute__ ((pure))
191   #define FUNC_ATTR_CONST __attribute__((const))
192   #define FUNC_ATTR_USED __attribute__((used))
193   #define FUNC_ATTR_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
194   #define FUNC_ATTR_ALWAYS_INLINE __attribute__((always_inline))
195 
196   #ifdef __clang__
197     // clang only
198   #elif defined(__INTEL_COMPILER)
199     // intel only
200   #else
201     // gcc only
202   #endif
203 #else
204   #define FUNC_ATTR_MALLOC
205   #define FUNC_ATTR_ALLOC_SIZE(x)
206   #define FUNC_ATTR_ALLOC_SIZE_PROD(x,y)
207   #define FUNC_ATTR_ALLOC_ALIGN(x)
208   #define FUNC_ATTR_PURE
209   #define FUNC_ATTR_CONST
210   #define FUNC_ATTR_USED
211   #define FUNC_ATTR_WARN_UNUSED_RESULT
212   #define FUNC_ATTR_ALWAYS_INLINE
213 #endif
214 
215 /* printf format check attributes */
216 #if defined(__clang__) || defined(__GNUC__)
217 #define R_PRINTF_CHECK(fmt, dots) __attribute__ ((format (printf, fmt, dots)))
218 #else
219 #define R_PRINTF_CHECK(fmt, dots)
220 #endif
221 
222 #include <r_types_base.h>
223 
224 #undef _FILE_OFFSET_BITS
225 #define _FILE_OFFSET_BITS 64
226 #undef _GNU_SOURCE
227 #define _GNU_SOURCE
228 
229 #include <stdio.h>
230 #include <string.h>
231 #include <stdlib.h>
232 #include <stdarg.h>
233 #include <fcntl.h> /* for O_RDONLY */
234 #include <r_endian.h> /* needs size_t */
235 
236 #ifdef __cplusplus
237 extern "C" {
238 #endif
239 
240 #define TODO(x) eprintf(__func__"  " x)
241 
242 // TODO: FS or R_SYS_DIR ??
243 #undef FS
244 #if __WINDOWS__
245 #define FS "\\"
246 #define R_SYS_DIR "\\"
247 #define R_SYS_ENVSEP ";"
248 #define R_SYS_HOME "USERPROFILE"
249 #define R_SYS_TMP "TEMP"
250 #else
251 #define FS "/"
252 #define R_SYS_DIR "/"
253 #define R_SYS_ENVSEP ":"
254 #define R_SYS_HOME "HOME"
255 #define R_SYS_TMP "TMPDIR"
256 #endif
257 
258 #define R_JOIN_2_PATHS(p1, p2) p1 R_SYS_DIR p2
259 #define R_JOIN_3_PATHS(p1, p2, p3) p1 R_SYS_DIR p2 R_SYS_DIR p3
260 #define R_JOIN_4_PATHS(p1, p2, p3, p4) p1 R_SYS_DIR p2 R_SYS_DIR p3 R_SYS_DIR p4
261 #define R_JOIN_5_PATHS(p1, p2, p3, p4, p5) p1 R_SYS_DIR p2 R_SYS_DIR p3 R_SYS_DIR p4 R_SYS_DIR p5
262 
263 #ifndef __packed
264 #define __packed __attribute__((__packed__))
265 #endif
266 
267 typedef int (*PrintfCallback)(const char *str, ...) R_PRINTF_CHECK(1, 2);
268 
269 /* compile-time introspection helpers */
270 #define CTO(y,z) ((size_t) &((y*)0)->z)
271 #define CTA(x,y,z) (x+CTO(y,z))
272 #define CTI(x,y,z) (*((size_t*)(CTA(x,y,z))))
273 #define CTS(x,y,z,t,v) {t* _=(t*)CTA(x,y,z);*_=v;}
274 
275 #ifdef R_IPI
276 #undef R_IPI
277 #endif
278 
279 #define R_IPI
280 
281 #ifdef R_HEAP
282 #undef R_HEAP
283 #endif
284 #define R_HEAP
285 
286 #ifdef R_API
287 #undef R_API
288 #endif
289 #if R_SWIG
290   #define R_API export
291 #elif R_INLINE
292   #define R_API inline
293 #else
294   #if defined(__GNUC__) && __GNUC__ >= 4
295     #define R_API __attribute__((visibility("default")))
296   #elif defined(_MSC_VER)
297     #define R_API __declspec(dllexport)
298   #else
299     #define R_API
300   #endif
301 #endif
302 
303 #define R_LIB_VERSION_HEADER(x) \
304 R_API const char *x##_version(void)
305 #define R_LIB_VERSION(x) \
306 R_API const char *x##_version(void) { return "" R2_GITTAP; }
307 
308 #define BITS2BYTES(x) (((x)/8)+(((x)%8)?1:0))
309 #define ZERO_FILL(x) memset (&x, 0, sizeof (x))
310 #define R_NEWS0(x,y) (x*)calloc(y,sizeof(x))
311 #define R_NEWS(x,y) (x*)malloc(sizeof(x)*(y))
312 #define R_NEW0(x) (x*)calloc(1,sizeof(x))
313 #define R_NEW(x) (x*)malloc(sizeof(x))
314 #define R_NEWCOPY(x,y) (x*)r_new_copy(sizeof(x), y)
315 
r_new_copy(int size,void * data)316 static inline void *r_new_copy(int size, void *data) {
317 	void *a = malloc(size);
318 	if (a) {
319 		memcpy (a, data, size);
320 	}
321 	return a;
322 }
323 // TODO: Make R_NEW_COPY be 1 arg, not two
324 #define R_NEW_COPY(x,y) x=(void*)malloc(sizeof(y));memcpy(x,y,sizeof(y))
325 #define R_MEM_ALIGN(x) ((void *)(size_t)(((ut64)(size_t)x) & 0xfffffffffffff000LL))
326 #define R_ARRAY_SIZE(x) (sizeof (x) / sizeof ((x)[0]))
327 #define R_PTR_MOVE(d,s) d=s;s=NULL;
328 
329 #define R_PTR_ALIGN(v,t) \
330 	((char *)(((size_t)(v) ) \
331 	& ~(t - 1)))
332 #define R_PTR_ALIGN_NEXT(v,t) \
333 	((char *)(((size_t)(v) + (t - 1)) \
334 	& ~(t - 1)))
335 
336 #define R_BIT_SET(x,y) (((ut8*)x)[y>>4] |= (1<<(y&0xf)))
337 #define R_BIT_UNSET(x,y) (((ut8*)x)[y>>4] &= ~(1<<(y&0xf)))
338 #define R_BIT_TOGGLE(x, y) ( R_BIT_CHK (x, y) ? \
339 		R_BIT_UNSET (x, y): R_BIT_SET (x, y))
340 
341 //#define R_BIT_CHK(x,y) ((((const ut8*)x)[y>>4] & (1<<(y&0xf))))
342 #define R_BIT_CHK(x,y) (*(x) & (1<<(y)))
343 
344 /* try for C99, but provide backwards compatibility */
345 #if defined(_MSC_VER) && (_MSC_VER <= 1800)
346 #define __func__ __FUNCTION__
347 #endif
348 
349 #define PERROR_WITH_FILELINE 0
350 
351 #if PERROR_WITH_FILELINE
352 /* make error messages useful by prepending file, line, and function name */
353 #define _perror(str,file,line,func) \
354   { \
355 	  char buf[256]; \
356 	  snprintf(buf,sizeof(buf),"[%s:%d %s] %s",file,line,func,str); \
357 	  r_sys_perror_str(buf); \
358   }
359 #define perror(x) _perror(x,__FILE__,__LINE__,__func__)
360 #define r_sys_perror(x) _perror(x,__FILE__,__LINE__,__func__)
361 #else
362 #define r_sys_perror(x) r_sys_perror_str(x);
363 #endif
364 
365 #if __UNIX__
366 #include <sys/types.h>
367 #include <sys/stat.h>
368 #include <fcntl.h>
369 #include <dirent.h>
370 #include <unistd.h>
371 #include <sys/time.h>
372 #ifdef __HAIKU__
373 // Original macro cast it to clockid_t
374 #undef CLOCK_MONOTONIC
375 #define CLOCK_MONOTONIC 0
376 #endif
377 #endif
378 
379 #ifndef HAVE_EPRINTF
380 #define eprintf(...) fprintf(stderr,__VA_ARGS__)
381 #define HAVE_EPRINTF 1
382 #endif
383 
384 #ifndef typeof
385 #define typeof(arg) __typeof__(arg)
386 #endif
387 
388 #if 1
389 #define r_offsetof(type, member) offsetof(type, member)
390 #else
391 #if __SDB_WINDOWS__
392 #define r_offsetof(type, member) ((unsigned long) (ut64)&((type*)0)->member)
393 #else
394 #define r_offsetof(type, member) ((unsigned long) &((type*)0)->member)
395 #endif
396 #endif
397 
398 
399 #define R_FREE(x) { free((void *)x); x = NULL; }
400 
401 #if __WINDOWS__
402 #define HAVE_REGEXP 0
403 #else
404 #define HAVE_REGEXP 1
405 #endif
406 
407 #if __WINDOWS__
408 #define PFMT64x "I64x"
409 #define PFMT64d "I64d"
410 #define PFMT64u "I64u"
411 #define PFMT64o "I64o"
412 #define PFMTSZx "Ix"
413 #define PFMTSZd "Id"
414 #define PFMTSZu "Iu"
415 #define PFMTSZo "Io"
416 #define LDBLFMT "f"
417 #define HHXFMT  "x"
418 #else
419 #define PFMT64x "llx"
420 #define PFMT64d "lld"
421 #define PFMT64u "llu"
422 #define PFMT64o "llo"
423 #define PFMTSZx "zx"
424 #define PFMTSZd "zd"
425 #define PFMTSZu "zu"
426 #define PFMTSZo "zo"
427 #define LDBLFMT "Lf"
428 #define HHXFMT  "hhx"
429 #endif
430 
431 #define PFMTDPTR "td"
432 
433 #define PFMT32x "x"
434 #define PFMT32d "d"
435 #define PFMT32u "u"
436 #define PFMT32o "o"
437 
438 #ifndef O_BINARY
439 #define O_BINARY 0
440 #endif
441 
442 #if __APPLE__
443 # if __i386__
444 # define R_SYS_BASE ((ut64)0x1000)
445 # elif __x86_64__
446 # define R_SYS_BASE ((ut64)0x100000000)
447 # else
448 # define R_SYS_BASE ((ut64)0x1000)
449 # endif
450 #elif __WINDOWS__
451 # define R_SYS_BASE ((ut64)0x01001000)
452 #else // linux, bsd, ...
453 # if __arm__ || __arm64__
454 # define R_SYS_BASE ((ut64)0x4000)
455 # else
456 # define R_SYS_BASE ((ut64)0x8048000)
457 # endif
458 #endif
459 
460 /* arch */
461 #if __i386__
462 #define R_SYS_ARCH "x86"
463 #define R_SYS_BITS R_SYS_BITS_32
464 #define R_SYS_ENDIAN 0
465 #elif __EMSCRIPTEN__
466 #define R_SYS_ARCH "wasm"
467 #define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
468 #define R_SYS_ENDIAN 0
469 #elif __x86_64__
470 #define R_SYS_ARCH "x86"
471 #define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
472 #define R_SYS_ENDIAN 0
473 #elif __POWERPC__
474 # define R_SYS_ARCH "ppc"
475 # ifdef __powerpc64__
476 #  define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
477 # else
478 #  define R_SYS_BITS R_SYS_BITS_32
479 # endif
480 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
481 #  define R_SYS_ENDIAN 0
482 # else
483 #  define R_SYS_ENDIAN 1
484 # endif
485 #elif __arm__
486 #define R_SYS_ARCH "arm"
487 #define R_SYS_BITS R_SYS_BITS_32
488 #define R_SYS_ENDIAN 0
489 #elif __arm64__ || __aarch64__
490 #define R_SYS_ARCH "arm"
491 #define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
492 #define R_SYS_ENDIAN 0
493 #elif __arc__
494 #define R_SYS_ARCH "arc"
495 #define R_SYS_BITS R_SYS_BITS_32
496 #define R_SYS_ENDIAN 0
497 #elif __s390x__
498 #define R_SYS_ARCH "sysz"
499 #define R_SYS_BITS R_SYS_BITS_64
500 #define R_SYS_ENDIAN 1
501 #elif __sparc__
502 #define R_SYS_ARCH "sparc"
503 #define R_SYS_BITS R_SYS_BITS_32
504 #define R_SYS_ENDIAN 1
505 #elif __mips__
506 #define R_SYS_ARCH "mips"
507 #define R_SYS_BITS R_SYS_BITS_32
508 #define R_SYS_ENDIAN 1
509 #elif __EMSCRIPTEN__
510 /* we should default to wasm when ready */
511 #define R_SYS_ARCH "x86"
512 #define R_SYS_BITS R_SYS_BITS_32
513 #elif __riscv__ || __riscv
514 # define R_SYS_ARCH "riscv"
515 # define R_SYS_ENDIAN 0
516 # if __riscv_xlen == 32
517 #  define R_SYS_BITS R_SYS_BITS_32
518 # else
519 #  define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
520 # endif
521 #else
522 #ifdef _MSC_VER
523 #ifdef _WIN64
524 #define R_SYS_ARCH "x86"
525 #define R_SYS_BITS (R_SYS_BITS_32 | R_SYS_BITS_64)
526 #define R_SYS_ENDIAN 0
527 #define __x86_64__ 1
528 #else
529 #define R_SYS_ARCH "x86"
530 #define R_SYS_BITS (R_SYS_BITS_32)
531 #define __i386__ 1
532 #define R_SYS_ENDIAN 0
533 #endif
534 #else
535 #define R_SYS_ARCH "unknown"
536 #define R_SYS_BITS R_SYS_BITS_32
537 #define R_SYS_ENDIAN 0
538 #endif
539 #endif
540 
541 #define R_SYS_ENDIAN_NONE 0
542 #define R_SYS_ENDIAN_LITTLE 1
543 #define R_SYS_ENDIAN_BIG 2
544 #define R_SYS_ENDIAN_BI 3
545 
546 typedef enum {
547 	R_SYS_ARCH_NONE = 0,
548 	R_SYS_ARCH_X86,
549 	R_SYS_ARCH_ARM,
550 	R_SYS_ARCH_PPC,
551 	R_SYS_ARCH_M68K,
552 	R_SYS_ARCH_JAVA,
553 	R_SYS_ARCH_MIPS,
554 	R_SYS_ARCH_SPARC,
555 	R_SYS_ARCH_XAP,
556 	R_SYS_ARCH_MSIL,
557 	R_SYS_ARCH_OBJD,
558 	R_SYS_ARCH_BF,
559 	R_SYS_ARCH_SH,
560 	R_SYS_ARCH_AVR,
561 	R_SYS_ARCH_DALVIK,
562 	R_SYS_ARCH_Z80,
563 	R_SYS_ARCH_ARC,
564 	R_SYS_ARCH_I8080,
565 	R_SYS_ARCH_RAR,
566 	R_SYS_ARCH_8051,
567 	R_SYS_ARCH_TMS320,
568 	R_SYS_ARCH_EBC,
569 	R_SYS_ARCH_H8300,
570 	R_SYS_ARCH_CR16,
571 	R_SYS_ARCH_V850,
572 	R_SYS_ARCH_SYSZ,
573 	R_SYS_ARCH_XCORE,
574 	R_SYS_ARCH_PROPELLER,
575 	R_SYS_ARCH_MSP430,
576 	R_SYS_ARCH_CRIS,
577 	R_SYS_ARCH_HPPA,
578 	R_SYS_ARCH_V810,
579 	R_SYS_ARCH_LM32,
580 	R_SYS_ARCH_RISCV
581 } RSysArch;
582 
583 #if HAVE_CLOCK_NANOSLEEP && CLOCK_MONOTONIC && (__linux__ || (__FreeBSD__ && __FreeBSD_version >= 1101000) || (__NetBSD__ && __NetBSD_Version__ >= 700000000))
584 #define HAS_CLOCK_NANOSLEEP 1
585 #else
586 #define HAS_CLOCK_NANOSLEEP 0
587 #endif
588 
589 /* os */
590 #if defined (__QNX__)
591 #define R_SYS_OS "qnx"
592 //#elif TARGET_OS_IPHONE
593 //#define R_SYS_OS "ios"
594 #elif defined (__APPLE__)
595 #define R_SYS_OS "darwin"
596 #elif defined (__linux__)
597 #define R_SYS_OS "linux"
598 #elif defined (__WINDOWS__)
599 #define R_SYS_OS "windows"
600 #elif defined (__NetBSD__ )
601 #define R_SYS_OS "netbsd"
602 #elif defined (__OpenBSD__)
603 #define R_SYS_OS "openbsd"
604 #elif defined (__FreeBSD__) || defined (__FreeBSD_kernel__)
605 #define R_SYS_OS "freebsd"
606 #elif defined (__HAIKU__)
607 #define R_SYS_OS "haiku"
608 #else
609 #define R_SYS_OS "unknown"
610 #endif
611 
612 #ifdef __cplusplus
613 }
614 #endif
615 
r_run_call1(void * fcn,void * arg1)616 static inline void r_run_call1(void *fcn, void *arg1) {
617 	((void (*)(void *))(fcn))(arg1);
618 }
619 
r_run_call2(void * fcn,void * arg1,void * arg2)620 static inline void r_run_call2(void *fcn, void *arg1, void *arg2) {
621 	((void (*)(void *, void *))(fcn))(arg1, arg2);
622 }
623 
r_run_call3(void * fcn,void * arg1,void * arg2,void * arg3)624 static inline void r_run_call3(void *fcn, void *arg1, void *arg2, void *arg3) {
625 	((void (*)(void *, void *, void *))(fcn))(arg1, arg2, arg3);
626 }
627 
r_run_call4(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4)628 static inline void r_run_call4(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4) {
629 	((void (*)(void *, void *, void *, void *))(fcn))(arg1, arg2, arg3, arg4);
630 }
631 
r_run_call5(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5)632 static inline void r_run_call5(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5) {
633 	((void (*)(void *, void *, void *, void *, void *))(fcn))(arg1, arg2, arg3, arg4, arg5);
634 }
635 
r_run_call6(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5,void * arg6)636 static inline void r_run_call6(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5,
637 	void *arg6) {
638 	((void (*)(void *, void *, void *, void *, void *, void *))(fcn))
639 		(arg1, arg2, arg3, arg4, arg5, arg6);
640 }
641 
r_run_call7(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5,void * arg6,void * arg7)642 static inline void r_run_call7(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5,
643 	void *arg6, void *arg7) {
644 	((void (*)(void *, void *, void *, void *, void *, void *, void *))(fcn))
645 		(arg1, arg2, arg3, arg4, arg5, arg6, arg7);
646 }
647 
r_run_call8(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5,void * arg6,void * arg7,void * arg8)648 static inline void r_run_call8(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5,
649 	void *arg6, void *arg7, void *arg8) {
650 	((void (*)(void *, void *, void *, void *, void *, void *, void *, void *))(fcn))
651 		(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
652 }
653 
r_run_call9(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5,void * arg6,void * arg7,void * arg8,void * arg9)654 static inline void r_run_call9(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5,
655 	void *arg6, void *arg7, void *arg8, void *arg9) {
656 	((void (*)(void *, void *, void *, void *, void *, void *, void *, void *, void *))(fcn))
657 		(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
658 }
659 
r_run_call10(void * fcn,void * arg1,void * arg2,void * arg3,void * arg4,void * arg5,void * arg6,void * arg7,void * arg8,void * arg9,void * arg10)660 static inline void r_run_call10(void *fcn, void *arg1, void *arg2, void *arg3, void *arg4, void *arg5,
661 	void *arg6, void *arg7, void *arg8, void *arg9, void *arg10) {
662 	((void (*)(void *, void *, void *, void *, void *, void *, void *, void *, void *, void *))(fcn))
663 		(arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10);
664 }
665 
666 #ifndef container_of
667 # ifdef _MSC_VER
668 #  define container_of(ptr, type, member) ((type *)((char *)(ptr) - offsetof(type, member)))
669 # else
670 #  define container_of(ptr, type, member) ((type *)((char *)(__typeof__(((type *)0)->member) *){ptr} - offsetof(type, member)))
671 # endif
672 #endif
673 
674 // reference counter
675 typedef int RRef;
676 
677 #define R_REF_NAME refcount
678 #define r_ref(x) x->R_REF_NAME++;
679 #define r_ref_init(x) x->R_REF_NAME = 1
680 #define r_unref(x,f) { assert (x->R_REF_NAME> 0); if (!--(x->R_REF_NAME)) { f(x); } }
681 
682 #define R_REF_TYPE RRef R_REF_NAME
683 #define R_REF_FUNCTIONS(s, n) \
684 static inline void n##_ref(s *x) { x->R_REF_NAME++; } \
685 static inline void n##_unref(s *x) { r_unref (x, n##_free); }
686 
687 #endif // R2_TYPES_H
688