xref: /freebsd/sys/contrib/openzfs/include/os/freebsd/spl/sys/ccompile.h (revision b1c1ee4429fcca8f69873a8be66184e68e1b19d7)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or https://opensource.org/licenses/CDDL-1.0.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 /*
24  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 #ifndef	_SYS_CCOMPILE_H
29 #define	_SYS_CCOMPILE_H
30 
31 /*
32  * This file contains definitions designed to enable different compilers
33  * to be used harmoniously on Solaris systems.
34  */
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #if defined(INVARIANTS) && !defined(ZFS_DEBUG)
41 #define	ZFS_DEBUG
42 #undef 	NDEBUG
43 #endif
44 
45 #define	EXPORT_SYMBOL(x)
46 
47 #define	asm __asm
48 #ifdef ZFS_DEBUG
49 #undef NDEBUG
50 #endif
51 #if !defined(ZFS_DEBUG) && !defined(NDEBUG)
52 #define	NDEBUG
53 #endif
54 
55 #ifndef EINTEGRITY
56 #define	EINTEGRITY 97 /* EINTEGRITY is new in 13 */
57 #endif
58 
59 /*
60  * These are bespoke errnos used in ZFS. We map them to their closest FreeBSD
61  * equivalents. This gives us more useful error messages from strerror(3).
62  */
63 #define	ECKSUM	EINTEGRITY
64 #define	EFRAGS	ENOSPC
65 
66 /* Similar for ENOACTIVE */
67 #define	ENOTACTIVE	ECANCELED
68 
69 #define	EREMOTEIO EREMOTE
70 #define	ECHRNG ENXIO
71 #define	ETIME ETIMEDOUT
72 
73 #ifndef LOCORE
74 #ifndef HAVE_RPC_TYPES
75 #ifndef _KERNEL
76 typedef int bool_t;
77 typedef int enum_t;
78 #endif
79 #endif
80 #endif
81 
82 #ifndef __cplusplus
83 #define	__init
84 #define	__exit
85 #endif
86 
87 #if defined(_KERNEL) || defined(_STANDALONE)
88 #define	param_set_charp(a, b) (0)
89 #define	ATTR_UID AT_UID
90 #define	ATTR_GID AT_GID
91 #define	ATTR_MODE AT_MODE
92 #define	ATTR_XVATTR	AT_XVATTR
93 #define	ATTR_CTIME	AT_CTIME
94 #define	ATTR_MTIME	AT_MTIME
95 #define	ATTR_ATIME	AT_ATIME
96 #if defined(_STANDALONE)
97 #define	vmem_free kmem_free
98 #define	vmem_zalloc kmem_zalloc
99 #define	vmem_alloc kmem_zalloc
100 #else
101 #define	vmem_free zfs_kmem_free
102 #define	vmem_zalloc(size, flags) zfs_kmem_alloc(size, flags | M_ZERO)
103 #define	vmem_alloc zfs_kmem_alloc
104 #endif
105 #define	MUTEX_NOLOCKDEP 0
106 #define	RW_NOLOCKDEP 0
107 
108 #else
109 #define	FALSE 0
110 #define	TRUE 1
111 	/*
112 	 * XXX We really need to consolidate on standard
113 	 * error codes in the common code
114 	 */
115 #define	ENOSTR ENOTCONN
116 #define	ENODATA EINVAL
117 
118 
119 #define	__BSD_VISIBLE 1
120 #ifndef	IN_BASE
121 #define	__POSIX_VISIBLE 201808
122 #define	__XSI_VISIBLE 1000
123 #endif
124 #define	ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
125 #define	mmap64 mmap
126 /* Note: this file can be used on linux/macOS when bootstrapping tools. */
127 #if defined(__FreeBSD__)
128 #define	open64 open
129 #define	pwrite64 pwrite
130 #define	ftruncate64 ftruncate
131 #define	lseek64 lseek
132 #define	pread64 pread
133 #define	stat64 stat
134 #define	lstat64 lstat
135 #define	statfs64 statfs
136 #define	readdir64 readdir
137 #define	dirent64 dirent
138 #endif
139 // Deprecated. Use P2ALIGN_TYPED instead.
140 // #define	P2ALIGN(x, align)		((x) & -(align))
141 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
142 #define	P2ROUNDUP(x, align)		((((x) - 1) | ((align) - 1)) + 1)
143 #define	P2PHASE(x, align)		((x) & ((align) - 1))
144 #define	P2NPHASE(x, align)		(-(x) & ((align) - 1))
145 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
146 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
147 #define	P2BOUNDARY(off, len, align) \
148 	(((off) ^ ((off) + (len) - 1)) > (align) - 1)
149 
150 /*
151  * Typed version of the P2* macros.  These macros should be used to ensure
152  * that the result is correctly calculated based on the data type of (x),
153  * which is passed in as the last argument, regardless of the data
154  * type of the alignment.  For example, if (x) is of type uint64_t,
155  * and we want to round it up to a page boundary using "PAGESIZE" as
156  * the alignment, we can do either
157  *
158  * P2ROUNDUP(x, (uint64_t)PAGESIZE)
159  * or
160  * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
161  */
162 #define	P2ALIGN_TYPED(x, align, type)   \
163 	((type)(x) & -(type)(align))
164 #define	P2PHASE_TYPED(x, align, type)   \
165 	((type)(x) & ((type)(align) - 1))
166 #define	P2NPHASE_TYPED(x, align, type)  \
167 	(-(type)(x) & ((type)(align) - 1))
168 #define	P2ROUNDUP_TYPED(x, align, type) \
169 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
170 #define	P2END_TYPED(x, align, type)     \
171 	(-(~(type)(x) & -(type)(align)))
172 #define	P2PHASEUP_TYPED(x, align, phase, type)  \
173 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
174 #define	P2CROSS_TYPED(x, y, align, type)        \
175 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
176 #define	P2SAMEHIGHBIT_TYPED(x, y, type) \
177 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
178 
179 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
180 #define	RLIM64_INFINITY RLIM_INFINITY
181 #ifndef HAVE_ERESTART
182 #define	ERESTART EAGAIN
183 #endif
184 #define	ABS(a)	((a) < 0 ? -(a) : (a))
185 
186 #endif
187 #ifdef	__cplusplus
188 }
189 #endif
190 
191 #endif	/* _SYS_CCOMPILE_H */
192