]> git.saurik.com Git - apple/xnu.git/blame - bsd/libkern/libkern.h
xnu-1699.24.23.tar.gz
[apple/xnu.git] / bsd / libkern / libkern.h
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*-
29 * Copyright (c) 1992, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)libkern.h 8.1 (Berkeley) 6/10/93
61 */
2d21ac55
A
62/*
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
66 * Version 2.0.
67 */
1c79356b
A
68
69#ifndef _LIBKERN_LIBKERN_H_
70#define _LIBKERN_LIBKERN_H_
71
9bccf70c 72#include <sys/appleapiopts.h>
91447636 73#include <stdint.h>
2d21ac55 74#include <stdarg.h> /* for platform-specific va_list */
91447636 75#include <string.h>
1c79356b
A
76#include <sys/cdefs.h>
77#include <sys/types.h>
91447636 78#include <mach/vm_param.h>
1c79356b 79
9bccf70c 80#ifdef __APPLE_API_OBSOLETE
1c79356b
A
81/* BCD conversions. */
82extern u_char const bcd2bin_data[];
83extern u_char const bin2bcd_data[];
1c79356b
A
84
85#define bcd2bin(bcd) (bcd2bin_data[bcd])
86#define bin2bcd(bin) (bin2bcd_data[bin])
9bccf70c 87#endif /* __APPLE_API_OBSOLETE */
1c79356b 88
9bccf70c
A
89#ifdef __APPLE_API_PRIVATE
90extern char const hex2ascii_data[];
91
92#define hex2ascii(hex) (hex2ascii_data[hex])
93#endif /* __APPLE_API_PRIVATE */
1c79356b
A
94
95__BEGIN_DECLS
96static inline int
97imax(int a, int b)
98{
99 return (a > b ? a : b);
100}
101static inline int
102imin(int a, int b)
103{
104 return (a < b ? a : b);
105}
106static inline long
107lmax(long a, long b)
108{
109 return (a > b ? a : b);
110}
111static inline long
112lmin(long a, long b)
113{
114 return (a < b ? a : b);
115}
116static inline u_int
117max(u_int a, u_int b)
118{
119 return (a > b ? a : b);
120}
121static inline u_int
122min(u_int a, u_int b)
123{
124 return (a < b ? a : b);
125}
b0d623f7
A
126static inline u_int32_t
127ulmax(u_int32_t a, u_int32_t b)
1c79356b
A
128{
129 return (a > b ? a : b);
130}
b0d623f7
A
131static inline u_int32_t
132ulmin(u_int32_t a, u_int32_t b)
1c79356b
A
133{
134 return (a < b ? a : b);
135}
136
2d21ac55
A
137
138
1c79356b 139/* Prototypes for non-quad routines. */
91447636
A
140extern int ffs(int);
141extern int locc(int, char *, u_int);
b0d623f7 142extern u_int32_t random(void);
91447636
A
143extern char *rindex(const char *, int);
144extern int scanc(u_int, u_char *, const u_char *, int);
145extern int skpc(int, int, char *);
146extern long strtol(const char*, char **, int);
147extern u_long strtoul(const char *, char **, int);
148extern quad_t strtoq(const char *, char **, int);
149extern u_quad_t strtouq(const char *, char **, int);
2d21ac55 150extern char *strsep(char **stringp, const char *delim);
91447636 151
2d21ac55
A
152int snprintf(char *, size_t, const char *, ...) __printflike(3,4);
153
154/* sprintf() is being deprecated. Please use snprintf() instead. */
155int sprintf(char *bufp, const char *, ...) __deprecated;
156int sscanf(const char *, char const *, ...) __scanflike(2,3);
157int printf(const char *, ...) __printflike(1,2);
158
b0d623f7
A
159#if KERNEL_PRIVATE
160int _consume_printf_args(int, ...);
161#endif
162
2d21ac55 163#if CONFIG_NO_PRINTF_STRINGS
b0d623f7
A
164#if KERNEL_PRIVATE
165#define printf(x, ...) _consume_printf_args( 0, ## __VA_ARGS__ )
166#else
2d21ac55
A
167#define printf(x, ...) do {} while (0)
168#endif
b0d623f7 169#endif
91447636
A
170
171uint32_t crc32(uint32_t crc, const void *bufp, size_t len);
172
173int copystr(const void *kfaddr, void *kdaddr, size_t len, size_t *done);
174int copyinstr(const user_addr_t uaddr, void *kaddr, size_t len, size_t *done);
175int copyoutstr(const void *kaddr, user_addr_t udaddr, size_t len, size_t *done);
176int copyin(const user_addr_t uaddr, void *kaddr, size_t len);
177int copyout(const void *kaddr, user_addr_t udaddr, size_t len);
178
2d21ac55
A
179int vsscanf(const char *, char const *, va_list);
180
181extern int vprintf(const char *, va_list);
182extern int vsnprintf(char *, size_t, const char *, va_list);
183
184/* vsprintf() is being deprecated. Please use vsnprintf() instead. */
185extern int vsprintf(char *bufp, const char *, va_list) __deprecated;
91447636 186
b0d623f7 187#ifdef KERNEL_PRIVATE
91447636
A
188extern void invalidate_icache(vm_offset_t, unsigned, int);
189extern void flush_dcache(vm_offset_t, unsigned, int);
b0d623f7
A
190#else
191extern void invalidate_icache(vm_offset_t, unsigned, int);
192extern void flush_dcache(vm_offset_t, unsigned, int);
193#endif
91447636
A
194extern void invalidate_icache64(addr64_t, unsigned, int);
195extern void flush_dcache64(addr64_t, unsigned, int);
196
2d21ac55
A
197
198/*
199 * assembly versions of clz... ideally we would just call
200 * __builtin_clz(num), unfortunately this one is ill defined
201 * by gcc for num=0
202 */
203static __inline__ unsigned int
204clz(unsigned int num)
205{
6d2010ae 206#if __i386__
2d21ac55
A
207 unsigned int result;
208 __asm__ volatile(
209 "bsrl %1, %0\n\t"
210 "cmovel %2, %0"
211 : "=r" (result)
212 : "rm" (num), "r" (63)
213 );
214 return 31 ^ result;
215
2d21ac55
A
216#else
217 return num?__builtin_clz(num):__builtin_clz(0);
218#endif
219}
220
221
1c79356b
A
222__END_DECLS
223
224#endif /* _LIBKERN_LIBKERN_H_ */