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