libkern.h revision 1.101 1 /* $NetBSD: libkern.h,v 1.101 2011/09/27 01:02:39 jym 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 #include <sys/systm.h>
41
42 #ifndef LIBKERN_INLINE
43 #define LIBKERN_INLINE static __inline
44 #define LIBKERN_BODY
45 #endif
46
47 LIBKERN_INLINE int imax(int, int) __unused;
48 LIBKERN_INLINE int imin(int, int) __unused;
49 LIBKERN_INLINE u_int max(u_int, u_int) __unused;
50 LIBKERN_INLINE u_int min(u_int, u_int) __unused;
51 LIBKERN_INLINE long lmax(long, long) __unused;
52 LIBKERN_INLINE long lmin(long, long) __unused;
53 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
54 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
55 LIBKERN_INLINE int abs(int) __unused;
56
57 LIBKERN_INLINE int isspace(int) __unused;
58 LIBKERN_INLINE int isascii(int) __unused;
59 LIBKERN_INLINE int isupper(int) __unused;
60 LIBKERN_INLINE int islower(int) __unused;
61 LIBKERN_INLINE int isalpha(int) __unused;
62 LIBKERN_INLINE int isdigit(int) __unused;
63 LIBKERN_INLINE int isxdigit(int) __unused;
64 LIBKERN_INLINE int toupper(int) __unused;
65 LIBKERN_INLINE int tolower(int) __unused;
66
67 #ifdef LIBKERN_BODY
68 LIBKERN_INLINE int
69 imax(int a, int b)
70 {
71 return (a > b ? a : b);
72 }
73 LIBKERN_INLINE int
74 imin(int a, int b)
75 {
76 return (a < b ? a : b);
77 }
78 LIBKERN_INLINE long
79 lmax(long a, long b)
80 {
81 return (a > b ? a : b);
82 }
83 LIBKERN_INLINE long
84 lmin(long a, long b)
85 {
86 return (a < b ? a : b);
87 }
88 LIBKERN_INLINE u_int
89 max(u_int a, u_int b)
90 {
91 return (a > b ? a : b);
92 }
93 LIBKERN_INLINE u_int
94 min(u_int a, u_int b)
95 {
96 return (a < b ? a : b);
97 }
98 LIBKERN_INLINE u_long
99 ulmax(u_long a, u_long b)
100 {
101 return (a > b ? a : b);
102 }
103 LIBKERN_INLINE u_long
104 ulmin(u_long a, u_long b)
105 {
106 return (a < b ? a : b);
107 }
108
109 LIBKERN_INLINE int
110 abs(int j)
111 {
112 return(j < 0 ? -j : j);
113 }
114
115 LIBKERN_INLINE int
116 isspace(int ch)
117 {
118 return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
119 }
120
121 LIBKERN_INLINE int
122 isascii(int ch)
123 {
124 return ((ch & ~0x7f) == 0);
125 }
126
127 LIBKERN_INLINE int
128 isupper(int ch)
129 {
130 return (ch >= 'A' && ch <= 'Z');
131 }
132
133 LIBKERN_INLINE int
134 islower(int ch)
135 {
136 return (ch >= 'a' && ch <= 'z');
137 }
138
139 LIBKERN_INLINE int
140 isalpha(int ch)
141 {
142 return (isupper(ch) || islower(ch));
143 }
144
145 LIBKERN_INLINE int
146 isdigit(int ch)
147 {
148 return (ch >= '0' && ch <= '9');
149 }
150
151 LIBKERN_INLINE int
152 isxdigit(int ch)
153 {
154 return (isdigit(ch) ||
155 (ch >= 'A' && ch <= 'F') ||
156 (ch >= 'a' && ch <= 'f'));
157 }
158
159 LIBKERN_INLINE int
160 toupper(int ch)
161 {
162 if (islower(ch))
163 return (ch - 0x20);
164 return (ch);
165 }
166
167 LIBKERN_INLINE int
168 tolower(int ch)
169 {
170 if (isupper(ch))
171 return (ch + 0x20);
172 return (ch);
173 }
174 #endif
175
176 #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
177
178 #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
179
180 #ifdef NDEBUG /* tradition! */
181 #define assert(e) ((void)0)
182 #else
183 #define assert(e) (__predict_true((e)) ? (void)0 : \
184 panic(__KASSERTSTR, "", #e, __FILE__, __LINE__))
185 #endif
186
187 #ifdef __COVERITY__
188 #ifndef DIAGNOSTIC
189 #define DIAGNOSTIC
190 #endif
191 #endif
192
193 #define CTASSERT(x) __CTASSERT(x)
194
195 #ifndef DIAGNOSTIC
196 #define _DIAGASSERT(a) (void)0
197 #ifdef lint
198 #define KASSERTMSG(e, msg, ...) /* NOTHING */
199 #define KASSERT(e) /* NOTHING */
200 #else /* !lint */
201 #define KASSERTMSG(e, msg, ...) ((void)0)
202 #define KASSERT(e) ((void)0)
203 #endif /* !lint */
204 #else /* DIAGNOSTIC */
205 #define _DIAGASSERT(a) assert(a)
206 #define KASSERTMSG(e, msg, ...) \
207 (__predict_true((e)) ? (void)0 : \
208 panic(__KASSERTSTR msg, "diagnostic ", #e, \
209 __FILE__, __LINE__, ## __VA_ARGS__))
210
211 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
212 panic(__KASSERTSTR, "diagnostic ", #e, \
213 __FILE__, __LINE__))
214 #endif
215
216 #ifndef DEBUG
217 #ifdef lint
218 #define KDASSERTMSG(e,msg, ...) /* NOTHING */
219 #define KDASSERT(e) /* NOTHING */
220 #else /* lint */
221 #define KDASSERTMSG(e,msg, ...) ((void)0)
222 #define KDASSERT(e) ((void)0)
223 #endif /* lint */
224 #else
225 #define KDASSERTMSG(e, msg, ...) \
226 (__predict_true((e)) ? (void)0 : \
227 panic(__KASSERTSTR msg, "debugging ", #e, \
228 __FILE__, __LINE__, ## __VA_ARGS__))
229
230 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
231 panic(__KASSERTSTR, "debugging ", #e, \
232 __FILE__, __LINE__))
233 #endif
234
235 /*
236 * XXX: For compatibility we use SMALL_RANDOM by default.
237 */
238 #define SMALL_RANDOM
239
240 #ifndef offsetof
241 #if __GNUC_PREREQ__(4, 0)
242 #define offsetof(type, member) __builtin_offsetof(type, member)
243 #else
244 #define offsetof(type, member) \
245 ((size_t)(unsigned long)(&(((type *)0)->member)))
246 #endif
247 #endif
248
249 #define MTPRNG_RLEN 624
250 struct mtprng_state {
251 unsigned int mt_idx;
252 uint32_t mt_elem[MTPRNG_RLEN];
253 uint32_t mt_count;
254 uint32_t mt_sparse[3];
255 };
256
257 /* Prototypes for which GCC built-ins exist. */
258 void *memcpy(void *, const void *, size_t);
259 int memcmp(const void *, const void *, size_t);
260 void *memset(void *, int, size_t);
261 #if __GNUC_PREREQ__(2, 95) && (__GNUC_PREREQ__(4, 0) || !defined(__vax__)) && \
262 !defined(_STANDALONE)
263 #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
264 #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
265 #endif
266 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__) && !defined(_STANDALONE)
267 #define memset(d, v, l) __builtin_memset(d, v, l)
268 #endif
269
270 char *strcpy(char *, const char *);
271 int strcmp(const char *, const char *);
272 size_t strlen(const char *);
273 size_t strnlen(const char *, size_t);
274 char *strsep(char **, const char *);
275 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
276 #define strcpy(d, s) __builtin_strcpy(d, s)
277 #define strcmp(a, b) __builtin_strcmp(a, b)
278 #define strlen(a) __builtin_strlen(a)
279 #endif
280
281 /* Functions for which we always use built-ins. */
282 #ifdef __GNUC__
283 #define alloca(s) __builtin_alloca(s)
284 #endif
285
286 /* These exist in GCC 3.x, but we don't bother. */
287 char *strcat(char *, const char *);
288 char *strncpy(char *, const char *, size_t);
289 int strncmp(const char *, const char *, size_t);
290 char *strchr(const char *, int);
291 char *strrchr(const char *, int);
292
293 char *strstr(const char *, const char *);
294
295 /*
296 * ffs is an instruction on vax.
297 */
298 int ffs(int);
299 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
300 #define ffs(x) __builtin_ffs(x)
301 #endif
302
303 void kern_assert(const char *, const char *, int, const char *);
304 unsigned int
305 bcdtobin(unsigned int);
306 unsigned int
307 bintobcd(unsigned int);
308 u_int32_t
309 inet_addr(const char *);
310 struct in_addr;
311 int inet_aton(const char *, struct in_addr *);
312 char *intoa(u_int32_t);
313 #define inet_ntoa(a) intoa((a).s_addr)
314 void *memchr(const void *, int, size_t);
315 void *memmove(void *, const void *, size_t);
316 int pmatch(const char *, const char *, const char **);
317 u_int32_t arc4random(void);
318 void arc4randbytes(void *, size_t);
319 #ifndef SMALL_RANDOM
320 void srandom(unsigned long);
321 char *initstate(unsigned long, char *, size_t);
322 char *setstate(char *);
323 #endif /* SMALL_RANDOM */
324 long random(void);
325 void mtprng_init32(struct mtprng_state *, uint32_t);
326 void mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
327 uint32_t mtprng_rawrandom(struct mtprng_state *);
328 uint32_t mtprng_random(struct mtprng_state *);
329 int scanc(u_int, const u_char *, const u_char *, int);
330 int skpc(int, size_t, u_char *);
331 int strcasecmp(const char *, const char *);
332 size_t strlcpy(char *, const char *, size_t);
333 size_t strlcat(char *, const char *, size_t);
334 int strncasecmp(const char *, const char *, size_t);
335 u_long strtoul(const char *, char **, int);
336 long long strtoll(const char *, char **, int);
337 unsigned long long strtoull(const char *, char **, int);
338 uintmax_t strtoumax(const char *, char **, int);
339 int snprintb(char *, size_t, const char *, uint64_t);
340 int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
341 int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
342 void *);
343 uint32_t crc32(uint32_t, const uint8_t *, size_t);
344 unsigned int popcount(unsigned int) __constfunc;
345 unsigned int popcountl(unsigned long) __constfunc;
346 unsigned int popcountll(unsigned long long) __constfunc;
347 unsigned int popcount32(uint32_t) __constfunc;
348 unsigned int popcount64(uint64_t) __constfunc;
349 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
350