blob: 4af9a10cc2d2ff00a039f66841bfa6af686e6283 [file] [log] [blame]
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -03001#ifndef PERF_LINUX_KERNEL_H_
2#define PERF_LINUX_KERNEL_H_
3
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +02004#include <stdarg.h>
5#include <stdio.h>
6#include <stdlib.h>
7#include <assert.h>
8
9#define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
10
11#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
12#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
13
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030014#ifndef offsetof
15#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
16#endif
17
18#ifndef container_of
19/**
20 * container_of - cast a member of a structure out to the containing structure
21 * @ptr: the pointer to the member.
22 * @type: the type of the container struct this is embedded in.
23 * @member: the name of the member within the struct.
24 *
25 */
26#define container_of(ptr, type, member) ({ \
27 const typeof(((type *)0)->member) * __mptr = (ptr); \
28 (type *)((char *)__mptr - offsetof(type, member)); })
29#endif
30
Arnaldo Carvalho de Melo19679362010-05-17 15:39:16 -030031#define BUILD_BUG_ON_ZERO(e) (sizeof(struct { int:-!!(e); }))
32
Arnaldo Carvalho de Melo52d422d2009-07-10 22:47:28 -030033#ifndef max
34#define max(x, y) ({ \
35 typeof(x) _max1 = (x); \
36 typeof(y) _max2 = (y); \
37 (void) (&_max1 == &_max2); \
38 _max1 > _max2 ? _max1 : _max2; })
39#endif
40
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020041#ifndef min
42#define min(x, y) ({ \
43 typeof(x) _min1 = (x); \
44 typeof(y) _min2 = (y); \
45 (void) (&_min1 == &_min2); \
46 _min1 < _min2 ? _min1 : _min2; })
47#endif
48
49#ifndef BUG_ON
Irina Tirdea8bf98b82012-09-08 08:35:51 +030050#ifdef NDEBUG
51#define BUG_ON(cond) do { if (cond) {} } while (0)
52#else
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020053#define BUG_ON(cond) assert(!(cond))
54#endif
Irina Tirdea8bf98b82012-09-08 08:35:51 +030055#endif
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020056
57/*
58 * Both need more care to handle endianness
59 * (Don't use bitmap_copy_le() for now)
60 */
61#define cpu_to_le64(x) (x)
62#define cpu_to_le32(x) (x)
63
64static inline int
65vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
66{
67 int i;
68 ssize_t ssize = size;
69
70 i = vsnprintf(buf, size, fmt, args);
71
72 return (i >= ssize) ? (ssize - 1) : i;
73}
74
75static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
76{
77 va_list args;
78 ssize_t ssize = size;
79 int i;
80
81 va_start(args, fmt);
82 i = vsnprintf(buf, size, fmt, args);
83 va_end(args);
84
85 return (i >= ssize) ? (ssize - 1) : i;
86}
87
88static inline unsigned long
89simple_strtoul(const char *nptr, char **endptr, int base)
90{
91 return strtoul(nptr, endptr, base);
92}
93
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -030094int eprintf(int level,
95 const char *fmt, ...) __attribute__((format(printf, 2, 3)));
96
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020097#ifndef pr_fmt
98#define pr_fmt(fmt) fmt
99#endif
100
101#define pr_err(fmt, ...) \
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -0300102 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200103#define pr_warning(fmt, ...) \
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -0300104 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200105#define pr_info(fmt, ...) \
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -0300106 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200107#define pr_debug(fmt, ...) \
108 eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
109#define pr_debugN(n, fmt, ...) \
110 eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
111#define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
112#define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200113#define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200114
Jiri Olsa339ce002012-06-19 17:48:11 +0200115/*
116 * This looks more complex than it should be. But we need to
117 * get the type for the ~ right in round_down (it needs to be
118 * as wide as the result!), and we want to evaluate the macro
119 * arguments just once each.
120 */
121#define __round_mask(x, y) ((__typeof__(x))((y)-1))
122#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
123#define round_down(x, y) ((x) & ~__round_mask(x, y))
124
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -0300125#endif