17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52c32020fSeschrock * Common Development and Distribution License (the "License"). 62c32020fSeschrock * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 227c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* 2688b7b0f2SMatthew Ahrens * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 277c478bd9Sstevel@tonic-gate * Use is subject to license terms. 281d99877fSAlexey Zaytsev * 29b819cea2SGordon Ross * Copyright 2013 Nexenta Systems, Inc. All rights reserved. 307c478bd9Sstevel@tonic-gate */ 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #ifndef _SYS_SYSMACROS_H 337c478bd9Sstevel@tonic-gate #define _SYS_SYSMACROS_H 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #include <sys/param.h> 36*4bf09fccSToomas Soome #include <sys/stddef.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #ifdef __cplusplus 397c478bd9Sstevel@tonic-gate extern "C" { 407c478bd9Sstevel@tonic-gate #endif 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate /* 437c478bd9Sstevel@tonic-gate * Some macros for units conversion 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate /* 467c478bd9Sstevel@tonic-gate * Disk blocks (sectors) and bytes. 477c478bd9Sstevel@tonic-gate */ 487c478bd9Sstevel@tonic-gate #define dtob(DD) ((DD) << DEV_BSHIFT) 497c478bd9Sstevel@tonic-gate #define btod(BB) (((BB) + DEV_BSIZE - 1) >> DEV_BSHIFT) 507c478bd9Sstevel@tonic-gate #define btodt(BB) ((BB) >> DEV_BSHIFT) 517c478bd9Sstevel@tonic-gate #define lbtod(BB) (((offset_t)(BB) + DEV_BSIZE - 1) >> DEV_BSHIFT) 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* common macros */ 547c478bd9Sstevel@tonic-gate #ifndef MIN 557c478bd9Sstevel@tonic-gate #define MIN(a, b) ((a) < (b) ? (a) : (b)) 567c478bd9Sstevel@tonic-gate #endif 577c478bd9Sstevel@tonic-gate #ifndef MAX 587c478bd9Sstevel@tonic-gate #define MAX(a, b) ((a) < (b) ? (b) : (a)) 597c478bd9Sstevel@tonic-gate #endif 607c478bd9Sstevel@tonic-gate #ifndef ABS 617c478bd9Sstevel@tonic-gate #define ABS(a) ((a) < 0 ? -(a) : (a)) 627c478bd9Sstevel@tonic-gate #endif 63b127ac41SPhilip Kirk #ifndef SIGNOF 64b127ac41SPhilip Kirk #define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0) 65b127ac41SPhilip Kirk #endif 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate #ifdef _KERNEL 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate /* 707c478bd9Sstevel@tonic-gate * Convert a single byte to/from binary-coded decimal (BCD). 717c478bd9Sstevel@tonic-gate */ 727c478bd9Sstevel@tonic-gate extern unsigned char byte_to_bcd[256]; 737c478bd9Sstevel@tonic-gate extern unsigned char bcd_to_byte[256]; 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate #define BYTE_TO_BCD(x) byte_to_bcd[(x) & 0xff] 767c478bd9Sstevel@tonic-gate #define BCD_TO_BYTE(x) bcd_to_byte[(x) & 0xff] 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * WARNING: The device number macros defined here should not be used by device 827c478bd9Sstevel@tonic-gate * drivers or user software. Device drivers should use the device functions 837c478bd9Sstevel@tonic-gate * defined in the DDI/DKI interface (see also ddi.h). Application software 847c478bd9Sstevel@tonic-gate * should make use of the library routines available in makedev(3). A set of 857c478bd9Sstevel@tonic-gate * new device macros are provided to operate on the expanded device number 867c478bd9Sstevel@tonic-gate * format supported in SVR4. Macro versions of the DDI device functions are 877c478bd9Sstevel@tonic-gate * provided for use by kernel proper routines only. Macro routines bmajor(), 887c478bd9Sstevel@tonic-gate * major(), minor(), emajor(), eminor(), and makedev() will be removed or 897c478bd9Sstevel@tonic-gate * their definitions changed at the next major release following SVR4. 907c478bd9Sstevel@tonic-gate */ 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate #define O_BITSMAJOR 7 /* # of SVR3 major device bits */ 937c478bd9Sstevel@tonic-gate #define O_BITSMINOR 8 /* # of SVR3 minor device bits */ 947c478bd9Sstevel@tonic-gate #define O_MAXMAJ 0x7f /* SVR3 max major value */ 957c478bd9Sstevel@tonic-gate #define O_MAXMIN 0xff /* SVR3 max minor value */ 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate 987c478bd9Sstevel@tonic-gate #define L_BITSMAJOR32 14 /* # of SVR4 major device bits */ 997c478bd9Sstevel@tonic-gate #define L_BITSMINOR32 18 /* # of SVR4 minor device bits */ 1007c478bd9Sstevel@tonic-gate #define L_MAXMAJ32 0x3fff /* SVR4 max major value */ 1017c478bd9Sstevel@tonic-gate #define L_MAXMIN32 0x3ffff /* MAX minor for 3b2 software drivers. */ 1027c478bd9Sstevel@tonic-gate /* For 3b2 hardware devices the minor is */ 1037c478bd9Sstevel@tonic-gate /* restricted to 256 (0-255) */ 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate #ifdef _LP64 1067c478bd9Sstevel@tonic-gate #define L_BITSMAJOR 32 /* # of major device bits in 64-bit Solaris */ 1077c478bd9Sstevel@tonic-gate #define L_BITSMINOR 32 /* # of minor device bits in 64-bit Solaris */ 1087c478bd9Sstevel@tonic-gate #define L_MAXMAJ 0xfffffffful /* max major value */ 1097c478bd9Sstevel@tonic-gate #define L_MAXMIN 0xfffffffful /* max minor value */ 1107c478bd9Sstevel@tonic-gate #else 1117c478bd9Sstevel@tonic-gate #define L_BITSMAJOR L_BITSMAJOR32 1127c478bd9Sstevel@tonic-gate #define L_BITSMINOR L_BITSMINOR32 1137c478bd9Sstevel@tonic-gate #define L_MAXMAJ L_MAXMAJ32 1147c478bd9Sstevel@tonic-gate #define L_MAXMIN L_MAXMIN32 1157c478bd9Sstevel@tonic-gate #endif 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate #ifdef _KERNEL 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate /* major part of a device internal to the kernel */ 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 1227c478bd9Sstevel@tonic-gate #define bmajor(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate /* get internal major part of expanded device number */ 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate #define getmajor(x) (major_t)((((dev_t)(x)) >> L_BITSMINOR) & L_MAXMAJ) 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate /* minor part of a device internal to the kernel */ 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate #define minor(x) (minor_t)((x) & O_MAXMIN) 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* get internal minor part of expanded device number */ 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate #define getminor(x) (minor_t)((x) & L_MAXMIN) 1357c478bd9Sstevel@tonic-gate 136b819cea2SGordon Ross #else /* _KERNEL */ 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate /* major part of a device external from the kernel (same as emajor below) */ 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate #define major(x) (major_t)((((unsigned)(x)) >> O_BITSMINOR) & O_MAXMAJ) 1417c478bd9Sstevel@tonic-gate 1427c478bd9Sstevel@tonic-gate /* minor part of a device external from the kernel (same as eminor below) */ 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate #define minor(x) (minor_t)((x) & O_MAXMIN) 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate #endif /* _KERNEL */ 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* create old device number */ 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate #define makedev(x, y) (unsigned short)(((x) << O_BITSMINOR) | ((y) & O_MAXMIN)) 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* make an new device number */ 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate #define makedevice(x, y) (dev_t)(((dev_t)(x) << L_BITSMINOR) | ((y) & L_MAXMIN)) 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate /* 1587c478bd9Sstevel@tonic-gate * emajor() allows kernel/driver code to print external major numbers 1597c478bd9Sstevel@tonic-gate * eminor() allows kernel/driver code to print external minor numbers 1607c478bd9Sstevel@tonic-gate */ 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate #define emajor(x) \ 1637c478bd9Sstevel@tonic-gate (major_t)(((unsigned int)(x) >> O_BITSMINOR) > O_MAXMAJ) ? \ 1647c478bd9Sstevel@tonic-gate NODEV : (((unsigned int)(x) >> O_BITSMINOR) & O_MAXMAJ) 1657c478bd9Sstevel@tonic-gate 1667c478bd9Sstevel@tonic-gate #define eminor(x) \ 1677c478bd9Sstevel@tonic-gate (minor_t)((x) & O_MAXMIN) 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * get external major and minor device 1717c478bd9Sstevel@tonic-gate * components from expanded device number 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate #define getemajor(x) (major_t)((((dev_t)(x) >> L_BITSMINOR) > L_MAXMAJ) ? \ 1747c478bd9Sstevel@tonic-gate NODEV : (((dev_t)(x) >> L_BITSMINOR) & L_MAXMAJ)) 1757c478bd9Sstevel@tonic-gate #define geteminor(x) (minor_t)((x) & L_MAXMIN) 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate /* 1787c478bd9Sstevel@tonic-gate * These are versions of the kernel routines for compressing and 1797c478bd9Sstevel@tonic-gate * expanding long device numbers that don't return errors. 1807c478bd9Sstevel@tonic-gate */ 1817c478bd9Sstevel@tonic-gate #if (L_BITSMAJOR32 == L_BITSMAJOR) && (L_BITSMINOR32 == L_BITSMINOR) 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate #define DEVCMPL(x) (x) 1847c478bd9Sstevel@tonic-gate #define DEVEXPL(x) (x) 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate #else 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate #define DEVCMPL(x) \ 1897c478bd9Sstevel@tonic-gate (dev32_t)((((x) >> L_BITSMINOR) > L_MAXMAJ32 || \ 1907c478bd9Sstevel@tonic-gate ((x) & L_MAXMIN) > L_MAXMIN32) ? NODEV32 : \ 1917c478bd9Sstevel@tonic-gate ((((x) >> L_BITSMINOR) << L_BITSMINOR32) | ((x) & L_MAXMIN32))) 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate #define DEVEXPL(x) \ 1947c478bd9Sstevel@tonic-gate (((x) == NODEV32) ? NODEV : \ 1957c478bd9Sstevel@tonic-gate makedevice(((x) >> L_BITSMINOR32) & L_MAXMAJ32, (x) & L_MAXMIN32)) 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate #endif /* L_BITSMAJOR32 ... */ 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate /* convert to old (SVR3.2) dev format */ 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate #define cmpdev(x) \ 2027c478bd9Sstevel@tonic-gate (o_dev_t)((((x) >> L_BITSMINOR) > O_MAXMAJ || \ 2037c478bd9Sstevel@tonic-gate ((x) & L_MAXMIN) > O_MAXMIN) ? NODEV : \ 2047c478bd9Sstevel@tonic-gate ((((x) >> L_BITSMINOR) << O_BITSMINOR) | ((x) & O_MAXMIN))) 2057c478bd9Sstevel@tonic-gate 2067c478bd9Sstevel@tonic-gate /* convert to new (SVR4) dev format */ 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate #define expdev(x) \ 2097c478bd9Sstevel@tonic-gate (dev_t)(((dev_t)(((x) >> O_BITSMINOR) & O_MAXMAJ) << L_BITSMINOR) | \ 2107c478bd9Sstevel@tonic-gate ((x) & O_MAXMIN)) 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate /* 2137c478bd9Sstevel@tonic-gate * Macro for checking power of 2 address alignment. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate #define IS_P2ALIGNED(v, a) ((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0) 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate /* 2187c478bd9Sstevel@tonic-gate * Macros for counting and rounding. 2197c478bd9Sstevel@tonic-gate */ 2207c478bd9Sstevel@tonic-gate #define howmany(x, y) (((x)+((y)-1))/(y)) 2217c478bd9Sstevel@tonic-gate #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate /* 2247c478bd9Sstevel@tonic-gate * Macro to determine if value is a power of 2 2257c478bd9Sstevel@tonic-gate */ 2267c478bd9Sstevel@tonic-gate #define ISP2(x) (((x) & ((x) - 1)) == 0) 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate /* 22988b7b0f2SMatthew Ahrens * Macros for various sorts of alignment and rounding. The "align" must 23088b7b0f2SMatthew Ahrens * be a power of 2. Often times it is a block, sector, or page. 23188b7b0f2SMatthew Ahrens */ 23288b7b0f2SMatthew Ahrens 23388b7b0f2SMatthew Ahrens /* 23488b7b0f2SMatthew Ahrens * return x rounded down to an align boundary 23588b7b0f2SMatthew Ahrens * eg, P2ALIGN(1200, 1024) == 1024 (1*align) 23688b7b0f2SMatthew Ahrens * eg, P2ALIGN(1024, 1024) == 1024 (1*align) 23788b7b0f2SMatthew Ahrens * eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align) 23888b7b0f2SMatthew Ahrens * eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align) 2397c478bd9Sstevel@tonic-gate */ 2407c478bd9Sstevel@tonic-gate #define P2ALIGN(x, align) ((x) & -(align)) 24188b7b0f2SMatthew Ahrens 2427c478bd9Sstevel@tonic-gate /* 24388b7b0f2SMatthew Ahrens * return x % (mod) align 24488b7b0f2SMatthew Ahrens * eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align) 24588b7b0f2SMatthew Ahrens * eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align) 24688b7b0f2SMatthew Ahrens */ 24788b7b0f2SMatthew Ahrens #define P2PHASE(x, align) ((x) & ((align) - 1)) 24888b7b0f2SMatthew Ahrens 24988b7b0f2SMatthew Ahrens /* 25088b7b0f2SMatthew Ahrens * return how much space is left in this block (but if it's perfectly 25188b7b0f2SMatthew Ahrens * aligned, return 0). 25288b7b0f2SMatthew Ahrens * eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x) 25388b7b0f2SMatthew Ahrens * eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x) 25488b7b0f2SMatthew Ahrens */ 25588b7b0f2SMatthew Ahrens #define P2NPHASE(x, align) (-(x) & ((align) - 1)) 25688b7b0f2SMatthew Ahrens 25788b7b0f2SMatthew Ahrens /* 25888b7b0f2SMatthew Ahrens * return x rounded up to an align boundary 25988b7b0f2SMatthew Ahrens * eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align) 26088b7b0f2SMatthew Ahrens * eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align) 26188b7b0f2SMatthew Ahrens */ 26288b7b0f2SMatthew Ahrens #define P2ROUNDUP(x, align) (-(-(x) & -(align))) 26388b7b0f2SMatthew Ahrens 26488b7b0f2SMatthew Ahrens /* 26588b7b0f2SMatthew Ahrens * return the ending address of the block that x is in 26688b7b0f2SMatthew Ahrens * eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1) 26788b7b0f2SMatthew Ahrens * eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1) 26888b7b0f2SMatthew Ahrens */ 26988b7b0f2SMatthew Ahrens #define P2END(x, align) (-(~(x) & -(align))) 27088b7b0f2SMatthew Ahrens 27188b7b0f2SMatthew Ahrens /* 27288b7b0f2SMatthew Ahrens * return x rounded up to the next phase (offset) within align. 27388b7b0f2SMatthew Ahrens * phase should be < align. 27488b7b0f2SMatthew Ahrens * eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase) 27588b7b0f2SMatthew Ahrens * eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase) 27688b7b0f2SMatthew Ahrens */ 27788b7b0f2SMatthew Ahrens #define P2PHASEUP(x, align, phase) ((phase) - (((phase) - (x)) & -(align))) 27888b7b0f2SMatthew Ahrens 27988b7b0f2SMatthew Ahrens /* 28088b7b0f2SMatthew Ahrens * return TRUE if adding len to off would cause it to cross an align 28188b7b0f2SMatthew Ahrens * boundary. 28288b7b0f2SMatthew Ahrens * eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314) 28388b7b0f2SMatthew Ahrens * eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284) 28488b7b0f2SMatthew Ahrens */ 28588b7b0f2SMatthew Ahrens #define P2BOUNDARY(off, len, align) \ 28688b7b0f2SMatthew Ahrens (((off) ^ ((off) + (len) - 1)) > (align) - 1) 28788b7b0f2SMatthew Ahrens 28888b7b0f2SMatthew Ahrens /* 28988b7b0f2SMatthew Ahrens * Return TRUE if they have the same highest bit set. 29088b7b0f2SMatthew Ahrens * eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000) 29188b7b0f2SMatthew Ahrens * eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000) 2927c478bd9Sstevel@tonic-gate */ 2937c478bd9Sstevel@tonic-gate #define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y))) 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate /* 2967c478bd9Sstevel@tonic-gate * Typed version of the P2* macros. These macros should be used to ensure 2977c478bd9Sstevel@tonic-gate * that the result is correctly calculated based on the data type of (x), 2987c478bd9Sstevel@tonic-gate * which is passed in as the last argument, regardless of the data 2997c478bd9Sstevel@tonic-gate * type of the alignment. For example, if (x) is of type uint64_t, 3007c478bd9Sstevel@tonic-gate * and we want to round it up to a page boundary using "PAGESIZE" as 3017c478bd9Sstevel@tonic-gate * the alignment, we can do either 3027c478bd9Sstevel@tonic-gate * P2ROUNDUP(x, (uint64_t)PAGESIZE) 3037c478bd9Sstevel@tonic-gate * or 3047c478bd9Sstevel@tonic-gate * P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t) 3057c478bd9Sstevel@tonic-gate */ 3067c478bd9Sstevel@tonic-gate #define P2ALIGN_TYPED(x, align, type) \ 3077c478bd9Sstevel@tonic-gate ((type)(x) & -(type)(align)) 3087c478bd9Sstevel@tonic-gate #define P2PHASE_TYPED(x, align, type) \ 3097c478bd9Sstevel@tonic-gate ((type)(x) & ((type)(align) - 1)) 3107c478bd9Sstevel@tonic-gate #define P2NPHASE_TYPED(x, align, type) \ 3117c478bd9Sstevel@tonic-gate (-(type)(x) & ((type)(align) - 1)) 3127c478bd9Sstevel@tonic-gate #define P2ROUNDUP_TYPED(x, align, type) \ 3137c478bd9Sstevel@tonic-gate (-(-(type)(x) & -(type)(align))) 3147c478bd9Sstevel@tonic-gate #define P2END_TYPED(x, align, type) \ 3157c478bd9Sstevel@tonic-gate (-(~(type)(x) & -(type)(align))) 3167c478bd9Sstevel@tonic-gate #define P2PHASEUP_TYPED(x, align, phase, type) \ 3177c478bd9Sstevel@tonic-gate ((type)(phase) - (((type)(phase) - (type)(x)) & -(type)(align))) 3187c478bd9Sstevel@tonic-gate #define P2CROSS_TYPED(x, y, align, type) \ 3197c478bd9Sstevel@tonic-gate (((type)(x) ^ (type)(y)) > (type)(align) - 1) 3207c478bd9Sstevel@tonic-gate #define P2SAMEHIGHBIT_TYPED(x, y, type) \ 3217c478bd9Sstevel@tonic-gate (((type)(x) ^ (type)(y)) < ((type)(x) & (type)(y))) 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate /* 3247c478bd9Sstevel@tonic-gate * Macros to atomically increment/decrement a variable. mutex and var 3257c478bd9Sstevel@tonic-gate * must be pointers. 3267c478bd9Sstevel@tonic-gate */ 3277c478bd9Sstevel@tonic-gate #define INCR_COUNT(var, mutex) mutex_enter(mutex), (*(var))++, mutex_exit(mutex) 3287c478bd9Sstevel@tonic-gate #define DECR_COUNT(var, mutex) mutex_enter(mutex), (*(var))--, mutex_exit(mutex) 3297c478bd9Sstevel@tonic-gate 3302c32020fSeschrock /* 3312c32020fSeschrock * Macros to declare bitfields - the order in the parameter list is 3322c32020fSeschrock * Low to High - that is, declare bit 0 first. We only support 8-bit bitfields 3332c32020fSeschrock * because if a field crosses a byte boundary it's not likely to be meaningful 3342c32020fSeschrock * without reassembly in its nonnative endianness. 3352c32020fSeschrock */ 3362c32020fSeschrock #if defined(_BIT_FIELDS_LTOH) 3372c32020fSeschrock #define DECL_BITFIELD2(_a, _b) \ 3382c32020fSeschrock uint8_t _a, _b 3392c32020fSeschrock #define DECL_BITFIELD3(_a, _b, _c) \ 3402c32020fSeschrock uint8_t _a, _b, _c 3412c32020fSeschrock #define DECL_BITFIELD4(_a, _b, _c, _d) \ 3422c32020fSeschrock uint8_t _a, _b, _c, _d 3432c32020fSeschrock #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \ 3442c32020fSeschrock uint8_t _a, _b, _c, _d, _e 3452c32020fSeschrock #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \ 3462c32020fSeschrock uint8_t _a, _b, _c, _d, _e, _f 3472c32020fSeschrock #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \ 3482c32020fSeschrock uint8_t _a, _b, _c, _d, _e, _f, _g 3492c32020fSeschrock #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \ 3502c32020fSeschrock uint8_t _a, _b, _c, _d, _e, _f, _g, _h 3512c32020fSeschrock #elif defined(_BIT_FIELDS_HTOL) 3522c32020fSeschrock #define DECL_BITFIELD2(_a, _b) \ 3532c32020fSeschrock uint8_t _b, _a 3542c32020fSeschrock #define DECL_BITFIELD3(_a, _b, _c) \ 3552c32020fSeschrock uint8_t _c, _b, _a 3562c32020fSeschrock #define DECL_BITFIELD4(_a, _b, _c, _d) \ 3572c32020fSeschrock uint8_t _d, _c, _b, _a 3582c32020fSeschrock #define DECL_BITFIELD5(_a, _b, _c, _d, _e) \ 3592c32020fSeschrock uint8_t _e, _d, _c, _b, _a 3602c32020fSeschrock #define DECL_BITFIELD6(_a, _b, _c, _d, _e, _f) \ 3612c32020fSeschrock uint8_t _f, _e, _d, _c, _b, _a 3622c32020fSeschrock #define DECL_BITFIELD7(_a, _b, _c, _d, _e, _f, _g) \ 3632c32020fSeschrock uint8_t _g, _f, _e, _d, _c, _b, _a 3642c32020fSeschrock #define DECL_BITFIELD8(_a, _b, _c, _d, _e, _f, _g, _h) \ 3652c32020fSeschrock uint8_t _h, _g, _f, _e, _d, _c, _b, _a 3662c32020fSeschrock #else 3672c32020fSeschrock #error One of _BIT_FIELDS_LTOH or _BIT_FIELDS_HTOL must be defined 3682c32020fSeschrock #endif /* _BIT_FIELDS_LTOH */ 3692c32020fSeschrock 3707c478bd9Sstevel@tonic-gate /* avoid any possibility of clashing with <stddef.h> version */ 371b819cea2SGordon Ross #if (defined(_KERNEL) || defined(_FAKE_KERNEL)) && !defined(_KMEMUSER) 3727c478bd9Sstevel@tonic-gate 3731d99877fSAlexey Zaytsev #define ARRAY_SIZE(x) (sizeof (x) / sizeof (x[0])) 374*4bf09fccSToomas Soome 3751d99877fSAlexey Zaytsev #endif /* _KERNEL, !_KMEMUSER */ 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate #ifdef __cplusplus 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate #endif 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate #endif /* _SYS_SYSMACROS_H */ 382