1*0a6a1f1dSLionel Sambuc /* $NetBSD: libkern.h,v 1.121 2015/08/30 07:55:45 uebayasi Exp $ */
258a2b000SEvgeniy Ivanov
358a2b000SEvgeniy Ivanov /*-
458a2b000SEvgeniy Ivanov * Copyright (c) 1992, 1993
558a2b000SEvgeniy Ivanov * The Regents of the University of California. All rights reserved.
658a2b000SEvgeniy Ivanov *
758a2b000SEvgeniy Ivanov * Redistribution and use in source and binary forms, with or without
858a2b000SEvgeniy Ivanov * modification, are permitted provided that the following conditions
958a2b000SEvgeniy Ivanov * are met:
1058a2b000SEvgeniy Ivanov * 1. Redistributions of source code must retain the above copyright
1158a2b000SEvgeniy Ivanov * notice, this list of conditions and the following disclaimer.
1258a2b000SEvgeniy Ivanov * 2. Redistributions in binary form must reproduce the above copyright
1358a2b000SEvgeniy Ivanov * notice, this list of conditions and the following disclaimer in the
1458a2b000SEvgeniy Ivanov * documentation and/or other materials provided with the distribution.
1558a2b000SEvgeniy Ivanov * 3. Neither the name of the University nor the names of its contributors
1658a2b000SEvgeniy Ivanov * may be used to endorse or promote products derived from this software
1758a2b000SEvgeniy Ivanov * without specific prior written permission.
1858a2b000SEvgeniy Ivanov *
1958a2b000SEvgeniy Ivanov * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058a2b000SEvgeniy Ivanov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158a2b000SEvgeniy Ivanov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258a2b000SEvgeniy Ivanov * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358a2b000SEvgeniy Ivanov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458a2b000SEvgeniy Ivanov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558a2b000SEvgeniy Ivanov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658a2b000SEvgeniy Ivanov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758a2b000SEvgeniy Ivanov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858a2b000SEvgeniy Ivanov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958a2b000SEvgeniy Ivanov * SUCH DAMAGE.
3058a2b000SEvgeniy Ivanov *
3158a2b000SEvgeniy Ivanov * @(#)libkern.h 8.2 (Berkeley) 8/5/94
3258a2b000SEvgeniy Ivanov */
3358a2b000SEvgeniy Ivanov
3458a2b000SEvgeniy Ivanov #ifndef _LIB_LIBKERN_LIBKERN_H_
3558a2b000SEvgeniy Ivanov #define _LIB_LIBKERN_LIBKERN_H_
3658a2b000SEvgeniy Ivanov
37*0a6a1f1dSLionel Sambuc #ifdef _KERNEL_OPT
38*0a6a1f1dSLionel Sambuc #include "opt_diagnostic.h"
39*0a6a1f1dSLionel Sambuc #endif
40*0a6a1f1dSLionel Sambuc
4158a2b000SEvgeniy Ivanov #include <sys/types.h>
4258a2b000SEvgeniy Ivanov #include <sys/inttypes.h>
4358a2b000SEvgeniy Ivanov #include <sys/null.h>
4458a2b000SEvgeniy Ivanov
4558a2b000SEvgeniy Ivanov #ifndef LIBKERN_INLINE
4658a2b000SEvgeniy Ivanov #define LIBKERN_INLINE static __inline
4758a2b000SEvgeniy Ivanov #define LIBKERN_BODY
4858a2b000SEvgeniy Ivanov #endif
4958a2b000SEvgeniy Ivanov
5058a2b000SEvgeniy Ivanov LIBKERN_INLINE int imax(int, int) __unused;
5158a2b000SEvgeniy Ivanov LIBKERN_INLINE int imin(int, int) __unused;
5258a2b000SEvgeniy Ivanov LIBKERN_INLINE u_int max(u_int, u_int) __unused;
5358a2b000SEvgeniy Ivanov LIBKERN_INLINE u_int min(u_int, u_int) __unused;
5458a2b000SEvgeniy Ivanov LIBKERN_INLINE long lmax(long, long) __unused;
5558a2b000SEvgeniy Ivanov LIBKERN_INLINE long lmin(long, long) __unused;
5658a2b000SEvgeniy Ivanov LIBKERN_INLINE u_long ulmax(u_long, u_long) __unused;
5758a2b000SEvgeniy Ivanov LIBKERN_INLINE u_long ulmin(u_long, u_long) __unused;
5858a2b000SEvgeniy Ivanov LIBKERN_INLINE int abs(int) __unused;
59*0a6a1f1dSLionel Sambuc LIBKERN_INLINE long labs(long) __unused;
60*0a6a1f1dSLionel Sambuc LIBKERN_INLINE long long llabs(long long) __unused;
61*0a6a1f1dSLionel Sambuc LIBKERN_INLINE intmax_t imaxabs(intmax_t) __unused;
6258a2b000SEvgeniy Ivanov
6358a2b000SEvgeniy Ivanov LIBKERN_INLINE int isspace(int) __unused;
6458a2b000SEvgeniy Ivanov LIBKERN_INLINE int isascii(int) __unused;
6558a2b000SEvgeniy Ivanov LIBKERN_INLINE int isupper(int) __unused;
6658a2b000SEvgeniy Ivanov LIBKERN_INLINE int islower(int) __unused;
6758a2b000SEvgeniy Ivanov LIBKERN_INLINE int isalpha(int) __unused;
68*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int isalnum(int) __unused;
6958a2b000SEvgeniy Ivanov LIBKERN_INLINE int isdigit(int) __unused;
7058a2b000SEvgeniy Ivanov LIBKERN_INLINE int isxdigit(int) __unused;
71*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int iscntrl(int) __unused;
72*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int isgraph(int) __unused;
73*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int isprint(int) __unused;
74*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int ispunct(int) __unused;
7558a2b000SEvgeniy Ivanov LIBKERN_INLINE int toupper(int) __unused;
7658a2b000SEvgeniy Ivanov LIBKERN_INLINE int tolower(int) __unused;
7758a2b000SEvgeniy Ivanov
7858a2b000SEvgeniy Ivanov #ifdef LIBKERN_BODY
7958a2b000SEvgeniy Ivanov LIBKERN_INLINE int
imax(int a,int b)8058a2b000SEvgeniy Ivanov imax(int a, int b)
8158a2b000SEvgeniy Ivanov {
8258a2b000SEvgeniy Ivanov return (a > b ? a : b);
8358a2b000SEvgeniy Ivanov }
8458a2b000SEvgeniy Ivanov LIBKERN_INLINE int
imin(int a,int b)8558a2b000SEvgeniy Ivanov imin(int a, int b)
8658a2b000SEvgeniy Ivanov {
8758a2b000SEvgeniy Ivanov return (a < b ? a : b);
8858a2b000SEvgeniy Ivanov }
8958a2b000SEvgeniy Ivanov LIBKERN_INLINE long
lmax(long a,long b)9058a2b000SEvgeniy Ivanov lmax(long a, long b)
9158a2b000SEvgeniy Ivanov {
9258a2b000SEvgeniy Ivanov return (a > b ? a : b);
9358a2b000SEvgeniy Ivanov }
9458a2b000SEvgeniy Ivanov LIBKERN_INLINE long
lmin(long a,long b)9558a2b000SEvgeniy Ivanov lmin(long a, long b)
9658a2b000SEvgeniy Ivanov {
9758a2b000SEvgeniy Ivanov return (a < b ? a : b);
9858a2b000SEvgeniy Ivanov }
9958a2b000SEvgeniy Ivanov LIBKERN_INLINE u_int
max(u_int a,u_int b)10058a2b000SEvgeniy Ivanov max(u_int a, u_int b)
10158a2b000SEvgeniy Ivanov {
10258a2b000SEvgeniy Ivanov return (a > b ? a : b);
10358a2b000SEvgeniy Ivanov }
10458a2b000SEvgeniy Ivanov LIBKERN_INLINE u_int
min(u_int a,u_int b)10558a2b000SEvgeniy Ivanov min(u_int a, u_int b)
10658a2b000SEvgeniy Ivanov {
10758a2b000SEvgeniy Ivanov return (a < b ? a : b);
10858a2b000SEvgeniy Ivanov }
10958a2b000SEvgeniy Ivanov LIBKERN_INLINE u_long
ulmax(u_long a,u_long b)11058a2b000SEvgeniy Ivanov ulmax(u_long a, u_long b)
11158a2b000SEvgeniy Ivanov {
11258a2b000SEvgeniy Ivanov return (a > b ? a : b);
11358a2b000SEvgeniy Ivanov }
11458a2b000SEvgeniy Ivanov LIBKERN_INLINE u_long
ulmin(u_long a,u_long b)11558a2b000SEvgeniy Ivanov ulmin(u_long a, u_long b)
11658a2b000SEvgeniy Ivanov {
11758a2b000SEvgeniy Ivanov return (a < b ? a : b);
11858a2b000SEvgeniy Ivanov }
11958a2b000SEvgeniy Ivanov
12058a2b000SEvgeniy Ivanov LIBKERN_INLINE int
abs(int j)12158a2b000SEvgeniy Ivanov abs(int j)
12258a2b000SEvgeniy Ivanov {
12358a2b000SEvgeniy Ivanov return(j < 0 ? -j : j);
12458a2b000SEvgeniy Ivanov }
12558a2b000SEvgeniy Ivanov
126*0a6a1f1dSLionel Sambuc LIBKERN_INLINE long
labs(long j)127*0a6a1f1dSLionel Sambuc labs(long j)
128*0a6a1f1dSLionel Sambuc {
129*0a6a1f1dSLionel Sambuc return(j < 0 ? -j : j);
130*0a6a1f1dSLionel Sambuc }
131*0a6a1f1dSLionel Sambuc
132*0a6a1f1dSLionel Sambuc LIBKERN_INLINE long long
llabs(long long j)133*0a6a1f1dSLionel Sambuc llabs(long long j)
134*0a6a1f1dSLionel Sambuc {
135*0a6a1f1dSLionel Sambuc return(j < 0 ? -j : j);
136*0a6a1f1dSLionel Sambuc }
137*0a6a1f1dSLionel Sambuc
138*0a6a1f1dSLionel Sambuc LIBKERN_INLINE intmax_t
imaxabs(intmax_t j)139*0a6a1f1dSLionel Sambuc imaxabs(intmax_t j)
140*0a6a1f1dSLionel Sambuc {
141*0a6a1f1dSLionel Sambuc return(j < 0 ? -j : j);
142*0a6a1f1dSLionel Sambuc }
143*0a6a1f1dSLionel Sambuc
14458a2b000SEvgeniy Ivanov LIBKERN_INLINE int
isspace(int ch)14558a2b000SEvgeniy Ivanov isspace(int ch)
14658a2b000SEvgeniy Ivanov {
14758a2b000SEvgeniy Ivanov return (ch == ' ' || (ch >= '\t' && ch <= '\r'));
14858a2b000SEvgeniy Ivanov }
14958a2b000SEvgeniy Ivanov
15058a2b000SEvgeniy Ivanov LIBKERN_INLINE int
isascii(int ch)15158a2b000SEvgeniy Ivanov isascii(int ch)
15258a2b000SEvgeniy Ivanov {
15358a2b000SEvgeniy Ivanov return ((ch & ~0x7f) == 0);
15458a2b000SEvgeniy Ivanov }
15558a2b000SEvgeniy Ivanov
15658a2b000SEvgeniy Ivanov LIBKERN_INLINE int
isupper(int ch)15758a2b000SEvgeniy Ivanov isupper(int ch)
15858a2b000SEvgeniy Ivanov {
15958a2b000SEvgeniy Ivanov return (ch >= 'A' && ch <= 'Z');
16058a2b000SEvgeniy Ivanov }
16158a2b000SEvgeniy Ivanov
16258a2b000SEvgeniy Ivanov LIBKERN_INLINE int
islower(int ch)16358a2b000SEvgeniy Ivanov islower(int ch)
16458a2b000SEvgeniy Ivanov {
16558a2b000SEvgeniy Ivanov return (ch >= 'a' && ch <= 'z');
16658a2b000SEvgeniy Ivanov }
16758a2b000SEvgeniy Ivanov
16858a2b000SEvgeniy Ivanov LIBKERN_INLINE int
isalpha(int ch)16958a2b000SEvgeniy Ivanov isalpha(int ch)
17058a2b000SEvgeniy Ivanov {
17158a2b000SEvgeniy Ivanov return (isupper(ch) || islower(ch));
17258a2b000SEvgeniy Ivanov }
17358a2b000SEvgeniy Ivanov
17458a2b000SEvgeniy Ivanov LIBKERN_INLINE int
isalnum(int ch)175*0a6a1f1dSLionel Sambuc isalnum(int ch)
176*0a6a1f1dSLionel Sambuc {
177*0a6a1f1dSLionel Sambuc return (isalpha(ch) || isdigit(ch));
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int
isdigit(int ch)18158a2b000SEvgeniy Ivanov isdigit(int ch)
18258a2b000SEvgeniy Ivanov {
18358a2b000SEvgeniy Ivanov return (ch >= '0' && ch <= '9');
18458a2b000SEvgeniy Ivanov }
18558a2b000SEvgeniy Ivanov
18658a2b000SEvgeniy Ivanov LIBKERN_INLINE int
isxdigit(int ch)18758a2b000SEvgeniy Ivanov isxdigit(int ch)
18858a2b000SEvgeniy Ivanov {
18958a2b000SEvgeniy Ivanov return (isdigit(ch) ||
19058a2b000SEvgeniy Ivanov (ch >= 'A' && ch <= 'F') ||
19158a2b000SEvgeniy Ivanov (ch >= 'a' && ch <= 'f'));
19258a2b000SEvgeniy Ivanov }
19358a2b000SEvgeniy Ivanov
19458a2b000SEvgeniy Ivanov LIBKERN_INLINE int
iscntrl(int ch)195*0a6a1f1dSLionel Sambuc iscntrl(int ch)
196*0a6a1f1dSLionel Sambuc {
197*0a6a1f1dSLionel Sambuc return ((ch >= 0x00 && ch <= 0x1F) || ch == 0x7F);
198*0a6a1f1dSLionel Sambuc }
199*0a6a1f1dSLionel Sambuc
200*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int
isgraph(int ch)201*0a6a1f1dSLionel Sambuc isgraph(int ch)
202*0a6a1f1dSLionel Sambuc {
203*0a6a1f1dSLionel Sambuc return (ch != ' ' && isprint(ch));
204*0a6a1f1dSLionel Sambuc }
205*0a6a1f1dSLionel Sambuc
206*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int
isprint(int ch)207*0a6a1f1dSLionel Sambuc isprint(int ch)
208*0a6a1f1dSLionel Sambuc {
209*0a6a1f1dSLionel Sambuc return (ch >= 0x20 && ch <= 0x7E);
210*0a6a1f1dSLionel Sambuc }
211*0a6a1f1dSLionel Sambuc
212*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int
ispunct(int ch)213*0a6a1f1dSLionel Sambuc ispunct(int ch)
214*0a6a1f1dSLionel Sambuc {
215*0a6a1f1dSLionel Sambuc return (isprint(ch) && ch != ' ' && !isalnum(ch));
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc LIBKERN_INLINE int
toupper(int ch)21958a2b000SEvgeniy Ivanov toupper(int ch)
22058a2b000SEvgeniy Ivanov {
22158a2b000SEvgeniy Ivanov if (islower(ch))
22258a2b000SEvgeniy Ivanov return (ch - 0x20);
22358a2b000SEvgeniy Ivanov return (ch);
22458a2b000SEvgeniy Ivanov }
22558a2b000SEvgeniy Ivanov
22658a2b000SEvgeniy Ivanov LIBKERN_INLINE int
tolower(int ch)22758a2b000SEvgeniy Ivanov tolower(int ch)
22858a2b000SEvgeniy Ivanov {
22958a2b000SEvgeniy Ivanov if (isupper(ch))
23058a2b000SEvgeniy Ivanov return (ch + 0x20);
23158a2b000SEvgeniy Ivanov return (ch);
23258a2b000SEvgeniy Ivanov }
23358a2b000SEvgeniy Ivanov #endif
23458a2b000SEvgeniy Ivanov
23558a2b000SEvgeniy Ivanov #define __NULL_STMT do { } while (/* CONSTCOND */ 0)
23658a2b000SEvgeniy Ivanov
23758a2b000SEvgeniy Ivanov #define __KASSERTSTR "kernel %sassertion \"%s\" failed: file \"%s\", line %d "
23858a2b000SEvgeniy Ivanov
23958a2b000SEvgeniy Ivanov #ifdef NDEBUG /* tradition! */
24058a2b000SEvgeniy Ivanov #define assert(e) ((void)0)
24158a2b000SEvgeniy Ivanov #else
24258a2b000SEvgeniy Ivanov #define assert(e) (__predict_true((e)) ? (void)0 : \
24358a2b000SEvgeniy Ivanov kern_assert(__KASSERTSTR, "", #e, __FILE__, __LINE__))
24458a2b000SEvgeniy Ivanov #endif
24558a2b000SEvgeniy Ivanov
24658a2b000SEvgeniy Ivanov #ifdef __COVERITY__
24758a2b000SEvgeniy Ivanov #ifndef DIAGNOSTIC
24858a2b000SEvgeniy Ivanov #define DIAGNOSTIC
24958a2b000SEvgeniy Ivanov #endif
25058a2b000SEvgeniy Ivanov #endif
25158a2b000SEvgeniy Ivanov
252*0a6a1f1dSLionel Sambuc #ifndef CTASSERT
25358a2b000SEvgeniy Ivanov #define CTASSERT(x) __CTASSERT(x)
254*0a6a1f1dSLionel Sambuc #endif
255*0a6a1f1dSLionel Sambuc #ifndef CTASSERT_SIGNED
25684d9c625SLionel Sambuc #define CTASSERT_SIGNED(x) __CTASSERT(((typeof(x))-1) < 0)
257*0a6a1f1dSLionel Sambuc #endif
258*0a6a1f1dSLionel Sambuc #ifndef CTASSERT_UNSIGNED
25984d9c625SLionel Sambuc #define CTASSERT_UNSIGNED(x) __CTASSERT(((typeof(x))-1) >= 0)
260*0a6a1f1dSLionel Sambuc #endif
26158a2b000SEvgeniy Ivanov
26258a2b000SEvgeniy Ivanov #ifndef DIAGNOSTIC
26358a2b000SEvgeniy Ivanov #define _DIAGASSERT(a) (void)0
26458a2b000SEvgeniy Ivanov #ifdef lint
26558a2b000SEvgeniy Ivanov #define KASSERTMSG(e, msg, ...) /* NOTHING */
26658a2b000SEvgeniy Ivanov #define KASSERT(e) /* NOTHING */
26758a2b000SEvgeniy Ivanov #else /* !lint */
26858a2b000SEvgeniy Ivanov #define KASSERTMSG(e, msg, ...) ((void)0)
26958a2b000SEvgeniy Ivanov #define KASSERT(e) ((void)0)
27058a2b000SEvgeniy Ivanov #endif /* !lint */
27158a2b000SEvgeniy Ivanov #else /* DIAGNOSTIC */
27258a2b000SEvgeniy Ivanov #define _DIAGASSERT(a) assert(a)
27358a2b000SEvgeniy Ivanov #define KASSERTMSG(e, msg, ...) \
27458a2b000SEvgeniy Ivanov (__predict_true((e)) ? (void)0 : \
27558a2b000SEvgeniy Ivanov kern_assert(__KASSERTSTR msg, "diagnostic ", #e, \
27658a2b000SEvgeniy Ivanov __FILE__, __LINE__, ## __VA_ARGS__))
27758a2b000SEvgeniy Ivanov
27858a2b000SEvgeniy Ivanov #define KASSERT(e) (__predict_true((e)) ? (void)0 : \
27958a2b000SEvgeniy Ivanov kern_assert(__KASSERTSTR, "diagnostic ", #e, \
28058a2b000SEvgeniy Ivanov __FILE__, __LINE__))
28158a2b000SEvgeniy Ivanov #endif
28258a2b000SEvgeniy Ivanov
28358a2b000SEvgeniy Ivanov #ifndef DEBUG
28458a2b000SEvgeniy Ivanov #ifdef lint
28558a2b000SEvgeniy Ivanov #define KDASSERTMSG(e,msg, ...) /* NOTHING */
28658a2b000SEvgeniy Ivanov #define KDASSERT(e) /* NOTHING */
28758a2b000SEvgeniy Ivanov #else /* lint */
28858a2b000SEvgeniy Ivanov #define KDASSERTMSG(e,msg, ...) ((void)0)
28958a2b000SEvgeniy Ivanov #define KDASSERT(e) ((void)0)
29058a2b000SEvgeniy Ivanov #endif /* lint */
29158a2b000SEvgeniy Ivanov #else
29258a2b000SEvgeniy Ivanov #define KDASSERTMSG(e, msg, ...) \
29358a2b000SEvgeniy Ivanov (__predict_true((e)) ? (void)0 : \
29458a2b000SEvgeniy Ivanov kern_assert(__KASSERTSTR msg, "debugging ", #e, \
29558a2b000SEvgeniy Ivanov __FILE__, __LINE__, ## __VA_ARGS__))
29658a2b000SEvgeniy Ivanov
29758a2b000SEvgeniy Ivanov #define KDASSERT(e) (__predict_true((e)) ? (void)0 : \
29858a2b000SEvgeniy Ivanov kern_assert(__KASSERTSTR, "debugging ", #e, \
29958a2b000SEvgeniy Ivanov __FILE__, __LINE__))
30058a2b000SEvgeniy Ivanov #endif
30158a2b000SEvgeniy Ivanov
30258a2b000SEvgeniy Ivanov /*
30358a2b000SEvgeniy Ivanov * XXX: For compatibility we use SMALL_RANDOM by default.
30458a2b000SEvgeniy Ivanov */
30558a2b000SEvgeniy Ivanov #define SMALL_RANDOM
30658a2b000SEvgeniy Ivanov
30758a2b000SEvgeniy Ivanov #ifndef offsetof
30858a2b000SEvgeniy Ivanov #if __GNUC_PREREQ__(4, 0)
30958a2b000SEvgeniy Ivanov #define offsetof(type, member) __builtin_offsetof(type, member)
31058a2b000SEvgeniy Ivanov #else
31158a2b000SEvgeniy Ivanov #define offsetof(type, member) \
31258a2b000SEvgeniy Ivanov ((size_t)(unsigned long)(&(((type *)0)->member)))
31358a2b000SEvgeniy Ivanov #endif
31458a2b000SEvgeniy Ivanov #endif
31558a2b000SEvgeniy Ivanov
316*0a6a1f1dSLionel Sambuc /*
317*0a6a1f1dSLionel Sambuc * Return the container of an embedded struct. Given x = &c->f,
318*0a6a1f1dSLionel Sambuc * container_of(x, T, f) yields c, where T is the type of c. Example:
319*0a6a1f1dSLionel Sambuc *
320*0a6a1f1dSLionel Sambuc * struct foo { ... };
321*0a6a1f1dSLionel Sambuc * struct bar {
322*0a6a1f1dSLionel Sambuc * int b_x;
323*0a6a1f1dSLionel Sambuc * struct foo b_foo;
324*0a6a1f1dSLionel Sambuc * ...
325*0a6a1f1dSLionel Sambuc * };
326*0a6a1f1dSLionel Sambuc *
327*0a6a1f1dSLionel Sambuc * struct bar b;
328*0a6a1f1dSLionel Sambuc * struct foo *fp = b.b_foo;
329*0a6a1f1dSLionel Sambuc *
330*0a6a1f1dSLionel Sambuc * Now we can get at b from fp by:
331*0a6a1f1dSLionel Sambuc *
332*0a6a1f1dSLionel Sambuc * struct bar *bp = container_of(fp, struct bar, b_foo);
333*0a6a1f1dSLionel Sambuc *
334*0a6a1f1dSLionel Sambuc * The 0*sizeof((PTR) - ...) causes the compiler to warn if the type of
335*0a6a1f1dSLionel Sambuc * *fp does not match the type of struct bar::b_foo.
336*0a6a1f1dSLionel Sambuc * We skip the validation for coverity runs to avoid warnings.
337*0a6a1f1dSLionel Sambuc */
338*0a6a1f1dSLionel Sambuc #ifdef __COVERITY__
339*0a6a1f1dSLionel Sambuc #define __validate_container_of(PTR, TYPE, FIELD) 0
340*0a6a1f1dSLionel Sambuc #else
341*0a6a1f1dSLionel Sambuc #define __validate_container_of(PTR, TYPE, FIELD) \
342*0a6a1f1dSLionel Sambuc (0 * sizeof((PTR) - &((TYPE *)(((char *)(PTR)) - \
343*0a6a1f1dSLionel Sambuc offsetof(TYPE, FIELD)))->FIELD))
344*0a6a1f1dSLionel Sambuc #endif
345*0a6a1f1dSLionel Sambuc
346*0a6a1f1dSLionel Sambuc #define container_of(PTR, TYPE, FIELD) \
347*0a6a1f1dSLionel Sambuc ((TYPE *)(((char *)(PTR)) - offsetof(TYPE, FIELD)) \
348*0a6a1f1dSLionel Sambuc + __validate_container_of(PTR, TYPE, FIELD))
349*0a6a1f1dSLionel Sambuc
35058a2b000SEvgeniy Ivanov #define MTPRNG_RLEN 624
35158a2b000SEvgeniy Ivanov struct mtprng_state {
35258a2b000SEvgeniy Ivanov unsigned int mt_idx;
35358a2b000SEvgeniy Ivanov uint32_t mt_elem[MTPRNG_RLEN];
35458a2b000SEvgeniy Ivanov uint32_t mt_count;
35558a2b000SEvgeniy Ivanov uint32_t mt_sparse[3];
35658a2b000SEvgeniy Ivanov };
35758a2b000SEvgeniy Ivanov
35858a2b000SEvgeniy Ivanov /* Prototypes for which GCC built-ins exist. */
35958a2b000SEvgeniy Ivanov void *memcpy(void *, const void *, size_t);
36058a2b000SEvgeniy Ivanov int memcmp(const void *, const void *, size_t);
36158a2b000SEvgeniy Ivanov void *memset(void *, int, size_t);
36258a2b000SEvgeniy Ivanov #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
36358a2b000SEvgeniy Ivanov #define memcpy(d, s, l) __builtin_memcpy(d, s, l)
36458a2b000SEvgeniy Ivanov #define memcmp(a, b, l) __builtin_memcmp(a, b, l)
36558a2b000SEvgeniy Ivanov #endif
36658a2b000SEvgeniy Ivanov #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
36758a2b000SEvgeniy Ivanov #define memset(d, v, l) __builtin_memset(d, v, l)
36858a2b000SEvgeniy Ivanov #endif
36958a2b000SEvgeniy Ivanov
37058a2b000SEvgeniy Ivanov char *strcpy(char *, const char *);
37158a2b000SEvgeniy Ivanov int strcmp(const char *, const char *);
37258a2b000SEvgeniy Ivanov size_t strlen(const char *);
37358a2b000SEvgeniy Ivanov size_t strnlen(const char *, size_t);
37458a2b000SEvgeniy Ivanov char *strsep(char **, const char *);
37558a2b000SEvgeniy Ivanov #if __GNUC_PREREQ__(2, 95) && !defined(_STANDALONE)
37658a2b000SEvgeniy Ivanov #define strcpy(d, s) __builtin_strcpy(d, s)
37758a2b000SEvgeniy Ivanov #define strcmp(a, b) __builtin_strcmp(a, b)
37858a2b000SEvgeniy Ivanov #define strlen(a) __builtin_strlen(a)
37958a2b000SEvgeniy Ivanov #endif
38058a2b000SEvgeniy Ivanov
38158a2b000SEvgeniy Ivanov /* Functions for which we always use built-ins. */
38258a2b000SEvgeniy Ivanov #ifdef __GNUC__
38358a2b000SEvgeniy Ivanov #define alloca(s) __builtin_alloca(s)
38458a2b000SEvgeniy Ivanov #endif
38558a2b000SEvgeniy Ivanov
38658a2b000SEvgeniy Ivanov /* These exist in GCC 3.x, but we don't bother. */
38758a2b000SEvgeniy Ivanov char *strcat(char *, const char *);
388*0a6a1f1dSLionel Sambuc size_t strcspn(const char *, const char *);
38958a2b000SEvgeniy Ivanov char *strncpy(char *, const char *, size_t);
390*0a6a1f1dSLionel Sambuc char *strncat(char *, const char *, size_t);
39158a2b000SEvgeniy Ivanov int strncmp(const char *, const char *, size_t);
39258a2b000SEvgeniy Ivanov char *strchr(const char *, int);
39358a2b000SEvgeniy Ivanov char *strrchr(const char *, int);
39458a2b000SEvgeniy Ivanov char *strstr(const char *, const char *);
395*0a6a1f1dSLionel Sambuc char *strpbrk(const char *, const char *);
396*0a6a1f1dSLionel Sambuc size_t strspn(const char *, const char *);
39758a2b000SEvgeniy Ivanov
39858a2b000SEvgeniy Ivanov /*
39958a2b000SEvgeniy Ivanov * ffs is an instruction on vax.
40058a2b000SEvgeniy Ivanov */
40158a2b000SEvgeniy Ivanov int ffs(int);
40258a2b000SEvgeniy Ivanov #if __GNUC_PREREQ__(2, 95) && (!defined(__vax__) || __GNUC_PREREQ__(4,1))
40358a2b000SEvgeniy Ivanov #define ffs(x) __builtin_ffs(x)
40458a2b000SEvgeniy Ivanov #endif
40558a2b000SEvgeniy Ivanov
40658a2b000SEvgeniy Ivanov void kern_assert(const char *, ...)
40758a2b000SEvgeniy Ivanov __attribute__((__format__(__printf__, 1, 2)));
40858a2b000SEvgeniy Ivanov u_int32_t
40958a2b000SEvgeniy Ivanov inet_addr(const char *);
41058a2b000SEvgeniy Ivanov struct in_addr;
41158a2b000SEvgeniy Ivanov int inet_aton(const char *, struct in_addr *);
41258a2b000SEvgeniy Ivanov char *intoa(u_int32_t);
41358a2b000SEvgeniy Ivanov #define inet_ntoa(a) intoa((a).s_addr)
41458a2b000SEvgeniy Ivanov void *memchr(const void *, int, size_t);
41558a2b000SEvgeniy Ivanov void *memmove(void *, const void *, size_t);
41658a2b000SEvgeniy Ivanov int pmatch(const char *, const char *, const char **);
41758a2b000SEvgeniy Ivanov #ifndef SMALL_RANDOM
41858a2b000SEvgeniy Ivanov void srandom(unsigned long);
41958a2b000SEvgeniy Ivanov char *initstate(unsigned long, char *, size_t);
42058a2b000SEvgeniy Ivanov char *setstate(char *);
42158a2b000SEvgeniy Ivanov #endif /* SMALL_RANDOM */
42258a2b000SEvgeniy Ivanov long random(void);
423*0a6a1f1dSLionel Sambuc void mi_vector_hash(const void * __restrict, size_t, uint32_t,
424*0a6a1f1dSLionel Sambuc uint32_t[3]);
42558a2b000SEvgeniy Ivanov void mtprng_init32(struct mtprng_state *, uint32_t);
42658a2b000SEvgeniy Ivanov void mtprng_initarray(struct mtprng_state *, const uint32_t *, size_t);
42758a2b000SEvgeniy Ivanov uint32_t mtprng_rawrandom(struct mtprng_state *);
42858a2b000SEvgeniy Ivanov uint32_t mtprng_random(struct mtprng_state *);
42958a2b000SEvgeniy Ivanov int scanc(u_int, const u_char *, const u_char *, int);
43058a2b000SEvgeniy Ivanov int skpc(int, size_t, u_char *);
43158a2b000SEvgeniy Ivanov int strcasecmp(const char *, const char *);
43258a2b000SEvgeniy Ivanov size_t strlcpy(char *, const char *, size_t);
43358a2b000SEvgeniy Ivanov size_t strlcat(char *, const char *, size_t);
43458a2b000SEvgeniy Ivanov int strncasecmp(const char *, const char *, size_t);
43558a2b000SEvgeniy Ivanov u_long strtoul(const char *, char **, int);
43658a2b000SEvgeniy Ivanov long long strtoll(const char *, char **, int);
43758a2b000SEvgeniy Ivanov unsigned long long strtoull(const char *, char **, int);
438*0a6a1f1dSLionel Sambuc intmax_t strtoimax(const char *, char **, int);
43958a2b000SEvgeniy Ivanov uintmax_t strtoumax(const char *, char **, int);
440*0a6a1f1dSLionel Sambuc intmax_t strtoi(const char * __restrict, char ** __restrict, int, intmax_t,
441*0a6a1f1dSLionel Sambuc intmax_t, int *);
442*0a6a1f1dSLionel Sambuc uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
443*0a6a1f1dSLionel Sambuc uintmax_t, int *);
444*0a6a1f1dSLionel Sambuc
44558a2b000SEvgeniy Ivanov int snprintb(char *, size_t, const char *, uint64_t);
44658a2b000SEvgeniy Ivanov int snprintb_m(char *, size_t, const char *, uint64_t, size_t);
44758a2b000SEvgeniy Ivanov int kheapsort(void *, size_t, size_t, int (*)(const void *, const void *),
44858a2b000SEvgeniy Ivanov void *);
44958a2b000SEvgeniy Ivanov uint32_t crc32(uint32_t, const uint8_t *, size_t);
450*0a6a1f1dSLionel Sambuc #if __GNUC_PREREQ__(4, 5) \
451*0a6a1f1dSLionel Sambuc && (defined(__alpha_cix__) || defined(__mips_popcount))
452*0a6a1f1dSLionel Sambuc #define popcount __builtin_popcount
453*0a6a1f1dSLionel Sambuc #define popcountl __builtin_popcountl
454*0a6a1f1dSLionel Sambuc #define popcountll __builtin_popcountll
455*0a6a1f1dSLionel Sambuc #define popcount32 __builtin_popcount
456*0a6a1f1dSLionel Sambuc #define popcount64 __builtin_popcountll
457*0a6a1f1dSLionel Sambuc #else
45858a2b000SEvgeniy Ivanov unsigned int popcount(unsigned int) __constfunc;
45958a2b000SEvgeniy Ivanov unsigned int popcountl(unsigned long) __constfunc;
46058a2b000SEvgeniy Ivanov unsigned int popcountll(unsigned long long) __constfunc;
46158a2b000SEvgeniy Ivanov unsigned int popcount32(uint32_t) __constfunc;
46258a2b000SEvgeniy Ivanov unsigned int popcount64(uint64_t) __constfunc;
463*0a6a1f1dSLionel Sambuc #endif
46484d9c625SLionel Sambuc
46584d9c625SLionel Sambuc void *explicit_memset(void *, int, size_t);
46684d9c625SLionel Sambuc int consttime_memequal(const void *, const void *, size_t);
467*0a6a1f1dSLionel Sambuc
468*0a6a1f1dSLionel Sambuc #ifdef notyet
469*0a6a1f1dSLionel Sambuc /*
470*0a6a1f1dSLionel Sambuc * LZF hashtable/state size: on uncompressible data and on a system with
471*0a6a1f1dSLionel Sambuc * a sufficiently large d-cache, a larger table produces a considerable
472*0a6a1f1dSLionel Sambuc * speed benefit. On systems with small memory and caches, however...
473*0a6a1f1dSLionel Sambuc */
474*0a6a1f1dSLionel Sambuc #if defined(__vax__) || defined(__m68k__)
475*0a6a1f1dSLionel Sambuc #define LZF_HLOG 14
476*0a6a1f1dSLionel Sambuc #else
477*0a6a1f1dSLionel Sambuc #define LZF_HLOG 15
478*0a6a1f1dSLionel Sambuc #endif
479*0a6a1f1dSLionel Sambuc typedef const uint8_t *LZF_STATE[1 << LZF_HLOG];
480*0a6a1f1dSLionel Sambuc
481*0a6a1f1dSLionel Sambuc unsigned int lzf_compress_r (const void *const, unsigned int, void *,
482*0a6a1f1dSLionel Sambuc unsigned int, LZF_STATE);
483*0a6a1f1dSLionel Sambuc unsigned int lzf_decompress (const void *const, unsigned int, void *,
484*0a6a1f1dSLionel Sambuc unsigned int);
485*0a6a1f1dSLionel Sambuc #endif
486*0a6a1f1dSLionel Sambuc
48758a2b000SEvgeniy Ivanov #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
488