blob: 388ab1bfd1141bbd29fb4540c65828c69ca44b52 [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 Melo52d422d2009-07-10 22:47:28 -030031#ifndef max
32#define max(x, y) ({ \
33 typeof(x) _max1 = (x); \
34 typeof(y) _max2 = (y); \
35 (void) (&_max1 == &_max2); \
36 _max1 > _max2 ? _max1 : _max2; })
37#endif
38
Frederic Weisbecker5a116dd2009-10-17 17:12:33 +020039#ifndef min
40#define min(x, y) ({ \
41 typeof(x) _min1 = (x); \
42 typeof(y) _min2 = (y); \
43 (void) (&_min1 == &_min2); \
44 _min1 < _min2 ? _min1 : _min2; })
45#endif
46
47#ifndef BUG_ON
48#define BUG_ON(cond) assert(!(cond))
49#endif
50
51/*
52 * Both need more care to handle endianness
53 * (Don't use bitmap_copy_le() for now)
54 */
55#define cpu_to_le64(x) (x)
56#define cpu_to_le32(x) (x)
57
58static inline int
59vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
60{
61 int i;
62 ssize_t ssize = size;
63
64 i = vsnprintf(buf, size, fmt, args);
65
66 return (i >= ssize) ? (ssize - 1) : i;
67}
68
69static inline int scnprintf(char * buf, size_t size, const char * fmt, ...)
70{
71 va_list args;
72 ssize_t ssize = size;
73 int i;
74
75 va_start(args, fmt);
76 i = vsnprintf(buf, size, fmt, args);
77 va_end(args);
78
79 return (i >= ssize) ? (ssize - 1) : i;
80}
81
82static inline unsigned long
83simple_strtoul(const char *nptr, char **endptr, int base)
84{
85 return strtoul(nptr, endptr, base);
86}
87
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -030088int eprintf(int level,
89 const char *fmt, ...) __attribute__((format(printf, 2, 3)));
90
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020091#ifndef pr_fmt
92#define pr_fmt(fmt) fmt
93#endif
94
95#define pr_err(fmt, ...) \
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -030096 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020097#define pr_warning(fmt, ...) \
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -030098 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -020099#define pr_info(fmt, ...) \
Arnaldo Carvalho de Melob4f52962010-03-11 20:12:42 -0300100 eprintf(0, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200101#define pr_debug(fmt, ...) \
102 eprintf(1, pr_fmt(fmt), ##__VA_ARGS__)
103#define pr_debugN(n, fmt, ...) \
104 eprintf(n, pr_fmt(fmt), ##__VA_ARGS__)
105#define pr_debug2(fmt, ...) pr_debugN(2, pr_fmt(fmt), ##__VA_ARGS__)
106#define pr_debug3(fmt, ...) pr_debugN(3, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200107#define pr_debug4(fmt, ...) pr_debugN(4, pr_fmt(fmt), ##__VA_ARGS__)
Arnaldo Carvalho de Melo6beba7a2009-10-21 17:34:06 -0200108
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -0300109#endif