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