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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef _LIBSPL_SYS_SYSMACROS_H
28 #define	_LIBSPL_SYS_SYSMACROS_H
29 
30 #include_next <sys/sysmacros.h>
31 
32 /* common macros */
33 #ifndef MIN
34 #define	MIN(a, b)	((a) < (b) ? (a) : (b))
35 #endif
36 #ifndef MAX
37 #define	MAX(a, b)	((a) < (b) ? (b) : (a))
38 #endif
39 #ifndef ABS
40 #define	ABS(a)		((a) < 0 ? -(a) : (a))
41 #endif
42 #ifndef ARRAY_SIZE
43 #define	ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
44 #endif
45 #ifndef	DIV_ROUND_UP
46 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
47 #endif
48 
49 #define	makedevice(maj, min)	makedev(maj, min)
50 #define	_sysconf(a)		sysconf(a)
51 
52 /*
53  * Compatibility macros/typedefs needed for Solaris -> Linux port
54  */
55 #define	P2ALIGN(x, align)	((x) & -(align))
56 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
57 #define	P2ROUNDUP(x, align)	((((x) - 1) | ((align) - 1)) + 1)
58 #define	P2BOUNDARY(off, len, align) \
59 				(((off) ^ ((off) + (len) - 1)) > (align) - 1)
60 #define	P2PHASE(x, align)	((x) & ((align) - 1))
61 #define	P2NPHASE(x, align)	(-(x) & ((align) - 1))
62 #define	P2NPHASE_TYPED(x, align, type) \
63 				(-(type)(x) & ((type)(align) - 1))
64 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
65 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
66 
67 /*
68  * Typed version of the P2* macros.  These macros should be used to ensure
69  * that the result is correctly calculated based on the data type of (x),
70  * which is passed in as the last argument, regardless of the data
71  * type of the alignment.  For example, if (x) is of type uint64_t,
72  * and we want to round it up to a page boundary using "PAGESIZE" as
73  * the alignment, we can do either
74  *      P2ROUNDUP(x, (uint64_t)PAGESIZE)
75  * or
76  *      P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
77  */
78 #define	P2ALIGN_TYPED(x, align, type)		\
79 	((type)(x) & -(type)(align))
80 #define	P2PHASE_TYPED(x, align, type)		\
81 	((type)(x) & ((type)(align) - 1))
82 #define	P2NPHASE_TYPED(x, align, type)		\
83 	(-(type)(x) & ((type)(align) - 1))
84 #define	P2ROUNDUP_TYPED(x, align, type)		\
85 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
86 #define	P2END_TYPED(x, align, type)		\
87 	(-(~(type)(x) & -(type)(align)))
88 #define	P2PHASEUP_TYPED(x, align, phase, type)	\
89 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
90 #define	P2CROSS_TYPED(x, y, align, type)	\
91 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
92 #define	P2SAMEHIGHBIT_TYPED(x, y, type)		\
93 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
94 
95 
96 /* avoid any possibility of clashing with <stddef.h> version */
97 #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof)
98 #define	offsetof(s, m)	((size_t)(&(((s *)0)->m)))
99 #endif
100 
101 #endif /* _LIBSPL_SYS_SYSMACROS_H */
102