1 /* $NetBSD: libkern.h,v 1.62 2006/04/22 15:33:33 thorpej 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 __P((int, int)) __attribute__ ((unused)); 47 LIBKERN_INLINE int imin __P((int, int)) __attribute__ ((unused)); 48 LIBKERN_INLINE u_int max __P((u_int, u_int)) __attribute__ ((unused)); 49 LIBKERN_INLINE u_int min __P((u_int, u_int)) __attribute__ ((unused)); 50 LIBKERN_INLINE long lmax __P((long, long)) __attribute__ ((unused)); 51 LIBKERN_INLINE long lmin __P((long, long)) __attribute__ ((unused)); 52 LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __attribute__ ((unused)); 53 LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __attribute__ ((unused)); 54 LIBKERN_INLINE int abs __P((int)) __attribute__ ((unused)); 55 56 LIBKERN_INLINE int isspace __P((int)) __attribute__((__unused__)); 57 LIBKERN_INLINE int isascii __P((int)) __attribute__((__unused__)); 58 LIBKERN_INLINE int isupper __P((int)) __attribute__((__unused__)); 59 LIBKERN_INLINE int islower __P((int)) __attribute__((__unused__)); 60 LIBKERN_INLINE int isalpha __P((int)) __attribute__((__unused__)); 61 LIBKERN_INLINE int isdigit __P((int)) __attribute__((__unused__)); 62 LIBKERN_INLINE int isxdigit __P((int)) __attribute__((__unused__)); 63 LIBKERN_INLINE int toupper __P((int)) __attribute__((__unused__)); 64 LIBKERN_INLINE int tolower __P((int)) __attribute__((__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 /* 176 * Return the number of elements in a statically-allocated array, 177 * __x. 178 */ 179 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0])) 180 181 /* __BIT(n): nth bit, where __BIT(0) == 0x1. */ 182 #define __BIT(__n) (((__n) == 32) ? 0 : ((uint32_t)1 << (__n))) 183 184 /* __BITS(m, n): bits m through n, m < n. */ 185 #define __BITS(__m, __n) \ 186 ((__BIT(MAX((__m), (__n)) + 1) - 1) ^ (__BIT(MIN((__m), (__n))) - 1)) 187 188 /* find least significant bit that is set */ 189 #define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask)) 190 191 #define SHIFTOUT(__x, __mask) (((__x) & (__mask)) / __LOWEST_SET_BIT(__mask)) 192 #define SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask)) 193 #define SHIFTOUT_MASK(__mask) SHIFTOUT((__mask), (__mask)) 194 195 #ifdef NDEBUG /* tradition! */ 196 #define assert(e) ((void)0) 197 #else 198 #ifdef __STDC__ 199 #define assert(e) (__predict_true((e)) ? (void)0 : \ 200 __assert("", __FILE__, __LINE__, #e)) 201 #else 202 #define assert(e) (__predict_true((e)) ? (void)0 : \ 203 __assert("", __FILE__, __LINE__, "e")) 204 #endif 205 #endif 206 207 #ifdef __COVERITY__ 208 #ifndef DIAGNOSTIC 209 #define DIAGNOSTIC 210 #endif 211 #endif 212 213 #ifndef DIAGNOSTIC 214 #define _DIAGASSERT(a) (void)0 215 #ifdef lint 216 #define KASSERT(e) /* NOTHING */ 217 #else /* !lint */ 218 #define KASSERT(e) ((void)0) 219 #endif /* !lint */ 220 #else /* DIAGNOSTIC */ 221 #define _DIAGASSERT(a) assert(a) 222 #ifdef __STDC__ 223 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 224 __assert("diagnostic ", __FILE__, __LINE__, #e)) 225 #else 226 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 227 __assert("diagnostic ", __FILE__, __LINE__, "e")) 228 #endif 229 #endif 230 231 #ifndef DEBUG 232 #ifdef lint 233 #define KDASSERT(e) /* NOTHING */ 234 #else /* lint */ 235 #define KDASSERT(e) ((void)0) 236 #endif /* lint */ 237 #else 238 #ifdef __STDC__ 239 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 240 __assert("debugging ", __FILE__, __LINE__, #e)) 241 #else 242 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 243 __assert("debugging ", __FILE__, __LINE__, "e")) 244 #endif 245 #endif 246 /* 247 * XXX: For compatibility we use SMALL_RANDOM by default. 248 */ 249 #define SMALL_RANDOM 250 251 #ifndef offsetof 252 #define offsetof(type, member) \ 253 ((size_t)(unsigned long)(&(((type *)0)->member))) 254 #endif 255 256 /* Prototypes for non-quad routines. */ 257 /* XXX notyet #ifdef _STANDALONE */ 258 int bcmp __P((const void *, const void *, size_t)); 259 void bzero __P((void *, size_t)); 260 /* #endif */ 261 262 /* Prototypes for which GCC built-ins exist. */ 263 void *memcpy __P((void *, const void *, size_t)); 264 int memcmp __P((const void *, const void *, size_t)); 265 void *memset __P((void *, int, size_t)); 266 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) 267 #define memcpy(d, s, l) __builtin_memcpy(d, s, l) 268 #define memcmp(a, b, l) __builtin_memcmp(a, b, l) 269 #define memset(d, v, l) __builtin_memset(d, v, l) 270 #endif 271 272 char *strcpy __P((char *, const char *)); 273 int strcmp __P((const char *, const char *)); 274 size_t strlen __P((const char *)); 275 char *strsep(char **, const char *); 276 #if __GNUC_PREREQ__(2, 95) 277 #define strcpy(d, s) __builtin_strcpy(d, s) 278 #define strcmp(a, b) __builtin_strcmp(a, b) 279 #define strlen(a) __builtin_strlen(a) 280 #endif 281 282 /* Functions for which we always use built-ins. */ 283 #ifdef __GNUC__ 284 #define alloca(s) __builtin_alloca(s) 285 #endif 286 287 /* These exist in GCC 3.x, but we don't bother. */ 288 char *strcat __P((char *, const char *)); 289 char *strncpy __P((char *, const char *, size_t)); 290 int strncmp __P((const char *, const char *, size_t)); 291 char *strchr __P((const char *, int)); 292 char *strrchr __P((const char *, int)); 293 294 char *strstr __P((const char *, const char *)); 295 296 /* 297 * ffs is an instruction on vax. 298 */ 299 int ffs __P((int)); 300 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) 301 #define ffs(x) __builtin_ffs(x) 302 #endif 303 304 void __assert __P((const char *, const char *, int, const char *)) 305 __attribute__((__noreturn__)); 306 unsigned int 307 bcdtobin __P((unsigned int)); 308 unsigned int 309 bintobcd __P((unsigned int)); 310 u_int32_t 311 inet_addr __P((const char *)); 312 struct in_addr; 313 int inet_aton __P((const char *, struct in_addr *)); 314 char *intoa __P((u_int32_t)); 315 #define inet_ntoa(a) intoa((a).s_addr) 316 void *memchr __P((const void *, int, size_t)); 317 void *memmove __P((void *, const void *, size_t)); 318 int pmatch __P((const char *, const char *, const char **)); 319 u_int32_t arc4random __P((void)); 320 void arc4randbytes __P((void *, size_t)); 321 #ifndef SMALL_RANDOM 322 void srandom __P((unsigned long)); 323 char *initstate __P((unsigned long, char *, size_t)); 324 char *setstate __P((char *)); 325 #endif /* SMALL_RANDOM */ 326 long random __P((void)); 327 int scanc __P((u_int, const u_char *, const u_char *, int)); 328 int skpc __P((int, size_t, u_char *)); 329 int strcasecmp __P((const char *, const char *)); 330 size_t strlcpy __P((char *, const char *, size_t)); 331 size_t strlcat __P((char *, const char *, size_t)); 332 int strncasecmp __P((const char *, const char *, size_t)); 333 u_long strtoul __P((const char *, char **, int)); 334 uintmax_t strtoumax __P((const char *, char **, int)); 335 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ 336