Fri Apr 17 17:24:46 2020 UTC ()
Slightly reorder for clarity, and add header.


(maxv)
diff -r1.139 -r1.140 src/sys/lib/libkern/libkern.h

cvs diff -r1.139 -r1.140 src/sys/lib/libkern/libkern.h (switch to unified diff)

--- src/sys/lib/libkern/libkern.h 2020/04/07 08:07:58 1.139
+++ src/sys/lib/libkern/libkern.h 2020/04/17 17:24:46 1.140
@@ -1,547 +1,544 @@ @@ -1,547 +1,544 @@
1/* $NetBSD: libkern.h,v 1.139 2020/04/07 08:07:58 skrll Exp $ */ 1/* $NetBSD: libkern.h,v 1.140 2020/04/17 17:24:46 maxv Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 1992, 1993 4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved. 5 * The Regents of the University of California. All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors 15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software 16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission. 17 * without specific prior written permission.
18 * 18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE. 29 * SUCH DAMAGE.
30 * 30 *
31 * @(#)libkern.h 8.2 (Berkeley) 8/5/94 31 * @(#)libkern.h 8.2 (Berkeley) 8/5/94
32 */ 32 */
33 33
34#ifndef _LIB_LIBKERN_LIBKERN_H_ 34#ifndef _LIB_LIBKERN_LIBKERN_H_
35#define _LIB_LIBKERN_LIBKERN_H_ 35#define _LIB_LIBKERN_LIBKERN_H_
36 36
37#ifdef _KERNEL_OPT 37#ifdef _KERNEL_OPT
38#include "opt_diagnostic.h" 38#include "opt_diagnostic.h"
39#include "opt_kasan.h" 39#include "opt_kasan.h"
40#include "opt_kcsan.h" 40#include "opt_kcsan.h"
 41#include "opt_kmsan.h"
