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