kernel.h revision 1.2 1 1.2 riastrad /* $NetBSD: kernel.h,v 1.2 2014/04/01 14:57:58 riastradh Exp $ */
2 1.1 skrll
3 1.1 skrll /*-
4 1.1 skrll * Copyright (c) 2013 The NetBSD Foundation, Inc.
5 1.1 skrll * All rights reserved.
6 1.1 skrll *
7 1.1 skrll * This code is derived from software contributed to The NetBSD Foundation
8 1.1 skrll * by Taylor R. Campbell.
9 1.1 skrll *
10 1.1 skrll * Redistribution and use in source and binary forms, with or without
11 1.1 skrll * modification, are permitted provided that the following conditions
12 1.1 skrll * are met:
13 1.1 skrll * 1. Redistributions of source code must retain the above copyright
14 1.1 skrll * notice, this list of conditions and the following disclaimer.
15 1.1 skrll * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 skrll * notice, this list of conditions and the following disclaimer in the
17 1.1 skrll * documentation and/or other materials provided with the distribution.
18 1.1 skrll *
19 1.1 skrll * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 skrll * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 skrll * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 skrll * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 skrll * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 skrll * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 skrll * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 skrll * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 skrll * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 skrll * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 skrll * POSSIBILITY OF SUCH DAMAGE.
30 1.1 skrll */
31 1.1 skrll
32 1.1 skrll #ifndef _LINUX_KERNEL_H_
33 1.1 skrll #define _LINUX_KERNEL_H_
34 1.1 skrll
35 1.1 skrll #include <sys/cdefs.h>
36 1.1 skrll #include <sys/types.h>
37 1.1 skrll #include <sys/param.h>
38 1.1 skrll
39 1.1 skrll #define __printf __printflike
40 1.1 skrll #define __user
41 1.1 skrll #define __must_check /* __attribute__((warn_unused_result)), if GCC */
42 1.1 skrll #define __always_unused __unused
43 1.1 skrll
44 1.1 skrll #define barrier() __insn_barrier()
45 1.1 skrll #define unlikely(X) __predict_false(X)
46 1.1 skrll
47 1.2 riastrad /*
48 1.2 riastrad * XXX Linux kludge to work around GCC uninitialized variable warning.
49 1.2 riastrad * Linux does `x = x', which is bollocks.
50 1.2 riastrad */
51 1.2 riastrad #define uninitialized_var(x) x = 0
52 1.1 skrll
53 1.1 skrll /* XXX These will multiply evaluate their arguments. */
54 1.1 skrll #define max_t(T, X, Y) MAX(X, Y)
55 1.1 skrll #define min_t(T, X, Y) MIN(X, Y)
56 1.1 skrll
57 1.1 skrll /*
58 1.2 riastrad * Rounding to nearest.
59 1.2 riastrad */
60 1.2 riastrad #define DIV_ROUND_CLOSEST(N, D) \
61 1.2 riastrad ((0 < (N)) ? (((N) + ((D) / 2)) / (D)) \
62 1.2 riastrad : (((N) - ((D) / 2)) / (D)))
63 1.2 riastrad
64 1.2 riastrad /*
65 1.1 skrll * Rounding to what may or may not be powers of two.
66 1.1 skrll */
67 1.1 skrll #define DIV_ROUND_UP(X, N) (((X) + (N) - 1) / (N))
68 1.1 skrll
69 1.1 skrll /*
70 1.1 skrll * Rounding to powers of two -- carefully avoiding multiple evaluation
71 1.1 skrll * of arguments and pitfalls with C integer arithmetic rules.
72 1.1 skrll */
73 1.1 skrll #define round_up(X, N) ((((X) - 1) | ((N) - 1)) + 1)
74 1.1 skrll #define round_down(X, N) ((X) & ~(uintmax_t)((N) - 1))
75 1.1 skrll
76 1.1 skrll /*
77 1.1 skrll * These select 32-bit halves of what may be 32- or 64-bit quantities,
78 1.1 skrll * for which straight 32-bit shifts may be undefined behaviour (and do
79 1.1 skrll * the wrong thing on most machines: return the input unshifted by
80 1.1 skrll * ignoring the upper bits of the shift count).
81 1.1 skrll */
82 1.1 skrll #define upper_32_bits(X) ((uint32_t) (((X) >> 16) >> 16))
83 1.1 skrll #define lower_32_bits(X) ((uint32_t) ((X) & 0xffffffffUL))
84 1.1 skrll
85 1.1 skrll /*
86 1.1 skrll * Given x = &c->f, container_of(x, T, f) gives us back c, where T is
87 1.1 skrll * the type of c.
88 1.1 skrll */
89 1.1 skrll #define container_of(PTR, TYPE, FIELD) \
90 1.1 skrll ((void)sizeof((PTR) - \
91 1.1 skrll &((TYPE *)(((char *)(PTR)) - \
92 1.1 skrll offsetof(TYPE, FIELD)))->FIELD), \
93 1.1 skrll ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD))))
94 1.1 skrll
95 1.1 skrll #define ARRAY_SIZE(ARRAY) __arraycount(ARRAY)
96 1.1 skrll
97 1.1 skrll #define swap(X, Y) do \
98 1.1 skrll { \
99 1.1 skrll /* XXX Kludge for type-safety. */ \
100 1.1 skrll if (&(X) != &(Y)) { \
101 1.1 skrll CTASSERT(sizeof(X) == sizeof(Y)); \
102 1.1 skrll /* XXX Can't do this much better without typeof. */ \
103 1.1 skrll char __swap_tmp[sizeof(X)]; \
104 1.1 skrll (void)memcpy(__swap_tmp, &(X), sizeof(X)); \
105 1.1 skrll (void)memcpy(&(X), &(Y), sizeof(X)); \
106 1.1 skrll (void)memcpy(&(Y), __swap_tmp, sizeof(X)); \
107 1.1 skrll } \
108 1.1 skrll } while (0)
109 1.1 skrll
110 1.1 skrll static inline int64_t
111 1.1 skrll abs64(int64_t x)
112 1.1 skrll {
113 1.1 skrll return (x < 0? (-x) : x);
114 1.1 skrll }
115 1.1 skrll
116 1.2 riastrad static int panic_timeout __unused = 0;
117 1.2 riastrad
118 1.1 skrll #endif /* _LINUX_KERNEL_H_ */
119