1 /* $NetBSD: libkern.h,v 1.47 2002/10/24 20:53:50 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. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)libkern.h 8.2 (Berkeley) 8/5/94 36 */ 37 38 #ifndef _LIB_LIBKERN_LIBKERN_H_ 39 #define _LIB_LIBKERN_LIBKERN_H_ 40 41 #include <sys/types.h> 42 43 #ifndef LIBKERN_INLINE 44 #define LIBKERN_INLINE static __inline 45 #define LIBKERN_BODY 46 #endif 47 48 LIBKERN_INLINE int imax __P((int, int)) __attribute__ ((unused)); 49 LIBKERN_INLINE int imin __P((int, int)) __attribute__ ((unused)); 50 LIBKERN_INLINE u_int max __P((u_int, u_int)) __attribute__ ((unused)); 51 LIBKERN_INLINE u_int min __P((u_int, u_int)) __attribute__ ((unused)); 52 LIBKERN_INLINE long lmax __P((long, long)) __attribute__ ((unused)); 53 LIBKERN_INLINE long lmin __P((long, long)) __attribute__ ((unused)); 54 LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __attribute__ ((unused)); 55 LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __attribute__ ((unused)); 56 LIBKERN_INLINE int abs __P((int)) __attribute__ ((unused)); 57 58 LIBKERN_INLINE int isspace __P((int)) __attribute__((__unused__)); 59 LIBKERN_INLINE int isascii __P((int)) __attribute__((__unused__)); 60 LIBKERN_INLINE int isupper __P((int)) __attribute__((__unused__)); 61 LIBKERN_INLINE int islower __P((int)) __attribute__((__unused__)); 62 LIBKERN_INLINE int isalpha __P((int)) __attribute__((__unused__)); 63 LIBKERN_INLINE int isdigit __P((int)) __attribute__((__unused__)); 64 LIBKERN_INLINE int isxdigit __P((int)) __attribute__((__unused__)); 65 LIBKERN_INLINE int toupper __P((int)) __attribute__((__unused__)); 66 LIBKERN_INLINE int tolower __P((int)) __attribute__((__unused__)); 67 68 #ifdef LIBKERN_BODY 69 LIBKERN_INLINE int 70 imax(int a, int b) 71 { 72 return (a > b ? a : b); 73 } 74 LIBKERN_INLINE int 75 imin(int a, int b) 76 { 77 return (a < b ? a : b); 78 } 79 LIBKERN_INLINE long 80 lmax(long a, long b) 81 { 82 return (a > b ? a : b); 83 } 84 LIBKERN_INLINE long 85 lmin(long a, long b) 86 { 87 return (a < b ? a : b); 88 } 89 LIBKERN_INLINE u_int 90 max(u_int a, u_int b) 91 { 92 return (a > b ? a : b); 93 } 94 LIBKERN_INLINE u_int 95 min(u_int a, u_int b) 96 { 97 return (a < b ? a : b); 98 } 99 LIBKERN_INLINE u_long 100 ulmax(u_long a, u_long b) 101 { 102 return (a > b ? a : b); 103 } 104 LIBKERN_INLINE u_long 105 ulmin(u_long a, u_long b) 106 { 107 return (a < b ? a : b); 108 } 109 110 LIBKERN_INLINE int 111 abs(int j) 112 { 113 return(j < 0 ? -j : j); 114 } 115 116 LIBKERN_INLINE int 117 isspace(int ch) 118 { 119 return (ch == ' ' || (ch >= '\t' && ch <= '\r')); 120 } 121 122 LIBKERN_INLINE int 123 isascii(int ch) 124 { 125 return ((ch & ~0x7f) == 0); 126 } 127 128 LIBKERN_INLINE int 129 isupper(int ch) 130 { 131 return (ch >= 'A' && ch <= 'Z'); 132 } 133 134 LIBKERN_INLINE int 135 islower(int ch) 136 { 137 return (ch >= 'a' && ch <= 'z'); 138 } 139 140 LIBKERN_INLINE int 141 isalpha(int ch) 142 { 143 return (isupper(ch) || islower(ch)); 144 } 145 146 LIBKERN_INLINE int 147 isdigit(int ch) 148 { 149 return (ch >= '0' && ch <= '9'); 150 } 151 152 LIBKERN_INLINE int 153 isxdigit(int ch) 154 { 155 return (isdigit(ch) || 156 (ch >= 'A' && ch <= 'F') || 157 (ch >= 'a' && ch <= 'f')); 158 } 159 160 LIBKERN_INLINE int 161 toupper(int ch) 162 { 163 if (islower(ch)) 164 return (ch - 0x20); 165 return (ch); 166 } 167 168 LIBKERN_INLINE int 169 tolower(int ch) 170 { 171 if (isupper(ch)) 172 return (ch + 0x20); 173 return (ch); 174 } 175 #endif 176 177 #ifdef NDEBUG /* tradition! */ 178 #define assert(e) ((void)0) 179 #else 180 #ifdef __STDC__ 181 #define assert(e) (__predict_true((e)) ? (void)0 : \ 182 __assert("", __FILE__, __LINE__, #e)) 183 #else 184 #define assert(e) (__predict_true((e)) ? (void)0 : \ 185 __assert("", __FILE__, __LINE__, "e")) 186 #endif 187 #endif 188 189 #ifndef DIAGNOSTIC 190 #ifdef lint 191 #define KASSERT(e) /* NOTHING */ 192 #else /* !lint */ 193 #define KASSERT(e) ((void)0) 194 #endif /* !lint */ 195 #else 196 #ifdef __STDC__ 197 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 198 __assert("diagnostic ", __FILE__, __LINE__, #e)) 199 #else 200 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \ 201 __assert("diagnostic ", __FILE__, __LINE__, "e")) 202 #endif 203 #endif 204 205 #ifndef DEBUG 206 #ifdef lint 207 #define KDASSERT(e) /* NOTHING */ 208 #else /* lint */ 209 #define KDASSERT(e) ((void)0) 210 #endif /* lint */ 211 #else 212 #ifdef __STDC__ 213 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 214 __assert("debugging ", __FILE__, __LINE__, #e)) 215 #else 216 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \ 217 __assert("debugging ", __FILE__, __LINE__, "e")) 218 #endif 219 #endif 220 221 #ifndef offsetof 222 #define offsetof(type, member) \ 223 ((size_t)(unsigned long)(&(((type *)0)->member))) 224 #endif 225 226 /* Prototypes for non-quad routines. */ 227 /* XXX notyet #ifdef _STANDALONE */ 228 int bcmp __P((const void *, const void *, size_t)); 229 void bzero __P((void *, size_t)); 230 /* #endif */ 231 232 /* Prototypes for which GCC built-ins exist. */ 233 void *memcpy __P((void *, const void *, size_t)); 234 int memcmp __P((const void *, const void *, size_t)); 235 void *memset __P((void *, int, size_t)); 236 #if __GNUC_PREREQ__(2, 95) 237 #define memcpy(d, s, l) __builtin_memcpy(d, s, l) 238 #define memcmp(a, b, l) __builtin_memcmp(a, b, l) 239 #define memset(d, v, l) __builtin_memset(d, v, l) 240 #endif 241 242 char *strcpy __P((char *, const char *)); 243 int strcmp __P((const char *, const char *)); 244 size_t strlen __P((const char *)); 245 #if __GNUC_PREREQ__(2, 95) 246 #define strcpy(d, s) __builtin_strcpy(d, s) 247 #define strcmp(a, b) __builtin_strcmp(a, b) 248 #define strlen(a) __builtin_strlen(a) 249 #endif 250 251 /* Functions for which we always use built-ins. */ 252 #ifdef __GNUC__ 253 #define alloca(s) __builtin_alloca(s) 254 #endif 255 256 /* These exist in GCC 3.x, but we don't bother. */ 257 char *strcat __P((char *, const char *)); 258 char *strncpy __P((char *, const char *, size_t)); 259 int strncmp __P((const char *, const char *, size_t)); 260 char *strchr __P((const char *, int)); 261 char *strrchr __P((const char *, int)); 262 263 char *strstr __P((const char *, const char *)); 264 265 /* 266 * ffs is an instruction on vax. 267 */ 268 int ffs __P((int)); 269 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) 270 #define ffs(x) __builtin_ffs(x) 271 #endif 272 273 void __assert __P((const char *, const char *, int, const char *)) 274 __attribute__((__noreturn__)); 275 u_int32_t 276 inet_addr __P((const char *)); 277 char *intoa __P((u_int32_t)); 278 #define inet_ntoa(a) intoa((a).s_addr) 279 void *memchr __P((const void *, int, size_t)); 280 void *memmove __P((void *, const void *, size_t)); 281 int pmatch __P((const char *, const char *, const char **)); 282 u_int32_t arc4random __P((void)); 283 void arc4randbytes __P((void *, size_t)); 284 u_long random __P((void)); 285 int scanc __P((u_int, const u_char *, const u_char *, int)); 286 int skpc __P((int, size_t, u_char *)); 287 int strcasecmp __P((const char *, const char *)); 288 int strncasecmp __P((const char *, const char *, size_t)); 289 u_long strtoul __P((const char *, char **, int)); 290 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */ 291