41#endif 42#endif
42 43
43#include <sys/types.h> 44#include <sys/types.h>
44#include <sys/inttypes.h> 45#include <sys/inttypes.h>
45#include <sys/null.h> 46#include <sys/null.h>
46 47
47#ifndef LIBKERN_INLINE 48#ifndef LIBKERN_INLINE
48#define LIBKERN_INLINE static __inline 49#define LIBKERN_INLINE static __inline
49#define LIBKERN_BODY 50#define LIBKERN_BODY
50#endif 51#endif
51 52
52LIBKERN_INLINE int imax(int, int) __unused; 53LIBKERN_INLINE int imax(int, int) __unused;
53LIBKERN_INLINE int imin(int, int) __unused; 54LIBKERN_INLINE int imin(int, int) __unused;
54LIBKERN_INLINE u_int uimax(u_int, u_int) __unused; 55LIBKERN_INLINE u_int uimax(u_int, u_int) __unused;
55LIBKERN_INLINE u_int uimin(u_int, u_int) __unused; 56LIBKERN_INLINE u_int uimin(u_int, u_int) __unused;
56LIBKERN_INLINE long lmax(long, long) __unused; 57LIBKERN_INLINE long lmax(long, long) __unused;
57LIBKERN_INLINE long lmin(long, long) __unused; 58LIBKERN_INLINE long lmin(long, long) __unused;
58LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused; 59LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
59LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused; 60LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
60LIBKERN_INLINE int abs(int) __unused; 61LIBKERN_INLINE int abs(int) __unused;
61LIBKERN_INLINE long labs(long) __unused; 62LIBKERN_INLINE long labs(long) __unused;
62LIBKERN_INLINE long long llabs(long long) __unused; 63LIBKERN_INLINE long long llabs(long long) __unused;
63LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused; 64LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
64 65
65LIBKERN_INLINE int isspace(int) __unused; 66LIBKERN_INLINE int isspace(int) __unused;
66LIBKERN_INLINE int isascii(int) __unused; 67LIBKERN_INLINE int isascii(int) __unused;
67LIBKERN_INLINE int isupper(int) __unused; 68LIBKERN_INLINE int isupper(int) __unused;
68LIBKERN_INLINE int islower(int) __unused; 69LIBKERN_INLINE int islower(int) __unused;
69LIBKERN_INLINE int isalpha(int) __unused; 70LIBKERN_INLINE int isalpha(int) __unused;
70LIBKERN_INLINE int isalnum(int) __unused; 71LIBKERN_INLINE int isalnum(int) __unused;
71LIBKERN_INLINE int isdigit(int) __unused; 72LIBKERN_INLINE int isdigit(int) __unused;
72LIBKERN_INLINE int isxdigit(int) __unused; 73LIBKERN_INLINE int isxdigit(int) __unused;
73LIBKERN_INLINE int iscntrl(int) __unused; 74LIBKERN_INLINE int iscntrl(int) __unused;
74LIBKERN_INLINE int isgraph(int) __unused; 75LIBKERN_INLINE int isgraph(int) __unused;
75LIBKERN_INLINE int isprint(int) __unused; 76LIBKERN_INLINE int isprint(int) __unused;
76LIBKERN_INLINE int ispunct(int) __unused; 77LIBKERN_INLINE int ispunct(int) __unused;
77LIBKERN_INLINE int toupper(int) __unused; 78LIBKERN_INLINE int toupper(int) __unused;
78LIBKERN_INLINE int tolower(int) __unused; 79LIBKERN_INLINE int tolower(int) __unused;
79 80
80#ifdef LIBKERN_BODY 81#ifdef LIBKERN_BODY
81LIBKERN_INLINE int 82LIBKERN_INLINE int
82imax(int a, int b) 83imax(int a, int b)
83{ 84{
84 return (a > b ? a : b); 85 return (a > b ? a : b);
85} 86}
86LIBKERN_INLINE int 87LIBKERN_INLINE int
87imin(int a, int b) 88imin(int a, int b)
88{ 89{
89 return (a < b ? a : b); 90 return (a < b ? a : b);
90} 91}
91LIBKERN_INLINE long 92LIBKERN_INLINE long
92lmax(long a, long b) 93lmax(long a, long b)
93{ 94{
94 return (a > b ? a : b); 95 return (a > b ? a : b);
95} 96}
96LIBKERN_INLINE long 97LIBKERN_INLINE long
97lmin(long a, long b) 98lmin(long a, long b)
98{ 99{
99 return (a < b ? a : b); 100 return (a < b ? a : b);
100} 101}
101LIBKERN_INLINE u_int 102LIBKERN_INLINE u_int
102uimax(u_int a, u_int b) 103uimax(u_int a, u_int b)
103{ 104{
104 return (a > b ? a : b); 105 return (a > b ? a : b);
105} 106}
106LIBKERN_INLINE u_int 107LIBKERN_INLINE u_int
107uimin(u_int a, u_int b) 108uimin(u_int a, u_int b)
108{ 109{
109 return (a < b ? a : b); 110 return (a < b ? a : b);
110} 111}
111LIBKERN_INLINE u_long 112LIBKERN_INLINE u_long
112ulmax(u_long a, u_long b) 113ulmax(u_long a, u_long b)
113{ 114{
114 return (a > b ? a : b); 115 return (a > b ? a : b);
115} 116}
116LIBKERN_INLINE u_long 117LIBKERN_INLINE u_long
117ulmin(u_long a, u_long b) 118ulmin(u_long a, u_long b)
118{ 119{
119 return (a < b ? a : b); 120 return (a < b ? a : b);
120} 121}
121 122
122LIBKERN_INLINE int 123LIBKERN_INLINE int
123abs(int j) 124abs(int j)
124{ 125{
125 return(j < 0 ? -j : j); 126 return(j < 0 ? -j : j);
126} 127}
127 128
128LIBKERN_INLINE long 129LIBKERN_INLINE long
129labs(long j) 130labs(long j)
130{ 131{
131 return(j < 0 ? -j : j); 132 return(j < 0 ? -j : j);
132} 133}
133 134
134LIBKERN_INLINE long long 135LIBKERN_INLINE long long
135llabs(long long j) 136llabs(long long j)
136{ 137{
137 return(j < 0 ? -j : j); 138 return(j < 0 ? -j : j);
138} 139}
139 140
140LIBKERN_INLINE intmax_t 141LIBKERN_INLINE intmax_t
141imaxabs(intmax_t j) 142imaxabs(intmax_t j)
142{ 143{
143 return(j < 0 ? -j : j); 144 return(j < 0 ? -j : j);
144} 145}
145 146
146LIBKERN_INLINE int 147LIBKERN_INLINE int
147isspace(int ch) 148isspace(int ch)
148{ 149{
149 return (ch == ' ' || (ch >= '\t' && ch <= '\r')); 150 return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
150} 151}
151 152
152LIBKERN_INLINE int 153LIBKERN_INLINE int
153isascii(int ch) 154isascii(int ch)
154{ 155{
155 return ((ch & ~0x7f) == 0); 156 return ((ch & ~0x7f) == 0);
156} 157}
157 158
158LIBKERN_INLINE int 159LIBKERN_INLINE int
159isupper(int ch) 160isupper(int ch)
160{ 161{
161 return (ch >= 'A' && ch <= 'Z'); 162 return (ch >= 'A' && ch <= 'Z');
162} 163}
163 164
164LIBKERN_INLINE int 165LIBKERN_INLINE int
165islower(int ch) 166islower(int ch)
166{ 167{
167 return (ch >= 'a' && ch <= 'z'); 168 return (ch >= 'a' && ch <= 'z');
168} 169}
169 170
170LIBKERN_INLINE int 171LIBKERN_INLINE int
171isalpha(int ch) 172isalpha(int ch)
172{ 173{
173 return (isupper(ch) || islower(ch)); 174 return (isupper(ch) || islower(ch));
174} 175}
175 176
176LIBKERN_INLINE int 177LIBKERN_INLINE int
177isalnum(int ch) 178isalnum(int ch)
178{ 179{
179 return (isalpha(ch) || isdigit(ch)); 180 return (isalpha(ch) || isdigit(ch));
180} 181}
181 182
182LIBKERN_INLINE int 183LIBKERN_INLINE int
183isdigit(int ch) 184isdigit(int ch)
184{ 185{
185 return (ch >= '0' && ch <= '9'); 186 return (ch >= '0' && ch <= '9');
186} 187}
187 188
188LIBKERN_INLINE int 189LIBKERN_INLINE int
189isxdigit(int ch) 190isxdigit(int ch)
190{ 191{
191 return (isdigit(ch) || 192 return (isdigit(ch) ||
192 (ch >= 'A' && ch <= 'F') || 193 (ch >= 'A' && ch <= 'F') ||
193 (ch >= 'a' && ch <= 'f')); 194 (ch >= 'a' && ch <= 'f'));
194} 195}
195 196
196LIBKERN_INLINE int 197LIBKERN_INLINE int
197iscntrl(int ch) 198iscntrl(int ch)
198{ 199{
199 return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F); 200 return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F);
200} 201}
201 202
202LIBKERN_INLINE int 203LIBKERN_INLINE int
203isgraph(int ch) 204isgraph(int ch)
204{ 205{
205 return (ch != ' ' && isprint(ch)); 206 return (ch != ' ' && isprint(ch));
206} 207}
207 208
208LIBKERN_INLINE int 209LIBKERN_INLINE int
209isprint(int ch) 210isprint(int ch)
210{ 211{
211 return (ch >= 0x20 && ch <= 0x7E); 212 return (ch >= 0x20 && ch <= 0x7E);
212} 213}
213 214
214LIBKERN_INLINE int 215LIBKERN_INLINE int
215ispunct(int ch) 216ispunct(int ch)
216{ 217{
217 return (isprint(ch) && ch != ' ' && !isalnum(ch)); 218 return (isprint(ch) && ch != ' ' && !isalnum(ch));
218} 219}
219 220
220LIBKERN_INLINE int 221LIBKERN_INLINE int
221toupper(int ch) 222toupper(int ch)
222{ 223{
223 if (islower(ch)) 224 if (islower(ch))
224 return (ch - 0x20); 225 return (ch - 0x20);
225 return (ch); 226 return (ch);
226} 227}
227 228
228LIBKERN_INLINE int 229LIBKERN_INLINE int
229tolower(int ch) 230tolower(int ch)
230{ 231{
231 if (isupper(ch)) 232 if (isupper(ch))
232 return (ch + 0x20); 233 return (ch + 0x20);
233 return (ch); 234 return (ch);
234} 235}
235#endif 236#endif
236 237
237#define __NULL_STMT do { } while (/* CONSTCOND */ 0) 238#define __NULL_STMT do { } while (/* CONSTCOND */ 0)
238 239
239#define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d " 240#define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
240 241
241#ifdef NDEBUG /* tradition! */ 242#ifdef NDEBUG /* tradition! */
242#define assert(e) ((void)0) 243#define assert(e) ((void)0)
243#else 244#else
244#define assert(e) (__predict_true((e)) ? (void)0 : \ 245#define assert(e) (__predict_true((e)) ? (void)0 : \
245 kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__)) 246 kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
246#endif 247#endif
247 248
248#ifdef __COVERITY__ 249#ifdef __COVERITY__
249#ifndef DIAGNOSTIC 250#ifndef DIAGNOSTIC
250#define DIAGNOSTIC 251#define DIAGNOSTIC
251#endif 252#endif
252#endif 253#endif
253 254
254#ifndef CTASSERT 255#ifndef CTASSERT
255#define CTASSERT(x) __CTASSERT(x) 256#define CTASSERT(x) __CTASSERT(x)
256#endif 257#endif
257#ifndef CTASSERT_SIGNED 258#ifndef CTASSERT_SIGNED
258#define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0) 259#define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0)
259#endif 260#endif
260#ifndef CTASSERT_UNSIGNED 261#ifndef CTASSERT_UNSIGNED
261#define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0) 262#define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0)
262#endif 263#endif
263 264
264#ifndef DIAGNOSTIC 265#ifndef DIAGNOSTIC
265#define _DIAGASSERT(a) (void)0 266#define _DIAGASSERT(a) (void)0
266#ifdef lint 267#ifdef lint
267#define KASSERTMSG(e, msg, ...) /* NOTHING */ 268#define KASSERTMSG(e, msg, ...) /* NOTHING */
268#define KASSERT(e) /* NOTHING */ 269#define KASSERT(e) /* NOTHING */
269#else /* !lint */ 270#else /* !lint */
270#define KASSERTMSG(e, msg, ...) ((void)0) 271#define KASSERTMSG(e, msg, ...) ((void)0)
271#define KASSERT(e) ((void)0) 272#define KASSERT(e) ((void)0)
272#endif /* !lint */ 273#endif /* !lint */
273#else /* DIAGNOSTIC */ 274#else /* DIAGNOSTIC */
274#define _DIAGASSERT(a) assert(a) 275#define _DIAGASSERT(a) assert(a)
275#define KASSERTMSG(e, msg, ...) \ 276#define KASSERTMSG(e, msg, ...) \
276 (__predict_true((e)) ? (void)0 : \ 277 (__predict_true((e)) ? (void)0 : \
277 kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \ 278 kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \
278 __FILE__, __LINE__, ## __VA_ARGS__)) 279 __FILE__, __LINE__, ## __VA_ARGS__))
279 280
280#define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 281#define KASSERT(e) (__predict_true((e)) ? (void)0 : \
281 kern_assert(__KASSERTSTR, "diagnostic ", #e, \ 282 kern_assert(__KASSERTSTR, "diagnostic ", #e, \
282 __FILE__, __LINE__)) 283 __FILE__, __LINE__))
283#endif 284#endif
284 285
285#ifndef DEBUG 286#ifndef DEBUG
286#ifdef lint 287#ifdef lint
287#define KDASSERTMSG(e,msg, ...) /* NOTHING */ 288#define KDASSERTMSG(e,msg, ...) /* NOTHING */
288#define KDASSERT(e) /* NOTHING */ 289#define KDASSERT(e) /* NOTHING */
289#else /* lint */ 290#else /* lint */
290#define KDASSERTMSG(e,msg, ...) ((void)0) 291#define KDASSERTMSG(e,msg, ...) ((void)0)
291#define KDASSERT(e) ((void)0) 292#define KDASSERT(e) ((void)0)
292#endif /* lint */ 293#endif /* lint */
293#else 294#else
294#define KDASSERTMSG(e, msg, ...) \ 295#define KDASSERTMSG(e, msg, ...) \
295 (__predict_true((e)) ? (void)0 : \ 296 (__predict_true((e)) ? (void)0 : \
296 kern_assert(__KASSERTSTR msg, "debugging ", #e, \ 297 kern_assert(__KASSERTSTR msg, "debugging ", #e, \
297 __FILE__, __LINE__, ## __VA_ARGS__)) 298 __FILE__, __LINE__, ## __VA_ARGS__))
298 299
299#define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 300#define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
300 kern_assert(__KASSERTSTR, "debugging ", #e, \ 301 kern_assert(__KASSERTSTR, "debugging ", #e, \
301 __FILE__, __LINE__)) 302 __FILE__, __LINE__))
302#endif 303#endif
303 304
304/* 305/*
305 * XXX: For compatibility we use SMALL_RANDOM by default. 306 * XXX: For compatibility we use SMALL_RANDOM by default.
306 */ 307 */
307#define SMALL_RANDOM 308#define SMALL_RANDOM
308 309
309#ifndef offsetof 310#ifndef offsetof
310#if __GNUC_PREREQ__(4, 0) 311#if __GNUC_PREREQ__(4, 0)
311#define offsetof(type, member) __builtin_offsetof(type, member) 312#define offsetof(type, member) __builtin_offsetof(type, member)
312#else 313#else
313#define offsetof(type, member) \ 314#define offsetof(type, member) \
314 ((size_t)(unsigned long)(&(((type *)0)->member))) 315 ((size_t)(unsigned long)(&(((type *)0)->member)))
315#endif 316#endif
316#endif 317#endif
317 318
318/* 319/*
319 * Return the container of an embedded struct. Given x = &c->f, 320 * Return the container of an embedded struct. Given x = &c->f,
320 * container_of(x, T, f) yields c, where T is the type of c. Example: 321 * container_of(x, T, f) yields c, where T is the type of c. Example:
321 * 322 *
322 * struct foo { ... }; 323 * struct foo { ... };
323 * struct bar { 324 * struct bar {
324 * int b_x; 325 * int b_x;
325 * struct foo b_foo; 326 * struct foo b_foo;
326 * ... 327 * ...
327 * }; 328 * };
328 * 329 *
329 * struct bar b; 330 * struct bar b;
330 * struct foo *fp = b.b_foo; 331 * struct foo *fp = b.b_foo;
331 * 332 *
332 * Now we can get at b from fp by: 333 * Now we can get at b from fp by:
333 * 334 *
334 * struct bar *bp = container_of(fp, struct bar, b_foo); 335 * struct bar *bp = container_of(fp, struct bar, b_foo);
335 * 336 *
336 * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of 337 * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
337 * *fp does not match the type of struct bar::b_foo. 338 * *fp does not match the type of struct bar::b_foo.
338 * We skip the validation for coverity runs to avoid warnings. 339 * We skip the validation for coverity runs to avoid warnings.
339 */ 340 */
340#if defined(__COVERITY__) || defined(__LGTM_BOT__) 341#if defined(__COVERITY__) || defined(__LGTM_BOT__)
341#define __validate_container_of(PTR, TYPE, FIELD) 0 342#define __validate_container_of(PTR, TYPE, FIELD) 0
342#define __validate_const_container_of(PTR, TYPE, FIELD) 0 343#define __validate_const_container_of(PTR, TYPE, FIELD) 0
343#else 344#else
344#define __validate_container_of(PTR, TYPE, FIELD) \ 345#define __validate_container_of(PTR, TYPE, FIELD) \
345 (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \ 346 (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \
346 offsetof(TYPE, FIELD)))->FIELD)) 347 offsetof(TYPE, FIELD)))->FIELD))
347#define __validate_const_container_of(PTR, TYPE, FIELD) \ 348#define __validate_const_container_of(PTR, TYPE, FIELD) \
348 (0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) - \ 349 (0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) - \
349 offsetof(TYPE, FIELD)))->FIELD)) 350 offsetof(TYPE, FIELD)))->FIELD))
350#endif 351#endif
351 352
352#define container_of(PTR, TYPE, FIELD) \ 353#define container_of(PTR, TYPE, FIELD) \
353 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \ 354 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \
354 + __validate_container_of(PTR, TYPE, FIELD)) 355 + __validate_container_of(PTR, TYPE, FIELD))
355#define const_container_of(PTR, TYPE, FIELD) \ 356#define const_container_of(PTR, TYPE, FIELD) \
356 ((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD)) \ 357 ((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD)) \
357 + __validate_const_container_of(PTR, TYPE, FIELD)) 358 + __validate_const_container_of(PTR, TYPE, FIELD))
358 359
359/* Prototypes for which GCC built-ins exist. */ 360/* Prototypes for which GCC built-ins exist. */
360void *memcpy(void *, const void *, size_t); 361void *memcpy(void *, const void *, size_t);
361int memcmp(const void *, const void *, size_t); 362int memcmp(const void *, const void *, size_t);
362void *memset(void *, int, size_t); 363void *memset(void *, int, size_t);
363void *memmem(const void *, size_t, const void *, size_t); 
364#if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE) 364#if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
365#if defined(_KERNEL) && defined(KASAN) 365#if defined(_KERNEL) && defined(KASAN)
366void *kasan_memcpy(void *, const void *, size_t); 366void *kasan_memcpy(void *, const void *, size_t);
367int kasan_memcmp(const void *, const void *, size_t); 367int kasan_memcmp(const void *, const void *, size_t);
368void *kasan_memset(void *, int, size_t); 368void *kasan_memset(void *, int, size_t);
369#define memcpy(d, s, l) kasan_memcpy(d, s, l) 369#define memcpy(d, s, l) kasan_memcpy(d, s, l)
370#define memcmp(a, b, l) kasan_memcmp(a, b, l) 370#define memcmp(a, b, l) kasan_memcmp(a, b, l)
371#define memset(d, v, l) kasan_memset(d, v, l) 371#define memset(d, v, l) kasan_memset(d, v, l)
372#elif defined(_KERNEL) && defined(KCSAN) 372#elif defined(_KERNEL) && defined(KCSAN)
373void *kcsan_memcpy(void *, const void *, size_t); 373void *kcsan_memcpy(void *, const void *, size_t);
374int kcsan_memcmp(const void *, const void *, size_t); 374int kcsan_memcmp(const void *, const void *, size_t);
375void *kcsan_memset(void *, int, size_t); 375void *kcsan_memset(void *, int, size_t);
376#define memcpy(d, s, l) kcsan_memcpy(d, s, l) 376#define memcpy(d, s, l) kcsan_memcpy(d, s, l)
377#define memcmp(a, b, l) kcsan_memcmp(a, b, l) 377#define memcmp(a, b, l) kcsan_memcmp(a, b, l)
378#define memset(d, v, l) kcsan_memset(d, v, l) 378#define memset(d, v, l) kcsan_memset(d, v, l)
379#elif defined(_KERNEL) && defined(KMSAN) 379#elif defined(_KERNEL) && defined(KMSAN)
380void *kmsan_memcpy(void *, const void *, size_t); 380void *kmsan_memcpy(void *, const void *, size_t);
381int kmsan_memcmp(const void *, const void *, size_t); 381int kmsan_memcmp(const void *, const void *, size_t);
382void *kmsan_memset(void *, int, size_t); 382void *kmsan_memset(void *, int, size_t);
383#define memcpy(d, s, l) kmsan_memcpy(d, s, l) 383#define memcpy(d, s, l) kmsan_memcpy(d, s, l)
384#define memcmp(a, b, l) kmsan_memcmp(a, b, l) 384#define memcmp(a, b, l) kmsan_memcmp(a, b, l)
385#define memset(d, v, l) kmsan_memset(d, v, l) 385#define memset(d, v, l) kmsan_memset(d, v, l)
386#else 386#else
387#define memcpy(d, s, l) __builtin_memcpy(d, s, l) 387#define memcpy(d, s, l) __builtin_memcpy(d, s, l)
388#define memcmp(a, b, l) __builtin_memcmp(a, b, l) 388#define memcmp(a, b, l) __builtin_memcmp(a, b, l)
389#define memset(d, v, l) __builtin_memset(d, v, l) 389#define memset(d, v, l) __builtin_memset(d, v, l)
390#endif 390#endif
391#endif 391#endif
 392void *memmem(const void *, size_t, const void *, size_t);
