Print this page
3373 gcc >= 4.5 concerns about offsetof()
Portions contributed by: Igor Pashev <pashev.igor@gmail.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/sys/sysmacros.h
+++ new/usr/src/uts/common/sys/sysmacros.h
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 22 /* All Rights Reserved */
23 23
24 24
25 25 /*
26 26 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 *
29 29 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
30 30 */
31 31
32 32 #ifndef _SYS_SYSMACROS_H
33 33 #define _SYS_SYSMACROS_H
34 34
35 35 #include <sys/param.h>
36 36
37 37 #ifdef __cplusplus
38 38 extern "C" {
39 39 #endif
40 40
41 41 /*
42 42 * Some macros for units conversion
43 43 */
44 44 /*
45 45 * Disk blocks (sectors) and bytes.
46 46 */
47 47 #define dtob(DD) ((DD) << DEV_BSHIFT)
48 48 #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
49 49 #define btodt(BB) ((BB) >> DEV_BSHIFT)
50 50 #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT)
51 51
52 52 /* common macros */
53 53 #ifndef MIN
54 54 #define MIN(a, b) ((a) < (b) ? (a) : (b))
55 55 #endif
56 56 #ifndef MAX
57 57 #define MAX(a, b) ((a) < (b) ? (b) : (a))
58 58 #endif
59 59 #ifndef ABS
60 60 #define ABS(a) ((a) < 0 ? -(a) : (a))
61 61 #endif
62 62 #ifndef SIGNOF
63 63 #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
64 64 #endif
65 65
66 66 #ifdef _KERNEL
67 67
68 68 /*
69 69 * Convert a single byte to/from binary-coded decimal (BCD).
70 70 */
71 71 extern unsigned char byte_to_bcd[256];
72 72 extern unsigned char bcd_to_byte[256];
73 73
74 74 #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff]
75 75 #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff]
76 76
77 77 #endif /* _KERNEL */
78 78
79 79 /*
80 80 * WARNING: The device number macros defined here should not be used by device
81 81 * drivers or user software. Device drivers should use the device functions
82 82 * defined in the DDI/DKI interface (see also ddi.h). Application software
83 83 * should make use of the library routines available in makedev(3). A set of
84 84 * new device macros are provided to operate on the expanded device number
85 85 * format supported in SVR4. Macro versions of the DDI device functions are
86 86 * provided for use by kernel proper routines only. Macro routines bmajor(),
87 87 * major(), minor(), emajor(), eminor(), and makedev() will be removed or
88 88 * their definitions changed at the next major release following SVR4.
89 89 */
90 90
91 91 #define O_BITSMAJOR 7 /* # of SVR3 major device bits */
92 92 #define O_BITSMINOR 8 /* # of SVR3 minor device bits */
93 93 #define O_MAXMAJ 0x7f /* SVR3 max major value */
94 94 #define O_MAXMIN 0xff /* SVR3 max minor value */
95 95
96 96
97 97 #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */
98 98 #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */
99 99 #define L_MAXMAJ32 0x3fff /* SVR4 max major value */
100 100 #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */
101 101 /* For 3b2 hardware devices the minor is */
102 102 /* restricted to 256 (0-255) */
103 103
104 104 #ifdef _LP64
105 105 #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */
106 106 #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */
107 107 #define L_MAXMAJ 0xfffffffful /* max major value */
108 108 #define L_MAXMIN 0xfffffffful /* max minor value */
109 109 #else
110 110 #define L_BITSMAJOR L_BITSMAJOR32
111 111 #define L_BITSMINOR L_BITSMINOR32
112 112 #define L_MAXMAJ L_MAXMAJ32
113 113 #define L_MAXMIN L_MAXMIN32
114 114 #endif
115 115
116 116 #ifdef _KERNEL
117 117
118 118 /* major part of a device internal to the kernel */
119 119
120 120 #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
121 121 #define bmajor(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
122 122
123 123 /* get internal major part of expanded device number */
124 124
125 125 #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ)
126 126
127 127 /* minor part of a device internal to the kernel */
128 128
129 129 #define minor(x) (minor_t)((x) & O_MAXMIN)
130 130
131 131 /* get internal minor part of expanded device number */
132 132
133 133 #define getminor(x) (minor_t)((x) & L_MAXMIN)
134 134
135 135 #else /* _KERNEL */
136 136
137 137 /* major part of a device external from the kernel (same as emajor below) */
138 138
139 139 #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ)
140 140
141 141 /* minor part of a device external from the kernel (same as eminor below) */
142 142
143 143 #define minor(x) (minor_t)((x) & O_MAXMIN)
144 144
145 145 #endif /* _KERNEL */
146 146
147 147 /* create old device number */
148 148
149 149 #define makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN))
150 150
151 151 /* make an new device number */
152 152
153 153 #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN))
154 154
155 155
156 156 /*
157 157 * emajor() allows kernel/driver code to print external major numbers
158 158 * eminor() allows kernel/driver code to print external minor numbers
159 159 */
160 160
161 161 #define emajor(x) \
162 162 (major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \
163 163 NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ)
164 164
165 165 #define eminor(x) \
166 166 (minor_t)((x) & O_MAXMIN)
167 167
168 168 /*
169 169 * get external major and minor device
170 170 * components from expanded device number
171 171 */
172 172 #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \
173 173 NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ))
174 174 #define geteminor(x) (minor_t)((x) & L_MAXMIN)
175 175
176 176 /*
177 177 * These are versions of the kernel routines for compressing and
178 178 * expanding long device numbers that don't return errors.
179 179 */
180 180 #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR)
181 181
182 182 #define DEVCMPL(x) (x)
183 183 #define DEVEXPL(x) (x)
184 184
185 185 #else
186 186
187 187 #define DEVCMPL(x) \
188 188 (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \
189 189 ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \
190 190 ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32)))
191 191
192 192 #define DEVEXPL(x) \
193 193 (((x) == NODEV32) ? NODEV : \
194 194 makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32))
195 195
196 196 #endif /* L_BITSMAJOR32 ... */
197 197
198 198 /* convert to old (SVR3.2) dev format */
199 199
200 200 #define cmpdev(x) \
201 201 (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \
202 202 ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \
203 203 ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN)))
204 204
205 205 /* convert to new (SVR4) dev format */
206 206
207 207 #define expdev(x) \
208 208 (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \
209 209 ((x) & O_MAXMIN))
210 210
211 211 /*
212 212 * Macro for checking power of 2 address alignment.
213 213 */
214 214 #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
215 215
216 216 /*
217 217 * Macros for counting and rounding.
218 218 */
219 219 #define howmany(x, y) (((x)+((y)-1))/(y))
220 220 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
221 221
222 222 /*
223 223 * Macro to determine if value is a power of 2
224 224 */
225 225 #define ISP2(x) (((x) & ((x) - 1)) == 0)
226 226
227 227 /*
228 228 * Macros for various sorts of alignment and rounding. The "align" must
229 229 * be a power of 2. Often times it is a block, sector, or page.
230 230 */
231 231
232 232 /*
233 233 * return x rounded down to an align boundary
234 234 * eg, P2ALIGN(1200, 1024) == 1024 (1*align)
235 235 * eg, P2ALIGN(1024, 1024) == 1024 (1*align)
236 236 * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
237 237 * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
238 238 */
239 239 #define P2ALIGN(x, align) ((x) & -(align))
240 240
241 241 /*
242 242 * return x % (mod) align
243 243 * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
244 244 * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
245 245 */
246 246 #define P2PHASE(x, align) ((x) & ((align) - 1))
247 247
248 248 /*
249 249 * return how much space is left in this block (but if it's perfectly
250 250 * aligned, return 0).
251 251 * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
252 252 * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
253 253 */
254 254 #define P2NPHASE(x, align) (-(x) & ((align) - 1))
255 255
256 256 /*
257 257 * return x rounded up to an align boundary
258 258 * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
259 259 * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
260 260 */
261 261 #define P2ROUNDUP(x, align) (-(-(x) & -(align)))
262 262
263 263 /*
264 264 * return the ending address of the block that x is in
265 265 * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
266 266 * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
267 267 */
268 268 #define P2END(x, align) (-(~(x) & -(align)))
269 269
270 270 /*
271 271 * return x rounded up to the next phase (offset) within align.
272 272 * phase should be < align.
273 273 * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
274 274 * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
275 275 */
276 276 #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align)))
277 277
278 278 /*
279 279 * return TRUE if adding len to off would cause it to cross an align
280 280 * boundary.
281 281 * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
282 282 * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
283 283 */
284 284 #define P2BOUNDARY(off, len, align) \
285 285 (((off) ^ ((off) + (len) - 1)) > (align) - 1)
286 286
287 287 /*
288 288 * Return TRUE if they have the same highest bit set.
289 289 * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
290 290 * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
291 291 */
292 292 #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
293 293
294 294 /*
295 295 * Typed version of the P2* macros. These macros should be used to ensure
296 296 * that the result is correctly calculated based on the data type of (x),
297 297 * which is passed in as the last argument, regardless of the data
298 298 * type of the alignment. For example, if (x) is of type uint64_t,
299 299 * and we want to round it up to a page boundary using "PAGESIZE" as
300 300 * the alignment, we can do either
301 301 * P2ROUNDUP(x, (uint64_t)PAGESIZE)
302 302 * or
303 303 * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
304 304 */
305 305 #define P2ALIGN_TYPED(x, align, type) \
306 306 ((type)(x) & -(type)(align))
307 307 #define P2PHASE_TYPED(x, align, type) \
308 308 ((type)(x) & ((type)(align) - 1))
309 309 #define P2NPHASE_TYPED(x, align, type) \
310 310 (-(type)(x) & ((type)(align) - 1))
311 311 #define P2ROUNDUP_TYPED(x, align, type) \
312 312 (-(-(type)(x) & -(type)(align)))
313 313 #define P2END_TYPED(x, align, type) \
314 314 (-(~(type)(x) & -(type)(align)))
315 315 #define P2PHASEUP_TYPED(x, align, phase, type) \
316 316 ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align)))
317 317 #define P2CROSS_TYPED(x, y, align, type) \
318 318 (((type)(x) ^ (type)(y)) > (type)(align) - 1)
319 319 #define P2SAMEHIGHBIT_TYPED(x, y, type) \
320 320 (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y)))
321 321
322 322 /*
323 323 * Macros to atomically increment/decrement a variable. mutex and var
324 324 * must be pointers.
325 325 */
326 326 #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex)
327 327 #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex)
328 328
329 329 /*
330 330 * Macros to declare bitfields - the order in the parameter list is
331 331 * Low to High - that is, declare bit 0 first. We only support 8-bit bitfields
332 332 * because if a field crosses a byte boundary it's not likely to be meaningful
333 333 * without reassembly in its nonnative endianness.
334 334 */
335 335 #if defined(_BIT_FIELDS_LTOH)
336 336 #define DECL_BITFIELD2(_a, _b) \
337 337 uint8_t _a, _b
338 338 #define DECL_BITFIELD3(_a, _b, _c) \
339 339 uint8_t _a, _b, _c
340 340 #define DECL_BITFIELD4(_a, _b, _c, _d) \
341 341 uint8_t _a, _b, _c, _d
342 342 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
343 343 uint8_t _a, _b, _c, _d, _e
344 344 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
345 345 uint8_t _a, _b, _c, _d, _e, _f
346 346 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
347 347 uint8_t _a, _b, _c, _d, _e, _f, _g
348 348 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
349 349 uint8_t _a, _b, _c, _d, _e, _f, _g, _h
350 350 #elif defined(_BIT_FIELDS_HTOL)
351 351 #define DECL_BITFIELD2(_a, _b) \
352 352 uint8_t _b, _a
353 353 #define DECL_BITFIELD3(_a, _b, _c) \
354 354 uint8_t _c, _b, _a
355 355 #define DECL_BITFIELD4(_a, _b, _c, _d) \
356 356 uint8_t _d, _c, _b, _a
357 357 #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \
358 358 uint8_t _e, _d, _c, _b, _a
359 359 #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \
360 360 uint8_t _f, _e, _d, _c, _b, _a
361 361 #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \
362 362 uint8_t _g, _f, _e, _d, _c, _b, _a
↓ open down ↓ |
362 lines elided |
↑ open up ↑ |
363 363 #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \
364 364 uint8_t _h, _g, _f, _e, _d, _c, _b, _a
365 365 #else
366 366 #error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined
367 367 #endif /* _BIT_FIELDS_LTOH */
368 368
369 369 /* avoid any possibility of clashing with <stddef.h> version */
370 370 #if (defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_KMEMUSER)
371 371
372 372 #if !defined(offsetof)
373 +#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
374 +#define offsetof(s, m) __builtin_offsetof(s, m)
375 +#else
373 376 #define offsetof(s, m) ((size_t)(&(((s *)0)->m)))
377 +#endif
374 378 #endif /* !offsetof */
375 379
376 380 #define container_of(m, s, name) \
377 381 (void *)((uintptr_t)(m) - (uintptr_t)offsetof(s, name))
378 382
379 383 #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0]))
380 384 #endif /* _KERNEL, !_KMEMUSER */
381 385
382 386 #ifdef __cplusplus
383 387 }
384 388 #endif
385 389
386 390 #endif /* _SYS_SYSMACROS_H */
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX