1 /* $NetBSD: libkern.h,v 1.147 2024/11/01 21:11:37 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 #include <sys/stddef.h> 47 #include <sys/container_of.h> 48 49 #include <lib/libkern/strlist.h> 50 51 #ifndef LIBKERN_INLINE 52 #define LIBKERN_INLINE static __inline 53 #define LIBKERN_BODY 54 #endif 55 56 LIBKERN_INLINE int imax(int, int) __unused; 57 LIBKERN_INLINE int imin(int, int) __unused; 58 LIBKERN_INLINE u_int uimax(u_int, u_int) __unused; 59 LIBKERN_INLINE u_int uimin(u_int, u_int) __unused; 60 LIBKERN_INLINE long lmax(long, long) __unused; 61 LIBKERN_INLINE long lmin(long, long) __unused; 62 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused; 63 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused; 64 LIBKERN_INLINE int abs(int) __unused; 65 LIBKERN_INLINE long labs(long) __unused; 66 LIBKERN_INLINE long long llabs(long long) __unused; 67 LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused; 68 69 LIBKERN_INLINE int isspace(int) __unused; 70 LIBKERN_INLINE int isascii(int) __unused; 71 LIBKERN_INLINE int isupper(int) __unused; 72 LIBKERN_INLINE int islower(int) __unused; 73 LIBKERN_INLINE int isalpha(int) __unused; 74 LIBKERN_INLINE int isalnum(int) __unused; 75 LIBKERN_INLINE int isdigit(int) __unused; 76 LIBKERN_INLINE int isxdigit(int) __unused; 77 LIBKERN_INLINE int iscntrl(int) __unused; 78 LIBKERN_INLINE int isgraph(int) __unused; 79 LIBKERN_INLINE int isprint(int) __unused; 80 LIBKERN_INLINE int ispunct(int) __unused; 81 LIBKERN_INLINE int toupper(int) __unused; 82 LIBKERN_INLINE int tolower(int) __unused; 83 84 #ifdef LIBKERN_BODY 85 LIBKERN_INLINE int 86 imax(int a, int b) 87 { 88 return (a > b ? a : b); 89 } 90 LIBKERN_INLINE int 91 imin(int a, int b) 92 { 93 return (a < b ? a : b); 94 } 95 LIBKERN_INLINE long 96 lmax(long a, long b) 97 { 98 return (a > b ? a : b); 99 } 100 LIBKERN_INLINE long 101 lmin(long a, long b) 102 { 103 return (a < b ? a : b); 104 } 105 LIBKERN_INLINE u_int 106 uimax(u_int a, u_int b) 107 { 108 return (a > b ? a : b); 109 } 110 LIBKERN_INLINE u_int 111 uimin(u_int a, u_int b) 112 { 113 return (a < b ? a : b); 114 } 115 LIBKERN_INLINE u_long 116 ulmax(u_long a, u_long b) 117 { 118 return (a > b ? a : b); 119 } 120 LIBKERN_INLINE u_long 121 ulmin(u_long a, u_long b) 122 { 123 return (a < b ? a : b); 124 } 125 126 LIBKERN_INLINE int 127 abs(int j) 128 { 129 return(j < 0 ? -j : j); 130 } 131 132 LIBKERN_INLINE long 133 labs(long j) 134 { 135 return(j < 0 ? -j : j); 136 } 137 138 LIBKERN_INLINE long long 139 llabs(long long j) 140 { 141 return(j < 0 ? -j : j); 142 } 143 144 LIBKERN_INLINE intmax_t 145 imaxabs(intmax_t j) 146 { 147 return(j < 0 ? -j : j); 148 } 149 150 LIBKERN_INLINE int 151 isspace(int ch) 152 { 153 return (ch == ' ' || (ch >= '\t' && ch <= '\r')); 154 } 155 156 LIBKERN_INLINE int 157 isascii(int ch) 158 { 159 return ((ch & ~0x7f) == 0); 160 } 161 162 LIBKERN_INLINE int 163 isupper(int ch) 164 { 165 return (ch >= 'A' && ch <= 'Z'); 166 } 167 168 LIBKERN_INLINE int 169 islower(int ch) 170 { 171 return (ch >= 'a' && ch <= 'z'); 172 } 173 174 LIBKERN_INLINE int 175 isalpha(int ch) 176 { 177 return (isupper(ch) || islower(ch)); 178 } 179 180 LIBKERN_INLINE int 181 isalnum(int ch) 182 { 183 return (isalpha(ch) || isdigit(ch)); 184 } 185 186 LIBKERN_INLINE int 187 isdigit(int ch) 188 { 189 return (ch >= '0' && ch <= '9'); 190 } 191 192 LIBKERN_INLINE int 193 isxdigit(int ch) 194 { 195 return (isdigit(ch) || 196 (ch >= 'A' && ch <= 'F') || 197 (ch >= 'a' && ch <= 'f')); 198 } 199 200 LIBKERN_INLINE int 201 iscntrl(int ch) 202 { 203 return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F); 204 } 205 206 LIBKERN_INLINE int 207 isgraph(int ch) 208 { 209 return (ch != ' ' && isprint(ch)); 210 } 211 212 LIBKERN_INLINE int 213 isprint(int ch) 214 { 215 return (ch >= 0x20 && ch <= 0x7E); 216 } 217 218 LIBKERN_INLINE int 219 ispunct(int ch) 220 { 221 return (isprint(ch) && ch != ' ' && !isalnum(ch)); 222 } 223 224 LIBKERN_INLINE int 225 toupper(int ch) 226 { 227 if (islower(ch)) 228 return (ch - 0x20); 229 return (ch); 230 } 231 232 LIBKERN_INLINE int 233 tolower(int ch) 234 { 235 if (isupper(ch)) 236 return (ch + 0x20); 237 return (ch); 238 } 239 #endif 240 241 #define __NULL_STMT do { } while (/* CONSTCOND */ 0) 242 243 #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d " 244 245 #ifdef NDEBUG /* tradition! */ 246 #define assert(e) ((void)0) 247 #else 248 #define assert(e) (__predict_true((e)) ? (void)0 : \ 249 kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__)) 250 #endif 251 252 #ifdef __COVERITY__ 253 #ifndef DIAGNOSTIC 254 #define DIAGNOSTIC 255 #endif 256 #endif 257 258 #ifndef CTASSERT 259 #define CTASSERT(x) __CTASSERT(x) 260 #endif 261 #ifndef CTASSERT_SIGNED 262 #define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0) 263 #endif 264 #ifndef CTASSERT_UNSIGNED 265 #define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0) 266 #endif 267 268 #ifndef DIAGNOSTIC 269 #define _DIAGASSERT(a) (void)0 270 #ifdef lint 271 #define KASSERTMSG(e, msg, ...) /* NOTHING */ 272 #define KASSERT(e) /* NOTHING */ 273 #else /* !lint */ 274 /* 275 * Make sure the expression compiles, but don't evaluate any of it. We 276 * use sizeof to inhibit evaluation, and cast to long so the expression 277 * can be integer- or pointer-valued without bringing in other header 278 * files. 279 */ 280 #define KASSERTMSG(e, msg, ...) ((void)sizeof((long)(e))) 281 #define KASSERT(e) ((void)sizeof((long)(e))) 282 #endif /* !lint */ 283 #else /* DIAGNOSTIC */ 284 #define _DIAGASSERT(a) assert(a) 285 #define KASSERTMSG(e, msg, ...) \ 286 (__predict_true((e)) ? (void)0 : \ 287 kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \ 288 __FILE__, __LINE__, ## __VA_ARGS__)) 289 290 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 291 kern_assert(__KASSERTSTR, "diagnostic ", #e, \ 292 __FILE__, __LINE__)) 293 #endif 294 295 #ifndef DEBUG 296 #ifdef lint 297 #define KDASSERTMSG(e,msg, ...) /* NOTHING */ 298 #define KDASSERT(e) /* NOTHING */ 299 #else /* lint */ 300 #define KDASSERTMSG(e,msg, ...) ((void)0) 301 #define KDASSERT(e) ((void)0) 302 #endif /* lint */ 303 #else 304 #define KDASSERTMSG(e, msg, ...) \ 305 (__predict_true((e)) ? (void)0 : \ 306 kern_assert(__KASSERTSTR msg, "debugging ", #e, \ 307 __FILE__, __LINE__, ## __VA_ARGS__)) 308 309 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 310 kern_assert(__KASSERTSTR, "debugging ", #e, \ 311 __FILE__, __LINE__)) 312 #endif 313 314 /* 315 * XXX: For compatibility we use SMALL_RANDOM by default. 316 */ 317 #define SMALL_RANDOM 318 319 /* Prototypes for which GCC built-ins exist. */ 320 void *memcpy(void *, const void *, size_t); 321 int memcmp(const void *, const void *, size_t); 322 void *memset(void *, int, size_t); 323 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE) 324 #if defined(_KERNEL) && defined(KASAN) 325 void *kasan_memcpy(void *, const void *, size_t); 326 int kasan_memcmp(const void *, const void *, size_t); 327 void *kasan_memset(void *, int, size_t); 328 #define memcpy(d, s, l) kasan_memcpy(d, s, l) 329 #define memcmp(a, b, l) kasan_memcmp(a, b, l) 330 #define memset(d, v, l) kasan_memset(d, v, l) 331 #elif defined(_KERNEL) && defined(KCSAN) 332 void *kcsan_memcpy(void *, const void *, size_t); 333 int kcsan_memcmp(const void *, const void *, size_t); 334 void *kcsan_memset(void *, int, size_t); 335 #define memcpy(d, s, l) kcsan_memcpy(d, s, l) 336 #define memcmp(a, b, l) kcsan_memcmp(a, b, l) 337 #define memset(d, v, l) kcsan_memset(d, v, l) 338 #elif defined(_KERNEL) && defined(KMSAN) 339 void *kmsan_memcpy(void *, const void *, size_t); 340 int kmsan_memcmp(const void *, const void *, size_t); 341 void *kmsan_memset(void *, int, size_t); 342 #define memcpy(d, s, l) kmsan_memcpy(d, s, l) 343 #define memcmp(a, b, l) kmsan_memcmp(a, b, l) 344 #define memset(d, v, l) kmsan_memset(d, v, l) 345 #else 346 #define memcpy(d, s, l) __builtin_memcpy(d, s, l) 347 #define memcmp(a, b, l) __builtin_memcmp(a, b, l) 348 #define memset(d, v, l) __builtin_memset(d, v, l) 349 #endif 350 #endif 351 void *memmem(const void *, size_t, const void *, size_t); 352 353 char *strcpy(char *, const char *); 354 int strcmp(const char *, const char *); 355 size_t strlen(const char *); 356 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE) 357 #if defined(_KERNEL) && defined(KASAN) 358 char *kasan_strcpy(char *, const char *); 359 int kasan_strcmp(const char *, const char *); 360 size_t kasan_strlen(const char *); 361 #define strcpy(d, s) kasan_strcpy(d, s) 362 #define strcmp(a, b) kasan_strcmp(a, b) 363 #define strlen(a) kasan_strlen(a) 364 #elif defined(_KERNEL) && defined(KCSAN) 365 char *kcsan_strcpy(char *, const char *); 366 int kcsan_strcmp(const char *, const char *); 367 size_t kcsan_strlen(const char *); 368 #define strcpy(d, s) kcsan_strcpy(d, s) 369 #define strcmp(a, b) kcsan_strcmp(a, b) 370 #define strlen(a) kcsan_strlen(a) 371 #elif defined(_KERNEL) && defined(KMSAN) 372 char *kmsan_strcpy(char *, const char *); 373 int kmsan_strcmp(const char *, const char *); 374 size_t kmsan_strlen(const char *); 375 #define strcpy(d, s) kmsan_strcpy(d, s) 376 #define strcmp(a, b) kmsan_strcmp(a, b) 377 #define strlen(a) kmsan_strlen(a) 378 #else 379 #define strcpy(d, s) __builtin_strcpy(d, s) 380 #define strcmp(a, b) __builtin_strcmp(a, b) 381 #define strlen(a) __builtin_strlen(a) 382 #endif 383 #endif 384 size_t strnlen(const char *, size_t); 385 char *strsep(char **, const char *); 386 387 /* Functions for which we always use built-ins. */ 388 #ifdef __GNUC__ 389 #define alloca(s) __builtin_alloca(s) 390 #endif 391 392 /* These exist in GCC 3.x, but we don't bother. */ 393 char *strcat(char *, const char *); 394 char *strchr(const char *, int); 395 char *strrchr(const char *, int); 396 #if defined(_KERNEL) && defined(KASAN) 397 char *kasan_strcat(char *, const char *); 398 char *kasan_strchr(const char *, int); 399 char *kasan_strrchr(const char *, int); 400 #define strcat(d, s) kasan_strcat(d, s) 401 #define strchr(s, c) kasan_strchr(s, c) 402 #define strrchr(s, c) kasan_strrchr(s, c) 403 #elif defined(_KERNEL) && defined(KMSAN) 404 char *kmsan_strcat(char *, const char *); 405 char *kmsan_strchr(const char *, int); 406 char *kmsan_strrchr(const char *, int); 407 #define strcat(d, s) kmsan_strcat(d, s) 408 #define strchr(s, c) kmsan_strchr(s, c) 409 #define strrchr(s, c) kmsan_strrchr(s, c) 410 #endif 411 size_t strcspn(const char *, const char *); 412 char *strncpy(char *, const char *, size_t); 413 char *strncat(char *, const char *, size_t); 414 int strncmp(const char *, const char *, size_t); 415 char *strstr(const char *, const char *); 416 char *strpbrk(const char *, const char *); 417 size_t strspn(const char *, const char *); 418 419 /* 420 * ffs is an instruction on vax. 421 */ 422 int ffs(int); 423 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1)) 424 #define ffs(x) __builtin_ffs(x) 425 #endif 426 427 void kern_assert(const char *, ...) 428 __attribute__((__format__(__printf__, 1, 2))); 429 u_int32_t 430 inet_addr(const char *); 431 struct in_addr; 432 int inet_aton(const char *, struct in_addr *); 433 char *intoa(u_int32_t); 434 #define inet_ntoa(a) intoa((a).s_addr) 435 void *memchr(const void *, int, size_t); 436 437 void *memmove(void *, const void *, size_t); 438 #if defined(_KERNEL) && defined(KASAN) 439 void *kasan_memmove(void *, const void *, size_t); 440 #define memmove(d, s, l) kasan_memmove(d, s, l) 441 #elif defined(_KERNEL) && defined(KCSAN) 442 void *kcsan_memmove(void *, const void *, size_t); 443 #define memmove(d, s, l) kcsan_memmove(d, s, l) 444 #elif defined(_KERNEL) && defined(KMSAN) 445 void *kmsan_memmove(void *, const void *, size_t); 446 #define memmove(d, s, l) kmsan_memmove(d, s, l) 447 #endif 448 449 int pmatch(const char *, const char *, const char **); 450 #ifndef SMALL_RANDOM 451 void srandom(unsigned long); 452 char *initstate(unsigned long, char *, size_t); 453 char *setstate(char *); 454 #endif /* SMALL_RANDOM */ 455 long random(void); 456 void mi_vector_hash(const void * __restrict, size_t, uint32_t, 457 uint32_t[3]); 458 int scanc(u_int, const u_char *, const u_char *, int); 459 int skpc(int, size_t, u_char *); 460 int strcasecmp(const char *, const char *); 461 size_t strlcpy(char * __restrict, const char * __restrict, size_t); 462 size_t strlcat(char * __restrict, const char * __restrict, size_t); 463 int strncasecmp(const char *, const char *, size_t); 464 u_long strtoul(const char *, char **, int); 465 long long strtoll(const char *, char **, int); 466 unsigned long long strtoull(const char *, char **, int); 467 intmax_t strtoimax(const char *, char **, int); 468 uintmax_t strtoumax(const char *, char **, int); 469 intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t, 470 intmax_t, int *); 471 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t, 472 uintmax_t, int *); 473 void hexdump(void (*)(const char *, ...) __printflike(1, 2), 474 const char *, const void *, size_t); 475 476 int snprintb(char *, size_t, const char *, uint64_t); 477 int snprintb_m(char *, size_t, const char *, uint64_t, size_t); 478 int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *), 479 void *); 480 uint32_t crc32(uint32_t, const uint8_t *, size_t); 481 #if __GNUC_PREREQ__(4, 5) \ 482 && (defined(__alpha_cix__) || defined(__mips_popcount)) 483 #define popcount __builtin_popcount 484 #define popcountl __builtin_popcountl 485 #define popcountll __builtin_popcountll 486 #define popcount32 __builtin_popcount 487 #define popcount64 __builtin_popcountll 488 #else 489 unsigned int popcount(unsigned int) __constfunc; 490 unsigned int popcountl(unsigned long) __constfunc; 491 unsigned int popcountll(unsigned long long) __constfunc; 492 unsigned int popcount32(uint32_t) __constfunc; 493 unsigned int popcount64(uint64_t) __constfunc; 494 #endif 495 496 void *explicit_memset(void *, int, size_t); 497 int consttime_memequal(const void *, const void *, size_t); 498 int strnvisx(char *, size_t, const char *, size_t, int); 499 #define VIS_OCTAL 0x01 500 #define VIS_SAFE 0x20 501 #define VIS_TRIM 0x40 502 503 struct disklabel; 504 void disklabel_swap(struct disklabel *, struct disklabel *); 505 uint16_t dkcksum(const struct disklabel *); 506 uint16_t dkcksum_sized(const struct disklabel *, size_t); 507 508 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ 509