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 #include <linux/wordpart.h>
25 
26 #define swap(a, b) \
27 	do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while(0)
28 
29 #define ARRAY_SIZE nitems
30 
31 #define scnprintf(str, size, fmt, arg...) snprintf(str, size, fmt, ## arg)
32 
33 #define min_t(t, a, b) ({ \
34 	t __min_a = (a); \
35 	t __min_b = (b); \
36 	__min_a < __min_b ? __min_a : __min_b; })
37 
38 #define max_t(t, a, b) ({ \
39 	t __max_a = (a); \
40 	t __max_b = (b); \
41 	__max_a > __max_b ? __max_a : __max_b; })
42 
43 #define MIN_T(t, a, b) min_t(t, a, b)
44 #define MAX_T(t, a, b) max_t(t, a, b)
45 
46 #define clamp_t(t, x, a, b) min_t(t, max_t(t, x, a), b)
47 #define clamp(x, a, b) clamp_t(__typeof(x), x, a, b)
48 #define clamp_val(x, a, b) clamp_t(__typeof(x), x, a, b)
49 
50 #define min(a, b) MIN(a, b)
51 #define max(a, b) MAX(a, b)
52 #define min3(x, y, z) MIN(x, MIN(y, z))
53 #define max3(x, y, z) MAX(x, MAX(y, z))
54 
55 #define min_not_zero(a, b) (a == 0) ? b : ((b == 0) ? a : min(a, b))
56 
57 static inline char *
kvasprintf(int flags,const char * fmt,va_list ap)58 kvasprintf(int flags, const char *fmt, va_list ap)
59 {
60 	char *buf;
61 	size_t len;
62 	va_list vl;
63 
64 	va_copy(vl, ap);
65 	len = vsnprintf(NULL, 0, fmt, vl);
66 	va_end(vl);
67 
68 	buf = malloc(len + 1, M_DRM, flags);
69 	if (buf) {
70 		vsnprintf(buf, len + 1, fmt, ap);
71 	}
72 
73 	return buf;
74 }
75 
76 static inline char *
kasprintf(int flags,const char * fmt,...)77 kasprintf(int flags, const char *fmt, ...)
78 {
79 	char *buf;
80 	va_list ap;
81 
82 	va_start(ap, fmt);
83 	buf = kvasprintf(flags, fmt, ap);
84 	va_end(ap);
85 
86 	return buf;
87 }
88 
89 static inline int
vscnprintf(char * buf,size_t size,const char * fmt,va_list ap)90 vscnprintf(char *buf, size_t size, const char *fmt, va_list ap)
91 {
92 	int nc;
93 
94 	nc = vsnprintf(buf, size, fmt, ap);
95 	if (nc > (size - 1))
96 		return (size - 1);
97 	else
98 		return nc;
99 }
100 
101 #define might_sleep()		assertwaitok()
102 #define might_sleep_if(x)	do {	\
103 	if (x)				\
104 		assertwaitok();		\
105 } while (0)
106 #define might_fault()
107 
108 #define add_taint(x, y)
109 #define TAINT_MACHINE_CHECK	0
110 #define TAINT_WARN		1
111 #define LOCKDEP_STILL_OK	0
112 
113 #define u64_to_user_ptr(x)	((void *)(uintptr_t)(x))
114 
115 #define _RET_IP_		__builtin_return_address(0)
116 #define _THIS_IP_		0
117 
118 #define STUB() do { printf("%s: stub\n", __func__); } while(0)
119 
120 #define PTR_IF(c, p)		((c) ? (p) : NULL)
121 
122 #endif
123