libkern.h revision 1.116.2.2 1 /* $NetBSD: libkern.h,v 1.116.2.2 2015/06/06 14:40:22 skrll 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
41 #ifndef LIBKERN_INLINE
42 #define LIBKERN_INLINE static __inline
43 #define LIBKERN_BODY
44 #endif
45
46 LIBKERN_INLINE int imax(int, int) __unused;
47 LIBKERN_INLINE int imin(int, int) __unused;
48 LIBKERN_INLINE u_int max(u_int, u_int) __unused;
49 LIBKERN_INLINE u_int min(u_int, u_int) __unused;
50 LIBKERN_INLINE long lmax(long, long) __unused;
51 LIBKERN_INLINE long lmin(long, long) __unused;
52 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
53 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
54 LIBKERN_INLINE int abs(int) __unused;
55 LIBKERN_INLINE long labs(long) __unused;
56 LIBKERN_INLINE long long llabs(long long) __unused;
57 LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
58
59 LIBKERN_INLINE int isspace(int) __unused;
60 LIBKERN_INLINE int isascii(int) __unused;
61 LIBKERN_INLINE int isupper(int) __unused;
62 LIBKERN_INLINE int islower(int) __unused;
63 LIBKERN_INLINE int isalpha(int) __unused;
64 LIBKERN_INLINE int isalnum(int) __unused;
65 LIBKERN_INLINE int isdigit(int) __unused;
66 LIBKERN_INLINE int isxdigit(int) __unused;
67 LIBKERN_INLINE int iscntrl(int) __unused;
68 LIBKERN_INLINE int isgraph(int) __unused;
69 LIBKERN_INLINE int isprint(int) __unused;
70 LIBKERN_INLINE int ispunct(int) __unused;
71 LIBKERN_INLINE int toupper(int) __unused;
72 LIBKERN_INLINE int tolower(int) __unused;
73
74 #ifdef LIBKERN_BODY
75 LIBKERN_INLINE int
76 imax(int a, int b)
77 {
78 return (a > b ? a : b);
79 }
80 LIBKERN_INLINE int
81 imin(int a, int b)
82 {
83 return (a < b ? a : b);
84 }
85 LIBKERN_INLINE long
86 lmax(long a, long b)
87 {
88 return (a > b ? a : b);
89 }
90 LIBKERN_INLINE long
91 lmin(long a, long b)
92 {
93 return (a < b ? a : b);
94 }
95 LIBKERN_INLINE u_int
96 max(u_int a, u_int b)
97 {
98 return (a > b ? a : b);
99 }
100 LIBKERN_INLINE u_int
101 min(u_int a, u_int b)
102 {
103 return (a < b ? a : b);
104 }
105 LIBKERN_INLINE u_long
106 ulmax(u_long a, u_long b)
107 {
108 return (a > b ? a : b);
109 }
110 LIBKERN_INLINE u_long
111 ulmin(u_long a, u_long b)
112 {
113 return (a < b ? a : b);
114 }
115
116 LIBKERN_INLINE int
117 abs(int j)
118 {
119 return(j < 0 ? -j : j);
120 }
121
122 LIBKERN_INLINE long
123 labs(long j)
124 {
125 return(j < 0 ? -j : j);
126 }
127
128 LIBKERN_INLINE long long
129 llabs(long long j)
130 {
131 return(j < 0 ? -j : j);
132 }
133
134 LIBKERN_INLINE intmax_t
135 imaxabs(intmax_t j)
136 {
137 return(j < 0 ? -j : j);
138 }
139
140 LIBKERN_INLINE int
141 isspace(int ch)
142 {
143 return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
144 }
145
146 LIBKERN_INLINE int
147 isascii(int ch)
148 {
149 return ((ch & ~0x7f) == 0);
150 }
151
152 LIBKERN_INLINE int
153 isupper(int ch)
154 {
155 return (ch >= 'A' && ch <= 'Z');
156 }
157
158 LIBKERN_INLINE int
159 islower(int ch)
160 {
161 return (ch >= 'a' && ch <= 'z');
162 }
163
164 LIBKERN_INLINE int
165 isalpha(int ch)
166 {
167 return (isupper(ch) || islower(ch));
168 }
169
170 LIBKERN_INLINE int
171 isalnum(int ch)
172 {
173 return (isalpha(ch) || isdigit(ch));
174 }
175
176 LIBKERN_INLINE int
177 isdigit(int ch)
178 {
179 return (ch >= '0' && ch <= '9');
180 }
181
182 LIBKERN_INLINE int
183 isxdigit(int ch)
184 {
185 return (isdigit(ch) ||
186 (ch >= 'A' && ch <= 'F') ||
187 (ch >= 'a' && ch <= 'f'));
188 }
189
190 LIBKERN_INLINE int
191 iscntrl(int ch)
192 {
193 return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F);
194 }
195
196 LIBKERN_INLINE int
197 isgraph(int ch)
198 {
199 return (ch != ' ' && isprint(ch));
200 }
201
202 LIBKERN_INLINE int
203 isprint(int ch)
204 {
205 return (ch >= 0x20 && ch <= 0x7E);
206 }
207
208 LIBKERN_INLINE int
209 ispunct(int ch)
210 {
211 return (isprint(ch) && ch != ' ' && !isalnum(ch));
212 }
213
214 LIBKERN_INLINE int
215 toupper(int ch)
216 {
217 if (islower(ch))
218 return (ch - 0x20);
219 return (ch);
220 }
221
222 LIBKERN_INLINE int
223 tolower(int ch)
224 {
225 if (isupper(ch))
226 return (ch + 0x20);
227 return (ch);
228 }
229 #endif
230
231 #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
232
233 #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
234
235 #ifdef NDEBUG /* tradition! */
236 #define assert(e) ((void)0)
237 #else
238 #define assert(e) (__predict_true((e)) ? (void)0 : \
239 kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
240 #endif
241
242 #ifdef __COVERITY__
243 #ifndef DIAGNOSTIC
244 #define DIAGNOSTIC
245 #endif
246 #endif
247
248 #ifndef CTASSERT
249 #define CTASSERT(x) __CTASSERT(x)
250 #endif
251 #ifndef CTASSERT_SIGNED
252 #define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0)
253 #endif
254 #ifndef CTASSERT_UNSIGNED
255 #define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0)
256 #endif
257
258 #ifndef DIAGNOSTIC
259 #define _DIAGASSERT(a) (void)0
260 #ifdef lint
261 #define KASSERTMSG(e, msg, ...) /* NOTHING */
262 #define KASSERT(e) /* NOTHING */
263 #else /* !lint */
264 #define KASSERTMSG(e, msg, ...) ((void)0)
265 #define KASSERT(e) ((void)0)
266 #endif /* !lint */
267 #else /* DIAGNOSTIC */
268 #define _DIAGASSERT(a) assert(a)
269 #define KASSERTMSG(e, msg, ...) \
270 (__predict_true((e)) ? (void)0 : \
271 kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \
272 __FILE__, __LINE__, ## __VA_ARGS__))
273
274 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
275 kern_assert(__KASSERTSTR, "diagnostic ", #e, \
276 __FILE__, __LINE__))
277 #endif
278
279 #ifndef DEBUG
280 #ifdef lint
281 #define KDASSERTMSG(e,msg, ...) /* NOTHING */
282 #define KDASSERT(e) /* NOTHING */
283 #else /* lint */
284 #define KDASSERTMSG(e,msg, ...) ((void)0)
285 #define KDASSERT(e) ((void)0)
286 #endif /* lint */
287 #else
288 #define KDASSERTMSG(e, msg, ...) \
289 (__predict_true((e)) ? (void)0 : \
290 kern_assert(__KASSERTSTR msg, "debugging ", #e, \
291 __FILE__, __LINE__, ## __VA_ARGS__))
292
293 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
294 kern_assert(__KASSERTSTR, "debugging ", #e, \
295 __FILE__, __LINE__))
296 #endif
297
298 /*
299 * XXX: For compatibility we use SMALL_RANDOM by default.
300 */
301 #define SMALL_RANDOM
302
303 #ifndef offsetof
304 #if __GNUC_PREREQ__(4, 0)
305 #define offsetof(type, member) __builtin_offsetof(type, member)
306 #else
307 #define offsetof(type, member) \
308 ((size_t)(unsigned long)(&(((type *)0)->member)))
309 #endif
310 #endif
311
312 /*
313 * Return the container of an embedded struct. Given x = &c->f,
314 * container_of(x, T, f) yields c, where T is the type of c. Example:
315 *
316 * struct foo { ... };
317 * struct bar {
318 * int b_x;
319 * struct foo b_foo;
320 * ...
321 * };
322 *
323 * struct bar b;
324 * struct foo *fp = b.b_foo;
325 *
326 * Now we can get at b from fp by:
327 *
328 * struct bar *bp = container_of(fp, struct bar, b_foo);
329 *
330 * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
331 * *fp does not match the type of struct bar::b_foo.
332 * We skip the validation for coverity runs to avoid warnings.
333 */
334 #ifdef __COVERITY__
335 #define __validate_container_of(PTR, TYPE, FIELD) 0
336 #else
337 #define __validate_container_of(PTR, TYPE, FIELD) \
338 (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \
339 offsetof(TYPE, FIELD)))->FIELD))
340 #endif
341
342 #define container_of(PTR, TYPE, FIELD) \
343 ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \
344 + __validate_container_of(PTR, TYPE, FIELD))
345
346 #define MTPRNG_RLEN 624
347 struct mtprng_state {
348 unsigned int mt_idx;
349 uint32_t mt_elem[MTPRNG_RLEN];
350 uint32_t mt_count;
351 uint32_t mt_sparse[3];
352 };
353
354 /* Prototypes for which GCC built-ins exist. */
355 void *memcpy(void *, const void *, size_t);
356 int memcmp(const void *, const void *, size_t);
357 void *memset(void *, int, size_t);
358 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
359 #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
360 #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
361 #endif
362 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
363 #define memset(d, v, l) __builtin_memset(d, v, l)
364 #endif
365
366 char *strcpy(char *, const char *);
367 int strcmp(const char *, const char *);
368 size_t strlen(const char *);
369 size_t strnlen(const char *, size_t);
370 char *strsep(char **, const char *);
371 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
372 #define strcpy(d, s) __builtin_strcpy(d, s)
373 #define strcmp(a, b) __builtin_strcmp(a, b)
374 #define strlen(a) __builtin_strlen(a)
375 #endif
376
377 /* Functions for which we always use built-ins. */
378 #ifdef __GNUC__
379 #define alloca(s) __builtin_alloca(s)
380 #endif
381
382 /* These exist in GCC 3.x, but we don't bother. */
383 char *strcat(char *, const char *);
384 size_t strcspn(const char *, const char *);
385 char *strncpy(char *, const char *, size_t);
386 char *strncat(char *, const char *, size_t);
387 int strncmp(const char *, const char *, size_t);
388 char *strchr(const char *, int);
389 char *strrchr(const char *, int);
390 char *strstr(const char *, const char *);
391 char *strpbrk(const char *, const char *);
392 size_t strspn(const char *, const char *);
393
394 /*
395 * ffs is an instruction on vax.
396 */
397 int ffs(int);
398 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
399 #define ffs(x) __builtin_ffs(x)
400 #endif
401
402 void kern_assert(const char *, ...)
403 __attribute__((__format__(__printf__, 1, 2)));
404 u_int32_t
405 inet_addr(const char *);
406 struct in_addr;
407 int inet_aton(const char *, struct in_addr *);
408 char *intoa(u_int32_t);
409 #define inet_ntoa(a) intoa((a).s_addr)
410 void *memchr(const void *, int, size_t);
411 void *memmove(void *, const void *, size_t);
412 int pmatch(const char *, const char *, const char **);
413 #ifndef SMALL_RANDOM
414 void srandom(unsigned long);
415 char *initstate(unsigned long, char *, size_t);
416 char *setstate(char *);
417 #endif /* SMALL_RANDOM */
418 long random(void);
419 void mi_vector_hash(const void * __restrict, size_t, uint32_t,
420 uint32_t[3]);
421 void mtprng_init32(struct mtprng_state *, uint32_t);
422 void mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
423 uint32_t mtprng_rawrandom(struct mtprng_state *);
424 uint32_t mtprng_random(struct mtprng_state *);
425 int scanc(u_int, const u_char *, const u_char *, int);
426 int skpc(int, size_t, u_char *);
427 int strcasecmp(const char *, const char *);
428 size_t strlcpy(char *, const char *, size_t);
429 size_t strlcat(char *, const char *, size_t);
430 int strncasecmp(const char *, const char *, size_t);
431 u_long strtoul(const char *, char **, int);
432 long long strtoll(const char *, char **, int);
433 unsigned long long strtoull(const char *, char **, int);
434 intmax_t strtoimax(const char *, char **, int);
435 uintmax_t strtoumax(const char *, char **, int);
436 intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
437 intmax_t, int *);
438 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
439 uintmax_t, int *);
440
441 int snprintb(char *, size_t, const char *, uint64_t);
442 int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
443 int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
444 void *);
445 uint32_t crc32(uint32_t, const uint8_t *, size_t);
446 #if __GNUC_PREREQ__(4, 5) \
447 && (defined(__alpha_cix__) || defined(__mips_popcount))
448 #define popcount __builtin_popcount
449 #define popcountl __builtin_popcountl
450 #define popcountll __builtin_popcountll
451 #define popcount32 __builtin_popcount
452 #define popcount64 __builtin_popcountll
453 #else
454 unsigned int popcount(unsigned int) __constfunc;
455 unsigned int popcountl(unsigned long) __constfunc;
456 unsigned int popcountll(unsigned long long) __constfunc;
457 unsigned int popcount32(uint32_t) __constfunc;
458 unsigned int popcount64(uint64_t) __constfunc;
459 #endif
460
461 void *explicit_memset(void *, int, size_t);
462 int consttime_memequal(const void *, const void *, size_t);
463
464 #ifdef notyet
465 /*
466 * LZF hashtable/state size: on uncompressible data and on a system with
467 * a sufficiently large d-cache, a larger table produces a considerable
468 * speed benefit. On systems with small memory and caches, however...
469 */
470 #if defined(__vax__) || defined(__m68k__)
471 #define LZF_HLOG 14
472 #else
473 #define LZF_HLOG 15
474 #endif
475 typedef const uint8_t *LZF_STATE[1 << LZF_HLOG];
476
477 unsigned int lzf_compress_r (const void *const, unsigned int, void *,
478 unsigned int, LZF_STATE);
479 unsigned int lzf_decompress (const void *const, unsigned int, void *,
480 unsigned int);
481 #endif
482
483 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
484