Wang Nan | 37fbe0a | 2015-06-01 07:37:47 +0000 | [diff] [blame] | 1 | #ifndef __TOOLS_LINUX_KERNEL_H |
| 2 | #define __TOOLS_LINUX_KERNEL_H |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 3 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 4 | #include <stdarg.h> |
Arnaldo Carvalho de Melo | d0761e3 | 2016-07-07 15:42:33 -0300 | [diff] [blame] | 5 | #include <stddef.h> |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 6 | #include <assert.h> |
Arnaldo Carvalho de Melo | 8607c1e | 2017-04-17 11:29:26 -0300 | [diff] [blame] | 7 | #include <linux/compiler.h> |
Adrian Hunter | 07fda55 | 2017-05-26 11:17:23 +0300 | [diff] [blame] | 8 | #include <endian.h> |
| 9 | #include <byteswap.h> |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 10 | |
Arnaldo Carvalho de Melo | eaa75b5 | 2017-02-22 17:42:40 -0300 | [diff] [blame] | 11 | #ifndef UINT_MAX |
| 12 | #define UINT_MAX (~0U) |
| 13 | #endif |
| 14 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 15 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 16 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 17 | #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1) |
| 18 | #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 19 | |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 20 | #ifndef offsetof |
| 21 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 22 | #endif |
| 23 | |
| 24 | #ifndef container_of |
| 25 | /** |
| 26 | * container_of - cast a member of a structure out to the containing structure |
| 27 | * @ptr: the pointer to the member. |
| 28 | * @type: the type of the container struct this is embedded in. |
| 29 | * @member: the name of the member within the struct. |
| 30 | * |
| 31 | */ |
| 32 | #define container_of(ptr, type, member) ({ \ |
| 33 | const typeof(((type *)0)->member) * __mptr = (ptr); \ |
| 34 | (type *)((char *)__mptr - offsetof(type, member)); }) |
| 35 | #endif |
| 36 | |
Levin, Alexander (Sasha Levin) | e58e871 | 2017-05-31 00:38:09 +0000 | [diff] [blame] | 37 | #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) |
Arnaldo Carvalho de Melo | 1967936 | 2010-05-17 15:39:16 -0300 | [diff] [blame] | 38 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) |
| 39 | |
Arnaldo Carvalho de Melo | 52d422d | 2009-07-10 22:47:28 -0300 | [diff] [blame] | 40 | #ifndef max |
| 41 | #define max(x, y) ({ \ |
| 42 | typeof(x) _max1 = (x); \ |
| 43 | typeof(y) _max2 = (y); \ |
| 44 | (void) (&_max1 == &_max2); \ |
| 45 | _max1 > _max2 ? _max1 : _max2; }) |
| 46 | #endif |
| 47 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 48 | #ifndef min |
| 49 | #define min(x, y) ({ \ |
| 50 | typeof(x) _min1 = (x); \ |
| 51 | typeof(y) _min2 = (y); \ |
| 52 | (void) (&_min1 == &_min2); \ |
| 53 | _min1 < _min2 ? _min1 : _min2; }) |
| 54 | #endif |
| 55 | |
Irina Tirdea | 86d5a70 | 2012-09-11 01:14:59 +0300 | [diff] [blame] | 56 | #ifndef roundup |
| 57 | #define roundup(x, y) ( \ |
| 58 | { \ |
| 59 | const typeof(y) __y = y; \ |
| 60 | (((x) + (__y - 1)) / __y) * __y; \ |
| 61 | } \ |
| 62 | ) |
| 63 | #endif |
| 64 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 65 | #ifndef BUG_ON |
Irina Tirdea | 8bf98b8 | 2012-09-08 08:35:51 +0300 | [diff] [blame] | 66 | #ifdef NDEBUG |
| 67 | #define BUG_ON(cond) do { if (cond) {} } while (0) |
| 68 | #else |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 69 | #define BUG_ON(cond) assert(!(cond)) |
| 70 | #endif |
Irina Tirdea | 8bf98b8 | 2012-09-08 08:35:51 +0300 | [diff] [blame] | 71 | #endif |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 72 | |
Adrian Hunter | 07fda55 | 2017-05-26 11:17:23 +0300 | [diff] [blame] | 73 | #if __BYTE_ORDER == __BIG_ENDIAN |
| 74 | #define cpu_to_le16 bswap_16 |
| 75 | #define cpu_to_le32 bswap_32 |
| 76 | #define cpu_to_le64 bswap_64 |
| 77 | #define le16_to_cpu bswap_16 |
| 78 | #define le32_to_cpu bswap_32 |
| 79 | #define le64_to_cpu bswap_64 |
| 80 | #define cpu_to_be16 |
| 81 | #define cpu_to_be32 |
| 82 | #define cpu_to_be64 |
| 83 | #define be16_to_cpu |
| 84 | #define be32_to_cpu |
| 85 | #define be64_to_cpu |
| 86 | #else |
| 87 | #define cpu_to_le16 |
| 88 | #define cpu_to_le32 |
| 89 | #define cpu_to_le64 |
| 90 | #define le16_to_cpu |
| 91 | #define le32_to_cpu |
| 92 | #define le64_to_cpu |
| 93 | #define cpu_to_be16 bswap_16 |
| 94 | #define cpu_to_be32 bswap_32 |
| 95 | #define cpu_to_be64 bswap_64 |
| 96 | #define be16_to_cpu bswap_16 |
| 97 | #define be32_to_cpu bswap_32 |
| 98 | #define be64_to_cpu bswap_64 |
| 99 | #endif |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 100 | |
Arnaldo Carvalho de Melo | d0761e3 | 2016-07-07 15:42:33 -0300 | [diff] [blame] | 101 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); |
| 102 | int scnprintf(char * buf, size_t size, const char * fmt, ...); |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 103 | |
Arnaldo Carvalho de Melo | 8607c1e | 2017-04-17 11:29:26 -0300 | [diff] [blame] | 104 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr)) |
| 105 | |
Jiri Olsa | 339ce00 | 2012-06-19 17:48:11 +0200 | [diff] [blame] | 106 | /* |
| 107 | * This looks more complex than it should be. But we need to |
| 108 | * get the type for the ~ right in round_down (it needs to be |
| 109 | * as wide as the result!), and we want to evaluate the macro |
| 110 | * arguments just once each. |
| 111 | */ |
| 112 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
| 113 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) |
| 114 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) |
| 115 | |
Levin, Alexander (Sasha Levin) | e58e871 | 2017-05-31 00:38:09 +0000 | [diff] [blame] | 116 | #define current_gfp_context(k) 0 |
| 117 | #define synchronize_sched() |
| 118 | |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 119 | #endif |