xref: /openbsd-src/sys/lib/libkern/libkern.h (revision e86eac0aa26ae70ca99c10330901a05f18155e9a)
1*e86eac0aSjsg /*	$OpenBSD: libkern.h,v 1.37 2023/12/21 02:57:14 jsg Exp $	*/
20c0430f8Sniklas /*	$NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $	*/
3df930be7Sderaadt 
4df930be7Sderaadt /*-
5df930be7Sderaadt  * Copyright (c) 1992, 1993
6df930be7Sderaadt  *	The Regents of the University of California.  All rights reserved.
7df930be7Sderaadt  *
8df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
9df930be7Sderaadt  * modification, are permitted provided that the following conditions
10df930be7Sderaadt  * are met:
11df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
12df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
13df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
14df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
15df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
1629295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
17df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
18df930be7Sderaadt  *    without specific prior written permission.
19df930be7Sderaadt  *
20df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df930be7Sderaadt  * SUCH DAMAGE.
31df930be7Sderaadt  *
32df930be7Sderaadt  *	@(#)libkern.h	8.1 (Berkeley) 6/10/93
33df930be7Sderaadt  */
34df930be7Sderaadt 
3561441e17Smickey #ifndef __LIBKERN_H__
3661441e17Smickey #define __LIBKERN_H__
3761441e17Smickey 
38df930be7Sderaadt #include <sys/types.h>
39df930be7Sderaadt 
400c0430f8Sniklas #ifndef LIBKERN_INLINE
410c0430f8Sniklas #define LIBKERN_INLINE	static __inline
420c0430f8Sniklas #define LIBKERN_BODY
430c0430f8Sniklas #endif
44df930be7Sderaadt 
450c0430f8Sniklas 
46c4071fd1Smillert LIBKERN_INLINE int imax(int, int);
47c4071fd1Smillert LIBKERN_INLINE int imin(int, int);
48c4071fd1Smillert LIBKERN_INLINE u_int max(u_int, u_int);
49c4071fd1Smillert LIBKERN_INLINE u_int min(u_int, u_int);
50c4071fd1Smillert LIBKERN_INLINE long lmax(long, long);
51c4071fd1Smillert LIBKERN_INLINE long lmin(long, long);
52c4071fd1Smillert LIBKERN_INLINE u_long ulmax(u_long, u_long);
53c4071fd1Smillert LIBKERN_INLINE u_long ulmin(u_long, u_long);
54c4071fd1Smillert LIBKERN_INLINE int abs(int);
550c0430f8Sniklas 
560c0430f8Sniklas #ifdef LIBKERN_BODY
570c0430f8Sniklas LIBKERN_INLINE int
imax(int a,int b)5883d1cf15Sjsg imax(int a, int b)
59df930be7Sderaadt {
60df930be7Sderaadt 	return (a > b ? a : b);
61df930be7Sderaadt }
620c0430f8Sniklas LIBKERN_INLINE int
imin(int a,int b)6383d1cf15Sjsg imin(int a, int b)
64df930be7Sderaadt {
65df930be7Sderaadt 	return (a < b ? a : b);
66df930be7Sderaadt }
670c0430f8Sniklas LIBKERN_INLINE long
lmax(long a,long b)6883d1cf15Sjsg lmax(long a, long b)
69df930be7Sderaadt {
70df930be7Sderaadt 	return (a > b ? a : b);
71df930be7Sderaadt }
720c0430f8Sniklas LIBKERN_INLINE long
lmin(long a,long b)7383d1cf15Sjsg lmin(long a, long b)
74df930be7Sderaadt {
75df930be7Sderaadt 	return (a < b ? a : b);
76df930be7Sderaadt }
770c0430f8Sniklas LIBKERN_INLINE u_int
max(u_int a,u_int b)7883d1cf15Sjsg max(u_int a, u_int b)
79df930be7Sderaadt {
80df930be7Sderaadt 	return (a > b ? a : b);
81df930be7Sderaadt }
820c0430f8Sniklas LIBKERN_INLINE u_int
min(u_int a,u_int b)8383d1cf15Sjsg min(u_int a, u_int b)
84df930be7Sderaadt {
85df930be7Sderaadt 	return (a < b ? a : b);
86df930be7Sderaadt }
870c0430f8Sniklas LIBKERN_INLINE u_long
ulmax(u_long a,u_long b)8883d1cf15Sjsg ulmax(u_long a, u_long b)
89df930be7Sderaadt {
90df930be7Sderaadt 	return (a > b ? a : b);
91df930be7Sderaadt }
920c0430f8Sniklas LIBKERN_INLINE u_long
ulmin(u_long a,u_long b)9383d1cf15Sjsg ulmin(u_long a, u_long b)
94df930be7Sderaadt {
95df930be7Sderaadt 	return (a < b ? a : b);
96df930be7Sderaadt }
97df930be7Sderaadt 
980c0430f8Sniklas LIBKERN_INLINE int
abs(int j)9983d1cf15Sjsg abs(int j)
100df930be7Sderaadt {
101df930be7Sderaadt 	return(j < 0 ? -j : j);
102df930be7Sderaadt }
1030c0430f8Sniklas #endif
104df930be7Sderaadt 
105f5fd2665Sniklas #ifdef NDEBUG						/* tradition! */
106f5fd2665Sniklas #define	assert(e)	((void)0)
107f5fd2665Sniklas #else
108f5fd2665Sniklas #define	assert(e)	((e) ? (void)0 :				    \
109f5fd2665Sniklas 			    __assert("", __FILE__, __LINE__, #e))
110f5fd2665Sniklas #endif
111f5fd2665Sniklas 
112a972b4a4Suebayasi #define	__KASSERTSTR	"kernel %sassertion \"%s\" failed: file \"%s\", line %d"
113a972b4a4Suebayasi 
114f5fd2665Sniklas #ifndef DIAGNOSTIC
115a972b4a4Suebayasi #define	KASSERTMSG(e, msg, ...)	((void)0)
116f5fd2665Sniklas #define	KASSERT(e)	((void)0)
117f5fd2665Sniklas #else
118a972b4a4Suebayasi #define	KASSERTMSG(e, msg, ...)	((e) ? (void)0 :			    \
119a972b4a4Suebayasi 			    panic(__KASSERTSTR " " msg, "diagnostic ", #e,  \
120a972b4a4Suebayasi 			    __FILE__, __LINE__, ## __VA_ARGS__))
121f5fd2665Sniklas #define	KASSERT(e)	((e) ? (void)0 :				    \
122f5fd2665Sniklas 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
123f5fd2665Sniklas #endif
124f5fd2665Sniklas 
125f5fd2665Sniklas #ifndef DEBUG
126a972b4a4Suebayasi #define	KDASSERTMSG(e, msg, ...)	((void)0)
127f5fd2665Sniklas #define	KDASSERT(e)	((void)0)
128f5fd2665Sniklas #else
129a972b4a4Suebayasi #define	KDASSERTMSG(e, msg, ...)	((e) ? (void)0 :		    \
130a972b4a4Suebayasi 			    panic(__KASSERTSTR " " msg, "debugging ", #e,   \
131a972b4a4Suebayasi 			    __FILE__, __LINE__, ## __VA_ARGS__))
132f5fd2665Sniklas #define	KDASSERT(e)	((e) ? (void)0 :				    \
133f5fd2665Sniklas 			    __assert("debugging ", __FILE__, __LINE__, #e))
134f5fd2665Sniklas #endif
135f5fd2665Sniklas 
1364d8b4d23Ssf #define	CTASSERT(x)	extern char  _ctassert[(x) ? 1 : -1 ]	\
1374d8b4d23Ssf 			    __attribute__((__unused__))
1384d8b4d23Ssf 
139df930be7Sderaadt /* Prototypes for non-quad routines. */
140c4071fd1Smillert void	 __assert(const char *, const char *, int, const char *)
141f5fd2665Sniklas 	    __attribute__ ((__noreturn__));
142c4071fd1Smillert int	 bcmp(const void *, const void *, size_t);
143a26aa419Sderaadt void	 bzero(void *, size_t);
144f5d2eefeStedu void	 explicit_bzero(void *, size_t);
145c4071fd1Smillert int	 ffs(int);
146383d2989Sjsg int	 fls(int);
147383d2989Sjsg int	 flsl(long);
148c4071fd1Smillert void	*memchr(const void *, int, size_t);
149c4071fd1Smillert int	 memcmp(const void *, const void *, size_t);
150b5be37d2Sderaadt void	*memset(void *, int c, size_t len);
1516a46a94aSderaadt u_int32_t random(void);
152a26aa419Sderaadt int	 scanc(u_int, const u_char *, const u_char [], int);
153c4071fd1Smillert int	 skpc(int, size_t, u_char *);
154c4071fd1Smillert size_t	 strlen(const char *);
155b857d3eaSitojun char	*strncpy(char *, const char *, size_t)
156b857d3eaSitojun 		__attribute__ ((__bounded__(__string__,1,3)));
157f264d37aSmatthew size_t	 strnlen(const char *, size_t);
158b857d3eaSitojun size_t	 strlcpy(char *, const char *, size_t)
159b857d3eaSitojun 		__attribute__ ((__bounded__(__string__,1,3)));
160b857d3eaSitojun size_t	 strlcat(char *, const char *, size_t)
161b857d3eaSitojun 		__attribute__ ((__bounded__(__string__,1,3)));
162c4071fd1Smillert int	 strcmp(const char *, const char *);
163c4071fd1Smillert int	 strncmp(const char *, const char *, size_t);
164c4071fd1Smillert int	 strncasecmp(const char *, const char *, size_t);
16585f30cb4Sdlg size_t	 getsn(char *, size_t)
16685f30cb4Sdlg 		__attribute__ ((__bounded__(__string__,1,2)));
167125da6d1Sderaadt char	*strchr(const char *, int);
168125da6d1Sderaadt char	*strrchr(const char *, int);
169bee02badSmatthew int	 timingsafe_bcmp(const void *, const void *, size_t);
170*e86eac0aSjsg char	*strnstr(const char *, const char *, size_t);
17161441e17Smickey 
17261441e17Smickey #endif /* __LIBKERN_H__ */
173