1 /*
2  * CDDL HEADER START
3  *
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
7  * with the License.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_SYS_CCOMPILE_H
28 #define	_SYS_CCOMPILE_H
29 
30 /*
31  * This file contains definitions designed to enable different compilers
32  * to be used harmoniously on Solaris systems.
33  */
34 
35 #ifdef	__cplusplus
36 extern "C" {
37 #endif
38 
39 /*
40  * Allow for version tests for compiler bugs and features.
41  */
42 #if defined(__GNUC__)
43 #define	__GNUC_VERSION	\
44 	(__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
45 #else
46 #define	__GNUC_VERSION	0
47 #endif
48 
49 #if defined(__ATTRIBUTE_IMPLEMENTED) || defined(__GNUC__)
50 
51 #if 0
52 /*
53  * analogous to lint's PRINTFLIKEn
54  */
55 #define	__sun_attr___PRINTFLIKE__(__n)	\
56 		__attribute__((__format__(printf, __n, (__n)+1)))
57 #define	__sun_attr___VPRINTFLIKE__(__n)	\
58 		__attribute__((__format__(printf, __n, 0)))
59 
60 #define	__sun_attr___KPRINTFLIKE__	__sun_attr___PRINTFLIKE__
61 #define	__sun_attr___KVPRINTFLIKE__	__sun_attr___VPRINTFLIKE__
62 #else
63 /*
64  * Currently the openzfs codebase has a lot of formatting errors
65  * which are not picked up in the linux build because they're not
66  * doing formatting checks. LLVM's kprintf implementation doesn't
67  * actually do format checks!
68  *
69  * For FreeBSD these break under gcc! LLVM shim'ed cmn_err as a
70  * format attribute but also didn't check anything.  If one
71  * replaces it with the above, all of the format issues
72  * in the codebase show up.
73  *
74  * Once those format string issues are addressed, the above
75  * should be flipped on once again.
76  */
77 #define	__sun_attr___PRINTFLIKE__(__n)
78 #define	__sun_attr___VPRINTFLIKE__(__n)
79 #define	__sun_attr___KPRINTFLIKE__(__n)
80 #define	__sun_attr___KVPRINTFLIKE__(__n)
81 
82 #endif
83 
84 /*
85  * This one's pretty obvious -- the function never returns
86  */
87 #define	__sun_attr___noreturn__ __attribute__((__noreturn__))
88 
89 /*
90  * This is an appropriate label for functions that do not
91  * modify their arguments, e.g. strlen()
92  */
93 #define	__sun_attr___pure__	__attribute__((__pure__))
94 
95 /*
96  * This is a stronger form of __pure__. Can be used for functions
97  * that do not modify their arguments and don't depend on global
98  * memory.
99  */
100 #define	__sun_attr___const__	__attribute__((__const__))
101 
102 /*
103  * structure packing like #pragma pack(1)
104  */
105 #define	__sun_attr___packed__	__attribute__((__packed__))
106 
107 #define	___sun_attr_inner(__a)	__sun_attr_##__a
108 #define	__sun_attr__(__a)	___sun_attr_inner __a
109 
110 #else	/* __ATTRIBUTE_IMPLEMENTED || __GNUC__ */
111 
112 #define	__sun_attr__(__a)
113 
114 #endif	/* __ATTRIBUTE_IMPLEMENTED || __GNUC__ */
115 
116 /*
117  * Shorthand versions for readability
118  */
119 
120 #define	__PRINTFLIKE(__n)	__sun_attr__((__PRINTFLIKE__(__n)))
121 #define	__VPRINTFLIKE(__n)	__sun_attr__((__VPRINTFLIKE__(__n)))
122 #define	__KPRINTFLIKE(__n)	__sun_attr__((__KPRINTFLIKE__(__n)))
123 #define	__KVPRINTFLIKE(__n)	__sun_attr__((__KVPRINTFLIKE__(__n)))
124 #if	defined(_KERNEL) || defined(_STANDALONE)
125 #define	__NORETURN		__sun_attr__((__noreturn__))
126 #endif /* _KERNEL || _STANDALONE */
127 #define	__CONST			__sun_attr__((__const__))
128 #define	__PURE			__sun_attr__((__pure__))
129 
130 #if defined(INVARIANTS) && !defined(ZFS_DEBUG)
131 #define	ZFS_DEBUG
132 #undef 	NDEBUG
133 #endif
134 
135 #define	EXPORT_SYMBOL(x)
136 #define	MODULE_AUTHOR(s)
137 #define	MODULE_DESCRIPTION(s)
138 #define	MODULE_LICENSE(s)
139 #define	module_param(a, b, c)
140 #define	module_param_call(a, b, c, d, e)
141 #define	module_param_named(a, b, c, d)
142 #define	MODULE_PARM_DESC(a, b)
143 #define	asm __asm
144 #ifdef ZFS_DEBUG
145 #undef NDEBUG
146 #endif
147 #if !defined(ZFS_DEBUG) && !defined(NDEBUG)
148 #define	NDEBUG
149 #endif
150 
151 #ifndef EINTEGRITY
152 #define	EINTEGRITY 97 /* EINTEGRITY is new in 13 */
153 #endif
154 
155 /*
156  * These are bespoke errnos used in ZFS. We map them to their closest FreeBSD
157  * equivalents. This gives us more useful error messages from strerror(3).
158  */
159 #define	ECKSUM	EINTEGRITY
160 #define	EFRAGS	ENOSPC
161 
162 /* Similar for ENOACTIVE */
163 #define	ENOTACTIVE	ECANCELED
164 
165 #define	EREMOTEIO EREMOTE
166 #define	ECHRNG ENXIO
167 #define	ETIME ETIMEDOUT
168 
169 #define	O_LARGEFILE 0
170 #define	O_RSYNC 0
171 #define	O_DSYNC 0
172 
173 #ifndef LOCORE
174 #ifndef HAVE_RPC_TYPES
175 typedef int bool_t;
176 typedef int enum_t;
177 #endif
178 #endif
179 
180 #ifndef __cplusplus
181 #define	__init
182 #define	__exit
183 #endif
184 
185 #if defined(_KERNEL) || defined(_STANDALONE)
186 #define	param_set_charp(a, b) (0)
187 #define	ATTR_UID AT_UID
188 #define	ATTR_GID AT_GID
189 #define	ATTR_MODE AT_MODE
190 #define	ATTR_XVATTR	AT_XVATTR
191 #define	ATTR_CTIME	AT_CTIME
192 #define	ATTR_MTIME	AT_MTIME
193 #define	ATTR_ATIME	AT_ATIME
194 #if defined(_STANDALONE)
195 #define	vmem_free kmem_free
196 #define	vmem_zalloc kmem_zalloc
197 #define	vmem_alloc kmem_zalloc
198 #else
199 #define	vmem_free zfs_kmem_free
200 #define	vmem_zalloc(size, flags) zfs_kmem_alloc(size, flags | M_ZERO)
201 #define	vmem_alloc zfs_kmem_alloc
202 #endif
203 #define	MUTEX_NOLOCKDEP 0
204 #define	RW_NOLOCKDEP 0
205 
206 #else
207 #define	FALSE 0
208 #define	TRUE 1
209 	/*
210 	 * XXX We really need to consolidate on standard
211 	 * error codes in the common code
212 	 */
213 #define	ENOSTR ENOTCONN
214 #define	ENODATA EINVAL
215 
216 
217 #define	__BSD_VISIBLE 1
218 #ifndef	IN_BASE
219 #define	__POSIX_VISIBLE 201808
220 #define	__XSI_VISIBLE 1000
221 #endif
222 #define	ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
223 #define	mmap64 mmap
224 /* Note: this file can be used on linux/macOS when bootstrapping tools. */
225 #if defined(__FreeBSD__)
226 #define	open64 open
227 #define	pwrite64 pwrite
228 #define	ftruncate64 ftruncate
229 #define	lseek64 lseek
230 #define	pread64 pread
231 #define	stat64 stat
232 #define	lstat64 lstat
233 #define	statfs64 statfs
234 #define	readdir64 readdir
235 #define	dirent64 dirent
236 #endif
237 #define	P2ALIGN(x, align)		((x) & -(align))
238 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
239 #define	P2ROUNDUP(x, align)		((((x) - 1) | ((align) - 1)) + 1)
240 #define	P2PHASE(x, align)		((x) & ((align) - 1))
241 #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
242 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
243 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
244 #define	P2BOUNDARY(off, len, align) \
245 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
246 
247 /*
248  * Typed version of the P2* macros.  These macros should be used to ensure
249  * that the result is correctly calculated based on the data type of (x),
250  * which is passed in as the last argument, regardless of the data
251  * type of the alignment.  For example, if (x) is of type uint64_t,
252  * and we want to round it up to a page boundary using "PAGESIZE" as
253  * the alignment, we can do either
254  *
255  * P2ROUNDUP(x, (uint64_t)PAGESIZE)
256  * or
257  * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
258  */
259 #define	P2ALIGN_TYPED(x, align, type)   \
260 	((type)(x) & -(type)(align))
261 #define	P2PHASE_TYPED(x, align, type)   \
262 	((type)(x) & ((type)(align) - 1))
263 #define	P2NPHASE_TYPED(x, align, type)  \
264 	(-(type)(x) & ((type)(align) - 1))
265 #define	P2ROUNDUP_TYPED(x, align, type) \
266 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
267 #define	P2END_TYPED(x, align, type)     \
268 	(-(~(type)(x) & -(type)(align)))
269 #define	P2PHASEUP_TYPED(x, align, phase, type)  \
270 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
271 #define	P2CROSS_TYPED(x, y, align, type)        \
272 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
273 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
274 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
275 
276 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
277 #define	RLIM64_INFINITY RLIM_INFINITY
278 #ifndef HAVE_ERESTART
279 #define	ERESTART EAGAIN
280 #endif
281 #define	ABS(a)	((a) < 0 ? -(a) : (a))
282 
283 #endif
284 #ifdef	__cplusplus
285 }
286 #endif
287 
288 #endif	/* _SYS_CCOMPILE_H */
289