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