autofs: disable by default
[unleashed.git] / include / sys / sysmacros.h
blob582e002ac5e1b6006274cc3b7ec4b24b7ff5d54e
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 http://www.opensolaris.org/os/licensing.
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.
29 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
32 #ifndef _SYS_SYSMACROS_H
33 #define _SYS_SYSMACROS_H
35 #include <sys/param.h>
36 #include <sys/stddef.h>
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
43 * Some macros for units conversion
46 * Disk blocks (sectors) and bytes.
48 #define dtob(DD) ((DD) << DEV_BSHIFT)
49 #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
50 #define btodt(BB) ((BB) >> DEV_BSHIFT)
51 #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
53 /* common macros */
54 #ifndef MIN
55 #define MIN(a, b) ((a) < (b) ? (a) : (b))
56 #endif
57 #ifndef MAX
58 #define MAX(a, b) ((a) < (b) ? (b) : (a))
59 #endif
60 #ifndef ABS
61 #define ABS(a) ((a) < 0 ? -(a) : (a))
62 #endif
63 #ifndef SIGNOF
64 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
65 #endif
67 #ifdef _KERNEL
70 * Convert a single byte to/from binary-coded decimal (BCD).
72 extern unsigned char byte_to_bcd[256];
73 extern unsigned char bcd_to_byte[256];
75 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
76 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
78 #endif /* _KERNEL */
81 * WARNING: The device number macros defined here should not be used by device
82 * drivers or user software. Device drivers should use the device functions
83 * defined in the DDI/DKI interface (see also ddi.h). Application software
84 * should make use of the library routines available in makedev(3C). A set of
85 * new device macros are provided to operate on the expanded device number
86 * format supported in SVR4. Macro versions of the DDI device functions are
87 * provided for use by kernel proper routines only.
90 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
91 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
92 #define O_MAXMAJ 0x7f /* SVR3 max major value */
93 #define O_MAXMIN 0xff /* SVR3 max minor value */
96 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
97 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
98 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
99 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
100 /* For 3b2 hardware devices the minor is */
101 /* restricted to 256 (0-255) */
103 #ifdef _LP64
104 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
105 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
106 #define L_MAXMAJ 0xfffffffful /* max major value */
107 #define L_MAXMIN 0xfffffffful /* max minor value */
108 #else
109 #define L_BITSMAJOR L_BITSMAJOR32
110 #define L_BITSMINOR L_BITSMINOR32
111 #define L_MAXMAJ L_MAXMAJ32
112 #define L_MAXMIN L_MAXMIN32
113 #endif
115 #ifdef _KERNEL
117 /* get internal major part of expanded device number */
119 #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
121 /* get internal minor part of expanded device number */
123 #define getminor(x) (minor_t)((x) & L_MAXMIN)
125 #endif /* _KERNEL */
127 /* make an new device number */
129 #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
132 * get external major and minor device
133 * components from expanded device number
135 #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
136 NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
137 #define geteminor(x) (minor_t)((x) & L_MAXMIN)
140 * These are versions of the kernel routines for compressing and
141 * expanding long device numbers that don't return errors.
143 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
145 #define DEVCMPL(x) (x)
146 #define DEVEXPL(x) (x)
148 #else
150 #define DEVCMPL(x) \
151 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
152 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
153 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
155 #define DEVEXPL(x) \
156 (((x) == NODEV32) ? NODEV : \
157 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
159 #endif /* L_BITSMAJOR32 ... */
161 /* convert to old (SVR3.2) dev format */
163 #define cmpdev(x) \
164 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
165 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
166 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
168 /* convert to new (SVR4) dev format */
170 #define expdev(x) \
171 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
172 ((x) & O_MAXMIN))
175 * Macro for checking power of 2 address alignment.
177 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
180 * Macros for counting and rounding.
182 #define howmany(x, y) (((x)+((y)-1))/(y))
183 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
186 * Macro to determine if value is a power of 2
188 #define ISP2(x) (((x) & ((x) - 1)) == 0)
191 * Macros for various sorts of alignment and rounding. The "align" must
192 * be a power of 2. Often times it is a block, sector, or page.
196 * return x rounded down to an align boundary
197 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
198 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
199 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
200 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
202 #define P2ALIGN(x, align) ((x) & -(align))
205 * return x % (mod) align
206 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
207 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
209 #define P2PHASE(x, align) ((x) & ((align) - 1))
212 * return how much space is left in this block (but if it's perfectly
213 * aligned, return 0).
214 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
215 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
217 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
220 * return x rounded up to an align boundary
221 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
222 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
224 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
227 * return the ending address of the block that x is in
228 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
229 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
231 #define P2END(x, align) (-(~(x) & -(align)))
234 * return x rounded up to the next phase (offset) within align.
235 * phase should be < align.
236 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
237 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
239 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
242 * return TRUE if adding len to off would cause it to cross an align
243 * boundary.
244 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
245 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
247 #define P2BOUNDARY(off, len, align) \
248 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
251 * Return TRUE if they have the same highest bit set.
252 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
253 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
255 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
258 * Typed version of the P2* macros. These macros should be used to ensure
259 * that the result is correctly calculated based on the data type of (x),
260 * which is passed in as the last argument, regardless of the data
261 * type of the alignment. For example, if (x) is of type uint64_t,
262 * and we want to round it up to a page boundary using "PAGESIZE" as
263 * the alignment, we can do either
264 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
265 * or
266 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
268 #define P2ALIGN_TYPED(x, align, type) \
269 ((type)(x) & -(type)(align))
270 #define P2PHASE_TYPED(x, align, type) \
271 ((type)(x) & ((type)(align) - 1))
272 #define P2NPHASE_TYPED(x, align, type) \
273 (-(type)(x) & ((type)(align) - 1))
274 #define P2ROUNDUP_TYPED(x, align, type) \
275 (-(-(type)(x) & -(type)(align)))
276 #define P2END_TYPED(x, align, type) \
277 (-(~(type)(x) & -(type)(align)))
278 #define P2PHASEUP_TYPED(x, align, phase, type) \
279 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
280 #define P2CROSS_TYPED(x, y, align, type) \
281 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
282 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
283 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
286 * Macros to atomically increment/decrement a variable. mutex and var
287 * must be pointers.
289 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
290 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
293 * Macros to declare bitfields - the order in the parameter list is
294 * Low to High - that is, declare bit 0 first. We only support 8-bit bitfields
295 * because if a field crosses a byte boundary it's not likely to be meaningful
296 * without reassembly in its nonnative endianness.
298 #if defined(_BIT_FIELDS_LTOH)
299 #define DECL_BITFIELD2(_a, _b) \
300 uint8_t _a, _b
301 #define DECL_BITFIELD3(_a, _b, _c) \
302 uint8_t _a, _b, _c
303 #define DECL_BITFIELD4(_a, _b, _c, _d) \
304 uint8_t _a, _b, _c, _d
305 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
306 uint8_t _a, _b, _c, _d, _e
307 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
308 uint8_t _a, _b, _c, _d, _e, _f
309 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
310 uint8_t _a, _b, _c, _d, _e, _f, _g
311 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
312 uint8_t _a, _b, _c, _d, _e, _f, _g, _h
313 #elif defined(_BIT_FIELDS_HTOL)
314 #define DECL_BITFIELD2(_a, _b) \
315 uint8_t _b, _a
316 #define DECL_BITFIELD3(_a, _b, _c) \
317 uint8_t _c, _b, _a
318 #define DECL_BITFIELD4(_a, _b, _c, _d) \
319 uint8_t _d, _c, _b, _a
320 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
321 uint8_t _e, _d, _c, _b, _a
322 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
323 uint8_t _f, _e, _d, _c, _b, _a
324 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
325 uint8_t _g, _f, _e, _d, _c, _b, _a
326 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
327 uint8_t _h, _g, _f, _e, _d, _c, _b, _a
328 #else
329 #error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
330 #endif /* _BIT_FIELDS_LTOH */
332 /* avoid any possibility of clashing with <stddef.h> version */
333 #if defined(_KERNEL) && !defined(_KMEMUSER)
335 #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
337 #endif /* _KERNEL, !_KMEMUSER */
339 #ifdef __cplusplus
341 #endif
343 #endif /* _SYS_SYSMACROS_H */