libkern.h revision 1.82 1 1.82 gmcgarry /* $NetBSD: libkern.h,v 1.82 2008/09/08 23:36:55 gmcgarry 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.62 thorpej #include <sys/inttypes.h>
39 1.52 christos #include <sys/null.h>
40 1.4 cgd
41 1.7 christos #ifndef LIBKERN_INLINE
42 1.55 perry #define LIBKERN_INLINE static __inline
43 1.7 christos #define LIBKERN_BODY
44 1.7 christos #endif
45 1.7 christos
46 1.82 gmcgarry LIBKERN_INLINE int imax __P((int, int)) __unused;
47 1.82 gmcgarry LIBKERN_INLINE int imin __P((int, int)) __unused;
48 1.82 gmcgarry LIBKERN_INLINE u_int max __P((u_int, u_int)) __unused;
49 1.82 gmcgarry LIBKERN_INLINE u_int min __P((u_int, u_int)) __unused;
50 1.82 gmcgarry LIBKERN_INLINE long lmax __P((long, long)) __unused;
51 1.82 gmcgarry LIBKERN_INLINE long lmin __P((long, long)) __unused;
52 1.82 gmcgarry LIBKERN_INLINE u_long ulmax __P((u_long, u_long)) __unused;
53 1.82 gmcgarry LIBKERN_INLINE u_long ulmin __P((u_long, u_long)) __unused;
54 1.82 gmcgarry LIBKERN_INLINE int abs __P((int)) __unused;
55 1.1 cgd
56 1.72 perry LIBKERN_INLINE int isspace __P((int)) __unused;
57 1.72 perry LIBKERN_INLINE int isascii __P((int)) __unused;
58 1.72 perry LIBKERN_INLINE int isupper __P((int)) __unused;
59 1.72 perry LIBKERN_INLINE int islower __P((int)) __unused;
60 1.72 perry LIBKERN_INLINE int isalpha __P((int)) __unused;
61 1.72 perry LIBKERN_INLINE int isdigit __P((int)) __unused;
62 1.72 perry LIBKERN_INLINE int isxdigit __P((int)) __unused;
63 1.72 perry LIBKERN_INLINE int toupper __P((int)) __unused;
64 1.72 perry LIBKERN_INLINE int tolower __P((int)) __unused;
65 1.33 thorpej
66 1.7 christos #ifdef LIBKERN_BODY
67 1.7 christos LIBKERN_INLINE int
68 1.44 matt imax(int a, int b)
69 1.1 cgd {
70 1.1 cgd return (a > b ? a : b);
71 1.1 cgd }
72 1.7 christos LIBKERN_INLINE int
73 1.44 matt imin(int a, int b)
74 1.1 cgd {
75 1.1 cgd return (a < b ? a : b);
76 1.1 cgd }
77 1.7 christos LIBKERN_INLINE long
78 1.44 matt lmax(long a, long b)
79 1.1 cgd {
80 1.1 cgd return (a > b ? a : b);
81 1.1 cgd }
82 1.7 christos LIBKERN_INLINE long
83 1.44 matt lmin(long a, long b)
84 1.1 cgd {
85 1.1 cgd return (a < b ? a : b);
86 1.1 cgd }
87 1.7 christos LIBKERN_INLINE u_int
88 1.44 matt max(u_int a, u_int b)
89 1.1 cgd {
90 1.1 cgd return (a > b ? a : b);
91 1.1 cgd }
92 1.7 christos LIBKERN_INLINE u_int
93 1.44 matt min(u_int a, u_int b)
94 1.1 cgd {
95 1.1 cgd return (a < b ? a : b);
96 1.1 cgd }
97 1.7 christos LIBKERN_INLINE u_long
98 1.44 matt ulmax(u_long a, u_long b)
99 1.1 cgd {
100 1.1 cgd return (a > b ? a : b);
101 1.1 cgd }
102 1.7 christos LIBKERN_INLINE u_long
103 1.44 matt ulmin(u_long a, u_long b)
104 1.1 cgd {
105 1.1 cgd return (a < b ? a : b);
106 1.5 leo }
107 1.5 leo
108 1.7 christos LIBKERN_INLINE int
109 1.44 matt abs(int j)
110 1.5 leo {
111 1.5 leo return(j < 0 ? -j : j);
112 1.33 thorpej }
113 1.33 thorpej
114 1.33 thorpej LIBKERN_INLINE int
115 1.44 matt isspace(int ch)
116 1.33 thorpej {
117 1.33 thorpej return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
118 1.33 thorpej }
119 1.33 thorpej
120 1.33 thorpej LIBKERN_INLINE int
121 1.44 matt isascii(int ch)
122 1.33 thorpej {
123 1.33 thorpej return ((ch & ~0x7f) == 0);
124 1.33 thorpej }
125 1.33 thorpej
126 1.33 thorpej LIBKERN_INLINE int
127 1.44 matt isupper(int ch)
128 1.33 thorpej {
129 1.33 thorpej return (ch >= 'A' && ch <= 'Z');
130 1.33 thorpej }
131 1.33 thorpej
132 1.33 thorpej LIBKERN_INLINE int
133 1.44 matt islower(int ch)
134 1.33 thorpej {
135 1.33 thorpej return (ch >= 'a' && ch <= 'z');
136 1.33 thorpej }
137 1.33 thorpej
138 1.33 thorpej LIBKERN_INLINE int
139 1.44 matt isalpha(int ch)
140 1.33 thorpej {
141 1.33 thorpej return (isupper(ch) || islower(ch));
142 1.33 thorpej }
143 1.33 thorpej
144 1.33 thorpej LIBKERN_INLINE int
145 1.44 matt isdigit(int ch)
146 1.33 thorpej {
147 1.33 thorpej return (ch >= '0' && ch <= '9');
148 1.33 thorpej }
149 1.33 thorpej
150 1.33 thorpej LIBKERN_INLINE int
151 1.44 matt isxdigit(int ch)
152 1.33 thorpej {
153 1.33 thorpej return (isdigit(ch) ||
154 1.33 thorpej (ch >= 'A' && ch <= 'F') ||
155 1.33 thorpej (ch >= 'a' && ch <= 'f'));
156 1.33 thorpej }
157 1.33 thorpej
158 1.33 thorpej LIBKERN_INLINE int
159 1.44 matt toupper(int ch)
160 1.33 thorpej {
161 1.33 thorpej if (islower(ch))
162 1.33 thorpej return (ch - 0x20);
163 1.33 thorpej return (ch);
164 1.33 thorpej }
165 1.33 thorpej
166 1.33 thorpej LIBKERN_INLINE int
167 1.44 matt tolower(int ch)
168 1.33 thorpej {
169 1.33 thorpej if (isupper(ch))
170 1.33 thorpej return (ch + 0x20);
171 1.33 thorpej return (ch);
172 1.1 cgd }
173 1.7 christos #endif
174 1.1 cgd
175 1.64 matt #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
176 1.59 dyoung
177 1.9 cgd #ifdef NDEBUG /* tradition! */
178 1.9 cgd #define assert(e) ((void)0)
179 1.10 cgd #else
180 1.9 cgd #ifdef __STDC__
181 1.30 thorpej #define assert(e) (__predict_true((e)) ? (void)0 : \
182 1.71 pooka __kernassert("", __FILE__, __LINE__, #e))
183 1.9 cgd #else
184 1.30 thorpej #define assert(e) (__predict_true((e)) ? (void)0 : \
185 1.71 pooka __kernassert("", __FILE__, __LINE__, "e"))
186 1.9 cgd #endif
187 1.9 cgd #endif
188 1.9 cgd
189 1.61 christos #ifdef __COVERITY__
190 1.61 christos #ifndef DIAGNOSTIC
191 1.61 christos #define DIAGNOSTIC
192 1.61 christos #endif
193 1.61 christos #endif
194 1.61 christos
195 1.80 matt #define CTASSERT(x) _CTASSERT(x, __LINE__)
196 1.80 matt #define _CTASSERT(x, y) __CTASSERT(x, y)
197 1.80 matt #define __CTASSERT(x, y) typedef char __ctassert ## y[(x) ? 1 : -1];
198 1.80 matt
199 1.61 christos #ifndef DIAGNOSTIC
200 1.52 christos #define _DIAGASSERT(a) (void)0
201 1.34 lukem #ifdef lint
202 1.81 matt #define KASSERTMSG(e, msg) /* NOTHING */
203 1.81 matt #define KASSERT(e) /* NOTHING */
204 1.34 lukem #else /* !lint */
205 1.81 matt #define KASSERTMSG(e, msg) ((void)0)
206 1.81 matt #define KASSERT(e) ((void)0)
207 1.34 lukem #endif /* !lint */
208 1.61 christos #else /* DIAGNOSTIC */
209 1.52 christos #define _DIAGASSERT(a) assert(a)
210 1.81 matt #define KASSERTMSG(e, msg) do { \
211 1.81 matt if (__predict_false((e))) \
212 1.81 matt panic msg; \
213 1.81 matt } while (/*CONSTCOND*/ 0)
214 1.9 cgd #ifdef __STDC__
215 1.30 thorpej #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
216 1.71 pooka __kernassert("diagnostic ", __FILE__, __LINE__, #e))
217 1.9 cgd #else
218 1.30 thorpej #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
219 1.71 pooka __kernassert("diagnostic ", __FILE__, __LINE__,"e"))
220 1.9 cgd #endif
221 1.9 cgd #endif
222 1.9 cgd
223 1.9 cgd #ifndef DEBUG
224 1.34 lukem #ifdef lint
225 1.34 lukem #define KDASSERT(e) /* NOTHING */
226 1.34 lukem #else /* lint */
227 1.9 cgd #define KDASSERT(e) ((void)0)
228 1.34 lukem #endif /* lint */
229 1.9 cgd #else
230 1.9 cgd #ifdef __STDC__
231 1.30 thorpej #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
232 1.71 pooka __kernassert("debugging ", __FILE__, __LINE__, #e))
233 1.9 cgd #else
234 1.30 thorpej #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
235 1.71 pooka __kernassert("debugging ", __FILE__, __LINE__, "e"))
236 1.9 cgd #endif
237 1.9 cgd #endif
238 1.53 christos /*
239 1.53 christos * XXX: For compatibility we use SMALL_RANDOM by default.
240 1.53 christos */
241 1.53 christos #define SMALL_RANDOM
242 1.19 thorpej
243 1.31 msaitoh #ifndef offsetof
244 1.47 christos #define offsetof(type, member) \
245 1.47 christos ((size_t)(unsigned long)(&(((type *)0)->member)))
246 1.31 msaitoh #endif
247 1.9 cgd
248 1.73 matt #define MTPRNG_RLEN 624
249 1.73 matt struct mtprng_state {
250 1.73 matt unsigned int mt_idx;
251 1.73 matt uint32_t mt_elem[MTPRNG_RLEN];
252 1.75 matt uint32_t mt_count;
253 1.74 matt uint32_t mt_sparse[3];
254 1.73 matt };
255 1.73 matt
256 1.1 cgd /* Prototypes for non-quad routines. */
257 1.37 perry /* XXX notyet #ifdef _STANDALONE */
258 1.36 simonb int bcmp __P((const void *, const void *, size_t));
259 1.79 christos void bcopy __P((const void *, void *, size_t));
260 1.36 simonb void bzero __P((void *, size_t));
261 1.37 perry /* #endif */
262 1.38 thorpej
263 1.38 thorpej /* Prototypes for which GCC built-ins exist. */
264 1.38 thorpej void *memcpy __P((void *, const void *, size_t));
265 1.38 thorpej int memcmp __P((const void *, const void *, size_t));
266 1.38 thorpej void *memset __P((void *, int, size_t));
267 1.63 matt #if __GNUC_PREREQ__(2, 95) && (__GNUC_PREREQ__(4, 0) || !defined(__vax__))
268 1.38 thorpej #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
269 1.38 thorpej #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
270 1.63 matt #endif
271 1.63 matt #if __GNUC_PREREQ__(2, 95) && !defined(__vax__)
272 1.38 thorpej #define memset(d, v, l) __builtin_memset(d, v, l)
273 1.38 thorpej #endif
274 1.38 thorpej
275 1.38 thorpej char *strcpy __P((char *, const char *));
276 1.38 thorpej int strcmp __P((const char *, const char *));
277 1.38 thorpej size_t strlen __P((const char *));
278 1.56 dyoung char *strsep(char **, const char *);
279 1.38 thorpej #if __GNUC_PREREQ__(2, 95)
280 1.38 thorpej #define strcpy(d, s) __builtin_strcpy(d, s)
281 1.38 thorpej #define strcmp(a, b) __builtin_strcmp(a, b)
282 1.38 thorpej #define strlen(a) __builtin_strlen(a)
283 1.39 thorpej #endif
284 1.39 thorpej
285 1.39 thorpej /* Functions for which we always use built-ins. */
286 1.39 thorpej #ifdef __GNUC__
287 1.39 thorpej #define alloca(s) __builtin_alloca(s)
288 1.38 thorpej #endif
289 1.38 thorpej
290 1.38 thorpej /* These exist in GCC 3.x, but we don't bother. */
291 1.38 thorpej char *strcat __P((char *, const char *));
292 1.38 thorpej char *strncpy __P((char *, const char *, size_t));
293 1.38 thorpej int strncmp __P((const char *, const char *, size_t));
294 1.38 thorpej char *strchr __P((const char *, int));
295 1.38 thorpej char *strrchr __P((const char *, int));
296 1.45 junyoung
297 1.45 junyoung char *strstr __P((const char *, const char *));
298 1.38 thorpej
299 1.42 ragge /*
300 1.42 ragge * ffs is an instruction on vax.
301 1.42 ragge */
302 1.38 thorpej int ffs __P((int));
303 1.69 matt #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
304 1.63 matt #define ffs(x) __builtin_ffs(x)
305 1.41 thorpej #endif
306 1.38 thorpej
307 1.71 pooka void __kernassert __P((const char *, const char *, int, const char *));
308 1.58 kleink unsigned int
309 1.58 kleink bcdtobin __P((unsigned int));
310 1.58 kleink unsigned int
311 1.58 kleink bintobcd __P((unsigned int));
312 1.28 simonb u_int32_t
313 1.52 christos inet_addr __P((const char *));
314 1.52 christos struct in_addr;
315 1.52 christos int inet_aton __P((const char *, struct in_addr *));
316 1.28 simonb char *intoa __P((u_int32_t));
317 1.28 simonb #define inet_ntoa(a) intoa((a).s_addr)
318 1.15 mjacob void *memchr __P((const void *, int, size_t));
319 1.23 perry void *memmove __P((void *, const void *, size_t));
320 1.22 christos int pmatch __P((const char *, const char *, const char **));
321 1.40 itojun u_int32_t arc4random __P((void));
322 1.46 tls void arc4randbytes __P((void *, size_t));
323 1.53 christos #ifndef SMALL_RANDOM
324 1.53 christos void srandom __P((unsigned long));
325 1.53 christos char *initstate __P((unsigned long, char *, size_t));
326 1.53 christos char *setstate __P((char *));
327 1.53 christos #endif /* SMALL_RANDOM */
328 1.53 christos long random __P((void));
329 1.74 matt void mtprng_init32(struct mtprng_state *, uint32_t);
330 1.74 matt void mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
331 1.74 matt uint32_t mtprng_rawrandom(struct mtprng_state *);
332 1.74 matt uint32_t mtprng_random(struct mtprng_state *);
333 1.11 cgd int scanc __P((u_int, const u_char *, const u_char *, int));
334 1.7 christos int skpc __P((int, size_t, u_char *));
335 1.32 thorpej int strcasecmp __P((const char *, const char *));
336 1.48 itojun size_t strlcpy __P((char *, const char *, size_t));
337 1.48 itojun size_t strlcat __P((char *, const char *, size_t));
338 1.28 simonb int strncasecmp __P((const char *, const char *, size_t));
339 1.28 simonb u_long strtoul __P((const char *, char **, int));
340 1.67 thorpej long long strtoll __P((const char *, char **, int));
341 1.67 thorpej unsigned long long strtoull __P((const char *, char **, int));
342 1.62 thorpej uintmax_t strtoumax __P((const char *, char **, int));
343 1.29 simonb #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
344