Home | History | Annotate | Line # | Download | only in libkern
libkern.h revision 1.53
      1 /*	$NetBSD: libkern.h,v 1.53 2005/12/21 14:24:44 christos 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 #ifdef NDEBUG						/* tradition! */
    175 #define	assert(e)	((void)0)
    176 #else
    177 #ifdef __STDC__
    178 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    179 			    __assert("", __FILE__, __LINE__, #e))
    180 #else
    181 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
    182 			    __assert("", __FILE__, __LINE__, "e"))
    183 #endif
    184 #endif
    185 
    186 #ifndef DIAGNOSTIC
    187 #define _DIAGASSERT(a)	(void)0
    188 #ifdef lint
    189 #define	KASSERT(e)	/* NOTHING */
    190 #else /* !lint */
    191 #define	KASSERT(e)	((void)0)
    192 #endif /* !lint */
    193 #else /* DIAGNOSTIC */
    194 #define _DIAGASSERT(a)	assert(a)
    195 #ifdef __STDC__
    196 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    197 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
    198 #else
    199 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    200 			    __assert("diagnostic ", __FILE__, __LINE__, "e"))
    201 #endif
    202 #endif
    203 
    204 #ifndef DEBUG
    205 #ifdef lint
    206 #define	KDASSERT(e)	/* NOTHING */
    207 #else /* lint */
    208 #define	KDASSERT(e)	((void)0)
    209 #endif /* lint */
    210 #else
    211 #ifdef __STDC__
    212 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    213 			    __assert("debugging ", __FILE__, __LINE__, #e))
    214 #else
    215 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
    216 			    __assert("debugging ", __FILE__, __LINE__, "e"))
    217 #endif
    218 #endif
    219 /*
    220  * XXX: For compatibility we use SMALL_RANDOM by default.
    221  */
    222 #define SMALL_RANDOM
    223 
    224 #ifndef offsetof
    225 #define	offsetof(type, member) \
    226     ((size_t)(unsigned long)(&(((type *)0)->member)))
    227 #endif
    228 
    229 /* Prototypes for non-quad routines. */
    230 /* XXX notyet #ifdef _STANDALONE */
    231 int	 bcmp __P((const void *, const void *, size_t));
    232 void	 bzero __P((void *, size_t));
    233 /* #endif */
    234 
    235 /* Prototypes for which GCC built-ins exist. */
    236 void	*memcpy __P((void *, const void *, size_t));
    237 int	 memcmp __P((const void *, const void *, size_t));
    238 void	*memset __P((void *, int, size_t));
    239 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
    240 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
    241 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
    242 #define	memset(d, v, l)		__builtin_memset(d, v, l)
    243 #endif
    244 
    245 char	*strcpy __P((char *, const char *));
    246 int	 strcmp __P((const char *, const char *));
    247 size_t	 strlen __P((const char *));
    248 #if __GNUC_PREREQ__(2, 95)
    249 #define	strcpy(d, s)		__builtin_strcpy(d, s)
    250 #define	strcmp(a, b)		__builtin_strcmp(a, b)
    251 #define	strlen(a)		__builtin_strlen(a)
    252 #endif
    253 
    254 /* Functions for which we always use built-ins. */
    255 #ifdef __GNUC__
    256 #define	alloca(s)		__builtin_alloca(s)
    257 #endif
    258 
    259 /* These exist in GCC 3.x, but we don't bother. */
    260 char	*strcat __P((char *, const char *));
    261 char	*strncpy __P((char *, const char *, size_t));
    262 int	 strncmp __P((const char *, const char *, size_t));
    263 char	*strchr __P((const char *, int));
    264 char	*strrchr __P((const char *, int));
    265 
    266 char	*strstr __P((const char *, const char *));
    267 
    268 /*
    269  * ffs is an instruction on vax.
    270  */
    271 int	 ffs __P((int));
    272 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
    273 #define	ffs(x)			__builtin_ffs(x)
    274 #endif
    275 
    276 void	 __assert __P((const char *, const char *, int, const char *))
    277 	    __attribute__((__noreturn__));
    278 u_int32_t
    279 	inet_addr __P((const char *));
    280 struct in_addr;
    281 int	inet_aton __P((const char *, struct in_addr *));
    282 char	*intoa __P((u_int32_t));
    283 #define inet_ntoa(a) intoa((a).s_addr)
    284 void	*memchr __P((const void *, int, size_t));
    285 void	*memmove __P((void *, const void *, size_t));
    286 int	 pmatch __P((const char *, const char *, const char **));
    287 u_int32_t arc4random __P((void));
    288 void	 arc4randbytes __P((void *, size_t));
    289 #ifndef SMALL_RANDOM
    290 void	 srandom __P((unsigned long));
    291 char	*initstate __P((unsigned long, char *, size_t));
    292 char	*setstate __P((char *));
    293 #endif /* SMALL_RANDOM */
    294 long	 random __P((void));
    295 int	 scanc __P((u_int, const u_char *, const u_char *, int));
    296 int	 skpc __P((int, size_t, u_char *));
    297 int	 strcasecmp __P((const char *, const char *));
    298 size_t	 strlcpy __P((char *, const char *, size_t));
    299 size_t	 strlcat __P((char *, const char *, size_t));
    300 int	 strncasecmp __P((const char *, const char *, size_t));
    301 u_long	 strtoul __P((const char *, char **, int));
    302 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
    303