xref: /netbsd/sys/lib/libkern/libkern.h (revision bf9ec67e)
1 /*	$NetBSD: libkern.h,v 1.40 2002/05/28 10:09:24 itojun 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)libkern.h	8.2 (Berkeley) 8/5/94
36  */
37 
38 #ifndef _LIB_LIBKERN_LIBKERN_H_
39 #define _LIB_LIBKERN_LIBKERN_H_
40 
41 #include <sys/types.h>
42 
43 #ifndef LIBKERN_INLINE
44 #define LIBKERN_INLINE	static __inline
45 #define LIBKERN_BODY
46 #endif
47 
48 LIBKERN_INLINE int imax __P((int, int)) __attribute__ ((unused));
49 LIBKERN_INLINE int imin __P((int, int)) __attribute__ ((unused));
50 LIBKERN_INLINE u_int max __P((u_int, u_int)) __attribute__ ((unused));
51 LIBKERN_INLINE u_int min __P((u_int, u_int)) __attribute__ ((unused));
52 LIBKERN_INLINE long lmax __P((long, long)) __attribute__ ((unused));
53 LIBKERN_INLINE long lmin __P((long, long)) __attribute__ ((unused));
54 LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __attribute__ ((unused));
55 LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __attribute__ ((unused));
56 LIBKERN_INLINE int abs __P((int)) __attribute__ ((unused));
57 
58 LIBKERN_INLINE int isspace __P((int)) __attribute__((__unused__));
59 LIBKERN_INLINE int isascii __P((int)) __attribute__((__unused__));
60 LIBKERN_INLINE int isupper __P((int)) __attribute__((__unused__));
61 LIBKERN_INLINE int islower __P((int)) __attribute__((__unused__));
62 LIBKERN_INLINE int isalpha __P((int)) __attribute__((__unused__));
63 LIBKERN_INLINE int isdigit __P((int)) __attribute__((__unused__));
64 LIBKERN_INLINE int isxdigit __P((int)) __attribute__((__unused__));
65 LIBKERN_INLINE int toupper __P((int)) __attribute__((__unused__));
66 LIBKERN_INLINE int tolower __P((int)) __attribute__((__unused__));
67 
68 #ifdef LIBKERN_BODY
69 LIBKERN_INLINE int
70 imax(a, b)
71 	int a, b;
72 {
73 	return (a > b ? a : b);
74 }
75 LIBKERN_INLINE int
76 imin(a, b)
77 	int a, b;
78 {
79 	return (a < b ? a : b);
80 }
81 LIBKERN_INLINE long
82 lmax(a, b)
83 	long a, b;
84 {
85 	return (a > b ? a : b);
86 }
87 LIBKERN_INLINE long
88 lmin(a, b)
89 	long a, b;
90 {
91 	return (a < b ? a : b);
92 }
93 LIBKERN_INLINE u_int
94 max(a, b)
95 	u_int a, b;
96 {
97 	return (a > b ? a : b);
98 }
99 LIBKERN_INLINE u_int
100 min(a, b)
101 	u_int a, b;
102 {
103 	return (a < b ? a : b);
104 }
105 LIBKERN_INLINE u_long
106 ulmax(a, b)
107 	u_long a, b;
108 {
109 	return (a > b ? a : b);
110 }
111 LIBKERN_INLINE u_long
112 ulmin(a, b)
113 	u_long a, b;
114 {
115 	return (a < b ? a : b);
116 }
117 
118 LIBKERN_INLINE int
119 abs(j)
120 	int j;
121 {
122 	return(j < 0 ? -j : j);
123 }
124 
125 LIBKERN_INLINE int
126 isspace(ch)
127 	int ch;
128 {
129 
130 	return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
131 }
132 
133 LIBKERN_INLINE int
134 isascii(ch)
135 	int ch;
136 {
137 
138 	return ((ch & ~0x7f) == 0);
139 }
140 
141 LIBKERN_INLINE int
142 isupper(ch)
143 	int ch;
144 {
145 
146 	return (ch >= 'A' && ch <= 'Z');
147 }
148 
149 LIBKERN_INLINE int
150 islower(ch)
151 	int ch;
152 {
153 
154 	return (ch >= 'a' && ch <= 'z');
155 }
156 
157 LIBKERN_INLINE int
158 isalpha(ch)
159 	int ch;
160 {
161 
162 	return (isupper(ch) || islower(ch));
163 }
164 
165 LIBKERN_INLINE int
166 isdigit(ch)
167 	int ch;
168 {
169 
170 	return (ch >= '0' && ch <= '9');
171 }
172 
173 LIBKERN_INLINE int
174 isxdigit(ch)
175 	int ch;
176 {
177 
178 	return (isdigit(ch) ||
179 	    (ch >= 'A' && ch <= 'F') ||
180 	    (ch >= 'a' && ch <= 'f'));
181 }
182 
183 LIBKERN_INLINE int
184 toupper(ch)
185 	int ch;
186 {
187 
188 	if (islower(ch))
189 		return (ch - 0x20);
190 	return (ch);
191 }
192 
193 LIBKERN_INLINE int
194 tolower(ch)
195 	int ch;
196 {
197 
198 	if (isupper(ch))
199 		return (ch + 0x20);
200 	return (ch);
201 }
202 #endif
203 
204 #ifdef NDEBUG						/* tradition! */
205 #define	assert(e)	((void)0)
206 #else
207 #ifdef __STDC__
208 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
209 			    __assert("", __FILE__, __LINE__, #e))
210 #else
211 #define	assert(e)	(__predict_true((e)) ? (void)0 :		    \
212 			    __assert("", __FILE__, __LINE__, "e"))
213 #endif
214 #endif
215 
216 #ifndef DIAGNOSTIC
217 #ifdef lint
218 #define	KASSERT(e)	/* NOTHING */
219 #else /* !lint */
220 #define	KASSERT(e)	((void)0)
221 #endif /* !lint */
222 #else
223 #ifdef __STDC__
224 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
225 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
226 #else
227 #define	KASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
228 			    __assert("diagnostic ", __FILE__, __LINE__, "e"))
229 #endif
230 #endif
231 
232 #ifndef DEBUG
233 #ifdef lint
234 #define	KDASSERT(e)	/* NOTHING */
235 #else /* lint */
236 #define	KDASSERT(e)	((void)0)
237 #endif /* lint */
238 #else
239 #ifdef __STDC__
240 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
241 			    __assert("debugging ", __FILE__, __LINE__, #e))
242 #else
243 #define	KDASSERT(e)	(__predict_true((e)) ? (void)0 :		    \
244 			    __assert("debugging ", __FILE__, __LINE__, "e"))
245 #endif
246 #endif
247 
248 #ifndef offsetof
249 #define	offsetof(type, member)	((size_t)(&((type *)0)->member))
250 #endif
251 
252 /* Prototypes for non-quad routines. */
253 /* XXX notyet #ifdef _STANDALONE */
254 int	 bcmp __P((const void *, const void *, size_t));
255 void	 bzero __P((void *, size_t));
256 /* #endif */
257 
258 /* Prototypes for which GCC built-ins exist. */
259 void	*memcpy __P((void *, const void *, size_t));
260 int	 memcmp __P((const void *, const void *, size_t));
261 void	*memset __P((void *, int, size_t));
262 #if __GNUC_PREREQ__(2, 95)
263 #define	memcpy(d, s, l)		__builtin_memcpy(d, s, l)
264 #define	memcmp(a, b, l)		__builtin_memcmp(a, b, l)
265 #define	memset(d, v, l)		__builtin_memset(d, v, l)
266 #endif
267 
268 char	*strcpy __P((char *, const char *));
269 int	 strcmp __P((const char *, const char *));
270 size_t	 strlen __P((const char *));
271 #if __GNUC_PREREQ__(2, 95)
272 #define	strcpy(d, s)		__builtin_strcpy(d, s)
273 #define	strcmp(a, b)		__builtin_strcmp(a, b)
274 #define	strlen(a)		__builtin_strlen(a)
275 #endif
276 
277 /* Functions for which we always use built-ins. */
278 #ifdef __GNUC__
279 #define	alloca(s)		__builtin_alloca(s)
280 #endif
281 
282 /* These exist in GCC 3.x, but we don't bother. */
283 char	*strcat __P((char *, const char *));
284 char	*strncpy __P((char *, const char *, size_t));
285 int	 strncmp __P((const char *, const char *, size_t));
286 char	*strchr __P((const char *, int));
287 char	*strrchr __P((const char *, int));
288 
289 /* This exists in GCC 3.x, but we don't bother (yet). */
290 int	 ffs __P((int));
291 
292 void	 __assert __P((const char *, const char *, int, const char *))
293 	    __attribute__((__noreturn__));
294 u_int32_t
295 	 inet_addr __P((const char *));
296 char	*intoa __P((u_int32_t));
297 #define inet_ntoa(a) intoa((a).s_addr)
298 void	*memchr __P((const void *, int, size_t));
299 void	*memmove __P((void *, const void *, size_t));
300 int	 pmatch __P((const char *, const char *, const char **));
301 u_int32_t arc4random __P((void));
302 u_long	 random __P((void));
303 int	 scanc __P((u_int, const u_char *, const u_char *, int));
304 int	 skpc __P((int, size_t, u_char *));
305 int	 strcasecmp __P((const char *, const char *));
306 int	 strncasecmp __P((const char *, const char *, size_t));
307 u_long	 strtoul __P((const char *, char **, int));
308 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
309