xref: /netbsd-src/sys/external/bsd/common/include/linux/kernel.h (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: kernel.h,v 1.6 2015/02/25 15:50:16 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #ifndef _LINUX_KERNEL_H_
33 #define _LINUX_KERNEL_H_
34 
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 
40 #define	oops_in_progress	(panicstr != NULL)
41 
42 #define	IS_ENABLED(option)	0 /* XXX Hmm...  */
43 
44 #define	__printf	__printflike
45 #define	__user
46 #define	__must_check	/* __attribute__((warn_unused_result)), if GCC */
47 #define	__always_unused	__unused
48 
49 #define	barrier()	__insn_barrier()
50 #define	likely(X)	__predict_true(X)
51 #define	unlikely(X)	__predict_false(X)
52 
53 /*
54  * XXX Linux kludge to work around GCC uninitialized variable warning.
55  * Linux does `x = x', which is bollocks.
56  */
57 #define	uninitialized_var(x)	x = 0
58 
59 /* XXX These will multiply evaluate their arguments.  */
60 #define	max_t(T, X, Y)	MAX(X, Y)
61 #define	min_t(T, X, Y)	MIN(X, Y)
62 
63 #define	clamp_t(T, X, MIN, MAX)	min_t(T, max_t(T, X, MIN), MAX)
64 #define	clamp(X, MN, MX)	MIN(MAX(X, MN), MX)
65 
66 /*
67  * Rounding to nearest.
68  */
69 #define	DIV_ROUND_CLOSEST(N, D)						\
70 	((0 < (N)) ? (((N) + ((D) / 2)) / (D))				\
71 	    : (((N) - ((D) / 2)) / (D)))
72 
73 /*
74  * Rounding to what may or may not be powers of two.
75  */
76 #define	DIV_ROUND_UP(X, N)	(((X) + (N) - 1) / (N))
77 #define	DIV_ROUND_UP_ULL(X, N)	DIV_ROUND_UP((unsigned long long)(X), (N))
78 
79 /*
80  * Rounding to powers of two -- carefully avoiding multiple evaluation
81  * of arguments and pitfalls with C integer arithmetic rules.
82  */
83 #define	round_up(X, N)		((((X) - 1) | ((N) - 1)) + 1)
84 #define	round_down(X, N)	((X) & ~(uintmax_t)((N) - 1))
85 
86 #define	IS_ALIGNED(X, N)	(((X) & ((N) - 1)) == 0)
87 
88 /*
89  * These select 32-bit halves of what may be 32- or 64-bit quantities,
90  * for which straight 32-bit shifts may be undefined behaviour (and do
91  * the wrong thing on most machines: return the input unshifted by
92  * ignoring the upper bits of the shift count).
93  */
94 #define	upper_32_bits(X)	((uint32_t) (((X) >> 16) >> 16))
95 #define	lower_32_bits(X)	((uint32_t) ((X) & 0xffffffffUL))
96 
97 /*
98  * Given x = &c->f, container_of(x, T, f) gives us back c, where T is
99  * the type of c.
100  */
101 #define	container_of(PTR, TYPE, FIELD)					\
102 	((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD) +		\
103 	    0*sizeof((PTR) -						\
104 		&((TYPE *)(((char *)(PTR)) -				\
105 			offsetof(TYPE, FIELD)))->FIELD)))
106 
107 #define	ARRAY_SIZE(ARRAY)	__arraycount(ARRAY)
108 
109 #define	swap(X, Y)	do						\
110 {									\
111 	/* XXX Kludge for type-safety.  */				\
112 	if (&(X) != &(Y)) {						\
113 		CTASSERT(sizeof(X) == sizeof(Y));			\
114 		/* XXX Can't do this much better without typeof.  */	\
115 		char __swap_tmp[sizeof(X)];				\
116 		(void)memcpy(__swap_tmp, &(X), sizeof(X));		\
117 		(void)memcpy(&(X), &(Y), sizeof(X));			\
118 		(void)memcpy(&(Y), __swap_tmp, sizeof(X));		\
119 	}								\
120 } while (0)
121 
122 static inline int64_t
123 abs64(int64_t x)
124 {
125 	return (x < 0? (-x) : x);
126 }
127 
128 static inline uintmax_t
129 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
130 {
131 	uintmax_t q = (x / divisor);
132 	uintmax_t r = (x % divisor);
133 
134 	return ((q * multiplier) + ((r * multiplier) / divisor));
135 }
136 
137 static int panic_timeout __unused = 0;
138 
139 static inline int
140 vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
141 {
142 	int ret;
143 
144 	ret = vsnprintf(buf, size, fmt, va);
145 	if (__predict_false(ret < 0))
146 		return ret;
147 	if (__predict_false(size == 0))
148 		return 0;
149 	if (__predict_false(size <= ret))
150 		return (size - 1);
151 
152 	return ret;
153 }
154 
155 static inline int
156 scnprintf(char *buf, size_t size, const char *fmt, ...)
157 {
158 	va_list va;
159 	int ret;
160 
161 	va_start(va, fmt);
162 	ret = vscnprintf(buf, size, fmt, va);
163 	va_end(va);
164 
165 	return ret;
166 }
167 
168 static inline int
169 kstrtol(const char *s, unsigned base, long *vp)
170 {
171 	long long v;
172 
173 	v = strtoll(s, NULL, base);
174 	if (v < LONG_MIN || LONG_MAX < v)
175 		return -ERANGE;
176 	*vp = v;
177 	return 0;
178 }
179 
180 #endif  /* _LINUX_KERNEL_H_ */
181