392 393
393char *strcpy(char *, const char *); 394char *strcpy(char *, const char *);
394int strcmp(const char *, const char *); 395int strcmp(const char *, const char *);
395size_t strlen(const char *); 396size_t strlen(const char *);
396size_t strnlen(const char *, size_t); 
397char *strsep(char **, const char *); 
398#if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE) 397#if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
399#if defined(_KERNEL) && defined(KASAN) 398#if defined(_KERNEL) && defined(KASAN)
400char *kasan_strcpy(char *, const char *); 399char *kasan_strcpy(char *, const char *);
401int kasan_strcmp(const char *, const char *); 400int kasan_strcmp(const char *, const char *);
402size_t kasan_strlen(const char *); 401size_t kasan_strlen(const char *);
403#define strcpy(d, s) kasan_strcpy(d, s) 402#define strcpy(d, s) kasan_strcpy(d, s)
404#define strcmp(a, b) kasan_strcmp(a, b) 403#define strcmp(a, b) kasan_strcmp(a, b)
405#define strlen(a) kasan_strlen(a) 404#define strlen(a) kasan_strlen(a)
406#elif defined(_KERNEL) && defined(KCSAN) 405#elif defined(_KERNEL) && defined(KCSAN)
407char *kcsan_strcpy(char *, const char *); 406char *kcsan_strcpy(char *, const char *);
408int kcsan_strcmp(const char *, const char *); 407int kcsan_strcmp(const char *, const char *);
409size_t kcsan_strlen(const char *); 408size_t kcsan_strlen(const char *);
410#define strcpy(d, s) kcsan_strcpy(d, s) 409#define strcpy(d, s) kcsan_strcpy(d, s)
411#define strcmp(a, b) kcsan_strcmp(a, b) 410#define strcmp(a, b) kcsan_strcmp(a, b)
412#define strlen(a) kcsan_strlen(a) 411#define strlen(a) kcsan_strlen(a)
413#elif defined(_KERNEL) && defined(KMSAN) 412#elif defined(_KERNEL) && defined(KMSAN)
414char *kmsan_strcpy(char *, const char *); 413char *kmsan_strcpy(char *, const char *);
415int kmsan_strcmp(const char *, const char *); 414int kmsan_strcmp(const char *, const char *);
416size_t kmsan_strlen(const char *); 415size_t kmsan_strlen(const char *);
417#define strcpy(d, s) kmsan_strcpy(d, s) 416#define strcpy(d, s) kmsan_strcpy(d, s)
418#define strcmp(a, b) kmsan_strcmp(a, b) 417#define strcmp(a, b) kmsan_strcmp(a, b)
419#define strlen(a) kmsan_strlen(a) 418#define strlen(a) kmsan_strlen(a)
420#else 419#else
421#define strcpy(d, s) __builtin_strcpy(d, s) 420#define strcpy(d, s) __builtin_strcpy(d, s)
422#define strcmp(a, b) __builtin_strcmp(a, b) 421#define strcmp(a, b) __builtin_strcmp(a, b)
423#define strlen(a) __builtin_strlen(a) 422#define strlen(a) __builtin_strlen(a)
424#endif 423#endif
425#endif 424#endif
 425size_t strnlen(const char *, size_t);
 426char *strsep(char **, const char *);
