4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #ifndef _SYS_CCOMPILE_H
28 #define _SYS_CCOMPILE_H
31 * This file contains definitions designed to enable different compilers
32 * to be used harmoniously on Solaris systems.
40 * Allow for version tests for compiler bugs and features.
43 #define __GNUC_VERSION \
44 (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
46 #define __GNUC_VERSION 0
49 #if defined(__ATTRIBUTE_IMPLEMENTED) || defined(__GNUC__)
52 * analogous to lint's PRINTFLIKEn
54 #define __sun_attr___PRINTFLIKE__(__n) \
55 __attribute__((__format__(printf, __n, (__n)+1)))
56 #define __sun_attr___VPRINTFLIKE__(__n) \
57 __attribute__((__format__(printf, __n, 0)))
60 * Handle the kernel printf routines that can take '%b' too
62 #if __GNUC_VERSION < 30402
64 * XX64 at least this doesn't work correctly yet with 3.4.1 anyway!
66 #define __sun_attr___KPRINTFLIKE__ __sun_attr___PRINTFLIKE__
67 #define __sun_attr___KVPRINTFLIKE__ __sun_attr___VPRINTFLIKE__
69 #define __sun_attr___KPRINTFLIKE__(__n) \
70 __attribute__((__format__(cmn_err, __n, (__n)+1)))
71 #define __sun_attr___KVPRINTFLIKE__(__n) \
72 __attribute__((__format__(cmn_err, __n, 0)))
76 * This one's pretty obvious -- the function never returns
78 #define __sun_attr___noreturn__ __attribute__((__noreturn__))
82 * This is an appropriate label for functions that do not
83 * modify their arguments, e.g. strlen()
85 #define __sun_attr___pure__ __attribute__((__pure__))
88 * This is a stronger form of __pure__. Can be used for functions
89 * that do not modify their arguments and don't depend on global
92 #define __sun_attr___const__ __attribute__((__const__))
95 * structure packing like #pragma pack(1)
97 #define __sun_attr___packed__ __attribute__((__packed__))
99 #define ___sun_attr_inner(__a) __sun_attr_##__a
100 #define __sun_attr__(__a) ___sun_attr_inner __a
102 #else /* __ATTRIBUTE_IMPLEMENTED || __GNUC__ */
104 #define __sun_attr__(__a)
106 #endif /* __ATTRIBUTE_IMPLEMENTED || __GNUC__ */
109 * Shorthand versions for readability
112 #define __PRINTFLIKE(__n) __sun_attr__((__PRINTFLIKE__(__n)))
113 #define __VPRINTFLIKE(__n) __sun_attr__((__VPRINTFLIKE__(__n)))
114 #define __KPRINTFLIKE(__n) __sun_attr__((__KPRINTFLIKE__(__n)))
115 #define __KVPRINTFLIKE(__n) __sun_attr__((__KVPRINTFLIKE__(__n)))
116 #if defined(_KERNEL) || defined(_STANDALONE)
117 #define __NORETURN __sun_attr__((__noreturn__))
118 #endif /* _KERNEL || _STANDALONE */
119 #define __CONST __sun_attr__((__const__))
120 #define __PURE __sun_attr__((__pure__))
122 #if defined(INVARIANTS) && !defined(ZFS_DEBUG)
127 #define EXPORT_SYMBOL(x)
128 #define MODULE_AUTHOR(s)
129 #define MODULE_DESCRIPTION(s)
130 #define MODULE_LICENSE(s)
131 #define module_param(a, b, c)
132 #define module_param_call(a, b, c, d, e)
133 #define module_param_named(a, b, c, d)
134 #define MODULE_PARM_DESC(a, b)
139 #if !defined(ZFS_DEBUG) && !defined(NDEBUG)
144 #define EINTEGRITY 97 /* EINTEGRITY is new in 13 */
148 * These are bespoke errnos used in ZFS. We map them to their closest FreeBSD
149 * equivalents. This gives us more useful error messages from strerror(3).
151 #define ECKSUM EINTEGRITY
152 #define EFRAGS ENOSPC
154 /* Similar for ENOACTIVE */
155 #define ENOTACTIVE ECANCELED
157 #define EREMOTEIO EREMOTE
159 #define ETIME ETIMEDOUT
161 #define O_LARGEFILE 0
166 #ifndef HAVE_RPC_TYPES
177 #if defined(_KERNEL) || defined(_STANDALONE)
178 #define param_set_charp(a, b) (0)
179 #define ATTR_UID AT_UID
180 #define ATTR_GID AT_GID
181 #define ATTR_MODE AT_MODE
182 #define ATTR_XVATTR AT_XVATTR
183 #define ATTR_CTIME AT_CTIME
184 #define ATTR_MTIME AT_MTIME
185 #define ATTR_ATIME AT_ATIME
186 #if defined(_STANDALONE)
187 #define vmem_free kmem_free
188 #define vmem_zalloc kmem_zalloc
189 #define vmem_alloc kmem_zalloc
191 #define vmem_free zfs_kmem_free
192 #define vmem_zalloc(size, flags) zfs_kmem_alloc(size, flags | M_ZERO)
193 #define vmem_alloc zfs_kmem_alloc
195 #define MUTEX_NOLOCKDEP 0
196 #define RW_NOLOCKDEP 0
202 * XXX We really need to consolidate on standard
203 * error codes in the common code
205 #define ENOSTR ENOTCONN
206 #define ENODATA EINVAL
209 #define __BSD_VISIBLE 1
211 #define __POSIX_VISIBLE 201808
212 #define __XSI_VISIBLE 1000
214 #define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
216 /* Note: this file can be used on linux/macOS when bootstrapping tools. */
217 #if defined(__FreeBSD__)
219 #define pwrite64 pwrite
220 #define ftruncate64 ftruncate
221 #define lseek64 lseek
222 #define pread64 pread
224 #define lstat64 lstat
225 #define statfs64 statfs
226 #define readdir64 readdir
227 #define dirent64 dirent
229 #define P2ALIGN(x, align) ((x) & -(align))
230 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
231 #define P2ROUNDUP(x, align) ((((x) - 1) | ((align) - 1)) + 1)
232 #define P2PHASE(x, align) ((x) & ((align) - 1))
233 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
234 #define ISP2(x) (((x) & ((x) - 1)) == 0)
235 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
236 #define P2BOUNDARY(off, len, align) \
237 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
240 * Typed version of the P2* macros. These macros should be used to ensure
241 * that the result is correctly calculated based on the data type of (x),
242 * which is passed in as the last argument, regardless of the data
243 * type of the alignment. For example, if (x) is of type uint64_t,
244 * and we want to round it up to a page boundary using "PAGESIZE" as
245 * the alignment, we can do either
247 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
249 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
251 #define P2ALIGN_TYPED(x, align, type) \
252 ((type)(x) & -(type)(align))
253 #define P2PHASE_TYPED(x, align, type) \
254 ((type)(x) & ((type)(align) - 1))
255 #define P2NPHASE_TYPED(x, align, type) \
256 (-(type)(x) & ((type)(align) - 1))
257 #define P2ROUNDUP_TYPED(x, align, type) \
258 ((((type)(x) - 1) | ((type)(align) - 1)) + 1)
259 #define P2END_TYPED(x, align, type) \
260 (-(~(type)(x) & -(type)(align)))
261 #define P2PHASEUP_TYPED(x, align, phase, type) \
262 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
263 #define P2CROSS_TYPED(x, y, align, type) \
264 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
265 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
266 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
268 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
269 #define RLIM64_INFINITY RLIM_INFINITY
270 #ifndef HAVE_ERESTART
271 #define ERESTART EAGAIN
273 #define ABS(a) ((a) < 0 ? -(a) : (a))
280 #endif /* _SYS_CCOMPILE_H */