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> |
| 7 | |
| 8 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) |
| 9 | |
Irina Tirdea | 9ac3e48 | 2012-09-11 01:15:01 +0300 | [diff] [blame] | 10 | #define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1) |
| 11 | #define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask)) |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 12 | |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 13 | #ifndef offsetof |
| 14 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) |
| 15 | #endif |
| 16 | |
| 17 | #ifndef container_of |
| 18 | /** |
| 19 | * container_of - cast a member of a structure out to the containing structure |
| 20 | * @ptr: the pointer to the member. |
| 21 | * @type: the type of the container struct this is embedded in. |
| 22 | * @member: the name of the member within the struct. |
| 23 | * |
| 24 | */ |
| 25 | #define container_of(ptr, type, member) ({ \ |
| 26 | const typeof(((type *)0)->member) * __mptr = (ptr); \ |
| 27 | (type *)((char *)__mptr - offsetof(type, member)); }) |
| 28 | #endif |
| 29 | |
Arnaldo Carvalho de Melo | 1967936 | 2010-05-17 15:39:16 -0300 | [diff] [blame] | 30 | #define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); })) |
| 31 | |
Arnaldo Carvalho de Melo | 52d422d | 2009-07-10 22:47:28 -0300 | [diff] [blame] | 32 | #ifndef max |
| 33 | #define max(x, y) ({ \ |
| 34 | typeof(x) _max1 = (x); \ |
| 35 | typeof(y) _max2 = (y); \ |
| 36 | (void) (&_max1 == &_max2); \ |
| 37 | _max1 > _max2 ? _max1 : _max2; }) |
| 38 | #endif |
| 39 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 40 | #ifndef min |
| 41 | #define min(x, y) ({ \ |
| 42 | typeof(x) _min1 = (x); \ |
| 43 | typeof(y) _min2 = (y); \ |
| 44 | (void) (&_min1 == &_min2); \ |
| 45 | _min1 < _min2 ? _min1 : _min2; }) |
| 46 | #endif |
| 47 | |
Irina Tirdea | 86d5a70 | 2012-09-11 01:14:59 +0300 | [diff] [blame] | 48 | #ifndef roundup |
| 49 | #define roundup(x, y) ( \ |
| 50 | { \ |
| 51 | const typeof(y) __y = y; \ |
| 52 | (((x) + (__y - 1)) / __y) * __y; \ |
| 53 | } \ |
| 54 | ) |
| 55 | #endif |
| 56 | |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 57 | #ifndef BUG_ON |
Irina Tirdea | 8bf98b8 | 2012-09-08 08:35:51 +0300 | [diff] [blame] | 58 | #ifdef NDEBUG |
| 59 | #define BUG_ON(cond) do { if (cond) {} } while (0) |
| 60 | #else |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 61 | #define BUG_ON(cond) assert(!(cond)) |
| 62 | #endif |
Irina Tirdea | 8bf98b8 | 2012-09-08 08:35:51 +0300 | [diff] [blame] | 63 | #endif |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * Both need more care to handle endianness |
| 67 | * (Don't use bitmap_copy_le() for now) |
| 68 | */ |
| 69 | #define cpu_to_le64(x) (x) |
| 70 | #define cpu_to_le32(x) (x) |
| 71 | |
Arnaldo Carvalho de Melo | d0761e3 | 2016-07-07 15:42:33 -0300 | [diff] [blame^] | 72 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args); |
| 73 | int scnprintf(char * buf, size_t size, const char * fmt, ...); |
Frederic Weisbecker | 5a116dd | 2009-10-17 17:12:33 +0200 | [diff] [blame] | 74 | |
Jiri Olsa | 339ce00 | 2012-06-19 17:48:11 +0200 | [diff] [blame] | 75 | /* |
| 76 | * This looks more complex than it should be. But we need to |
| 77 | * get the type for the ~ right in round_down (it needs to be |
| 78 | * as wide as the result!), and we want to evaluate the macro |
| 79 | * arguments just once each. |
| 80 | */ |
| 81 | #define __round_mask(x, y) ((__typeof__(x))((y)-1)) |
| 82 | #define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1) |
| 83 | #define round_down(x, y) ((x) & ~__round_mask(x, y)) |
| 84 | |
Arnaldo Carvalho de Melo | 43cbcd8 | 2009-07-01 12:28:37 -0300 | [diff] [blame] | 85 | #endif |