1 /* Public domain. */
2
3 #ifndef _LINUX_KERNEL_H
4 #define _LINUX_KERNEL_H
5
6 #include <sys/param.h>
7 #include <sys/systm.h>
8 #include <sys/stdarg.h>
9 #include <sys/malloc.h>
10
11 #include <linux/types.h>
12 #include <linux/compiler.h>
13 #include <linux/bitops.h>
14 #include <linux/log2.h>
15 #include <linux/linkage.h>
16 #include <linux/printk.h>
17 #include <linux/typecheck.h>
18 #include <linux/container_of.h>
19 #include <linux/stddef.h>
20 #include <linux/align.h>
21 #include <linux/math.h>
22 #include <linux/limits.h>
23 #include <asm/byteorder.h>
24
25 #define swap(a, b) \
26 do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(0)
27
28 #define ARRAY_SIZE nitems
29
30 #define lower_32_bits(n) ((u32)(n))
31 #define upper_32_bits(_val) ((u32)(((_val) >> 16) >> 16))
32
33 #define scnprintf(str, size, fmt, arg...) snprintf(str, size, fmt, ## arg)
34
35 #define min_t(t, a, b) ({ \
36 t __min_a = (a); \
37 t __min_b = (b); \
38 __min_a < __min_b ? __min_a : __min_b; })
39
40 #define max_t(t, a, b) ({ \
41 t __max_a = (a); \
42 t __max_b = (b); \
43 __max_a > __max_b ? __max_a : __max_b; })
44
45 #define clamp_t(t, x, a, b) min_t(t, max_t(t, x, a), b)
46 #define clamp(x, a, b) clamp_t(__typeof(x), x, a, b)
47 #define clamp_val(x, a, b) clamp_t(__typeof(x), x, a, b)
48
49 #define min(a, b) MIN(a, b)
50 #define max(a, b) MAX(a, b)
51 #define min3(x, y, z) MIN(x, MIN(y, z))
52 #define max3(x, y, z) MAX(x, MAX(y, z))
53
54 #define min_not_zero(a, b) (a == 0) ? b : ((b == 0) ? a : min(a, b))
55
56 static inline char *
kvasprintf(int flags,const char * fmt,va_list ap)57 kvasprintf(int flags, const char *fmt, va_list ap)
58 {
59 char *buf;
60 size_t len;
61 va_list vl;
62
63 va_copy(vl, ap);
64 len = vsnprintf(NULL, 0, fmt, vl);
65 va_end(vl);
66
67 buf = malloc(len + 1, M_DRM, flags);
68 if (buf) {
69 vsnprintf(buf, len + 1, fmt, ap);
70 }
71
72 return buf;
73 }
74
75 static inline char *
kasprintf(int flags,const char * fmt,...)76 kasprintf(int flags, const char *fmt, ...)
77 {
78 char *buf;
79 va_list ap;
80
81 va_start(ap, fmt);
82 buf = kvasprintf(flags, fmt, ap);
83 va_end(ap);
84
85 return buf;
86 }
87
88 static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list ap)89 vscnprintf(char *buf, size_t size, const char *fmt, va_list ap)
90 {
91 int nc;
92
93 nc = vsnprintf(buf, size, fmt, ap);
94 if (nc > (size - 1))
95 return (size - 1);
96 else
97 return nc;
98 }
99
100 #define might_sleep() assertwaitok()
101 #define might_sleep_if(x) do { \
102 if (x) \
103 assertwaitok(); \
104 } while (0)
105
106 #define add_taint(x, y)
107 #define TAINT_MACHINE_CHECK 0
108 #define TAINT_WARN 1
109 #define LOCKDEP_STILL_OK 0
110
111 #define u64_to_user_ptr(x) ((void *)(uintptr_t)(x))
112
113 #define _RET_IP_ __builtin_return_address(0)
114
115 #define STUB() do { printf("%s: stub\n", __func__); } while(0)
116
117 #define PTR_IF(c, p) ((c) ? (p) : NULL)
118
119 #endif
120