FreeBSD/Linux Kernel Cross Reference
sys/sys/libkern.h
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)libkern.h 8.1 (Berkeley) 6/10/93
30 * $FreeBSD: releng/9.0/sys/sys/libkern.h 226229 2011-10-10 19:07:55Z jkim $
31 */
32
33 #ifndef _SYS_LIBKERN_H_
34 #define _SYS_LIBKERN_H_
35
36 #include <sys/cdefs.h>
37 #include <sys/types.h>
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #endif
41
42 #ifndef LIBKERN_INLINE
43 #define LIBKERN_INLINE static __inline
44 #define LIBKERN_BODY
45 #endif
46
47 /* BCD conversions. */
48 extern u_char const bcd2bin_data[];
49 extern u_char const bin2bcd_data[];
50 extern char const hex2ascii_data[];
51
52 #define bcd2bin(bcd) (bcd2bin_data[bcd])
53 #define bin2bcd(bin) (bin2bcd_data[bin])
54 #define hex2ascii(hex) (hex2ascii_data[hex])
55
56 static __inline int imax(int a, int b) { return (a > b ? a : b); }
57 static __inline int imin(int a, int b) { return (a < b ? a : b); }
58 static __inline long lmax(long a, long b) { return (a > b ? a : b); }
59 static __inline long lmin(long a, long b) { return (a < b ? a : b); }
60 static __inline u_int max(u_int a, u_int b) { return (a > b ? a : b); }
61 static __inline u_int min(u_int a, u_int b) { return (a < b ? a : b); }
62 static __inline quad_t qmax(quad_t a, quad_t b) { return (a > b ? a : b); }
63 static __inline quad_t qmin(quad_t a, quad_t b) { return (a < b ? a : b); }
64 static __inline u_long ulmax(u_long a, u_long b) { return (a > b ? a : b); }
65 static __inline u_long ulmin(u_long a, u_long b) { return (a < b ? a : b); }
66 static __inline off_t omax(off_t a, off_t b) { return (a > b ? a : b); }
67 static __inline off_t omin(off_t a, off_t b) { return (a < b ? a : b); }
68
69 static __inline int abs(int a) { return (a < 0 ? -a : a); }
70 static __inline long labs(long a) { return (a < 0 ? -a : a); }
71 static __inline quad_t qabs(quad_t a) { return (a < 0 ? -a : a); }
72
73 /* Prototypes for non-quad routines. */
74 struct malloc_type;
75 uint32_t arc4random(void);
76 void arc4rand(void *ptr, u_int len, int reseed);
77 int bcmp(const void *, const void *, size_t);
78 void *bsearch(const void *, const void *, size_t,
79 size_t, int (*)(const void *, const void *));
80 #ifndef HAVE_INLINE_FFS
81 int ffs(int);
82 #endif
83 #ifndef HAVE_INLINE_FFSL
84 int ffsl(long);
85 #endif
86 #ifndef HAVE_INLINE_FLS
87 int fls(int);
88 #endif
89 #ifndef HAVE_INLINE_FLSL
90 int flsl(long);
91 #endif
92 int fnmatch(const char *, const char *, int);
93 void gets(char *, size_t, int);
94 int locc(int, char *, u_int);
95 void *memchr(const void *s, int c, size_t n);
96 int memcmp(const void *b1, const void *b2, size_t len);
97 void qsort(void *base, size_t nmemb, size_t size,
98 int (*compar)(const void *, const void *));
99 void qsort_r(void *base, size_t nmemb, size_t size, void *thunk,
100 int (*compar)(void *, const void *, const void *));
101 u_long random(void);
102 char *index(const char *, int);
103 char *rindex(const char *, int);
104 int scanc(u_int, const u_char *, const u_char *, int);
105 int skpc(int, int, char *);
106 void srandom(u_long);
107 int strcasecmp(const char *, const char *);
108 char *strcat(char * __restrict, const char * __restrict);
109 int strcmp(const char *, const char *);
110 char *strcpy(char * __restrict, const char * __restrict);
111 size_t strcspn(const char * __restrict, const char * __restrict) __pure;
112 char *strdup(const char *__restrict, struct malloc_type *);
113 size_t strlcat(char *, const char *, size_t);
114 size_t strlcpy(char *, const char *, size_t);
115 size_t strlen(const char *);
116 int strncasecmp(const char *, const char *, size_t);
117 int strncmp(const char *, const char *, size_t);
118 char *strncpy(char * __restrict, const char * __restrict, size_t);
119 size_t strnlen(const char *, size_t);
120 char *strsep(char **, const char *delim);
121 size_t strspn(const char *, const char *);
122 char *strstr(const char *, const char *);
123 int strvalid(const char *, size_t);
124
125 extern uint32_t crc32_tab[];
126
127 static __inline uint32_t
128 crc32_raw(const void *buf, size_t size, uint32_t crc)
129 {
130 const uint8_t *p = (const uint8_t *)buf;
131
132 while (size--)
133 crc = crc32_tab[(crc ^ *p++) & 0xFF] ^ (crc >> 8);
134 return (crc);
135 }
136
137 static __inline uint32_t
138 crc32(const void *buf, size_t size)
139 {
140 uint32_t crc;
141
142 crc = crc32_raw(buf, size, ~0U);
143 return (crc ^ ~0U);
144 }
145
146 uint32_t
147 calculate_crc32c(uint32_t crc32c, const unsigned char *buffer,
148 unsigned int length);
149
150
151 LIBKERN_INLINE void *memset(void *, int, size_t);
152 #ifdef LIBKERN_BODY
153 LIBKERN_INLINE void *
154 memset(void *b, int c, size_t len)
155 {
156 char *bb;
157
158 if (c == 0)
159 bzero(b, len);
160 else
161 for (bb = (char *)b; len--; )
162 *bb++ = c;
163 return (b);
164 }
165 #endif
166
167 static __inline char *
168 strchr(const char *p, int ch)
169 {
170 return (index(p, ch));
171 }
172
173 static __inline char *
174 strrchr(const char *p, int ch)
175 {
176 return (rindex(p, ch));
177 }
178
179 /* fnmatch() return values. */
180 #define FNM_NOMATCH 1 /* Match failed. */
181
182 /* fnmatch() flags. */
183 #define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
184 #define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
185 #define FNM_PERIOD 0x04 /* Period must be matched by period. */
186 #define FNM_LEADING_DIR 0x08 /* Ignore /<tail> after Imatch. */
187 #define FNM_CASEFOLD 0x10 /* Case insensitive search. */
188 #define FNM_IGNORECASE FNM_CASEFOLD
189 #define FNM_FILE_NAME FNM_PATHNAME
190
191 /* Visibility of characters in gets() */
192 #define GETS_NOECHO 0 /* Disable echoing of characters. */
193 #define GETS_ECHO 1 /* Enable echoing of characters. */
194 #define GETS_ECHOPASS 2 /* Print a * for every character. */
195
196 #endif /* !_SYS_LIBKERN_H_ */
Cache object: 9d1a6d1267a7b9274f7e60673b51b3ae
|