libkern.h revision 1.113.2.1 1 /* $NetBSD: libkern.h,v 1.113.2.1 2014/04/07 01:10:55 tls 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 #include <sys/inttypes.h>
39 #include <sys/null.h>
40
41 #ifndef LIBKERN_INLINE
42 #define LIBKERN_INLINE static __inline
43 #define LIBKERN_BODY
44 #endif
45
46 LIBKERN_INLINE int imax(int, int) __unused;
47 LIBKERN_INLINE int imin(int, int) __unused;
48 LIBKERN_INLINE u_int max(u_int, u_int) __unused;
49 LIBKERN_INLINE u_int min(u_int, u_int) __unused;
50 LIBKERN_INLINE long lmax(long, long) __unused;
51 LIBKERN_INLINE long lmin(long, long) __unused;
52 LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
53 LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
54 LIBKERN_INLINE int abs(int) __unused;
55 LIBKERN_INLINE long labs(long) __unused;
56 LIBKERN_INLINE long long llabs(long long) __unused;
57 LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
58
59 LIBKERN_INLINE int isspace(int) __unused;
60 LIBKERN_INLINE int isascii(int) __unused;
61 LIBKERN_INLINE int isupper(int) __unused;
62 LIBKERN_INLINE int islower(int) __unused;
63 LIBKERN_INLINE int isalpha(int) __unused;
64 LIBKERN_INLINE int isdigit(int) __unused;
65 LIBKERN_INLINE int isxdigit(int) __unused;
66 LIBKERN_INLINE int toupper(int) __unused;
67 LIBKERN_INLINE int tolower(int) __unused;
68
69 #ifdef LIBKERN_BODY
70 LIBKERN_INLINE int
71 imax(int a, int b)
72 {
73 return (a > b ? a : b);
74 }
75 LIBKERN_INLINE int
76 imin(int a, int b)
77 {
78 return (a < b ? a : b);
79 }
80 LIBKERN_INLINE long
81 lmax(long a, long b)
82 {
83 return (a > b ? a : b);
84 }
85 LIBKERN_INLINE long
86 lmin(long a, long b)
87 {
88 return (a < b ? a : b);
89 }
90 LIBKERN_INLINE u_int
91 max(u_int a, u_int b)
92 {
93 return (a > b ? a : b);
94 }
95 LIBKERN_INLINE u_int
96 min(u_int a, u_int b)
97 {
98 return (a < b ? a : b);
99 }
100 LIBKERN_INLINE u_long
101 ulmax(u_long a, u_long b)
102 {
103 return (a > b ? a : b);
104 }
105 LIBKERN_INLINE u_long
106 ulmin(u_long a, u_long b)
107 {
108 return (a < b ? a : b);
109 }
110
111 LIBKERN_INLINE int
112 abs(int j)
113 {
114 return(j < 0 ? -j : j);
115 }
116
117 LIBKERN_INLINE long
118 labs(long j)
119 {
120 return(j < 0 ? -j : j);
121 }
122
123 LIBKERN_INLINE long long
124 llabs(long long j)
125 {
126 return(j < 0 ? -j : j);
127 }
128
129 LIBKERN_INLINE intmax_t
130 imaxabs(intmax_t j)
131 {
132 return(j < 0 ? -j : j);
133 }
134
135 LIBKERN_INLINE int
136 isspace(int ch)
137 {
138 return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
139 }
140
141 LIBKERN_INLINE int
142 isascii(int ch)
143 {
144 return ((ch & ~0x7f) == 0);
145 }
146
147 LIBKERN_INLINE int
148 isupper(int ch)
149 {
150 return (ch >= 'A' && ch <= 'Z');
151 }
152
153 LIBKERN_INLINE int
154 islower(int ch)
155 {
156 return (ch >= 'a' && ch <= 'z');
157 }
158
159 LIBKERN_INLINE int
160 isalpha(int ch)
161 {
162 return (isupper(ch) || islower(ch));
163 }
164
165 LIBKERN_INLINE int
166 isdigit(int ch)
167 {
168 return (ch >= '0' && ch <= '9');
169 }
170
171 LIBKERN_INLINE int
172 isxdigit(int ch)
173 {
174 return (isdigit(ch) ||
175 (ch >= 'A' && ch <= 'F') ||
176 (ch >= 'a' && ch <= 'f'));
177 }
178
179 LIBKERN_INLINE int
180 toupper(int ch)
181 {
182 if (islower(ch))
183 return (ch - 0x20);
184 return (ch);
185 }
186
187 LIBKERN_INLINE int
188 tolower(int ch)
189 {
190 if (isupper(ch))
191 return (ch + 0x20);
192 return (ch);
193 }
194 #endif
195
196 #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
197
198 #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
199
200 #ifdef NDEBUG /* tradition! */
201 #define assert(e) ((void)0)
202 #else
203 #define assert(e) (__predict_true((e)) ? (void)0 : \
204 kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
205 #endif
206
207 #ifdef __COVERITY__
208 #ifndef DIAGNOSTIC
209 #define DIAGNOSTIC
210 #endif
211 #endif
212
213 #ifndef CTASSERT
214 #define CTASSERT(x) __CTASSERT(x)
215 #endif
216 #ifndef CTASSERT_SIGNED
217 #define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0)
218 #endif
219 #ifndef CTASSERT_UNSIGNED
220 #define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0)
221 #endif
222
223 #ifndef DIAGNOSTIC
224 #define _DIAGASSERT(a) (void)0
225 #ifdef lint
226 #define KASSERTMSG(e, msg, ...) /* NOTHING */
227 #define KASSERT(e) /* NOTHING */
228 #else /* !lint */
229 #define KASSERTMSG(e, msg, ...) ((void)0)
230 #define KASSERT(e) ((void)0)
231 #endif /* !lint */
232 #else /* DIAGNOSTIC */
233 #define _DIAGASSERT(a) assert(a)
234 #define KASSERTMSG(e, msg, ...) \
235 (__predict_true((e)) ? (void)0 : \
236 kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \
237 __FILE__, __LINE__, ## __VA_ARGS__))
238
239 #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
240 kern_assert(__KASSERTSTR, "diagnostic ", #e, \
241 __FILE__, __LINE__))
242 #endif
243
244 #ifndef DEBUG
245 #ifdef lint
246 #define KDASSERTMSG(e,msg, ...) /* NOTHING */
247 #define KDASSERT(e) /* NOTHING */
248 #else /* lint */
249 #define KDASSERTMSG(e,msg, ...) ((void)0)
250 #define KDASSERT(e) ((void)0)
251 #endif /* lint */
252 #else
253 #define KDASSERTMSG(e, msg, ...) \
254 (__predict_true((e)) ? (void)0 : \
255 kern_assert(__KASSERTSTR msg, "debugging ", #e, \
256 __FILE__, __LINE__, ## __VA_ARGS__))
257
258 #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
259 kern_assert(__KASSERTSTR, "debugging ", #e, \
260 __FILE__, __LINE__))
261 #endif
262
263 /*
264 * XXX: For compatibility we use SMALL_RANDOM by default.
265 */
266 #define SMALL_RANDOM
267
268 #ifndef offsetof
269 #if __GNUC_PREREQ__(4, 0)
270 #define offsetof(type, member) __builtin_offsetof(type, member)
271 #else
272 #define offsetof(type, member) \
273 ((size_t)(unsigned long)(&(((type *)0)->member)))
274 #endif
275 #endif
276
277 #define MTPRNG_RLEN 624
278 struct mtprng_state {
279 unsigned int mt_idx;
280 uint32_t mt_elem[MTPRNG_RLEN];
281 uint32_t mt_count;
282 uint32_t mt_sparse[3];
283 };
284
285 /* Prototypes for which GCC built-ins exist. */
286 void *memcpy(void *, const void *, size_t);
287 int memcmp(const void *, const void *, size_t);
288 void *memset(void *, int, size_t);
289 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
290 #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
291 #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
292 #endif
293 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
294 #define memset(d, v, l) __builtin_memset(d, v, l)
295 #endif
296
297 char *strcpy(char *, const char *);
298 int strcmp(const char *, const char *);
299 size_t strlen(const char *);
300 size_t strnlen(const char *, size_t);
301 char *strsep(char **, const char *);
302 #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
303 #define strcpy(d, s) __builtin_strcpy(d, s)
304 #define strcmp(a, b) __builtin_strcmp(a, b)
305 #define strlen(a) __builtin_strlen(a)
306 #endif
307
308 /* Functions for which we always use built-ins. */
309 #ifdef __GNUC__
310 #define alloca(s) __builtin_alloca(s)
311 #endif
312
313 /* These exist in GCC 3.x, but we don't bother. */
314 char *strcat(char *, const char *);
315 char *strncpy(char *, const char *, size_t);
316 char *strncat(char *, const char *, size_t);
317 int strncmp(const char *, const char *, size_t);
318 char *strchr(const char *, int);
319 char *strrchr(const char *, int);
320
321 char *strstr(const char *, const char *);
322
323 /*
324 * ffs is an instruction on vax.
325 */
326 int ffs(int);
327 #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
328 #define ffs(x) __builtin_ffs(x)
329 #endif
330
331 void kern_assert(const char *, ...)
332 __attribute__((__format__(__printf__, 1, 2)));
333 unsigned int
334 bcdtobin(unsigned int);
335 unsigned int
336 bintobcd(unsigned int);
337 u_int32_t
338 inet_addr(const char *);
339 struct in_addr;
340 int inet_aton(const char *, struct in_addr *);
341 char *intoa(u_int32_t);
342 #define inet_ntoa(a) intoa((a).s_addr)
343 void *memchr(const void *, int, size_t);
344 void *memmove(void *, const void *, size_t);
345 int pmatch(const char *, const char *, const char **);
346 #ifndef SMALL_RANDOM
347 void srandom(unsigned long);
348 char *initstate(unsigned long, char *, size_t);
349 char *setstate(char *);
350 #endif /* SMALL_RANDOM */
351 long random(void);
352 void mi_vector_hash(const void * __restrict, size_t, uint32_t,
353 uint32_t[3]);
354 void mtprng_init32(struct mtprng_state *, uint32_t);
355 void mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
356 uint32_t mtprng_rawrandom(struct mtprng_state *);
357 uint32_t mtprng_random(struct mtprng_state *);
358 int scanc(u_int, const u_char *, const u_char *, int);
359 int skpc(int, size_t, u_char *);
360 int strcasecmp(const char *, const char *);
361 size_t strlcpy(char *, const char *, size_t);
362 size_t strlcat(char *, const char *, size_t);
363 int strncasecmp(const char *, const char *, size_t);
364 u_long strtoul(const char *, char **, int);
365 long long strtoll(const char *, char **, int);
366 unsigned long long strtoull(const char *, char **, int);
367 intmax_t strtoimax(const char *, char **, int);
368 uintmax_t strtoumax(const char *, char **, int);
369 int snprintb(char *, size_t, const char *, uint64_t);
370 int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
371 int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
372 void *);
373 uint32_t crc32(uint32_t, const uint8_t *, size_t);
374 unsigned int popcount(unsigned int) __constfunc;
375 unsigned int popcountl(unsigned long) __constfunc;
376 unsigned int popcountll(unsigned long long) __constfunc;
377 unsigned int popcount32(uint32_t) __constfunc;
378 unsigned int popcount64(uint64_t) __constfunc;
379
380 void *explicit_memset(void *, int, size_t);
381 int consttime_memequal(const void *, const void *, size_t);
382
383 /*
384 * LZF hashtable/state size: on uncompressible data and on a system with
385 * a sufficiently large d-cache, a larger table produces a considerable
386 * speed benefit. On systems with small memory and caches, however...
387 */
388 #if defined(__vax__) || defined(__m68k__)
389 #define LZF_HLOG 14
390 #else
391 #define LZF_HLOG 15
392 #endif
393 typedef const uint8_t *LZF_STATE[1 << LZF_HLOG];
394
395 unsigned int lzf_compress_r (const void *const, unsigned int, void *,
396 unsigned int, LZF_STATE);
397 unsigned int lzf_decompress (const void *const, unsigned int, void *,
398 unsigned int);
399
400 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
401