Home | History | Annotate | Line # | Download | only in libkern
libkern.h revision 1.59
      1 /*	$NetBSD: libkern.h,v 1.59 2006/03/27 21:18:33 dyoung 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/null.h>
     39 
     40 #ifndef LIBKERN_INLINE
     41 #define LIBKERN_INLINE	static __inline
     42 #define LIBKERN_BODY
     43 #endif
     44 
     45 LIBKERN_INLINE int imax __P((int, int)) __attribute__ ((unused));
     46 LIBKERN_INLINE int imin __P((int, int)) __attribute__ ((unused));
     47 LIBKERN_INLINE u_int max __P((u_int, u_int)) __attribute__ ((unused));
     48 LIBKERN_INLINE u_int min __P((u_int, u_int)) __attribute__ ((unused));
     49 LIBKERN_INLINE long lmax __P((long, long)) __attribute__ ((unused));
     50 LIBKERN_INLINE long lmin __P((long, long)) __attribute__ ((unused));
     51 LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __attribute__ ((unused));
     52 LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __attribute__ ((unused));
     53 LIBKERN_INLINE int abs __P((int)) __attribute__ ((unused));
     54 
     55 LIBKERN_INLINE int isspace __P((int)) __attribute__((__unused__));
     56 LIBKERN_INLINE int isascii __P((int)) __attribute__((__unused__));
     57 LIBKERN_INLINE int isupper __P((int)) __attribute__((__unused__));
     58 LIBKERN_INLINE int islower __P((int)) __attribute__((__unused__));
     59 LIBKERN_INLINE int isalpha __P((int)) __attribute__((__unused__));
     60 LIBKERN_INLINE int isdigit __P((int)) __attribute__((__unused__));
     61 LIBKERN_INLINE int isxdigit __P((int)) __attribute__((__unused__));
     62 LIBKERN_INLINE int toupper __P((int)) __attribute__((__unused__));
     63 LIBKERN_INLINE int tolower __P((int)) __attribute__((__unused__));
     64 
     65 #ifdef LIBKERN_BODY
     66 LIBKERN_INLINE int
     67 imax(int a, int b)
     68 {
     69 	return (a > b ? a : b);
     70 }
     71 LIBKERN_INLINE int
     72 imin(int a, int b)
     73 {
     74 	return (a < b ? a : b);
     75 }
     76 LIBKERN_INLINE long
     77 lmax(long a, long b)
     78 {
     79 	return (a > b ? a : b);
     80 }
     81 LIBKERN_INLINE long
     82 lmin(long a, long b)
     83 {
     84 	return (a < b ? a : b);
     85 }
     86 LIBKERN_INLINE u_int
     87 max(u_int a, u_int b)
     88 {
     89 	return (a > b ? a : b);
     90 }
     91 LIBKERN_INLINE u_int
     92 min(u_int a, u_int b)
     93 {
     94 	return (a < b ? a : b);
     95 }
     96 LIBKERN_INLINE u_long
     97 ulmax(u_long a, u_long b)
     98 {
     99 	return (a > b ? a : b);
    100 }
    101 LIBKERN_INLINE u_long
    102 ulmin(u_long a, u_long b)
    103 {
    104 	return (a < b ? a : b);
    105 }
    106 
    107 LIBKERN_INLINE int
    108 abs(int j)
    109 {
    110 	return(j < 0 ? -j : j);
    111 }
    112 
    113 LIBKERN_INLINE int
    114 isspace(int ch)
    115 {
    116 	return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
    117 }
    118 
    119 LIBKERN_INLINE int
    120 isascii(int ch)
    121 {
    122 	return ((ch & ~0x7f) == 0);
    123 }
    124 
    125 LIBKERN_INLINE int
    126 isupper(int ch)
    127 {
    128 	return (ch >= 'A' && ch <= 'Z');
    129 }
    130 
    131 LIBKERN_INLINE int
    132 islower(int ch)
    133 {
    134 	return (ch >= 'a' && ch <= 'z');
    135 }
    136 
    137 LIBKERN_INLINE int
    138 isalpha(int ch)
    139 {
    140 	return (isupper(ch) || islower(ch));
    141 }
    142 
    143 LIBKERN_INLINE int
    144 isdigit(int ch)
    145 {
    146 	return (ch >= '0' && ch <= '9');
    147 }
    148 
    149 LIBKERN_INLINE int
    150 isxdigit(int ch)
    151 {
    152 	return (isdigit(ch) ||
    153 	    (ch >= 'A' && ch <= 'F') ||
    154 	    (ch >= 'a' && ch <= 'f'));
    155 }
    156 
    157 LIBKERN_INLINE int
    158 toupper(int ch)
    159 {
    160 	if (islower(ch))
    161 		return (ch - 0x20);
    162 	return (ch);
    163 }
    164 
    165 LIBKERN_INLINE int
    166 tolower(int ch)
    167 {
    168 	if (isupper(ch))
    169 		return (ch + 0x20);
    170 	return (ch);
    171 }
    172 #endif
    173 
    174 /*
    175  * Return the number of elements in a statically-allocated array,
    176  * __x.
    177  */
    178 #define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
    179 
    180 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */
    181 #define	__BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n)))
    182 
    183 /* __BITS(m, n): bits m through n, m < n. */
    184 #define	__BITS(__m, __n)	\
    185 	((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1))
    186 
    187 /* find least significant bit that is set */
    188 #define	__LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
    189 
    190 #define	SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask))
    191 #define	SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
    192 #define	SHIFTOUT_MASK(__mask) SHIFTOUT((__mask), (__mask))
    193 
    194 #ifdef NDEBUG						/* tradition! */
    195 #define	assert(e)	((void)0)
    196 #else
    197 #ifdef __STDC__
    198 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    199 			    __assert("", __FILE__, __LINE__, #e))
    200 #else
    201 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    202 			    __assert("", __FILE__, __LINE__, "e"))
    203 #endif
    204 #endif
    205 
    206 #ifndef DIAGNOSTIC
    207 #define _DIAGASSERT(a)	(void)0
    208 #ifdef lint
    209 #define	KASSERT(e)	/* NOTHING */
    210 #else /* !lint */
    211 #define	KASSERT(e)	((void)0)
    212 #endif /* !lint */
    213 #else /* DIAGNOSTIC */
    214 #define _DIAGASSERT(a)	assert(a)
    215 #ifdef __STDC__
    216 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    217 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
    218 #else
    219 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    220 			    __assert("diagnostic ", __FILE__, __LINE__, "e"))
    221 #endif
    222 #endif
    223 
    224 #ifndef DEBUG
    225 #ifdef lint
    226 #define	KDASSERT(e)	/* NOTHING */
    227 #else /* lint */
    228 #define	KDASSERT(e)	((void)0)
    229 #endif /* lint */
    230 #else
    231 #ifdef __STDC__
    232 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    233 			    __assert("debugging ", __FILE__, __LINE__, #e))
    234 #else
    235 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    236 			    __assert("debugging ", __FILE__, __LINE__, "e"))
    237 #endif
    238 #endif
    239 /*
    240  * XXX: For compatibility we use SMALL_RANDOM by default.
    241  */
    242 #define SMALL_RANDOM
    243 
    244 #ifndef offsetof
    245 #define	offsetof(type, member) \
    246     ((size_t)(unsigned long)(&(((type *)0)->member)))
    247 #endif
    248 
    249 /* Prototypes for non-quad routines. */
    250 /* XXX notyet #ifdef _STANDALONE */
    251 int	 bcmp __P((const void *, const void *, size_t));
    252 void	 bzero __P((void *, size_t));
    253 /* #endif */
    254 
    255 /* Prototypes for which GCC built-ins exist. */
    256 void	*memcpy __P((void *, const void *, size_t));
    257 int	 memcmp __P((const void *, const void *, size_t));
    258 void	*memset __P((void *, int, size_t));
    259 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
    260 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
    261 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
    262 #define	memset(d, v, l)		__builtin_memset(d, v, l)
    263 #endif
    264 
    265 char	*strcpy __P((char *, const char *));
    266 int	 strcmp __P((const char *, const char *));
    267 size_t	 strlen __P((const char *));
    268 char	*strsep(char **, const char *);
    269 #if __GNUC_PREREQ__(2, 95)
    270 #define	strcpy(d, s)		__builtin_strcpy(d, s)
    271 #define	strcmp(a, b)		__builtin_strcmp(a, b)
    272 #define	strlen(a)		__builtin_strlen(a)
    273 #endif
    274 
    275 /* Functions for which we always use built-ins. */
    276 #ifdef __GNUC__
    277 #define	alloca(s)		__builtin_alloca(s)
    278 #endif
    279 
    280 /* These exist in GCC 3.x, but we don't bother. */
    281 char	*strcat __P((char *, const char *));
    282 char	*strncpy __P((char *, const char *, size_t));
    283 int	 strncmp __P((const char *, const char *, size_t));
    284 char	*strchr __P((const char *, int));
    285 char	*strrchr __P((const char *, int));
    286 
    287 char	*strstr __P((const char *, const char *));
    288 
    289 /*
    290  * ffs is an instruction on vax.
    291  */
    292 int	 ffs __P((int));
    293 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
    294 #define	ffs(x)			__builtin_ffs(x)
    295 #endif
    296 
    297 void	 __assert __P((const char *, const char *, int, const char *))
    298 	    __attribute__((__noreturn__));
    299 unsigned int
    300 	bcdtobin __P((unsigned int));
    301 unsigned int
    302 	bintobcd __P((unsigned int));
    303 u_int32_t
    304 	inet_addr __P((const char *));
    305 struct in_addr;
    306 int	inet_aton __P((const char *, struct in_addr *));
    307 char	*intoa __P((u_int32_t));
    308 #define inet_ntoa(a) intoa((a).s_addr)
    309 void	*memchr __P((const void *, int, size_t));
    310 void	*memmove __P((void *, const void *, size_t));
    311 int	 pmatch __P((const char *, const char *, const char **));
    312 u_int32_t arc4random __P((void));
    313 void	 arc4randbytes __P((void *, size_t));
    314 #ifndef SMALL_RANDOM
    315 void	 srandom __P((unsigned long));
    316 char	*initstate __P((unsigned long, char *, size_t));
    317 char	*setstate __P((char *));
    318 #endif /* SMALL_RANDOM */
    319 long	 random __P((void));
    320 int	 scanc __P((u_int, const u_char *, const u_char *, int));
    321 int	 skpc __P((int, size_t, u_char *));
    322 int	 strcasecmp __P((const char *, const char *));
    323 size_t	 strlcpy __P((char *, const char *, size_t));
    324 size_t	 strlcat __P((char *, const char *, size_t));
    325 int	 strncasecmp __P((const char *, const char *, size_t));
    326 u_long	 strtoul __P((const char *, char **, int));
    327 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
    328