1 #ifndef __XEN_TOOLS_COMMON_MACROS__ 2 #define __XEN_TOOLS_COMMON_MACROS__ 3 4 /* 5 * Caution: 6 * 7 * This header must be completely self-contained. There are no external 8 * references to variables or functions allowed, as the file might be included 9 * for different runtime environments, such as firmware or target and build 10 * host programs. 11 */ 12 13 #ifndef BUILD_BUG_ON 14 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) 15 #define BUILD_BUG_ON(p) ({ _Static_assert(!(p), "!(" #p ")"); }) 16 #else 17 #define BUILD_BUG_ON(p) ((void)sizeof(char[1 - 2 * !!(p)])) 18 #endif 19 #endif 20 21 #ifndef ARRAY_SIZE 22 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(*a)) 23 #endif 24 25 #ifndef MAX 26 #define MAX(x, y) ((x) > (y) ? (x) : (y)) 27 #endif 28 29 #ifndef MIN 30 #define MIN(x, y) ((x) < (y) ? (x) : (y)) 31 #endif 32 33 #ifndef min 34 #define min(x, y) \ 35 ({ \ 36 const typeof(x) _x = (x); \ 37 const typeof(y) _y = (y); \ 38 (void) (&_x == &_y); \ 39 (_x < _y) ? _x : _y; \ 40 }) 41 #endif 42 43 #ifndef max 44 #define max(x, y) \ 45 ({ \ 46 const typeof(x) _x = (x); \ 47 const typeof(y) _y = (y); \ 48 (void)(&_x == &_y); \ 49 (_x > _y) ? _x : _y; \ 50 }) 51 #endif 52 53 #ifndef min_t 54 #define min_t(type, x, y) \ 55 ({ \ 56 const type _x = (x); \ 57 const type _y = (y); \ 58 (_x < _y) ? _x: _y; \ 59 }) 60 #endif 61 62 #ifndef max_t 63 #define max_t(type, x, y) \ 64 ({ \ 65 const type _x = (x); \ 66 const type _y = (y); \ 67 (_x > _y) ? _x: _y; \ 68 }) 69 #endif 70 71 #ifndef ROUNDUP 72 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1)) 73 #endif 74 75 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m))) 76 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m)) 77 78 #ifndef __must_check 79 #define __must_check __attribute__((__warn_unused_result__)) 80 #endif 81 82 #ifndef __packed 83 #define __packed __attribute__((__packed__)) 84 #endif 85 86 #define container_of(ptr, type, member) ({ \ 87 typeof(((type *)0)->member) *mptr__ = (ptr); \ 88 (type *)((char *)mptr__ - offsetof(type, member)); \ 89 }) 90 91 #define __AC(X, Y) (X ## Y) 92 #define _AC(X, Y) __AC(X, Y) 93 94 /* Size macros. */ 95 #define MB(_mb) (_AC(_mb, ULL) << 20) 96 #define GB(_gb) (_AC(_gb, ULL) << 30) 97 98 #define get_unaligned_t(type, ptr) ({ \ 99 const struct { type x; } __packed *ptr_ = (typeof(ptr_))(ptr); \ 100 ptr_->x; \ 101 }) 102 103 #define put_unaligned_t(type, val, ptr) do { \ 104 struct { type x; } __packed *ptr_ = (typeof(ptr_))(ptr); \ 105 ptr_->x = (val); \ 106 } while (0) 107 108 #define get_unaligned(ptr) get_unaligned_t(typeof(*(ptr)), ptr) 109 #define put_unaligned(val, ptr) put_unaligned_t(typeof(*(ptr)), val, ptr) 110 111 #endif /* __XEN_TOOLS_COMMON_MACROS__ */ 112