xref: /openbsd-src/sys/lib/libkern/libkern.h (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: libkern.h,v 1.14 1997/11/30 21:47:45 mickey Exp $	*/
2 /*	$NetBSD: libkern.h,v 1.7 1996/03/14 18:52:08 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1992, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)libkern.h	8.1 (Berkeley) 6/10/93
37  */
38 
39 #ifndef __LIBKERN_H__
40 #define __LIBKERN_H__
41 
42 #include <sys/types.h>
43 
44 #ifndef LIBKERN_INLINE
45 #define LIBKERN_INLINE	static __inline
46 #define LIBKERN_BODY
47 #endif
48 
49 
50 LIBKERN_INLINE int imax __P((int, int));
51 LIBKERN_INLINE int imin __P((int, int));
52 LIBKERN_INLINE u_int max __P((u_int, u_int));
53 LIBKERN_INLINE u_int min __P((u_int, u_int));
54 LIBKERN_INLINE long lmax __P((long, long));
55 LIBKERN_INLINE long lmin __P((long, long));
56 LIBKERN_INLINE u_long ulmax __P((u_long, u_long));
57 LIBKERN_INLINE u_long ulmin __P((u_long, u_long));
58 LIBKERN_INLINE int abs __P((int));
59 
60 #ifdef LIBKERN_BODY
61 LIBKERN_INLINE int
62 imax(a, b)
63 	int a, b;
64 {
65 	return (a > b ? a : b);
66 }
67 LIBKERN_INLINE int
68 imin(a, b)
69 	int a, b;
70 {
71 	return (a < b ? a : b);
72 }
73 LIBKERN_INLINE long
74 lmax(a, b)
75 	long a, b;
76 {
77 	return (a > b ? a : b);
78 }
79 LIBKERN_INLINE long
80 lmin(a, b)
81 	long a, b;
82 {
83 	return (a < b ? a : b);
84 }
85 LIBKERN_INLINE u_int
86 max(a, b)
87 	u_int a, b;
88 {
89 	return (a > b ? a : b);
90 }
91 LIBKERN_INLINE u_int
92 min(a, b)
93 	u_int a, b;
94 {
95 	return (a < b ? a : b);
96 }
97 LIBKERN_INLINE u_long
98 ulmax(a, b)
99 	u_long a, b;
100 {
101 	return (a > b ? a : b);
102 }
103 LIBKERN_INLINE u_long
104 ulmin(a, b)
105 	u_long a, b;
106 {
107 	return (a < b ? a : b);
108 }
109 
110 LIBKERN_INLINE int
111 abs(j)
112 	int j;
113 {
114 	return(j < 0 ? -j : j);
115 }
116 #endif
117 
118 #ifdef NDEBUG						/* tradition! */
119 #define	assert(e)	((void)0)
120 #else
121 #ifdef __STDC__
122 #define	assert(e)	((e) ? (void)0 :				    \
123 			    __assert("", __FILE__, __LINE__, #e))
124 #else
125 #define	assert(e)	((e) ? (void)0 :				    \
126 			    __assert("", __FILE__, __LINE__, "e"))
127 #endif
128 #endif
129 
130 #ifndef DIAGNOSTIC
131 #define	KASSERT(e)	((void)0)
132 #else
133 #ifdef __STDC__
134 #define	KASSERT(e)	((e) ? (void)0 :				    \
135 			    __assert("diagnostic ", __FILE__, __LINE__, #e))
136 #else
137 #define	KASSERT(e)	((e) ? (void)0 :				    \
138 			    __assert("diagnostic ", __FILE__, __LINE__, "e"))
139 #endif
140 #endif
141 
142 #ifndef DEBUG
143 #define	KDASSERT(e)	((void)0)
144 #else
145 #ifdef __STDC__
146 #define	KDASSERT(e)	((e) ? (void)0 :				    \
147 			    __assert("debugging ", __FILE__, __LINE__, #e))
148 #else
149 #define	KDASSERT(e)	((e) ? (void)0 :				    \
150 			    __assert("debugging ", __FILE__, __LINE__, "e"))
151 #endif
152 #endif
153 
154 /* Prototypes for non-quad routines. */
155 void	 __assert __P((const char *, const char *, int, const char *))
156 	    __attribute__ ((__noreturn__));
157 int	 bcmp __P((const void *, const void *, size_t));
158 int	 ffs __P((int));
159 int	 locc __P((int, char *, u_int));
160 void	*memchr __P((const void *, int, size_t));
161 u_long	 random __P((void));
162 void	 srandom __P((u_long));
163 int	 scanc __P((u_int, const u_char *, const u_char *, int));
164 int	 skpc __P((int, size_t, u_char *));
165 size_t	 strlen __P((const char *));
166 char	*strcat __P((char *, const char *));
167 char	*strcpy __P((char *, const char *));
168 char	*strncpy __P((char *, const char *, size_t));
169 int	 strcmp __P((const char *, const char *));
170 int	 strncmp __P((const char *, const char *, size_t));
171 int	 strncasecmp __P((const char *, const char *, size_t));
172 int	 getsn __P((char *, int));
173 
174 extern u_int8_t const __bcd2bin[], __bin2bcd[];
175 #define	bcd2bin(b)	(__bcd2bin[(b)&0xff])
176 #define	bin2bcd(b)	(__bin2bcd[(b)&0xff])
177 
178 #endif /* __LIBKERN_H__ */
179