Home | History | Annotate | Line # | Download | only in linux
kernel.h revision 1.16
      1 /*	$NetBSD: kernel.h,v 1.16 2018/08/27 06:55:32 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 #include <lib/libkern/libkern.h>
     41 #include <linux/bitops.h>
     42 #include <linux/printk.h>
     43 #include <linux/slab.h>
     44 
     45 #define U16_MAX UINT16_MAX
     46 #define U32_MAX UINT32_MAX
     47 #define U64_MAX UINT64_MAX
     48 
     49 #define	oops_in_progress	(panicstr != NULL)
     50 
     51 #define	IS_ENABLED(option)	(option)
     52 #define	IS_BUILTIN(option)	(1) /* Probably... */
     53 
     54 #define	__printf	__printflike
     55 #define	__user
     56 #define	__must_check	/* __attribute__((warn_unused_result)), if GCC */
     57 #define	__always_unused	__unused
     58 
     59 #define	barrier()	__insn_barrier()
     60 #define	likely(X)	__predict_true(X)
     61 #define	unlikely(X)	__predict_false(X)
     62 
     63 /*
     64  * XXX Linux kludge to work around GCC uninitialized variable warning.
     65  * Linux does `x = x', which is bollocks.
     66  */
     67 #define	uninitialized_var(x)	x = 0
     68 
     69 /* XXX These will multiply evaluate their arguments.  */
     70 #define	min(X, Y)	MIN(X, Y)
     71 #define	max(X, Y)	MAX(X, Y)
     72 
     73 #define	max_t(T, X, Y)	MAX(X, Y)
     74 #define	min_t(T, X, Y)	MIN(X, Y)
     75 
     76 #define	clamp_t(T, X, MIN, MAX)	min_t(T, max_t(T, X, MIN), MAX)
     77 #define	clamp(X, MN, MX)	MIN(MAX(X, MN), MX)
     78 
     79 #define	min3(X, Y, Z)	MIN(X, MIN(Y, Z))
     80 #define	max3(X, Y, Z)	MAX(X, MAX(Y, Z))
     81 
     82 /*
     83  * Rounding to nearest.
     84  */
     85 #define	DIV_ROUND_CLOSEST(N, D)						\
     86 	((0 < (N)) ? (((N) + ((D) / 2)) / (D))				\
     87 	    : (((N) - ((D) / 2)) / (D)))
     88 
     89 /*
     90  * Rounding to what may or may not be powers of two.
     91  */
     92 #define	DIV_ROUND_UP(X, N)	(((X) + (N) - 1) / (N))
     93 #define	DIV_ROUND_UP_ULL(X, N)	DIV_ROUND_UP((unsigned long long)(X), (N))
     94 
     95 /*
     96  * Rounding to powers of two -- carefully avoiding multiple evaluation
     97  * of arguments and pitfalls with C integer arithmetic rules.
     98  */
     99 #define	round_up(X, N)		((((X) - 1) | ((N) - 1)) + 1)
    100 #define	round_down(X, N)	((X) & ~(uintmax_t)((N) - 1))
    101 
    102 #define	IS_ALIGNED(X, N)	(((X) & ((N) - 1)) == 0)
    103 
    104 /*
    105  * These select 32-bit halves of what may be 32- or 64-bit quantities,
    106  * for which straight 32-bit shifts may be undefined behaviour (and do
    107  * the wrong thing on most machines: return the input unshifted by
    108  * ignoring the upper bits of the shift count).
    109  */
    110 #define	upper_32_bits(X)	((uint32_t) (((X) >> 16) >> 16))
    111 #define	lower_32_bits(X)	((uint32_t) ((X) & 0xffffffffUL))
    112 
    113 #define	ARRAY_SIZE(ARRAY)	__arraycount(ARRAY)
    114 
    115 #define	swap(X, Y)	do						\
    116 {									\
    117 	/* XXX Kludge for type-safety.  */				\
    118 	if (&(X) != &(Y)) {						\
    119 		CTASSERT(sizeof(X) == sizeof(Y));			\
    120 		/* XXX Can't do this much better without typeof.  */	\
    121 		char __swap_tmp[sizeof(X)];				\
    122 		(void)memcpy(__swap_tmp, &(X), sizeof(X));		\
    123 		(void)memcpy(&(X), &(Y), sizeof(X));			\
    124 		(void)memcpy(&(Y), __swap_tmp, sizeof(X));		\
    125 	}								\
    126 } while (0)
    127 
    128 static inline int64_t
    129 abs64(int64_t x)
    130 {
    131 	return (x < 0? (-x) : x);
    132 }
    133 
    134 static inline uintmax_t
    135 mult_frac(uintmax_t x, uintmax_t multiplier, uintmax_t divisor)
    136 {
    137 	uintmax_t q = (x / divisor);
    138 	uintmax_t r = (x % divisor);
    139 
    140 	return ((q * multiplier) + ((r * multiplier) / divisor));
    141 }
    142 
    143 static int panic_timeout __unused = 0;
    144 
    145 static inline int
    146 vscnprintf(char *buf, size_t size, const char *fmt, va_list va)
    147 {
    148 	int ret;
    149 
    150 	ret = vsnprintf(buf, size, fmt, va);
    151 	if (__predict_false(ret < 0))
    152 		return ret;
    153 	if (__predict_false(size == 0))
    154 		return 0;
    155 	if (__predict_false(size <= ret))
    156 		return (size - 1);
    157 
    158 	return ret;
    159 }
    160 
    161 static inline int
    162 scnprintf(char *buf, size_t size, const char *fmt, ...)
    163 {
    164 	va_list va;
    165 	int ret;
    166 
    167 	va_start(va, fmt);
    168 	ret = vscnprintf(buf, size, fmt, va);
    169 	va_end(va);
    170 
    171 	return ret;
    172 }
    173 
    174 static inline int
    175 kstrtol(const char *s, unsigned base, long *vp)
    176 {
    177 	long long v;
    178 
    179 	v = strtoll(s, NULL, base);
    180 	if (v < LONG_MIN || LONG_MAX < v)
    181 		return -ERANGE;
    182 	*vp = v;
    183 	return 0;
    184 }
    185 
    186 static inline char *
    187 kvasprintf(gfp_t gfp, const char *fmt, va_list va)
    188 {
    189 	char *str;
    190 	int len, len1 __diagused;
    191 
    192 	len = vsnprintf(NULL, 0, fmt, va);
    193 	str = kmalloc(len + 1, gfp);
    194 	if (str == NULL)
    195 		return NULL;
    196 	len1 = vsnprintf(str, len + 1, fmt, va);
    197 	KASSERT(len1 == len);
    198 
    199 	return str;
    200 }
    201 
    202 static inline char * __printflike(2,3)
    203 kasprintf(gfp_t gfp, const char *fmt, ...)
    204 {
    205 	va_list va;
    206 	char *str;
    207 
    208 	va_start(va, fmt);
    209 	str = kvasprintf(gfp, fmt, va);
    210 	va_end(va);
    211 
    212 	return str;
    213 }
    214 
    215 #endif  /* _LINUX_KERNEL_H_ */
    216