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