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