Home | History | Annotate | Line # | Download | only in libkern
libkern.h revision 1.100
      1 /*	$NetBSD: libkern.h,v 1.100 2011/09/25 20:31:18 jym 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 #include <sys/types.h>
     38 #include <sys/inttypes.h>
     39 #include <sys/null.h>
     40 
     41 #ifndef LIBKERN_INLINE
     42 #define LIBKERN_INLINE	static __inline
     43 #define LIBKERN_BODY
     44 #endif
     45 
     46 LIBKERN_INLINE int imax(int, int) __unused;
     47 LIBKERN_INLINE int imin(int, int) __unused;
     48 LIBKERN_INLINE u_int max(u_int, u_int) __unused;
     49 LIBKERN_INLINE u_int min(u_int, u_int) __unused;
     50 LIBKERN_INLINE long lmax(long, long) __unused;
     51 LIBKERN_INLINE long lmin(long, long) __unused;
     52 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
     53 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
     54 LIBKERN_INLINE int abs(int) __unused;
     55 
     56 LIBKERN_INLINE int isspace(int) __unused;
     57 LIBKERN_INLINE int isascii(int) __unused;
     58 LIBKERN_INLINE int isupper(int) __unused;
     59 LIBKERN_INLINE int islower(int) __unused;
     60 LIBKERN_INLINE int isalpha(int) __unused;
     61 LIBKERN_INLINE int isdigit(int) __unused;
     62 LIBKERN_INLINE int isxdigit(int) __unused;
     63 LIBKERN_INLINE int toupper(int) __unused;
     64 LIBKERN_INLINE int tolower(int) __unused;
     65 
     66 #ifdef LIBKERN_BODY
     67 LIBKERN_INLINE int
     68 imax(int a, int b)
     69 {
     70 	return (a > b ? a : b);
     71 }
     72 LIBKERN_INLINE int
     73 imin(int a, int b)
     74 {
     75 	return (a < b ? a : b);
     76 }
     77 LIBKERN_INLINE long
     78 lmax(long a, long b)
     79 {
     80 	return (a > b ? a : b);
     81 }
     82 LIBKERN_INLINE long
     83 lmin(long a, long b)
     84 {
     85 	return (a < b ? a : b);
     86 }
     87 LIBKERN_INLINE u_int
     88 max(u_int a, u_int b)
     89 {
     90 	return (a > b ? a : b);
     91 }
     92 LIBKERN_INLINE u_int
     93 min(u_int a, u_int b)
     94 {
     95 	return (a < b ? a : b);
     96 }
     97 LIBKERN_INLINE u_long
     98 ulmax(u_long a, u_long b)
     99 {
    100 	return (a > b ? a : b);
    101 }
    102 LIBKERN_INLINE u_long
    103 ulmin(u_long a, u_long b)
    104 {
    105 	return (a < b ? a : b);
    106 }
    107 
    108 LIBKERN_INLINE int
    109 abs(int j)
    110 {
    111 	return(j < 0 ? -j : j);
    112 }
    113 
    114 LIBKERN_INLINE int
    115 isspace(int ch)
    116 {
    117 	return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
    118 }
    119 
    120 LIBKERN_INLINE int
    121 isascii(int ch)
    122 {
    123 	return ((ch & ~0x7f) == 0);
    124 }
    125 
    126 LIBKERN_INLINE int
    127 isupper(int ch)
    128 {
    129 	return (ch >= 'A' && ch <= 'Z');
    130 }
    131 
    132 LIBKERN_INLINE int
    133 islower(int ch)
    134 {
    135 	return (ch >= 'a' && ch <= 'z');
    136 }
    137 
    138 LIBKERN_INLINE int
    139 isalpha(int ch)
    140 {
    141 	return (isupper(ch) || islower(ch));
    142 }
    143 
    144 LIBKERN_INLINE int
    145 isdigit(int ch)
    146 {
    147 	return (ch >= '0' && ch <= '9');
    148 }
    149 
    150 LIBKERN_INLINE int
    151 isxdigit(int ch)
    152 {
    153 	return (isdigit(ch) ||
    154 	    (ch >= 'A' && ch <= 'F') ||
    155 	    (ch >= 'a' && ch <= 'f'));
    156 }
    157 
    158 LIBKERN_INLINE int
    159 toupper(int ch)
    160 {
    161 	if (islower(ch))
    162 		return (ch - 0x20);
    163 	return (ch);
    164 }
    165 
    166 LIBKERN_INLINE int
    167 tolower(int ch)
    168 {
    169 	if (isupper(ch))
    170 		return (ch + 0x20);
    171 	return (ch);
    172 }
    173 #endif
    174 
    175 #define	__NULL_STMT		do { } while (/* CONSTCOND */ 0)
    176 
    177 #ifdef NDEBUG						/* tradition! */
    178 #define	assert(e)	((void)0)
    179 #else
    180 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    181 			    kern_assert("", __FILE__, __LINE__, #e))
    182 #endif
    183 
    184 #ifdef __COVERITY__
    185 #ifndef DIAGNOSTIC
    186 #define DIAGNOSTIC
    187 #endif
    188 #endif
    189 
    190 #define	CTASSERT(x)		__CTASSERT(x)
    191 
    192 #ifndef DIAGNOSTIC
    193 #define _DIAGASSERT(a)	(void)0
    194 #ifdef lint
    195 #define	KASSERTMSG(e, msg)	/* NOTHING */
    196 #define	KASSERT(e)		/* NOTHING */
    197 #else /* !lint */
    198 #define	KASSERTMSG(e, msg)	((void)0)
    199 #define	KASSERT(e)		((void)0)
    200 #endif /* !lint */
    201 #else /* DIAGNOSTIC */
    202 #define _DIAGASSERT(a)	assert(a)
    203 #define	KASSERTMSG(e, msg) do {		\
    204 	if (__predict_false(!(e)))	\
    205 		panic msg;		\
    206 	} while (/*CONSTCOND*/ 0)
    207 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    208 			    kern_assert("diagnostic ", __FILE__, __LINE__, #e))
    209 #endif
    210 
    211 #ifndef DEBUG
    212 #ifdef lint
    213 #define	KDASSERTMSG(e,msg)	/* NOTHING */
    214 #define	KDASSERT(e)		/* NOTHING */
    215 #else /* lint */
    216 #define	KDASSERTMSG(e,msg)	((void)0)
    217 #define	KDASSERT(e)		((void)0)
    218 #endif /* lint */
    219 #else
    220 #define	KDASSERTMSG(e, msg) do {	\
    221 	if (__predict_false(!(e)))	\
    222 		panic msg;		\
    223 	} while (/*CONSTCOND*/ 0)
    224 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    225 			    kern_assert("debugging ", __FILE__, __LINE__, #e))
    226 #endif
    227 /*
    228  * XXX: For compatibility we use SMALL_RANDOM by default.
    229  */
    230 #define SMALL_RANDOM
    231 
    232 #ifndef offsetof
    233 #if __GNUC_PREREQ__(4, 0)
    234 #define offsetof(type, member)	__builtin_offsetof(type, member)
    235 #else
    236 #define	offsetof(type, member) \
    237     ((size_t)(unsigned long)(&(((type *)0)->member)))
    238 #endif
    239 #endif
    240 
    241 #define	MTPRNG_RLEN		624
    242 struct mtprng_state {
    243 	unsigned int mt_idx;
    244 	uint32_t mt_elem[MTPRNG_RLEN];
    245 	uint32_t mt_count;
    246 	uint32_t mt_sparse[3];
    247 };
    248 
    249 /* Prototypes for which GCC built-ins exist. */
    250 void	*memcpy(void *, const void *, size_t);
    251 int	 memcmp(const void *, const void *, size_t);
    252 void	*memset(void *, int, size_t);
    253 #if __GNUC_PREREQ__(2, 95) && (__GNUC_PREREQ__(4, 0) || !defined(__vax__)) && \
    254     !defined(_STANDALONE)
    255 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
    256 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
    257 #endif
    258 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) && !defined(_STANDALONE)
    259 #define	memset(d, v, l)		__builtin_memset(d, v, l)
    260 #endif
    261 
    262 char	*strcpy(char *, const char *);
    263 int	 strcmp(const char *, const char *);
    264 size_t	 strlen(const char *);
    265 size_t	 strnlen(const char *, size_t);
    266 char	*strsep(char **, const char *);
    267 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    268 #define	strcpy(d, s)		__builtin_strcpy(d, s)
    269 #define	strcmp(a, b)		__builtin_strcmp(a, b)
    270 #define	strlen(a)		__builtin_strlen(a)
    271 #endif
    272 
    273 /* Functions for which we always use built-ins. */
    274 #ifdef __GNUC__
    275 #define	alloca(s)		__builtin_alloca(s)
    276 #endif
    277 
    278 /* These exist in GCC 3.x, but we don't bother. */
    279 char	*strcat(char *, const char *);
    280 char	*strncpy(char *, const char *, size_t);
    281 int	 strncmp(const char *, const char *, size_t);
    282 char	*strchr(const char *, int);
    283 char	*strrchr(const char *, int);
    284 
    285 char	*strstr(const char *, const char *);
    286 
    287 /*
    288  * ffs is an instruction on vax.
    289  */
    290 int	 ffs(int);
    291 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
    292 #define	ffs(x)		__builtin_ffs(x)
    293 #endif
    294 
    295 void	 kern_assert(const char *, const char *, int, const char *);
    296 unsigned int
    297 	bcdtobin(unsigned int);
    298 unsigned int
    299 	bintobcd(unsigned int);
    300 u_int32_t
    301 	inet_addr(const char *);
    302 struct in_addr;
    303 int	inet_aton(const char *, struct in_addr *);
    304 char	*intoa(u_int32_t);
    305 #define inet_ntoa(a) intoa((a).s_addr)
    306 void	*memchr(const void *, int, size_t);
    307 void	*memmove(void *, const void *, size_t);
    308 int	 pmatch(const char *, const char *, const char **);
    309 u_int32_t arc4random(void);
    310 void	 arc4randbytes(void *, size_t);
    311 #ifndef SMALL_RANDOM
    312 void	 srandom(unsigned long);
    313 char	*initstate(unsigned long, char *, size_t);
    314 char	*setstate(char *);
    315 #endif /* SMALL_RANDOM */
    316 long	 random(void);
    317 void	 mtprng_init32(struct mtprng_state *, uint32_t);
    318 void	 mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
    319 uint32_t mtprng_rawrandom(struct mtprng_state *);
    320 uint32_t mtprng_random(struct mtprng_state *);
    321 int	 scanc(u_int, const u_char *, const u_char *, int);
    322 int	 skpc(int, size_t, u_char *);
    323 int	 strcasecmp(const char *, const char *);
    324 size_t	 strlcpy(char *, const char *, size_t);
    325 size_t	 strlcat(char *, const char *, size_t);
    326 int	 strncasecmp(const char *, const char *, size_t);
    327 u_long	 strtoul(const char *, char **, int);
    328 long long strtoll(const char *, char **, int);
    329 unsigned long long strtoull(const char *, char **, int);
    330 uintmax_t strtoumax(const char *, char **, int);
    331 int	 snprintb(char *, size_t, const char *, uint64_t);
    332 int	 snprintb_m(char *, size_t, const char *, uint64_t, size_t);
    333 int	 kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
    334 		   void *);
    335 uint32_t crc32(uint32_t, const uint8_t *, size_t);
    336 unsigned int	popcount(unsigned int) __constfunc;
    337 unsigned int	popcountl(unsigned long) __constfunc;
    338 unsigned int	popcountll(unsigned long long) __constfunc;
    339 unsigned int	popcount32(uint32_t) __constfunc;
    340 unsigned int	popcount64(uint64_t) __constfunc;
    341 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
    342