| 1 |
/* |
| 2 |
* This program is free software; you can redistribute it and/or modify |
| 3 |
* it under the terms of the GNU General Public License as published by |
| 4 |
* the Free Software Foundation; either version 2 of the License, or |
| 5 |
* (at your option) any later version. |
| 6 |
* |
| 7 |
* This program is distributed in the hope that it will be useful, |
| 8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 |
* GNU General Public License for more details. |
| 11 |
* |
| 12 |
* You should have received a copy of the GNU General Public License along |
| 13 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 14 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 |
*/ |
| 16 |
|
| 17 |
#ifndef __SHARED_COMPILER_H |
| 18 |
#define __SHARED_COMPILER_H |
| 19 |
|
| 20 |
#define __noreturn __attribute__((noreturn)) |
| 21 |
#define __inline __attribute__((always_inline)) |
| 22 |
#define __hot __attribute__((hot)) |
| 23 |
#define __cold __attribute__((cold)) |
| 24 |
#define __force |
| 25 |
#define __in_section(s) __attribute__((section("." # s))) |
| 26 |
#define __visible __attribute__((externally_visible)) |
| 27 |
#ifndef __attribute_const__ |
| 28 |
#define __attribute_const__ __attribute__((__const__)) |
| 29 |
#endif |
| 30 |
|
| 31 |
#include "swab.h" |
| 32 |
#include "little_endian.h" |
| 33 |
|
| 34 |
#define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d)) |
| 35 |
|
| 36 |
#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
| 37 |
#define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1) |
| 38 |
|
| 39 |
#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a) - 1) |
| 40 |
#define __ALIGN_MASK(x, mask) (((x) + (mask)) & ~(mask)) |
| 41 |
|
| 42 |
#define __roundup(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) |
| 43 |
|
| 44 |
#define __must_be_array(a) \ |
| 45 |
BUILD_BUG_ON_ZERO(__builtin_types_compatible_p(typeof(a), typeof(&a[0]))) |
| 46 |
#define ARRAY_SIZE(arr) (sizeof((arr)) / sizeof((arr)[0]) + __must_be_array(arr)) |
| 47 |
|
| 48 |
#define BIT(b) (1 << (b)) |
| 49 |
#define MASK(w) (BIT(w) - 1) |
| 50 |
|
| 51 |
/** |
| 52 |
* __struct_group() - Create a mirrored named and anonyomous struct |
| 53 |
* |
| 54 |
* @TAG: The tag name for the named sub-struct (usually empty) |
| 55 |
* @NAME: The identifier name of the mirrored sub-struct |
| 56 |
* @ATTRS: Any struct attributes (usually empty) |
| 57 |
* @MEMBERS: The member declarations for the mirrored structs |
| 58 |
* |
| 59 |
* Used to create an anonymous union of two structs with identical layout |
| 60 |
* and size: one anonymous and one named. The former's members can be used |
| 61 |
* normally without sub-struct naming, and the latter can be used to |
| 62 |
* reason about the start, end, and size of the group of struct members. |
| 63 |
* The named struct can also be explicitly tagged for layer reuse, as well |
| 64 |
* as both having struct attributes appended. |
| 65 |
*/ |
| 66 |
#define __struct_group(TAG, NAME, ATTRS, MEMBERS...) \ |
| 67 |
union { \ |
| 68 |
struct { MEMBERS } ATTRS; \ |
| 69 |
struct TAG { MEMBERS } ATTRS NAME; \ |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* struct_group() - Wrap a set of declarations in a mirrored struct |
| 74 |
* |
| 75 |
* @NAME: The identifier name of the mirrored sub-struct |
| 76 |
* @MEMBERS: The member declarations for the mirrored structs |
| 77 |
* |
| 78 |
* Used to create an anonymous union of two structs with identical |
| 79 |
* layout and size: one anonymous and one named. The former can be |
| 80 |
* used normally without sub-struct naming, and the latter can be |
| 81 |
* used to reason about the start, end, and size of the group of |
| 82 |
* struct members. |
| 83 |
*/ |
| 84 |
#define struct_group(NAME, MEMBERS...) \ |
| 85 |
__struct_group(/* no tag */, NAME, /* no attrs */, MEMBERS) |
| 86 |
|
| 87 |
#undef offsetof |
| 88 |
#ifdef __compiler_offsetof |
| 89 |
# define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER) |
| 90 |
#else |
| 91 |
# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 92 |
#endif |
| 93 |
|
| 94 |
/** |
| 95 |
* sizeof_field() - Report the size of a struct field in bytes |
| 96 |
* |
| 97 |
* @TYPE: The structure containing the field of interest |
| 98 |
* @MEMBER: The field to return the size of |
| 99 |
*/ |
| 100 |
#define sizeof_field(TYPE, MEMBER) sizeof((((TYPE *)0)->MEMBER)) |
| 101 |
|
| 102 |
/** |
| 103 |
* offsetofend() - Report the offset of a struct field within the struct |
| 104 |
* |
| 105 |
* @TYPE: The type of the structure |
| 106 |
* @MEMBER: The member within the structure to get the end offset of |
| 107 |
*/ |
| 108 |
#define offsetofend(TYPE, MEMBER) \ |
| 109 |
(offsetof(TYPE, MEMBER) + sizeof_field(TYPE, MEMBER)) |
| 110 |
|
| 111 |
#define likely(x) __builtin_expect(!!(x), 1) |
| 112 |
#define unlikely(x) __builtin_expect(!!(x), 0) |
| 113 |
|
| 114 |
#define min(x, y) ({ \ |
| 115 |
typeof(x) _min1 = (x); \ |
| 116 |
typeof(y) _min2 = (y); \ |
| 117 |
(void) (&_min1 == &_min2); \ |
| 118 |
_min1 < _min2 ? _min1 : _min2; }) |
| 119 |
|
| 120 |
#define max(x, y) ({ \ |
| 121 |
typeof(x) _max1 = (x); \ |
| 122 |
typeof(y) _max2 = (y); \ |
| 123 |
(void) (&_max1 == &_max2); \ |
| 124 |
_max1 > _max2 ? _max1 : _max2; }) |
| 125 |
|
| 126 |
#define min_t(type, x, y) ({ \ |
| 127 |
type __min1 = (x); \ |
| 128 |
type __min2 = (y); \ |
| 129 |
__min1 < __min2 ? __min1 : __min2; }) |
| 130 |
|
| 131 |
#define max_t(type, x, y) ({ \ |
| 132 |
type __max1 = (x); \ |
| 133 |
type __max2 = (y); \ |
| 134 |
__max1 > __max2 ? __max1 : __max2; }) |
| 135 |
|
| 136 |
|
| 137 |
#define container_of(ptr, type, member) ({ \ |
| 138 |
const typeof(((type *)0)->member) * __mptr = (ptr); \ |
| 139 |
(type *)(((unsigned long)__mptr - offsetof(type, member))); }) |
| 140 |
|
| 141 |
#define MAX_ERRNO 4095 |
| 142 |
|
| 143 |
#define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO) |
| 144 |
|
| 145 |
static inline void *ERR_PTR(long errornr) |
| 146 |
{ |
| 147 |
return (void *) errornr; |
| 148 |
} |
| 149 |
|
| 150 |
static inline long PTR_ERR(const void *ptr) |
| 151 |
{ |
| 152 |
return (long) ptr; |
| 153 |
} |
| 154 |
|
| 155 |
static inline long IS_ERR(const void *ptr) |
| 156 |
{ |
| 157 |
return IS_ERR_VALUE((unsigned long)ptr); |
| 158 |
} |
| 159 |
|
| 160 |
static inline long IS_ERR_OR_NULL(const void *ptr) |
| 161 |
{ |
| 162 |
return !ptr || IS_ERR_VALUE((unsigned long)ptr); |
| 163 |
} |
| 164 |
|
| 165 |
static inline unsigned int hweight8(unsigned int w) |
| 166 |
{ |
| 167 |
unsigned int res = w - ((w >> 1) & 0x55); |
| 168 |
res = (res & 0x33) + ((res >> 2) & 0x33); |
| 169 |
return (res + (res >> 4)) & 0x0F; |
| 170 |
} |
| 171 |
|
| 172 |
static inline unsigned int hweight16(unsigned int w) |
| 173 |
{ |
| 174 |
unsigned int res = w - ((w >> 1) & 0x5555); |
| 175 |
res = (res & 0x3333) + ((res >> 2) & 0x3333); |
| 176 |
res = (res + (res >> 4)) & 0x0F0F; |
| 177 |
return (res + (res >> 8)) & 0x00FF; |
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* DECLARE_FLEX_ARRAY() - Declare a flexible array usable in a union |
| 182 |
* |
| 183 |
* @TYPE: The type of each flexible array element |
| 184 |
* @NAME: The name of the flexible array member |
| 185 |
* |
| 186 |
* In order to have a flexible array member in a union or alone in a |
| 187 |
* struct, it needs to be wrapped in an anonymous struct with at least 1 |
| 188 |
* named member, but that member can be empty. |
| 189 |
*/ |
| 190 |
#define DECLARE_FLEX_ARRAY(TYPE, NAME) \ |
| 191 |
struct { \ |
| 192 |
struct { } __empty_ ## NAME; \ |
| 193 |
TYPE NAME[]; \ |
| 194 |
} |
| 195 |
|
| 196 |
#define __bf_shf(x) (__builtin_ffsll(x) - 1) |
| 197 |
|
| 198 |
#define FIELD_MAX(_mask) \ |
| 199 |
({ \ |
| 200 |
(typeof(_mask))((_mask) >> __bf_shf(_mask)); \ |
| 201 |
}) |
| 202 |
|
| 203 |
#if __has_attribute(__error__) |
| 204 |
# define __compiletime_error(msg) __attribute__((__error__(msg))) |
| 205 |
#else |
| 206 |
# define __compiletime_error(msg) |
| 207 |
#endif |
| 208 |
|
| 209 |
extern void __compiletime_error("value doesn't fit into mask") |
| 210 |
__field_overflow(void); |
| 211 |
extern void __compiletime_error("bad bitfield mask") |
| 212 |
__bad_mask(void); |
| 213 |
static inline u64 field_multiplier(u32 field) |
| 214 |
{ |
| 215 |
if ((field | (field - 1)) & ((field | (field - 1)) + 1)) |
| 216 |
__bad_mask(); |
| 217 |
return field & -field; |
| 218 |
} |
| 219 |
static inline u64 field_mask(const u32 field) |
| 220 |
{ |
| 221 |
return field / field_multiplier(field); |
| 222 |
} |
| 223 |
#define field_max(field) ((typeof(field))field_mask(field)) |
| 224 |
#define ____MAKE_OP(type,base,to,from) \ |
| 225 |
static inline __##type type##_encode_bits(base v, base field) \ |
| 226 |
{ \ |
| 227 |
if (__builtin_constant_p(v) && (v & ~field_mask(field))) \ |
| 228 |
__field_overflow(); \ |
| 229 |
return to((v & field_mask(field)) * field_multiplier(field)); \ |
| 230 |
} \ |
| 231 |
static inline __##type type##_replace_bits(__##type old, \ |
| 232 |
base val, base field) \ |
| 233 |
{ \ |
| 234 |
return (old & ~to(field)) | type##_encode_bits(val, field); \ |
| 235 |
} \ |
| 236 |
static inline void type##p_replace_bits(__##type *p, \ |
| 237 |
base val, base field) \ |
| 238 |
{ \ |
| 239 |
*p = (*p & ~to(field)) | type##_encode_bits(val, field); \ |
| 240 |
} \ |
| 241 |
static inline base type##_get_bits(__##type v, base field) \ |
| 242 |
{ \ |
| 243 |
return (from(v) & field)/field_multiplier(field); \ |
| 244 |
} |
| 245 |
#define __MAKE_OP(size) \ |
| 246 |
____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \ |
| 247 |
____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \ |
| 248 |
____MAKE_OP(u##size,u##size,,) |
| 249 |
____MAKE_OP(u8,u8,,) |
| 250 |
__MAKE_OP(16) |
| 251 |
__MAKE_OP(32) |
| 252 |
#undef __MAKE_OP |
| 253 |
#undef ____MAKE_OP |
| 254 |
|
| 255 |
#endif /* __SHARED_COMPILER_H */ |