Home | History | Annotate | Line # | Download | only in libkern
libkern.h revision 1.107.2.2
      1  1.107.2.2     rmind /*	$NetBSD: libkern.h,v 1.107.2.2 2014/05/18 17:46:08 rmind 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.1       cgd #include <sys/types.h>
     38       1.62   thorpej #include <sys/inttypes.h>
     39       1.52  christos #include <sys/null.h>
     40        1.4       cgd 
     41        1.7  christos #ifndef LIBKERN_INLINE
     42       1.55     perry #define LIBKERN_INLINE	static __inline
     43        1.7  christos #define LIBKERN_BODY
     44        1.7  christos #endif
     45        1.7  christos 
     46       1.86       dsl LIBKERN_INLINE int imax(int, int) __unused;
     47       1.86       dsl LIBKERN_INLINE int imin(int, int) __unused;
     48       1.86       dsl LIBKERN_INLINE u_int max(u_int, u_int) __unused;
     49       1.86       dsl LIBKERN_INLINE u_int min(u_int, u_int) __unused;
     50       1.86       dsl LIBKERN_INLINE long lmax(long, long) __unused;
     51       1.86       dsl LIBKERN_INLINE long lmin(long, long) __unused;
     52       1.86       dsl LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
     53       1.86       dsl LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
     54       1.86       dsl LIBKERN_INLINE int abs(int) __unused;
     55  1.107.2.2     rmind LIBKERN_INLINE long labs(long) __unused;
     56  1.107.2.2     rmind LIBKERN_INLINE long long llabs(long long) __unused;
     57  1.107.2.2     rmind LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
     58       1.86       dsl 
     59       1.86       dsl LIBKERN_INLINE int isspace(int) __unused;
     60       1.86       dsl LIBKERN_INLINE int isascii(int) __unused;
     61       1.86       dsl LIBKERN_INLINE int isupper(int) __unused;
     62       1.86       dsl LIBKERN_INLINE int islower(int) __unused;
     63       1.86       dsl LIBKERN_INLINE int isalpha(int) __unused;
     64       1.86       dsl LIBKERN_INLINE int isdigit(int) __unused;
     65       1.86       dsl LIBKERN_INLINE int isxdigit(int) __unused;
     66       1.86       dsl LIBKERN_INLINE int toupper(int) __unused;
     67       1.86       dsl LIBKERN_INLINE int tolower(int) __unused;
     68       1.33   thorpej 
     69        1.7  christos #ifdef LIBKERN_BODY
     70        1.7  christos LIBKERN_INLINE int
     71       1.44      matt imax(int a, int b)
     72        1.1       cgd {
     73        1.1       cgd 	return (a > b ? a : b);
     74        1.1       cgd }
     75        1.7  christos LIBKERN_INLINE int
     76       1.44      matt imin(int a, int b)
     77        1.1       cgd {
     78        1.1       cgd 	return (a < b ? a : b);
     79        1.1       cgd }
     80        1.7  christos LIBKERN_INLINE long
     81       1.44      matt lmax(long a, long b)
     82        1.1       cgd {
     83        1.1       cgd 	return (a > b ? a : b);
     84        1.1       cgd }
     85        1.7  christos LIBKERN_INLINE long
     86       1.44      matt lmin(long a, long b)
     87        1.1       cgd {
     88        1.1       cgd 	return (a < b ? a : b);
     89        1.1       cgd }
     90        1.7  christos LIBKERN_INLINE u_int
     91       1.44      matt max(u_int a, u_int b)
     92        1.1       cgd {
     93        1.1       cgd 	return (a > b ? a : b);
     94        1.1       cgd }
     95        1.7  christos LIBKERN_INLINE u_int
     96       1.44      matt min(u_int a, u_int b)
     97        1.1       cgd {
     98        1.1       cgd 	return (a < b ? a : b);
     99        1.1       cgd }
    100        1.7  christos LIBKERN_INLINE u_long
    101       1.44      matt ulmax(u_long a, u_long b)
    102        1.1       cgd {
    103        1.1       cgd 	return (a > b ? a : b);
    104        1.1       cgd }
    105        1.7  christos LIBKERN_INLINE u_long
    106       1.44      matt ulmin(u_long a, u_long b)
    107        1.1       cgd {
    108        1.1       cgd 	return (a < b ? a : b);
    109        1.5       leo }
    110        1.5       leo 
    111        1.7  christos LIBKERN_INLINE int
    112       1.44      matt abs(int j)
    113        1.5       leo {
    114        1.5       leo 	return(j < 0 ? -j : j);
    115       1.33   thorpej }
    116       1.33   thorpej 
    117  1.107.2.2     rmind LIBKERN_INLINE long
    118  1.107.2.2     rmind labs(long j)
    119  1.107.2.2     rmind {
    120  1.107.2.2     rmind 	return(j < 0 ? -j : j);
    121  1.107.2.2     rmind }
    122  1.107.2.2     rmind 
    123  1.107.2.2     rmind LIBKERN_INLINE long long
    124  1.107.2.2     rmind llabs(long long j)
    125  1.107.2.2     rmind {
    126  1.107.2.2     rmind 	return(j < 0 ? -j : j);
    127  1.107.2.2     rmind }
    128  1.107.2.2     rmind 
    129  1.107.2.2     rmind LIBKERN_INLINE intmax_t
    130  1.107.2.2     rmind imaxabs(intmax_t j)
    131  1.107.2.2     rmind {
    132  1.107.2.2     rmind 	return(j < 0 ? -j : j);
    133  1.107.2.2     rmind }
    134  1.107.2.2     rmind 
    135       1.33   thorpej LIBKERN_INLINE int
    136       1.44      matt isspace(int ch)
    137       1.33   thorpej {
    138       1.33   thorpej 	return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
    139       1.33   thorpej }
    140       1.33   thorpej 
    141       1.33   thorpej LIBKERN_INLINE int
    142       1.44      matt isascii(int ch)
    143       1.33   thorpej {
    144       1.33   thorpej 	return ((ch & ~0x7f) == 0);
    145       1.33   thorpej }
    146       1.33   thorpej 
    147       1.33   thorpej LIBKERN_INLINE int
    148       1.44      matt isupper(int ch)
    149       1.33   thorpej {
    150       1.33   thorpej 	return (ch >= 'A' && ch <= 'Z');
    151       1.33   thorpej }
    152       1.33   thorpej 
    153       1.33   thorpej LIBKERN_INLINE int
    154       1.44      matt islower(int ch)
    155       1.33   thorpej {
    156       1.33   thorpej 	return (ch >= 'a' && ch <= 'z');
    157       1.33   thorpej }
    158       1.33   thorpej 
    159       1.33   thorpej LIBKERN_INLINE int
    160       1.44      matt isalpha(int ch)
    161       1.33   thorpej {
    162       1.33   thorpej 	return (isupper(ch) || islower(ch));
    163       1.33   thorpej }
    164       1.33   thorpej 
    165       1.33   thorpej LIBKERN_INLINE int
    166       1.44      matt isdigit(int ch)
    167       1.33   thorpej {
    168       1.33   thorpej 	return (ch >= '0' && ch <= '9');
    169       1.33   thorpej }
    170       1.33   thorpej 
    171       1.33   thorpej LIBKERN_INLINE int
    172       1.44      matt isxdigit(int ch)
    173       1.33   thorpej {
    174       1.33   thorpej 	return (isdigit(ch) ||
    175       1.33   thorpej 	    (ch >= 'A' && ch <= 'F') ||
    176       1.33   thorpej 	    (ch >= 'a' && ch <= 'f'));
    177       1.33   thorpej }
    178       1.33   thorpej 
    179       1.33   thorpej LIBKERN_INLINE int
    180       1.44      matt toupper(int ch)
    181       1.33   thorpej {
    182       1.33   thorpej 	if (islower(ch))
    183       1.33   thorpej 		return (ch - 0x20);
    184       1.33   thorpej 	return (ch);
    185       1.33   thorpej }
    186       1.33   thorpej 
    187       1.33   thorpej LIBKERN_INLINE int
    188       1.44      matt tolower(int ch)
    189       1.33   thorpej {
    190       1.33   thorpej 	if (isupper(ch))
    191       1.33   thorpej 		return (ch + 0x20);
    192       1.33   thorpej 	return (ch);
    193        1.1       cgd }
    194        1.7  christos #endif
    195        1.1       cgd 
    196       1.64      matt #define	__NULL_STMT		do { } while (/* CONSTCOND */ 0)
    197       1.59    dyoung 
    198      1.101       jym #define __KASSERTSTR  "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
    199      1.101       jym 
    200        1.9       cgd #ifdef NDEBUG						/* tradition! */
    201        1.9       cgd #define	assert(e)	((void)0)
    202       1.10       cgd #else
    203       1.30   thorpej #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    204      1.102  christos 			    kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
    205        1.9       cgd #endif
    206        1.9       cgd 
    207       1.61  christos #ifdef __COVERITY__
    208       1.61  christos #ifndef DIAGNOSTIC
    209       1.61  christos #define DIAGNOSTIC
    210       1.61  christos #endif
    211       1.61  christos #endif
    212       1.61  christos 
    213  1.107.2.2     rmind #ifndef	CTASSERT
    214       1.97      matt #define	CTASSERT(x)		__CTASSERT(x)
    215  1.107.2.2     rmind #endif
    216  1.107.2.2     rmind #ifndef	CTASSERT_SIGNED
    217      1.105     rmind #define	CTASSERT_SIGNED(x)	__CTASSERT(((typeof(x))-1) < 0)
    218  1.107.2.2     rmind #endif
    219  1.107.2.2     rmind #ifndef	CTASSERT_UNSIGNED
    220      1.105     rmind #define	CTASSERT_UNSIGNED(x)	__CTASSERT(((typeof(x))-1) >= 0)
    221  1.107.2.2     rmind #endif
    222       1.80      matt 
    223       1.61  christos #ifndef DIAGNOSTIC
    224       1.52  christos #define _DIAGASSERT(a)	(void)0
    225       1.34     lukem #ifdef lint
    226      1.101       jym #define	KASSERTMSG(e, msg, ...)	/* NOTHING */
    227       1.81      matt #define	KASSERT(e)		/* NOTHING */
    228       1.34     lukem #else /* !lint */
    229      1.101       jym #define	KASSERTMSG(e, msg, ...)	((void)0)
    230       1.81      matt #define	KASSERT(e)		((void)0)
    231       1.34     lukem #endif /* !lint */
    232       1.61  christos #else /* DIAGNOSTIC */
    233       1.52  christos #define _DIAGASSERT(a)	assert(a)
    234      1.101       jym #define	KASSERTMSG(e, msg, ...)		\
    235      1.101       jym 			(__predict_true((e)) ? (void)0 :		    \
    236      1.102  christos 			    kern_assert(__KASSERTSTR msg, "diagnostic ", #e,	    \
    237      1.101       jym 				__FILE__, __LINE__, ## __VA_ARGS__))
    238      1.101       jym 
    239       1.30   thorpej #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    240      1.102  christos 			    kern_assert(__KASSERTSTR, "diagnostic ", #e,	    \
    241      1.101       jym 				__FILE__, __LINE__))
    242        1.9       cgd #endif
    243        1.9       cgd 
    244        1.9       cgd #ifndef DEBUG
    245       1.34     lukem #ifdef lint
    246      1.101       jym #define	KDASSERTMSG(e,msg, ...)	/* NOTHING */
    247       1.96      matt #define	KDASSERT(e)		/* NOTHING */
    248       1.34     lukem #else /* lint */
    249      1.101       jym #define	KDASSERTMSG(e,msg, ...)	((void)0)
    250       1.96      matt #define	KDASSERT(e)		((void)0)
    251       1.34     lukem #endif /* lint */
    252        1.9       cgd #else
    253      1.101       jym #define	KDASSERTMSG(e, msg, ...)	\
    254      1.101       jym 			(__predict_true((e)) ? (void)0 :		    \
    255      1.102  christos 			    kern_assert(__KASSERTSTR msg, "debugging ", #e,	    \
    256      1.101       jym 				__FILE__, __LINE__, ## __VA_ARGS__))
    257      1.101       jym 
    258       1.30   thorpej #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    259      1.102  christos 			    kern_assert(__KASSERTSTR, "debugging ", #e,	    \
    260      1.101       jym 				__FILE__, __LINE__))
    261        1.9       cgd #endif
    262      1.101       jym 
    263       1.53  christos /*
    264       1.53  christos  * XXX: For compatibility we use SMALL_RANDOM by default.
    265       1.53  christos  */
    266       1.53  christos #define SMALL_RANDOM
    267       1.19   thorpej 
    268       1.31   msaitoh #ifndef offsetof
    269       1.98      matt #if __GNUC_PREREQ__(4, 0)
    270       1.98      matt #define offsetof(type, member)	__builtin_offsetof(type, member)
    271       1.98      matt #else
    272       1.47  christos #define	offsetof(type, member) \
    273       1.47  christos     ((size_t)(unsigned long)(&(((type *)0)->member)))
    274       1.31   msaitoh #endif
    275       1.98      matt #endif
    276        1.9       cgd 
    277       1.73      matt #define	MTPRNG_RLEN		624
    278       1.73      matt struct mtprng_state {
    279       1.73      matt 	unsigned int mt_idx;
    280       1.73      matt 	uint32_t mt_elem[MTPRNG_RLEN];
    281       1.75      matt 	uint32_t mt_count;
    282       1.74      matt 	uint32_t mt_sparse[3];
    283       1.73      matt };
    284       1.73      matt 
    285       1.38   thorpej /* Prototypes for which GCC built-ins exist. */
    286       1.86       dsl void	*memcpy(void *, const void *, size_t);
    287       1.86       dsl int	 memcmp(const void *, const void *, size_t);
    288       1.86       dsl void	*memset(void *, int, size_t);
    289      1.103       chs #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    290       1.38   thorpej #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
    291       1.38   thorpej #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
    292       1.63      matt #endif
    293      1.103       chs #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    294       1.38   thorpej #define	memset(d, v, l)		__builtin_memset(d, v, l)
    295       1.38   thorpej #endif
    296       1.38   thorpej 
    297       1.86       dsl char	*strcpy(char *, const char *);
    298       1.86       dsl int	 strcmp(const char *, const char *);
    299       1.86       dsl size_t	 strlen(const char *);
    300       1.99       jym size_t	 strnlen(const char *, size_t);
    301       1.56    dyoung char	*strsep(char **, const char *);
    302       1.88   tsutsui #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    303       1.38   thorpej #define	strcpy(d, s)		__builtin_strcpy(d, s)
    304       1.38   thorpej #define	strcmp(a, b)		__builtin_strcmp(a, b)
    305       1.38   thorpej #define	strlen(a)		__builtin_strlen(a)
    306       1.39   thorpej #endif
    307       1.39   thorpej 
    308       1.39   thorpej /* Functions for which we always use built-ins. */
    309       1.39   thorpej #ifdef __GNUC__
    310       1.39   thorpej #define	alloca(s)		__builtin_alloca(s)
    311       1.38   thorpej #endif
    312       1.38   thorpej 
    313       1.38   thorpej /* These exist in GCC 3.x, but we don't bother. */
    314       1.86       dsl char	*strcat(char *, const char *);
    315       1.86       dsl char	*strncpy(char *, const char *, size_t);
    316  1.107.2.2     rmind char	*strncat(char *, const char *, size_t);
    317       1.86       dsl int	 strncmp(const char *, const char *, size_t);
    318       1.86       dsl char	*strchr(const char *, int);
    319       1.86       dsl char	*strrchr(const char *, int);
    320       1.45  junyoung 
    321       1.86       dsl char	*strstr(const char *, const char *);
    322       1.38   thorpej 
    323       1.42     ragge /*
    324       1.42     ragge  * ffs is an instruction on vax.
    325       1.42     ragge  */
    326       1.86       dsl int	 ffs(int);
    327       1.69      matt #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
    328       1.63      matt #define	ffs(x)		__builtin_ffs(x)
    329       1.41   thorpej #endif
    330       1.38   thorpej 
    331      1.102  christos void	 kern_assert(const char *, ...)
    332      1.102  christos     __attribute__((__format__(__printf__, 1, 2)));
    333       1.58    kleink unsigned int
    334       1.86       dsl 	bcdtobin(unsigned int);
    335       1.58    kleink unsigned int
    336       1.86       dsl 	bintobcd(unsigned int);
    337       1.28    simonb u_int32_t
    338       1.86       dsl 	inet_addr(const char *);
    339       1.52  christos struct in_addr;
    340       1.86       dsl int	inet_aton(const char *, struct in_addr *);
    341       1.86       dsl char	*intoa(u_int32_t);
    342       1.28    simonb #define inet_ntoa(a) intoa((a).s_addr)
    343       1.86       dsl void	*memchr(const void *, int, size_t);
    344       1.86       dsl void	*memmove(void *, const void *, size_t);
    345       1.86       dsl int	 pmatch(const char *, const char *, const char **);
    346       1.53  christos #ifndef SMALL_RANDOM
    347       1.86       dsl void	 srandom(unsigned long);
    348       1.86       dsl char	*initstate(unsigned long, char *, size_t);
    349       1.86       dsl char	*setstate(char *);
    350       1.53  christos #endif /* SMALL_RANDOM */
    351       1.86       dsl long	 random(void);
    352  1.107.2.2     rmind void	 mi_vector_hash(const void * __restrict, size_t, uint32_t,
    353  1.107.2.2     rmind 	    uint32_t[3]);
    354       1.74      matt void	 mtprng_init32(struct mtprng_state *, uint32_t);
    355       1.74      matt void	 mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
    356       1.74      matt uint32_t mtprng_rawrandom(struct mtprng_state *);
    357       1.74      matt uint32_t mtprng_random(struct mtprng_state *);
    358       1.86       dsl int	 scanc(u_int, const u_char *, const u_char *, int);
    359       1.86       dsl int	 skpc(int, size_t, u_char *);
    360       1.86       dsl int	 strcasecmp(const char *, const char *);
    361       1.86       dsl size_t	 strlcpy(char *, const char *, size_t);
    362       1.86       dsl size_t	 strlcat(char *, const char *, size_t);
    363       1.86       dsl int	 strncasecmp(const char *, const char *, size_t);
    364       1.86       dsl u_long	 strtoul(const char *, char **, int);
    365       1.86       dsl long long strtoll(const char *, char **, int);
    366       1.86       dsl unsigned long long strtoull(const char *, char **, int);
    367  1.107.2.2     rmind intmax_t  strtoimax(const char *, char **, int);
    368       1.86       dsl uintmax_t strtoumax(const char *, char **, int);
    369       1.86       dsl int	 snprintb(char *, size_t, const char *, uint64_t);
    370       1.91  pgoyette int	 snprintb_m(char *, size_t, const char *, uint64_t, size_t);
    371       1.84        ad int	 kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
    372       1.84        ad 		   void *);
    373       1.90       tls uint32_t crc32(uint32_t, const uint8_t *, size_t);
    374       1.92     joerg unsigned int	popcount(unsigned int) __constfunc;
    375       1.92     joerg unsigned int	popcountl(unsigned long) __constfunc;
    376       1.92     joerg unsigned int	popcountll(unsigned long long) __constfunc;
    377       1.92     joerg unsigned int	popcount32(uint32_t) __constfunc;
    378       1.92     joerg unsigned int	popcount64(uint64_t) __constfunc;
    379      1.106  drochner 
    380  1.107.2.1     rmind void	*explicit_memset(void *, int, size_t);
    381      1.107  riastrad int	consttime_memequal(const void *, const void *, size_t);
    382       1.29    simonb #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
    383