blob: 801b927499f22f1683404d2ba60a7116fd04bd77 [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>
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +02008
Arnaldo Carvalho de Meloeaa75b52017-02-22 17:42:40 -03009#ifndef UINT_MAX
10#define UINT_MAX (~0U)
11#endif
12
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020013#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
14
Irina Tirdea9ac3e482012-09-11 01:15:01 +030015#define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
16#define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020017
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030018#ifndef offsetof
19#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
20#endif
21
22#ifndef container_of
23/**
24 * container_of - cast a member of a structure out to the containing structure
25 * @ptr: the pointer to the member.
26 * @type: the type of the container struct this is embedded in.
27 * @member: the name of the member within the struct.
28 *
29 */
30#define container_of(ptr, type, member) ({ \
31 const typeof(((type *)0)->member) * __mptr = (ptr); \
32 (type *)((char *)__mptr - offsetof(type, member)); })
33#endif
34
Levin, Alexander (Sasha Levin)e58e8712017-05-31 00:38:09 +000035#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -030036#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
37
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030038#ifndef max
39#define max(x, y) ({ \
40 typeof(x) _max1 = (x); \
41 typeof(y) _max2 = (y); \
42 (void) (&_max1 == &_max2); \
43 _max1 > _max2 ? _max1 : _max2; })
44#endif
45
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020046#ifndef min
47#define min(x, y) ({ \
48 typeof(x) _min1 = (x); \
49 typeof(y) _min2 = (y); \
50 (void) (&_min1 == &_min2); \
51 _min1 < _min2 ? _min1 : _min2; })
52#endif
53
Irina Tirdea86d5a702012-09-11 01:14:59 +030054#ifndef roundup
55#define roundup(x, y) ( \
56{ \
57 const typeof(y) __y = y; \
58 (((x) + (__y - 1)) / __y) * __y; \
59} \
60)
61#endif
62
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020063#ifndef BUG_ON
Irina Tirdea8bf98b82012-09-08 08:35:51 +030064#ifdef NDEBUG
65#define BUG_ON(cond) do { if (cond) {} } while (0)
66#else
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020067#define BUG_ON(cond) assert(!(cond))
68#endif
Irina Tirdea8bf98b82012-09-08 08:35:51 +030069#endif
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020070
71/*
72 * Both need more care to handle endianness
73 * (Don't use bitmap_copy_le() for now)
74 */
75#define cpu_to_le64(x) (x)
76#define cpu_to_le32(x) (x)
77
Arnaldo Carvalho de Melod0761e32016-07-07 15:42:33 -030078int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
79int scnprintf(char * buf, size_t size, const char * fmt, ...);
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020080
Arnaldo Carvalho de Melo8607c1e2017-04-17 11:29:26 -030081#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
82
Jiri Olsa339ce002012-06-19 17:48:11 +020083/*
84 * This looks more complex than it should be. But we need to
85 * get the type for the ~ right in round_down (it needs to be
86 * as wide as the result!), and we want to evaluate the macro
87 * arguments just once each.
88 */
89#define __round_mask(x, y) ((__typeof__(x))((y)-1))
90#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
91#define round_down(x, y) ((x) & ~__round_mask(x, y))
92
Levin, Alexander (Sasha Levin)e58e8712017-05-31 00:38:09 +000093#define current_gfp_context(k) 0
94#define synchronize_sched()
95
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030096#endif