Home | History | Annotate | Line # | Download | only in libkern
libkern.h revision 1.52
      1 /*	$NetBSD: libkern.h,v 1.52 2005/12/20 19:35:12 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 #ifndef offsetof
    221 #define	offsetof(type, member) \
    222     ((size_t)(unsigned long)(&(((type *)0)->member)))
    223 #endif
    224 
    225 /* Prototypes for non-quad routines. */
    226 /* XXX notyet #ifdef _STANDALONE */
    227 int	 bcmp __P((const void *, const void *, size_t));
    228 void	 bzero __P((void *, size_t));
    229 /* #endif */
    230 
    231 /* Prototypes for which GCC built-ins exist. */
    232 void	*memcpy __P((void *, const void *, size_t));
    233 int	 memcmp __P((const void *, const void *, size_t));
    234 void	*memset __P((void *, int, size_t));
    235 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
    236 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
    237 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
    238 #define	memset(d, v, l)		__builtin_memset(d, v, l)
    239 #endif
    240 
    241 char	*strcpy __P((char *, const char *));
    242 int	 strcmp __P((const char *, const char *));
    243 size_t	 strlen __P((const char *));
    244 #if __GNUC_PREREQ__(2, 95)
    245 #define	strcpy(d, s)		__builtin_strcpy(d, s)
    246 #define	strcmp(a, b)		__builtin_strcmp(a, b)
    247 #define	strlen(a)		__builtin_strlen(a)
    248 #endif
    249 
    250 /* Functions for which we always use built-ins. */
    251 #ifdef __GNUC__
    252 #define	alloca(s)		__builtin_alloca(s)
    253 #endif
    254 
    255 /* These exist in GCC 3.x, but we don't bother. */
    256 char	*strcat __P((char *, const char *));
    257 char	*strncpy __P((char *, const char *, size_t));
    258 int	 strncmp __P((const char *, const char *, size_t));
    259 char	*strchr __P((const char *, int));
    260 char	*strrchr __P((const char *, int));
    261 
    262 char	*strstr __P((const char *, const char *));
    263 
    264 /*
    265  * ffs is an instruction on vax.
    266  */
    267 int	 ffs __P((int));
    268 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
    269 #define	ffs(x)			__builtin_ffs(x)
    270 #endif
    271 
    272 void	 __assert __P((const char *, const char *, int, const char *))
    273 	    __attribute__((__noreturn__));
    274 u_int32_t
    275 	inet_addr __P((const char *));
    276 struct in_addr;
    277 int	inet_aton __P((const char *, struct in_addr *));
    278 char	*intoa __P((u_int32_t));
    279 #define inet_ntoa(a) intoa((a).s_addr)
    280 void	*memchr __P((const void *, int, size_t));
    281 void	*memmove __P((void *, const void *, size_t));
    282 int	 pmatch __P((const char *, const char *, const char **));
    283 u_int32_t arc4random __P((void));
    284 void	 arc4randbytes __P((void *, size_t));
    285 u_long	 random __P((void));
    286 int	 scanc __P((u_int, const u_char *, const u_char *, int));
    287 int	 skpc __P((int, size_t, u_char *));
    288 int	 strcasecmp __P((const char *, const char *));
    289 size_t	 strlcpy __P((char *, const char *, size_t));
    290 size_t	 strlcat __P((char *, const char *, size_t));
    291 int	 strncasecmp __P((const char *, const char *, size_t));
    292 u_long	 strtoul __P((const char *, char **, int));
    293 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
    294