FreeBSD: Use a statement expression to implement SET_ERROR() (#16284)
[zfs.git] / include / os / freebsd / spl / sys / sysmacros.h
blob2c9f4438d769bef172b68d331136dda127672509
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 #ifndef _SYS_SYSMACROS_H
31 #define _SYS_SYSMACROS_H
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/isa_defs.h>
36 #include <sys/libkern.h>
37 #include <sys/zone.h>
38 #include <sys/condvar.h>
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
45 * Some macros for units conversion
48 * Disk blocks (sectors) and bytes.
50 #define dtob(DD) ((DD) << DEV_BSHIFT)
51 #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
52 #define btodt(BB) ((BB) >> DEV_BSHIFT)
53 #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
55 /* common macros */
56 #ifndef MIN
57 #define MIN(a, b) ((a) < (b) ? (a) : (b))
58 #endif
59 #ifndef MAX
60 #define MAX(a, b) ((a) < (b) ? (b) : (a))
61 #endif
62 #ifndef ABS
63 #define ABS(a) ((a) < 0 ? -(a) : (a))
64 #endif
65 #ifndef SIGNOF
66 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
67 #endif
68 #ifndef ARRAY_SIZE
69 #define ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
70 #endif
71 #ifndef DIV_ROUND_UP
72 #define DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
73 #endif
75 #ifdef _STANDALONE
76 #define boot_ncpus 1
77 #else /* _STANDALONE */
78 #define boot_ncpus mp_ncpus
79 #endif /* _STANDALONE */
80 #define kpreempt_disable() critical_enter()
81 #define kpreempt_enable() critical_exit()
82 #define CPU_SEQID curcpu
83 #define CPU_SEQID_UNSTABLE curcpu
84 #define is_system_labeled() 0
86 * Convert a single byte to/from binary-coded decimal (BCD).
88 extern unsigned char byte_to_bcd[256];
89 extern unsigned char bcd_to_byte[256];
91 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
92 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
95 * WARNING: The device number macros defined here should not be used by device
96 * drivers or user software. Device drivers should use the device functions
97 * defined in the DDI/DKI interface (see also ddi.h). Application software
98 * should make use of the library routines available in makedev(3). A set of
99 * new device macros are provided to operate on the expanded device number
100 * format supported in SVR4. Macro versions of the DDI device functions are
101 * provided for use by kernel proper routines only. Macro routines bmajor(),
102 * major(), minor(), emajor(), eminor(), and makedev() will be removed or
103 * their definitions changed at the next major release following SVR4.
106 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
107 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
108 #define O_MAXMAJ 0x7f /* SVR3 max major value */
109 #define O_MAXMIN 0xff /* SVR3 max minor value */
112 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
113 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
114 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
115 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
116 /* For 3b2 hardware devices the minor is */
117 /* restricted to 256 (0-255) */
119 #ifdef _LP64
120 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
121 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
122 #define L_MAXMAJ 0xfffffffful /* max major value */
123 #define L_MAXMIN 0xfffffffful /* max minor value */
124 #else
125 #define L_BITSMAJOR L_BITSMAJOR32
126 #define L_BITSMINOR L_BITSMINOR32
127 #define L_MAXMAJ L_MAXMAJ32
128 #define L_MAXMIN L_MAXMIN32
129 #endif
132 * These are versions of the kernel routines for compressing and
133 * expanding long device numbers that don't return errors.
135 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
137 #define DEVCMPL(x) (x)
138 #define DEVEXPL(x) (x)
140 #else
142 #define DEVCMPL(x) \
143 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
144 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
145 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
147 #define DEVEXPL(x) \
148 (((x) == NODEV32) ? NODEV : \
149 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
151 #endif /* L_BITSMAJOR32 ... */
153 /* convert to old (SVR3.2) dev format */
155 #define cmpdev(x) \
156 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
157 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
158 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
160 /* convert to new (SVR4) dev format */
162 #define expdev(x) \
163 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
164 ((x) & O_MAXMIN))
167 * Macro for checking power of 2 address alignment.
169 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
172 * Macros for counting and rounding.
174 #define howmany(x, y) (((x)+((y)-1))/(y))
175 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
178 * Macro to determine if value is a power of 2
180 #define ISP2(x) (((x) & ((x) - 1)) == 0)
183 * Macros for various sorts of alignment and rounding. The "align" must
184 * be a power of 2. Often times it is a block, sector, or page.
188 * return x rounded down to an align boundary
189 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
190 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
191 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
192 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
194 // Deprecated. Use P2ALIGN_TYPED instead.
195 // #define P2ALIGN(x, align) ((x) & -(align))
198 * return x % (mod) align
199 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
200 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
202 #define P2PHASE(x, align) ((x) & ((align) - 1))
205 * return how much space is left in this block (but if it's perfectly
206 * aligned, return 0).
207 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
208 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
210 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
213 * return x rounded up to an align boundary
214 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
215 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
217 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
220 * return the ending address of the block that x is in
221 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
222 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
224 #define P2END(x, align) (-(~(x) & -(align)))
227 * return x rounded up to the next phase (offset) within align.
228 * phase should be < align.
229 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
230 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
232 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
235 * return TRUE if adding len to off would cause it to cross an align
236 * boundary.
237 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
238 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
240 #define P2BOUNDARY(off, len, align) \
241 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
244 * Return TRUE if they have the same highest bit set.
245 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
246 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
248 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
251 * Typed version of the P2* macros. These macros should be used to ensure
252 * that the result is correctly calculated based on the data type of (x),
253 * which is passed in as the last argument, regardless of the data
254 * type of the alignment. For example, if (x) is of type uint64_t,
255 * and we want to round it up to a page boundary using "PAGESIZE" as
256 * the alignment, we can do either
257 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
258 * or
259 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
261 #define P2ALIGN_TYPED(x, align, type) \
262 ((type)(x) & -(type)(align))
263 #define P2PHASE_TYPED(x, align, type) \
264 ((type)(x) & ((type)(align) - 1))
265 #define P2NPHASE_TYPED(x, align, type) \
266 (-(type)(x) & ((type)(align) - 1))
267 #define P2ROUNDUP_TYPED(x, align, type) \
268 (-(-(type)(x) & -(type)(align)))
269 #define P2END_TYPED(x, align, type) \
270 (-(~(type)(x) & -(type)(align)))
271 #define P2PHASEUP_TYPED(x, align, phase, type) \
272 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
273 #define P2CROSS_TYPED(x, y, align, type) \
274 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
275 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
276 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
279 * Macros to atomically increment/decrement a variable. mutex and var
280 * must be pointers.
282 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
283 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
285 #if !defined(_KMEMUSER) && !defined(offsetof)
287 /* avoid any possibility of clashing with <stddef.h> version */
289 #define offsetof(type, field) __offsetof(type, field)
290 #endif
293 * Find highest one bit set.
294 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
295 * High order bit is 31 (or 63 in _LP64 kernel).
297 static __inline int
298 highbit(ulong_t i)
300 #if defined(HAVE_INLINE_FLSL)
301 return (flsl(i));
302 #else
303 int h = 1;
305 if (i == 0)
306 return (0);
307 #ifdef _LP64
308 if (i & 0xffffffff00000000ul) {
309 h += 32; i >>= 32;
311 #endif
312 if (i & 0xffff0000) {
313 h += 16; i >>= 16;
315 if (i & 0xff00) {
316 h += 8; i >>= 8;
318 if (i & 0xf0) {
319 h += 4; i >>= 4;
321 if (i & 0xc) {
322 h += 2; i >>= 2;
324 if (i & 0x2) {
325 h += 1;
327 return (h);
328 #endif
332 * Find highest one bit set.
333 * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
335 static __inline int
336 highbit64(uint64_t i)
338 #if defined(HAVE_INLINE_FLSLL)
339 return (flsll(i));
340 #else
341 int h = 1;
343 if (i == 0)
344 return (0);
345 if (i & 0xffffffff00000000ULL) {
346 h += 32; i >>= 32;
348 if (i & 0xffff0000) {
349 h += 16; i >>= 16;
351 if (i & 0xff00) {
352 h += 8; i >>= 8;
354 if (i & 0xf0) {
355 h += 4; i >>= 4;
357 if (i & 0xc) {
358 h += 2; i >>= 2;
360 if (i & 0x2) {
361 h += 1;
363 return (h);
364 #endif
367 #ifdef __cplusplus
369 #endif
371 #endif /* _SYS_SYSMACROS_H */