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