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 
172 #ifndef LOCORE
173 #ifndef HAVE_RPC_TYPES
174 typedef int bool_t;
175 typedef int enum_t;
176 #endif
177 #endif
178 
179 #ifndef __cplusplus
180 #define	__init
181 #define	__exit
182 #endif
183 
184 #if defined(_KERNEL) || defined(_STANDALONE)
185 #define	param_set_charp(a, b) (0)
186 #define	ATTR_UID AT_UID
187 #define	ATTR_GID AT_GID
188 #define	ATTR_MODE AT_MODE
189 #define	ATTR_XVATTR	AT_XVATTR
190 #define	ATTR_CTIME	AT_CTIME
191 #define	ATTR_MTIME	AT_MTIME
192 #define	ATTR_ATIME	AT_ATIME
193 #if defined(_STANDALONE)
194 #define	vmem_free kmem_free
195 #define	vmem_zalloc kmem_zalloc
196 #define	vmem_alloc kmem_zalloc
197 #else
198 #define	vmem_free zfs_kmem_free
199 #define	vmem_zalloc(size, flags) zfs_kmem_alloc(size, flags | M_ZERO)
200 #define	vmem_alloc zfs_kmem_alloc
201 #endif
202 #define	MUTEX_NOLOCKDEP 0
203 #define	RW_NOLOCKDEP 0
204 
205 #else
206 #define	FALSE 0
207 #define	TRUE 1
208 	/*
209 	 * XXX We really need to consolidate on standard
210 	 * error codes in the common code
211 	 */
212 #define	ENOSTR ENOTCONN
213 #define	ENODATA EINVAL
214 
215 
216 #define	__BSD_VISIBLE 1
217 #ifndef	IN_BASE
218 #define	__POSIX_VISIBLE 201808
219 #define	__XSI_VISIBLE 1000
220 #endif
221 #define	ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
222 #define	mmap64 mmap
223 /* Note: this file can be used on linux/macOS when bootstrapping tools. */
224 #if defined(__FreeBSD__)
225 #define	open64 open
226 #define	pwrite64 pwrite
227 #define	ftruncate64 ftruncate
228 #define	lseek64 lseek
229 #define	pread64 pread
230 #define	stat64 stat
231 #define	lstat64 lstat
232 #define	statfs64 statfs
233 #define	readdir64 readdir
234 #define	dirent64 dirent
235 #endif
236 #define	P2ALIGN(x, align)		((x) & -(align))
237 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
238 #define	P2ROUNDUP(x, align)		((((x) - 1) | ((align) - 1)) + 1)
239 #define	P2PHASE(x, align)		((x) & ((align) - 1))
240 #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
241 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
242 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
243 #define	P2BOUNDARY(off, len, align) \
244 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
245 
246 /*
247  * Typed version of the P2* macros.  These macros should be used to ensure
248  * that the result is correctly calculated based on the data type of (x),
249  * which is passed in as the last argument, regardless of the data
250  * type of the alignment.  For example, if (x) is of type uint64_t,
251  * and we want to round it up to a page boundary using "PAGESIZE" as
252  * the alignment, we can do either
253  *
254  * P2ROUNDUP(x, (uint64_t)PAGESIZE)
255  * or
256  * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
257  */
258 #define	P2ALIGN_TYPED(x, align, type)   \
259 	((type)(x) & -(type)(align))
260 #define	P2PHASE_TYPED(x, align, type)   \
261 	((type)(x) & ((type)(align) - 1))
262 #define	P2NPHASE_TYPED(x, align, type)  \
263 	(-(type)(x) & ((type)(align) - 1))
264 #define	P2ROUNDUP_TYPED(x, align, type) \
265 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
266 #define	P2END_TYPED(x, align, type)     \
267 	(-(~(type)(x) & -(type)(align)))
268 #define	P2PHASEUP_TYPED(x, align, phase, type)  \
269 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
270 #define	P2CROSS_TYPED(x, y, align, type)        \
271 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
272 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
273 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
274 
275 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
276 #define	RLIM64_INFINITY RLIM_INFINITY
277 #ifndef HAVE_ERESTART
278 #define	ERESTART EAGAIN
279 #endif
280 #define	ABS(a)	((a) < 0 ? -(a) : (a))
281 
282 #endif
283 #ifdef	__cplusplus
284 }
285 #endif
286 
287 #endif	/* _SYS_CCOMPILE_H */
288