0001
0002 #ifndef _TOOLS_LINUX_COMPILER_H_
0003 #define _TOOLS_LINUX_COMPILER_H_
0004
0005 #include <linux/compiler_types.h>
0006
0007 #ifndef __compiletime_error
0008 # define __compiletime_error(message)
0009 #endif
0010
0011 #ifdef __OPTIMIZE__
0012 # define __compiletime_assert(condition, msg, prefix, suffix) \
0013 do { \
0014 extern void prefix ## suffix(void) __compiletime_error(msg); \
0015 if (!(condition)) \
0016 prefix ## suffix(); \
0017 } while (0)
0018 #else
0019 # define __compiletime_assert(condition, msg, prefix, suffix) do { } while (0)
0020 #endif
0021
0022 #define _compiletime_assert(condition, msg, prefix, suffix) \
0023 __compiletime_assert(condition, msg, prefix, suffix)
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 #define compiletime_assert(condition, msg) \
0035 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
0036
0037
0038
0039 #define barrier() __asm__ __volatile__("": : :"memory")
0040
0041 #ifndef __always_inline
0042 # define __always_inline inline __attribute__((always_inline))
0043 #endif
0044
0045 #ifndef noinline
0046 #define noinline
0047 #endif
0048
0049
0050 #ifndef __same_type
0051 # define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
0052 #endif
0053
0054 #ifdef __ANDROID__
0055
0056
0057
0058
0059
0060
0061 #undef __always_inline
0062 #define __always_inline inline
0063 #endif
0064
0065 #define __user
0066 #define __rcu
0067 #define __read_mostly
0068
0069 #ifndef __attribute_const__
0070 # define __attribute_const__
0071 #endif
0072
0073 #ifndef __maybe_unused
0074 # define __maybe_unused __attribute__((unused))
0075 #endif
0076
0077 #ifndef __used
0078 # define __used __attribute__((__unused__))
0079 #endif
0080
0081 #ifndef __packed
0082 # define __packed __attribute__((__packed__))
0083 #endif
0084
0085 #ifndef __force
0086 # define __force
0087 #endif
0088
0089 #ifndef __weak
0090 # define __weak __attribute__((weak))
0091 #endif
0092
0093 #ifndef likely
0094 # define likely(x) __builtin_expect(!!(x), 1)
0095 #endif
0096
0097 #ifndef unlikely
0098 # define unlikely(x) __builtin_expect(!!(x), 0)
0099 #endif
0100
0101 #ifndef __init
0102 # define __init
0103 #endif
0104
0105 #include <linux/types.h>
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 typedef __u8 __attribute__((__may_alias__)) __u8_alias_t;
0119 typedef __u16 __attribute__((__may_alias__)) __u16_alias_t;
0120 typedef __u32 __attribute__((__may_alias__)) __u32_alias_t;
0121 typedef __u64 __attribute__((__may_alias__)) __u64_alias_t;
0122
0123 static __always_inline void __read_once_size(const volatile void *p, void *res, int size)
0124 {
0125 switch (size) {
0126 case 1: *(__u8_alias_t *) res = *(volatile __u8_alias_t *) p; break;
0127 case 2: *(__u16_alias_t *) res = *(volatile __u16_alias_t *) p; break;
0128 case 4: *(__u32_alias_t *) res = *(volatile __u32_alias_t *) p; break;
0129 case 8: *(__u64_alias_t *) res = *(volatile __u64_alias_t *) p; break;
0130 default:
0131 barrier();
0132 __builtin_memcpy((void *)res, (const void *)p, size);
0133 barrier();
0134 }
0135 }
0136
0137 static __always_inline void __write_once_size(volatile void *p, void *res, int size)
0138 {
0139 switch (size) {
0140 case 1: *(volatile __u8_alias_t *) p = *(__u8_alias_t *) res; break;
0141 case 2: *(volatile __u16_alias_t *) p = *(__u16_alias_t *) res; break;
0142 case 4: *(volatile __u32_alias_t *) p = *(__u32_alias_t *) res; break;
0143 case 8: *(volatile __u64_alias_t *) p = *(__u64_alias_t *) res; break;
0144 default:
0145 barrier();
0146 __builtin_memcpy((void *)p, (const void *)res, size);
0147 barrier();
0148 }
0149 }
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172 #define READ_ONCE(x) \
0173 ({ \
0174 union { typeof(x) __val; char __c[1]; } __u = \
0175 { .__c = { 0 } }; \
0176 __read_once_size(&(x), __u.__c, sizeof(x)); \
0177 __u.__val; \
0178 })
0179
0180 #define WRITE_ONCE(x, val) \
0181 ({ \
0182 union { typeof(x) __val; char __c[1]; } __u = \
0183 { .__val = (val) }; \
0184 __write_once_size(&(x), __u.__c, sizeof(x)); \
0185 __u.__val; \
0186 })
0187
0188
0189 #ifndef __fallthrough
0190 # define __fallthrough
0191 #endif
0192
0193
0194 #define ___PASTE(a, b) a##b
0195 #define __PASTE(a, b) ___PASTE(a, b)
0196
0197 #endif