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