mailmap: add mail alias
[transsip-mirror.git] / src / built_in.h
blobad8bb602969cc114a215d9744be52d7a865537c1
1 /*
2 * transsip - the telephony toolkit
3 * By Daniel Borkmann <daniel@transsip.org>
4 * Copyright 2011, 2012 Daniel Borkmann <dborkma@tik.ee.ethz.ch>
5 * Subject to the GPL, version 2.
6 */
8 #ifndef BUILT_IN_H
9 #define BUILT_IN_H
11 #include <stdint.h>
12 #include <assert.h>
14 typedef uint64_t u64;
15 typedef uint32_t u32;
16 typedef uint16_t u16;
17 typedef uint8_t u8;
19 #ifndef __aligned_16
20 # define __aligned_16 __attribute__((aligned(16)))
21 #endif
23 #ifndef likely
24 # define likely(x) __builtin_expect(!!(x), 1)
25 #endif
27 #ifndef unlikely
28 # define unlikely(x) __builtin_expect(!!(x), 0)
29 #endif
31 #ifndef __extension__
32 # define __extension__
33 #endif
35 #ifndef __deprecated
36 # define __deprecated /* unimplemented */
37 #endif
39 #ifndef __read_mostly
40 # define __read_mostly __attribute__((__section__(".data.read_mostly")))
41 #endif
43 #ifndef __always_inline
44 # define __always_inline inline
45 #endif
47 #ifndef __hidden
48 # define __hidden __attribute__((visibility("hidden")))
49 #endif
51 /* From Linux */
52 #ifndef array_size
53 # define array_size(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
54 #endif
56 #ifndef __must_be_array
57 # define __must_be_array(x) \
58 build_bug_on_zero(__builtin_types_compatible_p(typeof(x), typeof(&x[0])))
59 #endif
61 #ifndef max
62 # define max(a, b) \
63 ({ \
64 typeof (a) _a = (a); \
65 typeof (b) _b = (b); \
66 _a > _b ? _a : _b; \
68 #endif
70 #ifndef min
71 # define min(a, b) \
72 ({ \
73 typeof (a) _a = (a); \
74 typeof (b) _b = (b); \
75 _a < _b ? _a : _b; \
77 #endif
79 #define anon(return_type, body_and_args) \
80 ({ \
81 return_type __fn__ body_and_args \
82 __fn__; \
86 * Example usage:
88 * qsort(&argv[1], argc - 1, sizeof(argv[1]),
89 * anon(int, (const void * a, const void * b) {
90 * return strcmp(*(char * const *) a, *(char * const *) b);
91 * }));
94 #ifndef bug
95 # define bug() assert(0)
96 #endif
98 #ifndef bug_on
99 # define bug_on(cond) assert(!(cond))
100 #endif
102 #ifndef build_bug_on_zero
103 # define build_bug_on_zero(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
104 #endif
106 #endif /* BUILT_IN_H */