xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/sys/sysmacros.h (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
15  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
16  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
17  */
18 
19 #ifndef _LIBSPL_SYS_SYSMACROS_H
20 #define	_LIBSPL_SYS_SYSMACROS_H
21 
22 #include <stdint.h>
23 #include <limits.h>
24 
25 #ifdef __linux__
26 /*
27  * On Linux, we need the system-provided sysmacros.h to get the makedev(),
28  * major() and minor() definitions for makedevice() below. FreeBSD does not
29  * have this header, so include_next won't find it and will abort. So, we
30  * protect it with a platform check.
31  */
32 #ifndef IN_BASE
33 #include_next <sys/sysmacros.h>
34 #endif
35 #endif
36 
37 #ifdef __FreeBSD__
38 #include <sys/param.h>
39 #endif
40 
41 /* common macros */
42 #ifndef MIN
43 #define	MIN(a, b)	((a) < (b) ? (a) : (b))
44 #endif
45 #ifndef MAX
46 #define	MAX(a, b)	((a) < (b) ? (b) : (a))
47 #endif
48 #ifndef ABS
49 #define	ABS(a)		((a) < 0 ? -(a) : (a))
50 #endif
51 #ifndef ARRAY_SIZE
52 #define	ARRAY_SIZE(a) (sizeof (a) / sizeof (a[0]))
53 #endif
54 #ifndef	DIV_ROUND_UP
55 #define	DIV_ROUND_UP(n, d)	(((n) + (d) - 1) / (d))
56 #endif
57 
58 #define	makedevice(maj, min)	makedev(maj, min)
59 #define	_sysconf(a)		sysconf(a)
60 
61 /*
62  * Compatibility macros/typedefs needed for Solaris -> Linux port
63  */
64 // Deprecated. Use P2ALIGN_TYPED instead.
65 // #define	P2ALIGN(x, align)	((x) & -(align))
66 #define	P2CROSS(x, y, align)	(((x) ^ (y)) > (align) - 1)
67 #define	P2ROUNDUP(x, align)	((((x) - 1) | ((align) - 1)) + 1)
68 #define	P2BOUNDARY(off, len, align) \
69 				(((off) ^ ((off) + (len) - 1)) > (align) - 1)
70 #define	P2PHASE(x, align)	((x) & ((align) - 1))
71 #define	P2NPHASE(x, align)	(-(x) & ((align) - 1))
72 #define	P2NPHASE_TYPED(x, align, type) \
73 				(-(type)(x) & ((type)(align) - 1))
74 #define	ISP2(x)			(((x) & ((x) - 1)) == 0)
75 #define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
76 
77 /*
78  * Typed version of the P2* macros.  These macros should be used to ensure
79  * that the result is correctly calculated based on the data type of (x),
80  * which is passed in as the last argument, regardless of the data
81  * type of the alignment.  For example, if (x) is of type uint64_t,
82  * and we want to round it up to a page boundary using "PAGESIZE" as
83  * the alignment, we can do either
84  *      P2ROUNDUP(x, (uint64_t)PAGESIZE)
85  * or
86  *      P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
87  */
88 #define	P2ALIGN_TYPED(x, align, type)		\
89 	((type)(x) & -(type)(align))
90 #define	P2PHASE_TYPED(x, align, type)		\
91 	((type)(x) & ((type)(align) - 1))
92 #define	P2NPHASE_TYPED(x, align, type)		\
93 	(-(type)(x) & ((type)(align) - 1))
94 #define	P2ROUNDUP_TYPED(x, align, type)		\
95 	((((type)(x) - 1) | ((type)(align) - 1)) + 1)
96 #define	P2END_TYPED(x, align, type)		\
97 	(-(~(type)(x) & -(type)(align)))
98 #define	P2PHASEUP_TYPED(x, align, phase, type)	\
99 	((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
100 #define	P2CROSS_TYPED(x, y, align, type)	\
101 	(((type)(x) ^ (type)(y)) > (type)(align) - 1)
102 #define	P2SAMEHIGHBIT_TYPED(x, y, type)		\
103 	(((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
104 
105 #define	max_ncpus	64
106 #define	boot_ncpus	(sysconf(_SC_NPROCESSORS_ONLN))
107 
108 /*
109  * Process priorities as defined by setpriority(2) and getpriority(2).
110  */
111 #define	minclsyspri	19
112 #define	defclsyspri	0
113 /* Write issue taskq priority. */
114 #define	wtqclsyspri	-19
115 #define	maxclsyspri	-20
116 
117 #define	CPU_SEQID	((uintptr_t)pthread_self() & (max_ncpus - 1))
118 #define	CPU_SEQID_UNSTABLE	CPU_SEQID
119 
120 /*
121  * Find highest one bit set.
122  * Returns bit number + 1 of highest bit that is set, otherwise returns 0.
123  */
124 static inline int
highbit64(uint64_t i)125 highbit64(uint64_t i)
126 {
127 	if (i == 0)
128 		return (0);
129 
130 	return (CHAR_BIT * sizeof (uint64_t) - __builtin_clzll(i));
131 }
132 
133 /*
134  * Find lowest one bit set.
135  * Returns bit number + 1 of lowest bit that is set, otherwise returns 0.
136  * The __builtin_ffsll() function is supported by both GCC and Clang.
137  */
138 static inline int
lowbit64(uint64_t i)139 lowbit64(uint64_t i)
140 {
141 	if (i == 0)
142 		return (0);
143 
144 	return (__builtin_ffsll(i));
145 }
146 
147 #endif /* _SYS_SYSMACROS_H */
148