xref: /openbsd/sys/lib/libkern/libkern.h (revision a6445c1d)
1 /*	$OpenBSD: libkern.h,v 1.34 2014/07/13 23:49:40 uebayasi Exp $	*/
2 /*	$NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)libkern.h	8.1 (Berkeley) 6/10/93
33  */
34 
35 #ifndef __LIBKERN_H__
36 #define __LIBKERN_H__
37 
38 #include <sys/types.h>
39 
40 #ifndef LIBKERN_INLINE
41 #define LIBKERN_INLINE	static __inline
42 #define LIBKERN_BODY
43 #endif
44 
45 
46 LIBKERN_INLINE int imax(int, int);
47 LIBKERN_INLINE int imin(int, int);
48 LIBKERN_INLINE u_int max(u_int, u_int);
49 LIBKERN_INLINE u_int min(u_int, u_int);
50 LIBKERN_INLINE long lmax(long, long);
51 LIBKERN_INLINE long lmin(long, long);
52 LIBKERN_INLINE u_long ulmax(u_long, u_long);
53 LIBKERN_INLINE u_long ulmin(u_long, u_long);
54 LIBKERN_INLINE int abs(int);
55 
56 #ifdef LIBKERN_BODY
57 LIBKERN_INLINE int
58 imax(int a, int b)
59 {
60 	return (a > b ? a : b);
61 }
62 LIBKERN_INLINE int
63 imin(int a, int b)
64 {
65 	return (a < b ? a : b);
66 }
67 LIBKERN_INLINE long
68 lmax(long a, long b)
69 {
70 	return (a > b ? a : b);
71 }
72 LIBKERN_INLINE long
73 lmin(long a, long b)
74 {
75 	return (a < b ? a : b);
76 }
77 LIBKERN_INLINE u_int
78 max(u_int a, u_int b)
79 {
80 	return (a > b ? a : b);
81 }
82 LIBKERN_INLINE u_int
83 min(u_int a, u_int b)
84 {
85 	return (a < b ? a : b);
86 }
87 LIBKERN_INLINE u_long
88 ulmax(u_long a, u_long b)
89 {
90 	return (a > b ? a : b);
91 }
92 LIBKERN_INLINE u_long
93 ulmin(u_long a, u_long b)
94 {
95 	return (a < b ? a : b);
96 }
97 
98 LIBKERN_INLINE int
99 abs(int j)
100 {
101 	return(j < 0 ? -j : j);
102 }
103 #endif
104 
105 #ifdef NDEBUG						/* tradition! */
106 #define	assert(e)	((void)0)
107 #else
108 #ifdef __STDC__
109 #define	assert(e)	((e) ? (void)0 :				    \
110 			    __assert("", __FILE__, __LINE__, #e))
111 #else
112 #define	assert(e)	((e) ? (void)0 :				    \
113 			    __assert("", __FILE__, __LINE__, "e"))
114 #endif
115 #endif
116 
117 #define	__KASSERTSTR	"kernel %sassertion \"%s\" failed: file \"%s\", line %d"
118 
119 #ifndef DIAGNOSTIC
120 #define	KASSERTMSG(e, msg, ...)	((void)0)
121 #define	KASSERT(e)	((void)0)
122 #else
123 #ifdef __STDC__
124 #define	KASSERTMSG(e, msg, ...)	((e) ? (void)0 :			    \
125 			    panic(__KASSERTSTR " " msg, "diagnostic ", #e,  \
126 			    __FILE__, __LINE__, ## __VA_ARGS__))
127 #define	KASSERT(e)	((e) ? (void)0 :				    \
128 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
129 #else
130 #define	KASSERTMSG(e, msg, ...)	((e) ? (void)0 :			    \
131 			    panic(__KASSERTSTR " " msg, "diagnostic ", "e", \
132 			    __FILE__, __LINE__, ## __VA_ARGS__))
133 #define	KASSERT(e)	((e) ? (void)0 :				    \
134 			    __assert("diagnostic ", __FILE__, __LINE__, "e"))
135 #endif
136 #endif
137 
138 #ifndef DEBUG
139 #define	KDASSERTMSG(e, msg, ...)	((void)0)
140 #define	KDASSERT(e)	((void)0)
141 #else
142 #ifdef __STDC__
143 #define	KDASSERTMSG(e, msg, ...)	((e) ? (void)0 :		    \
144 			    panic(__KASSERTSTR " " msg, "debugging ", #e,   \
145 			    __FILE__, __LINE__, ## __VA_ARGS__))
146 #define	KDASSERT(e)	((e) ? (void)0 :				    \
147 			    __assert("debugging ", __FILE__, __LINE__, #e))
148 #else
149 #define	KDASSERTMSG(e, msg, ...)	((e) ? (void)0 :		    \
150 			    panic(__KASSERTSTR " " msg, "debugging ", "e",  \
151 			    __FILE__, __LINE__, ## __VA_ARGS__))
152 #define	KDASSERT(e)	((e) ? (void)0 :				    \
153 			    __assert("debugging ", __FILE__, __LINE__, "e"))
154 #endif
155 #endif
156 
157 #define	CTASSERT(x)	extern char  _ctassert[(x) ? 1 : -1 ]	\
158 			    __attribute__((__unused__))
159 
160 /* Prototypes for non-quad routines. */
161 void	 __assert(const char *, const char *, int, const char *)
162 	    __attribute__ ((__noreturn__));
163 int	 bcmp(const void *, const void *, size_t);
164 void	 bzero(void *, size_t);
165 void	 explicit_bzero(void *, size_t);
166 int	 ffs(int);
167 int	 fls(int);
168 int	 flsl(long);
169 void	*memchr(const void *, int, size_t);
170 int	 memcmp(const void *, const void *, size_t);
171 void	*memset(void *, int c, size_t len);
172 u_int32_t random(void);
173 int	 scanc(u_int, const u_char *, const u_char [], int);
174 int	 skpc(int, size_t, u_char *);
175 size_t	 strlen(const char *);
176 char	*strncpy(char *, const char *, size_t)
177 		__attribute__ ((__bounded__(__string__,1,3)));
178 size_t	 strnlen(const char *, size_t);
179 size_t	 strlcpy(char *, const char *, size_t)
180 		__attribute__ ((__bounded__(__string__,1,3)));
181 size_t	 strlcat(char *, const char *, size_t)
182 		__attribute__ ((__bounded__(__string__,1,3)));
183 int	 strcmp(const char *, const char *);
184 int	 strncmp(const char *, const char *, size_t);
185 int	 strncasecmp(const char *, const char *, size_t);
186 int	 getsn(char *, int);
187 char	*strchr(const char *, int);
188 char	*strrchr(const char *, int);
189 int	 timingsafe_bcmp(const void *, const void *, size_t);
190 
191 #endif /* __LIBKERN_H__ */
192