]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - tools/include/linux/kernel.h
tools include: Adopt __same_type() and __must_be_array() from the kernel
[mirror_ubuntu-artful-kernel.git] / tools / include / linux / kernel.h
CommitLineData
37fbe0a4
WN
1#ifndef __TOOLS_LINUX_KERNEL_H
2#define __TOOLS_LINUX_KERNEL_H
43cbcd8a 3
5a116dd2 4#include <stdarg.h>
d0761e37 5#include <stddef.h>
5a116dd2
FW
6#include <assert.h>
7
eaa75b51
ACM
8#ifndef UINT_MAX
9#define UINT_MAX (~0U)
10#endif
11
5a116dd2
FW
12#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
13
9ac3e487
IT
14#define PERF_ALIGN(x, a) __PERF_ALIGN_MASK(x, (typeof(x))(a)-1)
15#define __PERF_ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
5a116dd2 16
43cbcd8a
ACM
17#ifndef offsetof
18#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
19#endif
20
21#ifndef container_of
22/**
23 * container_of - cast a member of a structure out to the containing structure
24 * @ptr: the pointer to the member.
25 * @type: the type of the container struct this is embedded in.
26 * @member: the name of the member within the struct.
27 *
28 */
29#define container_of(ptr, type, member) ({ \
30 const typeof(((type *)0)->member) * __mptr = (ptr); \
31 (type *)((char *)__mptr - offsetof(type, member)); })
32#endif
33
1967936d
ACM
34#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
35
52d422de
ACM
36#ifndef max
37#define max(x, y) ({ \
38 typeof(x) _max1 = (x); \
39 typeof(y) _max2 = (y); \
40 (void) (&_max1 == &_max2); \
41 _max1 > _max2 ? _max1 : _max2; })
42#endif
43
5a116dd2
FW
44#ifndef min
45#define min(x, y) ({ \
46 typeof(x) _min1 = (x); \
47 typeof(y) _min2 = (y); \
48 (void) (&_min1 == &_min2); \
49 _min1 < _min2 ? _min1 : _min2; })
50#endif
51
86d5a70c
IT
52#ifndef roundup
53#define roundup(x, y) ( \
54{ \
55 const typeof(y) __y = y; \
56 (((x) + (__y - 1)) / __y) * __y; \
57} \
58)
59#endif
60
5a116dd2 61#ifndef BUG_ON
8bf98b89
IT
62#ifdef NDEBUG
63#define BUG_ON(cond) do { if (cond) {} } while (0)
64#else
5a116dd2
FW
65#define BUG_ON(cond) assert(!(cond))
66#endif
8bf98b89 67#endif
5a116dd2
FW
68
69/*
70 * Both need more care to handle endianness
71 * (Don't use bitmap_copy_le() for now)
72 */
73#define cpu_to_le64(x) (x)
74#define cpu_to_le32(x) (x)
75
d0761e37
ACM
76int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
77int scnprintf(char * buf, size_t size, const char * fmt, ...);
5a116dd2 78
339ce005
JO
79/*
80 * This looks more complex than it should be. But we need to
81 * get the type for the ~ right in round_down (it needs to be
82 * as wide as the result!), and we want to evaluate the macro
83 * arguments just once each.
84 */
85#define __round_mask(x, y) ((__typeof__(x))((y)-1))
86#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
87#define round_down(x, y) ((x) & ~__round_mask(x, y))
88
43cbcd8a 89#endif