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