blob: 039bb85e4171700a1aefa65ee63e76ae102f5fc6 [file] [log] [blame]
Wang Nan37fbe0a2015-06-01 07:37:47 +00001#ifndef __TOOLS_LINUX_KERNEL_H
2#define __TOOLS_LINUX_KERNEL_H
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -03003
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +02004#include <stdarg.h>
Arnaldo Carvalho de Melod0761e32016-07-07 15:42:33 -03005#include <stddef.h>
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +02006#include <assert.h>
Arnaldo Carvalho de Melo8607c1e2017-04-17 11:29:26 -03007#include <linux/compiler.h>
Adrian Hunter07fda552017-05-26 11:17:23 +03008#include <endian.h>
9#include <byteswap.h>
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020010
Arnaldo Carvalho de Meloeaa75b52017-02-22 17:42:40 -030011#ifndef UINT_MAX
12#define UINT_MAX (~0U)
13#endif
14
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020015#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
16
Irina Tirdea9ac3e482012-09-11 01:15:01 +030017#define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
18#define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020019
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030020#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
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -030037#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
38
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030039#ifndef max
40#define max(x, y) ({ \
41 typeof(x) _max1 = (x); \
42 typeof(y) _max2 = (y); \
43 (void) (&_max1 == &_max2); \
44 _max1 > _max2 ? _max1 : _max2; })
45#endif
46
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020047#ifndef min
48#define min(x, y) ({ \
49 typeof(x) _min1 = (x); \
50 typeof(y) _min2 = (y); \
51 (void) (&_min1 == &_min2); \
52 _min1 < _min2 ? _min1 : _min2; })
53#endif
54
Irina Tirdea86d5a702012-09-11 01:14:59 +030055#ifndef roundup
56#define roundup(x, y) ( \
57{ \
58 const typeof(y) __y = y; \
59 (((x) + (__y - 1)) / __y) * __y; \
60} \
61)
62#endif
63
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020064#ifndef BUG_ON
Irina Tirdea8bf98b82012-09-08 08:35:51 +030065#ifdef NDEBUG
66#define BUG_ON(cond) do { if (cond) {} } while (0)
67#else
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020068#define BUG_ON(cond) assert(!(cond))
69#endif
Irina Tirdea8bf98b82012-09-08 08:35:51 +030070#endif
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020071
Adrian Hunter07fda552017-05-26 11:17:23 +030072#if __BYTE_ORDER == __BIG_ENDIAN
73#define cpu_to_le16 bswap_16
74#define cpu_to_le32 bswap_32
75#define cpu_to_le64 bswap_64
76#define le16_to_cpu bswap_16
77#define le32_to_cpu bswap_32
78#define le64_to_cpu bswap_64
79#define cpu_to_be16
80#define cpu_to_be32
81#define cpu_to_be64
82#define be16_to_cpu
83#define be32_to_cpu
84#define be64_to_cpu
85#else
86#define cpu_to_le16
87#define cpu_to_le32
88#define cpu_to_le64
89#define le16_to_cpu
90#define le32_to_cpu
91#define le64_to_cpu
92#define cpu_to_be16 bswap_16
93#define cpu_to_be32 bswap_32
94#define cpu_to_be64 bswap_64
95#define be16_to_cpu bswap_16
96#define be32_to_cpu bswap_32
97#define be64_to_cpu bswap_64
98#endif
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020099
Arnaldo Carvalho de Melod0761e32016-07-07 15:42:33 -0300100int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
101int scnprintf(char * buf, size_t size, const char * fmt, ...);
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +0200102
Arnaldo Carvalho de Melo8607c1e2017-04-17 11:29:26 -0300103#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
104
Jiri Olsa339ce002012-06-19 17:48:11 +0200105/*
106 * This looks more complex than it should be. But we need to
107 * get the type for the ~ right in round_down (it needs to be
108 * as wide as the result!), and we want to evaluate the macro
109 * arguments just once each.
110 */
111#define __round_mask(x, y) ((__typeof__(x))((y)-1))
112#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
113#define round_down(x, y) ((x) & ~__round_mask(x, y))
114
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -0300115#endif