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
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.
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]
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #ifndef _SOL_SYS_SYSMACROS_H
28 #define _SOL_SYS_SYSMACROS_H
30 #include_next <sys/sysmacros.h>
32 #define makedevice(maj,min) makedev(maj,min)
36 #define MIN(a, b) ((a) < (b) ? (a) : (b))
39 #define MAX(a, b) ((a) < (b) ? (b) : (a))
42 #define ABS(a) ((a) < 0 ? -(a) : (a))
46 * Compatibility macros/typedefs needed for Solaris -> Linux port
48 #define P2ALIGN(x, align) ((x) & -(align))
49 #define P2CROSS(x, y, align) (((x) ^ (y)) > (align) - 1)
50 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
51 #define P2ROUNDUP_TYPED(x, align, type) \
52 (-(-(type)(x) & -(type)(align)))
53 #define P2PHASE(x, align) ((x) & ((align) - 1))
54 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
55 #define P2NPHASE_TYPED(x, align, type) \
56 (-(type)(x) & ((type)(align) - 1))
57 #define ISP2(x) (((x) & ((x) - 1)) == 0)
58 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
61 * Typed version of the P2* macros. These macros should be used to ensure
62 * that the result is correctly calculated based on the data type of (x),
63 * which is passed in as the last argument, regardless of the data
64 * type of the alignment. For example, if (x) is of type uint64_t,
65 * and we want to round it up to a page boundary using "PAGESIZE" as
66 * the alignment, we can do either
67 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
69 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
71 #define P2ALIGN_TYPED(x, align, type) \
72 ((type)(x) & -(type)(align))
73 #define P2PHASE_TYPED(x, align, type) \
74 ((type)(x) & ((type)(align) - 1))
75 #define P2NPHASE_TYPED(x, align, type) \
76 (-(type)(x) & ((type)(align) - 1))
77 #define P2ROUNDUP_TYPED(x, align, type) \
78 (-(-(type)(x) & -(type)(align)))
79 #define P2END_TYPED(x, align, type) \
80 (-(~(type)(x) & -(type)(align)))
81 #define P2PHASEUP_TYPED(x, align, phase, type) \
82 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
83 #define P2CROSS_TYPED(x, y, align, type) \
84 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
85 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
86 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
88 #if defined(_KERNEL) && !defined(_KMEMUSER) && !defined(offsetof)
90 /* avoid any possibility of clashing with <stddef.h> version */
92 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))