426 427
427/* Functions for which we always use built-ins. */ 428/* Functions for which we always use built-ins. */
428#ifdef __GNUC__ 429#ifdef __GNUC__
429#define alloca(s) __builtin_alloca(s) 430#define alloca(s) __builtin_alloca(s)
430#endif 431#endif
431 432
 433/* These exist in GCC 3.x, but we don't bother. */
432char *strcat(char *, const char *); 434char *strcat(char *, const char *);
433char *strchr(const char *, int); 435char *strchr(const char *, int);
434char *strrchr(const char *, int); 436char *strrchr(const char *, int);
435/* These exist in GCC 3.x, but we don't bother. */ 
436#if defined(_KERNEL) && defined(KASAN) 437#if defined(_KERNEL) && defined(KASAN)
437char *kasan_strcat(char *, const char *); 438char *kasan_strcat(char *, const char *);
438char *kasan_strchr(const char *, int); 439char *kasan_strchr(const char *, int);
439char *kasan_strrchr(const char *, int); 440char *kasan_strrchr(const char *, int);
440#define strcat(d, s) kasan_strcat(d, s) 441#define strcat(d, s) kasan_strcat(d, s)
441#define strchr(s, c) kasan_strchr(s, c) 442#define strchr(s, c) kasan_strchr(s, c)
442#define strrchr(s, c) kasan_strrchr(s, c) 443#define strrchr(s, c) kasan_strrchr(s, c)
443#elif defined(_KERNEL) && defined(KMSAN) 444#elif defined(_KERNEL) && defined(KMSAN)
444char *kmsan_strcat(char *, const char *); 445char *kmsan_strcat(char *, const char *);
445char *kmsan_strchr(const char *, int); 446char *kmsan_strchr(const char *, int);
446char *kmsan_strrchr(const char *, int); 447char *kmsan_strrchr(const char *, int);
447#define strcat(d, s) kmsan_strcat(d, s) 448#define strcat(d, s) kmsan_strcat(d, s)
448#define strchr(s, c) kmsan_strchr(s, c) 449#define strchr(s, c) kmsan_strchr(s, c)
449#define strrchr(s, c) kmsan_strrchr(s, c) 450#define strrchr(s, c) kmsan_strrchr(s, c)
450#else 
451char *strcat(char *, const char *); 
452char *strchr(const char *, int); 
453char *strrchr(const char *, int); 
454#endif 451#endif
455size_t strcspn(const char *, const char *); 452size_t strcspn(const char *, const char *);
456char *strncpy(char *, const char *, size_t); 453char *strncpy(char *, const char *, size_t);
457char *strncat(char *, const char *, size_t); 454char *strncat(char *, const char *, size_t);
458int strncmp(const char *, const char *, size_t); 455int strncmp(const char *, const char *, size_t);
459char *strstr(const char *, const char *); 456char *strstr(const char *, const char *);
460char *strpbrk(const char *, const char *); 457char *strpbrk(const char *, const char *);
461size_t strspn(const char *, const char *); 458size_t strspn(const char *, const char *);
462 459
463/* 460/*
464 * ffs is an instruction on vax. 461 * ffs is an instruction on vax.
465 */ 462 */
466int ffs(int); 463int ffs(int);
467#if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1)) 464#if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
468#define ffs(x) __builtin_ffs(x) 465#define ffs(x) __builtin_ffs(x)
469#endif 466#endif
470 467
471void kern_assert(const char *, ...) 468void kern_assert(const char *, ...)
472 __attribute__((__format__(__printf__, 1, 2))); 469 __attribute__((__format__(__printf__, 1, 2)));
473u_int32_t 470u_int32_t
474 inet_addr(const char *); 471 inet_addr(const char *);
475struct in_addr; 472struct in_addr;
476int inet_aton(const char *, struct in_addr *); 473int inet_aton(const char *, struct in_addr *);
477char *intoa(u_int32_t); 474char *intoa(u_int32_t);
478#define inet_ntoa(a) intoa((a).s_addr) 475#define inet_ntoa(a) intoa((a).s_addr)
479void *memchr(const void *, int, size_t); 476void *memchr(const void *, int, size_t);
480 477
481void *memmove(void *, const void *, size_t); 478void *memmove(void *, const void *, size_t);
482#if defined(_KERNEL) && defined(KASAN) 479#if defined(_KERNEL) && defined(KASAN)
483void *kasan_memmove(void *, const void *, size_t); 480void *kasan_memmove(void *, const void *, size_t);
484#define memmove(d, s, l) kasan_memmove(d, s, l) 481#define memmove(d, s, l) kasan_memmove(d, s, l)
485#elif defined(_KERNEL) && defined(KCSAN) 482#elif defined(_KERNEL) && defined(KCSAN)
486void *kcsan_memmove(void *, const void *, size_t); 483void *kcsan_memmove(void *, const void *, size_t);
487#define memmove(d, s, l) kcsan_memmove(d, s, l) 484#define memmove(d, s, l) kcsan_memmove(d, s, l)
488#elif defined(_KERNEL) && defined(KMSAN) 485#elif defined(_KERNEL) && defined(KMSAN)
489void *kmsan_memmove(void *, const void *, size_t); 486void *kmsan_memmove(void *, const void *, size_t);
490#define memmove(d, s, l) kmsan_memmove(d, s, l) 487#define memmove(d, s, l) kmsan_memmove(d, s, l)
491#endif 488#endif
492 489
493int pmatch(const char *, const char *, const char **); 490int pmatch(const char *, const char *, const char **);
494#ifndef SMALL_RANDOM 491#ifndef SMALL_RANDOM
495void srandom(unsigned long); 492void srandom(unsigned long);
496char *initstate(unsigned long, char *, size_t); 493char *initstate(unsigned long, char *, size_t);
497char *setstate(char *); 494char *setstate(char *);
498#endif /* SMALL_RANDOM */ 495#endif /* SMALL_RANDOM */
499long random(void); 496long random(void);
500void mi_vector_hash(const void * __restrict, size_t, uint32_t, 497void mi_vector_hash(const void * __restrict, size_t, uint32_t,
501 uint32_t[3]); 498 uint32_t[3]);
502int scanc(u_int, const u_char *, const u_char *, int); 499int scanc(u_int, const u_char *, const u_char *, int);
503int skpc(int, size_t, u_char *); 500int skpc(int, size_t, u_char *);
504int strcasecmp(const char *, const char *); 501int strcasecmp(const char *, const char *);
505size_t strlcpy(char *, const char *, size_t); 502size_t strlcpy(char *, const char *, size_t);
506size_t strlcat(char *, const char *, size_t); 503size_t strlcat(char *, const char *, size_t);
507int strncasecmp(const char *, const char *, size_t); 504int strncasecmp(const char *, const char *, size_t);
508u_long strtoul(const char *, char **, int); 505u_long strtoul(const char *, char **, int);
509long long strtoll(const char *, char **, int); 506long long strtoll(const char *, char **, int);
510unsigned long long strtoull(const char *, char **, int); 507unsigned long long strtoull(const char *, char **, int);
511intmax_t strtoimax(const char *, char **, int); 508intmax_t strtoimax(const char *, char **, int);
512uintmax_t strtoumax(const char *, char **, int); 509uintmax_t strtoumax(const char *, char **, int);
513intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t, 510intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
514 intmax_t, int *); 511 intmax_t, int *);
515uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t, 512uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
516 uintmax_t, int *); 513 uintmax_t, int *);
517void hexdump(void (*)(const char *, ...) __printflike(1, 2), 514void hexdump(void (*)(const char *, ...) __printflike(1, 2),
518 const char *, const void *, size_t); 515 const char *, const void *, size_t);
519 516
520int snprintb(char *, size_t, const char *, uint64_t); 517int snprintb(char *, size_t, const char *, uint64_t);
521int snprintb_m(char *, size_t, const char *, uint64_t, size_t); 518int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
522int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *), 519int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
523 void *); 520 void *);
524uint32_t crc32(uint32_t, const uint8_t *, size_t); 521uint32_t crc32(uint32_t, const uint8_t *, size_t);
525#if __GNUC_PREREQ__(4, 5) \ 522#if __GNUC_PREREQ__(4, 5) \
526 && (defined(__alpha_cix__) || defined(__mips_popcount)) 523 && (defined(__alpha_cix__) || defined(__mips_popcount))
527#define popcount __builtin_popcount 524#define popcount __builtin_popcount
528#define popcountl __builtin_popcountl 525#define popcountl __builtin_popcountl
529#define popcountll __builtin_popcountll 526#define popcountll __builtin_popcountll
530#define popcount32 __builtin_popcount 527#define popcount32 __builtin_popcount
531#define popcount64 __builtin_popcountll 528#define popcount64 __builtin_popcountll
532#else 529#else
533unsigned int popcount(unsigned int) __constfunc; 530unsigned int popcount(unsigned int) __constfunc;
534unsigned int popcountl(unsigned long) __constfunc; 531unsigned int popcountl(unsigned long) __constfunc;
535unsigned int popcountll(unsigned long long) __constfunc; 532unsigned int popcountll(unsigned long long) __constfunc;
536unsigned int popcount32(uint32_t) __constfunc; 533unsigned int popcount32(uint32_t) __constfunc;
537unsigned int popcount64(uint64_t) __constfunc; 534unsigned int popcount64(uint64_t) __constfunc;
538#endif 535#endif
539 536
540void *explicit_memset(void *, int, size_t); 537void *explicit_memset(void *, int, size_t);
541int consttime_memequal(const void *, const void *, size_t); 538int consttime_memequal(const void *, const void *, size_t);
542int strnvisx(char *, size_t, const char *, size_t, int); 539int strnvisx(char *, size_t, const char *, size_t, int);
543#define VIS_OCTAL 0x01 540#define VIS_OCTAL 0x01
544#define VIS_SAFE 0x20 541#define VIS_SAFE 0x20
545#define VIS_TRIM 0x40 542#define VIS_TRIM 0x40
546 543
547#endif /* !_LIB_LIBKERN_LIBKERN_H_ */ 544#endif /* !_LIB_LIBKERN_LIBKERN_H_ */