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