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