Home | History | Annotate | Line # | Download | only in libkern
libkern.h revision 1.126.2.1
      1 /*	$NetBSD: libkern.h,v 1.126.2.1 2018/07/28 04:38:08 pgoyette 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_diagnostic.h"
     39 #endif
     40 
     41 #include <sys/types.h>
     42 #include <sys/inttypes.h>
     43 #include <sys/null.h>
     44 
     45 #ifndef LIBKERN_INLINE
     46 #define LIBKERN_INLINE	static __inline
     47 #define LIBKERN_BODY
     48 #endif
     49 
     50 LIBKERN_INLINE int imax(int, int) __unused;
     51 LIBKERN_INLINE int imin(int, int) __unused;
     52 LIBKERN_INLINE u_int max(u_int, u_int) __unused;
     53 LIBKERN_INLINE u_int min(u_int, u_int) __unused;
     54 LIBKERN_INLINE long lmax(long, long) __unused;
     55 LIBKERN_INLINE long lmin(long, long) __unused;
     56 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
     57 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
     58 LIBKERN_INLINE int abs(int) __unused;
     59 LIBKERN_INLINE long labs(long) __unused;
     60 LIBKERN_INLINE long long llabs(long long) __unused;
     61 LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
     62 
     63 LIBKERN_INLINE int isspace(int) __unused;
     64 LIBKERN_INLINE int isascii(int) __unused;
     65 LIBKERN_INLINE int isupper(int) __unused;
     66 LIBKERN_INLINE int islower(int) __unused;
     67 LIBKERN_INLINE int isalpha(int) __unused;
     68 LIBKERN_INLINE int isalnum(int) __unused;
     69 LIBKERN_INLINE int isdigit(int) __unused;
     70 LIBKERN_INLINE int isxdigit(int) __unused;
     71 LIBKERN_INLINE int iscntrl(int) __unused;
     72 LIBKERN_INLINE int isgraph(int) __unused;
     73 LIBKERN_INLINE int isprint(int) __unused;
     74 LIBKERN_INLINE int ispunct(int) __unused;
     75 LIBKERN_INLINE int toupper(int) __unused;
     76 LIBKERN_INLINE int tolower(int) __unused;
     77 
     78 #ifdef LIBKERN_BODY
     79 LIBKERN_INLINE int
     80 imax(int a, int b)
     81 {
     82 	return (a > b ? a : b);
     83 }
     84 LIBKERN_INLINE int
     85 imin(int a, int b)
     86 {
     87 	return (a < b ? a : b);
     88 }
     89 LIBKERN_INLINE long
     90 lmax(long a, long b)
     91 {
     92 	return (a > b ? a : b);
     93 }
     94 LIBKERN_INLINE long
     95 lmin(long a, long b)
     96 {
     97 	return (a < b ? a : b);
     98 }
     99 LIBKERN_INLINE u_int
    100 max(u_int a, u_int b)
    101 {
    102 	return (a > b ? a : b);
    103 }
    104 LIBKERN_INLINE u_int
    105 min(u_int a, u_int b)
    106 {
    107 	return (a < b ? a : b);
    108 }
    109 LIBKERN_INLINE u_long
    110 ulmax(u_long a, u_long b)
    111 {
    112 	return (a > b ? a : b);
    113 }
    114 LIBKERN_INLINE u_long
    115 ulmin(u_long a, u_long b)
    116 {
    117 	return (a < b ? a : b);
    118 }
    119 
    120 LIBKERN_INLINE int
    121 abs(int j)
    122 {
    123 	return(j < 0 ? -j : j);
    124 }
    125 
    126 LIBKERN_INLINE long
    127 labs(long j)
    128 {
    129 	return(j < 0 ? -j : j);
    130 }
    131 
    132 LIBKERN_INLINE long long
    133 llabs(long long j)
    134 {
    135 	return(j < 0 ? -j : j);
    136 }
    137 
    138 LIBKERN_INLINE intmax_t
    139 imaxabs(intmax_t j)
    140 {
    141 	return(j < 0 ? -j : j);
    142 }
    143 
    144 LIBKERN_INLINE int
    145 isspace(int ch)
    146 {
    147 	return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
    148 }
    149 
    150 LIBKERN_INLINE int
    151 isascii(int ch)
    152 {
    153 	return ((ch & ~0x7f) == 0);
    154 }
    155 
    156 LIBKERN_INLINE int
    157 isupper(int ch)
    158 {
    159 	return (ch >= 'A' && ch <= 'Z');
    160 }
    161 
    162 LIBKERN_INLINE int
    163 islower(int ch)
    164 {
    165 	return (ch >= 'a' && ch <= 'z');
    166 }
    167 
    168 LIBKERN_INLINE int
    169 isalpha(int ch)
    170 {
    171 	return (isupper(ch) || islower(ch));
    172 }
    173 
    174 LIBKERN_INLINE int
    175 isalnum(int ch)
    176 {
    177 	return (isalpha(ch) || isdigit(ch));
    178 }
    179 
    180 LIBKERN_INLINE int
    181 isdigit(int ch)
    182 {
    183 	return (ch >= '0' && ch <= '9');
    184 }
    185 
    186 LIBKERN_INLINE int
    187 isxdigit(int ch)
    188 {
    189 	return (isdigit(ch) ||
    190 	    (ch >= 'A' && ch <= 'F') ||
    191 	    (ch >= 'a' && ch <= 'f'));
    192 }
    193 
    194 LIBKERN_INLINE int
    195 iscntrl(int ch)
    196 {
    197 	return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F);
    198 }
    199 
    200 LIBKERN_INLINE int
    201 isgraph(int ch)
    202 {
    203 	return (ch != ' ' && isprint(ch));
    204 }
    205 
    206 LIBKERN_INLINE int
    207 isprint(int ch)
    208 {
    209 	return (ch >= 0x20 && ch <= 0x7E);
    210 }
    211 
    212 LIBKERN_INLINE int
    213 ispunct(int ch)
    214 {
    215 	return (isprint(ch) && ch != ' ' && !isalnum(ch));
    216 }
    217 
    218 LIBKERN_INLINE int
    219 toupper(int ch)
    220 {
    221 	if (islower(ch))
    222 		return (ch - 0x20);
    223 	return (ch);
    224 }
    225 
    226 LIBKERN_INLINE int
    227 tolower(int ch)
    228 {
    229 	if (isupper(ch))
    230 		return (ch + 0x20);
    231 	return (ch);
    232 }
    233 #endif
    234 
    235 #define	__NULL_STMT		do { } while (/* CONSTCOND */ 0)
    236 
    237 #define __KASSERTSTR  "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
    238 
    239 #ifdef NDEBUG						/* tradition! */
    240 #define	assert(e)	((void)0)
    241 #else
    242 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    243 			    kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
    244 #endif
    245 
    246 #ifdef __COVERITY__
    247 #ifndef DIAGNOSTIC
    248 #define DIAGNOSTIC
    249 #endif
    250 #endif
    251 
    252 #ifndef	CTASSERT
    253 #define	CTASSERT(x)		__CTASSERT(x)
    254 #endif
    255 #ifndef	CTASSERT_SIGNED
    256 #define	CTASSERT_SIGNED(x)	__CTASSERT(((typeof(x))-1) < 0)
    257 #endif
    258 #ifndef	CTASSERT_UNSIGNED
    259 #define	CTASSERT_UNSIGNED(x)	__CTASSERT(((typeof(x))-1) >= 0)
    260 #endif
    261 
    262 #ifndef DIAGNOSTIC
    263 #define _DIAGASSERT(a)	(void)0
    264 #ifdef lint
    265 #define	KASSERTMSG(e, msg, ...)	/* NOTHING */
    266 #define	KASSERT(e)		/* NOTHING */
    267 #else /* !lint */
    268 #define	KASSERTMSG(e, msg, ...)	((void)0)
    269 #define	KASSERT(e)		((void)0)
    270 #endif /* !lint */
    271 #else /* DIAGNOSTIC */
    272 #define _DIAGASSERT(a)	assert(a)
    273 #define	KASSERTMSG(e, msg, ...)		\
    274 			(__predict_true((e)) ? (void)0 :		    \
    275 			    kern_assert(__KASSERTSTR msg, "diagnostic ", #e,	    \
    276 				__FILE__, __LINE__, ## __VA_ARGS__))
    277 
    278 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    279 			    kern_assert(__KASSERTSTR, "diagnostic ", #e,	    \
    280 				__FILE__, __LINE__))
    281 #endif
    282 
    283 #ifndef DEBUG
    284 #ifdef lint
    285 #define	KDASSERTMSG(e,msg, ...)	/* NOTHING */
    286 #define	KDASSERT(e)		/* NOTHING */
    287 #else /* lint */
    288 #define	KDASSERTMSG(e,msg, ...)	((void)0)
    289 #define	KDASSERT(e)		((void)0)
    290 #endif /* lint */
    291 #else
    292 #define	KDASSERTMSG(e, msg, ...)	\
    293 			(__predict_true((e)) ? (void)0 :		    \
    294 			    kern_assert(__KASSERTSTR msg, "debugging ", #e,	    \
    295 				__FILE__, __LINE__, ## __VA_ARGS__))
    296 
    297 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    298 			    kern_assert(__KASSERTSTR, "debugging ", #e,	    \
    299 				__FILE__, __LINE__))
    300 #endif
    301 
    302 /*
    303  * XXX: For compatibility we use SMALL_RANDOM by default.
    304  */
    305 #define SMALL_RANDOM
    306 
    307 #ifndef offsetof
    308 #if __GNUC_PREREQ__(4, 0)
    309 #define offsetof(type, member)	__builtin_offsetof(type, member)
    310 #else
    311 #define	offsetof(type, member) \
    312     ((size_t)(unsigned long)(&(((type *)0)->member)))
    313 #endif
    314 #endif
    315 
    316 /*
    317  * Return the container of an embedded struct.  Given x = &c->f,
    318  * container_of(x, T, f) yields c, where T is the type of c.  Example:
    319  *
    320  *	struct foo { ... };
    321  *	struct bar {
    322  *		int b_x;
    323  *		struct foo b_foo;
    324  *		...
    325  *	};
    326  *
    327  *	struct bar b;
    328  *	struct foo *fp = b.b_foo;
    329  *
    330  * Now we can get at b from fp by:
    331  *
    332  *	struct bar *bp = container_of(fp, struct bar, b_foo);
    333  *
    334  * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
    335  * *fp does not match the type of struct bar::b_foo.
    336  * We skip the validation for coverity runs to avoid warnings.
    337  */
    338 #ifdef __COVERITY__
    339 #define __validate_container_of(PTR, TYPE, FIELD) 0
    340 #define __validate_const_container_of(PTR, TYPE, FIELD) 0
    341 #else
    342 #define __validate_container_of(PTR, TYPE, FIELD)			\
    343     (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) -			\
    344     offsetof(TYPE, FIELD)))->FIELD))
    345 #define __validate_const_container_of(PTR, TYPE, FIELD)			\
    346     (0 * sizeof((PTR) - &((const TYPE *)(((const char *)(PTR)) -	\
    347     offsetof(TYPE, FIELD)))->FIELD))
    348 #endif
    349 
    350 #define	container_of(PTR, TYPE, FIELD)					\
    351     ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD))			\
    352 	+ __validate_container_of(PTR, TYPE, FIELD))
    353 #define	const_container_of(PTR, TYPE, FIELD)				\
    354     ((const TYPE *)(((const char *)(PTR)) - offsetof(TYPE, FIELD))	\
    355 	+ __validate_const_container_of(PTR, TYPE, FIELD))
    356 
    357 #define	MTPRNG_RLEN		624
    358 struct mtprng_state {
    359 	unsigned int mt_idx;
    360 	uint32_t mt_elem[MTPRNG_RLEN];
    361 	uint32_t mt_count;
    362 	uint32_t mt_sparse[3];
    363 };
    364 
    365 /* Prototypes for which GCC built-ins exist. */
    366 void	*memcpy(void *, const void *, size_t);
    367 int	 memcmp(const void *, const void *, size_t);
    368 void	*memset(void *, int, size_t);
    369 void	*memmem(const void *, size_t, const void *, size_t);
    370 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    371 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
    372 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
    373 #endif
    374 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    375 #define	memset(d, v, l)		__builtin_memset(d, v, l)
    376 #endif
    377 
    378 char	*strcpy(char *, const char *);
    379 int	 strcmp(const char *, const char *);
    380 size_t	 strlen(const char *);
    381 size_t	 strnlen(const char *, size_t);
    382 char	*strsep(char **, const char *);
    383 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
    384 #define	strcpy(d, s)		__builtin_strcpy(d, s)
    385 #define	strcmp(a, b)		__builtin_strcmp(a, b)
    386 #define	strlen(a)		__builtin_strlen(a)
    387 #endif
    388 
    389 /* Functions for which we always use built-ins. */
    390 #ifdef __GNUC__
    391 #define	alloca(s)		__builtin_alloca(s)
    392 #endif
    393 
    394 /* These exist in GCC 3.x, but we don't bother. */
    395 char	*strcat(char *, const char *);
    396 size_t	 strcspn(const char *, const char *);
    397 char	*strncpy(char *, const char *, size_t);
    398 char	*strncat(char *, const char *, size_t);
    399 int	 strncmp(const char *, const char *, size_t);
    400 char	*strchr(const char *, int);
    401 char	*strrchr(const char *, int);
    402 char	*strstr(const char *, const char *);
    403 char	*strpbrk(const char *, const char *);
    404 size_t	 strspn(const char *, const char *);
    405 
    406 /*
    407  * ffs is an instruction on vax.
    408  */
    409 int	 ffs(int);
    410 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
    411 #define	ffs(x)		__builtin_ffs(x)
    412 #endif
    413 
    414 void	 kern_assert(const char *, ...)
    415     __attribute__((__format__(__printf__, 1, 2)));
    416 u_int32_t
    417 	inet_addr(const char *);
    418 struct in_addr;
    419 int	inet_aton(const char *, struct in_addr *);
    420 char	*intoa(u_int32_t);
    421 #define inet_ntoa(a) intoa((a).s_addr)
    422 void	*memchr(const void *, int, size_t);
    423 void	*memmove(void *, const void *, size_t);
    424 int	 pmatch(const char *, const char *, const char **);
    425 #ifndef SMALL_RANDOM
    426 void	 srandom(unsigned long);
    427 char	*initstate(unsigned long, char *, size_t);
    428 char	*setstate(char *);
    429 #endif /* SMALL_RANDOM */
    430 long	 random(void);
    431 void	 mi_vector_hash(const void * __restrict, size_t, uint32_t,
    432 	    uint32_t[3]);
    433 void	 mtprng_init32(struct mtprng_state *, uint32_t);
    434 void	 mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
    435 uint32_t mtprng_rawrandom(struct mtprng_state *);
    436 uint32_t mtprng_random(struct mtprng_state *);
    437 int	 scanc(u_int, const u_char *, const u_char *, int);
    438 int	 skpc(int, size_t, u_char *);
    439 int	 strcasecmp(const char *, const char *);
    440 size_t	 strlcpy(char *, const char *, size_t);
    441 size_t	 strlcat(char *, const char *, size_t);
    442 int	 strncasecmp(const char *, const char *, size_t);
    443 u_long	 strtoul(const char *, char **, int);
    444 long long strtoll(const char *, char **, int);
    445 unsigned long long strtoull(const char *, char **, int);
    446 intmax_t  strtoimax(const char *, char **, int);
    447 uintmax_t strtoumax(const char *, char **, int);
    448 intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
    449     intmax_t, int *);
    450 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
    451     uintmax_t, int *);
    452 void	 hexdump(void (*)(const char *, ...) __printflike(1, 2),
    453     const char *, const void *, size_t);
    454 
    455 int	 snprintb(char *, size_t, const char *, uint64_t);
    456 int	 snprintb_m(char *, size_t, const char *, uint64_t, size_t);
    457 int	 kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
    458 		   void *);
    459 uint32_t crc32(uint32_t, const uint8_t *, size_t);
    460 #if __GNUC_PREREQ__(4, 5) \
    461     && (defined(__alpha_cix__) || defined(__mips_popcount))
    462 #define	popcount	__builtin_popcount
    463 #define	popcountl	__builtin_popcountl
    464 #define	popcountll	__builtin_popcountll
    465 #define	popcount32	__builtin_popcount
    466 #define	popcount64	__builtin_popcountll
    467 #else
    468 unsigned int	popcount(unsigned int) __constfunc;
    469 unsigned int	popcountl(unsigned long) __constfunc;
    470 unsigned int	popcountll(unsigned long long) __constfunc;
    471 unsigned int	popcount32(uint32_t) __constfunc;
    472 unsigned int	popcount64(uint64_t) __constfunc;
    473 #endif
    474 
    475 void	*explicit_memset(void *, int, size_t);
    476 int	consttime_memequal(const void *, const void *, size_t);
    477 int	strnvisx(char *, size_t, const char *, size_t, int);
    478 #define VIS_OCTAL	0x01
    479 #define VIS_SAFE	0x20
    480 #define VIS_TRIM	0x40
    481 
    482 #ifdef notyet
    483 /*
    484  * LZF hashtable/state size: on uncompressible data and on a system with
    485  * a sufficiently large d-cache, a larger table produces a considerable
    486  * speed benefit.  On systems with small memory and caches, however...
    487  */
    488 #if defined(__vax__) || defined(__m68k__)
    489 #define LZF_HLOG 14
    490 #else
    491 #define LZF_HLOG 15
    492 #endif
    493 typedef const uint8_t *LZF_STATE[1 << LZF_HLOG];
    494 
    495 unsigned int lzf_compress_r (const void *const, unsigned int, void *,
    496 			     unsigned int, LZF_STATE);
    497 unsigned int lzf_decompress (const void *const, unsigned int, void *,
    498 			     unsigned int);
    499 #endif
    500 
    501 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
    502