xref: /netbsd-src/sys/lib/libkern/libkern.h (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: libkern.h,v 1.50 2003/08/13 11:34:24 ragge 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 
39 #ifndef LIBKERN_INLINE
40 #define LIBKERN_INLINE	static __inline
41 #define LIBKERN_BODY
42 #endif
43 
44 LIBKERN_INLINE int imax __P((int, int)) __attribute__ ((unused));
45 LIBKERN_INLINE int imin __P((int, int)) __attribute__ ((unused));
46 LIBKERN_INLINE u_int max __P((u_int, u_int)) __attribute__ ((unused));
47 LIBKERN_INLINE u_int min __P((u_int, u_int)) __attribute__ ((unused));
48 LIBKERN_INLINE long lmax __P((long, long)) __attribute__ ((unused));
49 LIBKERN_INLINE long lmin __P((long, long)) __attribute__ ((unused));
50 LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __attribute__ ((unused));
51 LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __attribute__ ((unused));
52 LIBKERN_INLINE int abs __P((int)) __attribute__ ((unused));
53 
54 LIBKERN_INLINE int isspace __P((int)) __attribute__((__unused__));
55 LIBKERN_INLINE int isascii __P((int)) __attribute__((__unused__));
56 LIBKERN_INLINE int isupper __P((int)) __attribute__((__unused__));
57 LIBKERN_INLINE int islower __P((int)) __attribute__((__unused__));
58 LIBKERN_INLINE int isalpha __P((int)) __attribute__((__unused__));
59 LIBKERN_INLINE int isdigit __P((int)) __attribute__((__unused__));
60 LIBKERN_INLINE int isxdigit __P((int)) __attribute__((__unused__));
61 LIBKERN_INLINE int toupper __P((int)) __attribute__((__unused__));
62 LIBKERN_INLINE int tolower __P((int)) __attribute__((__unused__));
63 
64 #ifdef LIBKERN_BODY
65 LIBKERN_INLINE int
66 imax(int a, int b)
67 {
68 	return (a > b ? a : b);
69 }
70 LIBKERN_INLINE int
71 imin(int a, int b)
72 {
73 	return (a < b ? a : b);
74 }
75 LIBKERN_INLINE long
76 lmax(long a, long b)
77 {
78 	return (a > b ? a : b);
79 }
80 LIBKERN_INLINE long
81 lmin(long a, long b)
82 {
83 	return (a < b ? a : b);
84 }
85 LIBKERN_INLINE u_int
86 max(u_int a, u_int b)
87 {
88 	return (a > b ? a : b);
89 }
90 LIBKERN_INLINE u_int
91 min(u_int a, u_int b)
92 {
93 	return (a < b ? a : b);
94 }
95 LIBKERN_INLINE u_long
96 ulmax(u_long a, u_long b)
97 {
98 	return (a > b ? a : b);
99 }
100 LIBKERN_INLINE u_long
101 ulmin(u_long a, u_long b)
102 {
103 	return (a < b ? a : b);
104 }
105 
106 LIBKERN_INLINE int
107 abs(int j)
108 {
109 	return(j < 0 ? -j : j);
110 }
111 
112 LIBKERN_INLINE int
113 isspace(int ch)
114 {
115 	return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
116 }
117 
118 LIBKERN_INLINE int
119 isascii(int ch)
120 {
121 	return ((ch & ~0x7f) == 0);
122 }
123 
124 LIBKERN_INLINE int
125 isupper(int ch)
126 {
127 	return (ch >= 'A' && ch <= 'Z');
128 }
129 
130 LIBKERN_INLINE int
131 islower(int ch)
132 {
133 	return (ch >= 'a' && ch <= 'z');
134 }
135 
136 LIBKERN_INLINE int
137 isalpha(int ch)
138 {
139 	return (isupper(ch) || islower(ch));
140 }
141 
142 LIBKERN_INLINE int
143 isdigit(int ch)
144 {
145 	return (ch >= '0' && ch <= '9');
146 }
147 
148 LIBKERN_INLINE int
149 isxdigit(int ch)
150 {
151 	return (isdigit(ch) ||
152 	    (ch >= 'A' && ch <= 'F') ||
153 	    (ch >= 'a' && ch <= 'f'));
154 }
155 
156 LIBKERN_INLINE int
157 toupper(int ch)
158 {
159 	if (islower(ch))
160 		return (ch - 0x20);
161 	return (ch);
162 }
163 
164 LIBKERN_INLINE int
165 tolower(int ch)
166 {
167 	if (isupper(ch))
168 		return (ch + 0x20);
169 	return (ch);
170 }
171 #endif
172 
173 #ifdef NDEBUG						/* tradition! */
174 #define	assert(e)	((void)0)
175 #else
176 #ifdef __STDC__
177 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
178 			    __assert("", __FILE__, __LINE__, #e))
179 #else
180 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
181 			    __assert("", __FILE__, __LINE__, "e"))
182 #endif
183 #endif
184 
185 #ifndef DIAGNOSTIC
186 #ifdef lint
187 #define	KASSERT(e)	/* NOTHING */
188 #else /* !lint */
189 #define	KASSERT(e)	((void)0)
190 #endif /* !lint */
191 #else
192 #ifdef __STDC__
193 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
194 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
195 #else
196 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
197 			    __assert("diagnostic ", __FILE__, __LINE__, "e"))
198 #endif
199 #endif
200 
201 #ifndef DEBUG
202 #ifdef lint
203 #define	KDASSERT(e)	/* NOTHING */
204 #else /* lint */
205 #define	KDASSERT(e)	((void)0)
206 #endif /* lint */
207 #else
208 #ifdef __STDC__
209 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
210 			    __assert("debugging ", __FILE__, __LINE__, #e))
211 #else
212 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
213 			    __assert("debugging ", __FILE__, __LINE__, "e"))
214 #endif
215 #endif
216 
217 #ifndef offsetof
218 #define	offsetof(type, member) \
219     ((size_t)(unsigned long)(&(((type *)0)->member)))
220 #endif
221 
222 /* Prototypes for non-quad routines. */
223 /* XXX notyet #ifdef _STANDALONE */
224 int	 bcmp __P((const void *, const void *, size_t));
225 void	 bzero __P((void *, size_t));
226 /* #endif */
227 
228 /* Prototypes for which GCC built-ins exist. */
229 void	*memcpy __P((void *, const void *, size_t));
230 int	 memcmp __P((const void *, const void *, size_t));
231 void	*memset __P((void *, int, size_t));
232 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
233 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
234 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
235 #define	memset(d, v, l)		__builtin_memset(d, v, l)
236 #endif
237 
238 char	*strcpy __P((char *, const char *));
239 int	 strcmp __P((const char *, const char *));
240 size_t	 strlen __P((const char *));
241 #if __GNUC_PREREQ__(2, 95)
242 #define	strcpy(d, s)		__builtin_strcpy(d, s)
243 #define	strcmp(a, b)		__builtin_strcmp(a, b)
244 #define	strlen(a)		__builtin_strlen(a)
245 #endif
246 
247 /* Functions for which we always use built-ins. */
248 #ifdef __GNUC__
249 #define	alloca(s)		__builtin_alloca(s)
250 #endif
251 
252 /* These exist in GCC 3.x, but we don't bother. */
253 char	*strcat __P((char *, const char *));
254 char	*strncpy __P((char *, const char *, size_t));
255 int	 strncmp __P((const char *, const char *, size_t));
256 char	*strchr __P((const char *, int));
257 char	*strrchr __P((const char *, int));
258 
259 char	*strstr __P((const char *, const char *));
260 
261 /*
262  * ffs is an instruction on vax.
263  */
264 int	 ffs __P((int));
265 #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
266 #define	ffs(x)			__builtin_ffs(x)
267 #endif
268 
269 void	 __assert __P((const char *, const char *, int, const char *))
270 	    __attribute__((__noreturn__));
271 u_int32_t
272 	 inet_addr __P((const char *));
273 char	*intoa __P((u_int32_t));
274 #define inet_ntoa(a) intoa((a).s_addr)
275 void	*memchr __P((const void *, int, size_t));
276 void	*memmove __P((void *, const void *, size_t));
277 int	 pmatch __P((const char *, const char *, const char **));
278 u_int32_t arc4random __P((void));
279 void	 arc4randbytes __P((void *, size_t));
280 u_long	 random __P((void));
281 int	 scanc __P((u_int, const u_char *, const u_char *, int));
282 int	 skpc __P((int, size_t, u_char *));
283 int	 strcasecmp __P((const char *, const char *));
284 size_t	 strlcpy __P((char *, const char *, size_t));
285 size_t	 strlcat __P((char *, const char *, size_t));
286 int	 strncasecmp __P((const char *, const char *, size_t));
287 u_long	 strtoul __P((const char *, char **, int));
288 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
289