sysmacros.h revision b127ac411761a3d8d642d9342d9cac2785e1faaa
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _SYS_SYSMACROS_H
#define _SYS_SYSMACROS_H
#ifdef __cplusplus
extern "C" {
#endif
/*
* Some macros for units conversion
*/
/*
* Disk blocks (sectors) and bytes.
*/
/* common macros */
#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#endif
#ifndef ABS
#define ABS(a) ((a) < 0 ? -(a) : (a))
#endif
#ifndef SIGNOF
#define SIGNOF(a) ((a) < 0 ? -1 : (a) > 0)
#endif
#ifdef _KERNEL
/*
*/
extern unsigned char byte_to_bcd[256];
extern unsigned char bcd_to_byte[256];
#endif /* _KERNEL */
/*
* WARNING: The device number macros defined here should not be used by device
* drivers or user software. Device drivers should use the device functions
* should make use of the library routines available in makedev(3). A set of
* new device macros are provided to operate on the expanded device number
* format supported in SVR4. Macro versions of the DDI device functions are
* provided for use by kernel proper routines only. Macro routines bmajor(),
* major(), minor(), emajor(), eminor(), and makedev() will be removed or
* their definitions changed at the next major release following SVR4.
*/
/* For 3b2 hardware devices the minor is */
/* restricted to 256 (0-255) */
#ifdef _LP64
#else
#define L_BITSMAJOR L_BITSMAJOR32
#define L_BITSMINOR L_BITSMINOR32
#define L_MAXMAJ L_MAXMAJ32
#define L_MAXMIN L_MAXMIN32
#endif
#ifdef _KERNEL
/* major part of a device internal to the kernel */
/* get internal major part of expanded device number */
/* minor part of a device internal to the kernel */
/* get internal minor part of expanded device number */
#else
/* major part of a device external from the kernel (same as emajor below) */
/* minor part of a device external from the kernel (same as eminor below) */
#endif /* _KERNEL */
/* create old device number */
/* make an new device number */
/*
*/
#define emajor(x) \
#define eminor(x) \
/*
* get external major and minor device
* components from expanded device number
*/
/*
* These are versions of the kernel routines for compressing and
* expanding long device numbers that don't return errors.
*/
#define DEVCMPL(x) (x)
#define DEVEXPL(x) (x)
#else
#define DEVCMPL(x) \
#define DEVEXPL(x) \
#endif /* L_BITSMAJOR32 ... */
/* convert to old (SVR3.2) dev format */
#define cmpdev(x) \
/* convert to new (SVR4) dev format */
#define expdev(x) \
((x) & O_MAXMIN))
/*
* Macro for checking power of 2 address alignment.
*/
/*
* Macros for counting and rounding.
*/
#define howmany(x, y) (((x)+((y)-1))/(y))
#define roundup(x, y) ((((x)+((y)-1))/(y))*(y))
/*
* Macro to determine if value is a power of 2
*/
#define ISP2(x) (((x) & ((x) - 1)) == 0)
/*
* Macros for various sorts of alignment and rounding. The "align" must
* be a power of 2. Often times it is a block, sector, or page.
*/
/*
* return x rounded down to an align boundary
* eg, P2ALIGN(1200, 1024) == 1024 (1*align)
* eg, P2ALIGN(1024, 1024) == 1024 (1*align)
* eg, P2ALIGN(0x1234, 0x100) == 0x1200 (0x12*align)
* eg, P2ALIGN(0x5600, 0x100) == 0x5600 (0x56*align)
*/
/*
* return x % (mod) align
* eg, P2PHASE(0x1234, 0x100) == 0x34 (x-0x12*align)
* eg, P2PHASE(0x5600, 0x100) == 0x00 (x-0x56*align)
*/
/*
* return how much space is left in this block (but if it's perfectly
* aligned, return 0).
* eg, P2NPHASE(0x1234, 0x100) == 0xcc (0x13*align-x)
* eg, P2NPHASE(0x5600, 0x100) == 0x00 (0x56*align-x)
*/
/*
* return x rounded up to an align boundary
* eg, P2ROUNDUP(0x1234, 0x100) == 0x1300 (0x13*align)
* eg, P2ROUNDUP(0x5600, 0x100) == 0x5600 (0x56*align)
*/
/*
* return the ending address of the block that x is in
* eg, P2END(0x1234, 0x100) == 0x12ff (0x13*align - 1)
* eg, P2END(0x5600, 0x100) == 0x56ff (0x57*align - 1)
*/
/*
* return x rounded up to the next phase (offset) within align.
* phase should be < align.
* eg, P2PHASEUP(0x1234, 0x100, 0x10) == 0x1310 (0x13*align + phase)
* eg, P2PHASEUP(0x5600, 0x100, 0x10) == 0x5610 (0x56*align + phase)
*/
/*
* return TRUE if adding len to off would cause it to cross an align
* boundary.
* eg, P2BOUNDARY(0x1234, 0xe0, 0x100) == TRUE (0x1234 + 0xe0 == 0x1314)
* eg, P2BOUNDARY(0x1234, 0x50, 0x100) == FALSE (0x1234 + 0x50 == 0x1284)
*/
/*
* Return TRUE if they have the same highest bit set.
* eg, P2SAMEHIGHBIT(0x1234, 0x1001) == TRUE (the high bit is 0x1000)
* eg, P2SAMEHIGHBIT(0x1234, 0x3010) == FALSE (high bit of 0x3010 is 0x2000)
*/
#define P2SAMEHIGHBIT(x, y) (((x) ^ (y)) < ((x) & (y)))
/*
* Typed version of the P2* macros. These macros should be used to ensure
* that the result is correctly calculated based on the data type of (x),
* which is passed in as the last argument, regardless of the data
* type of the alignment. For example, if (x) is of type uint64_t,
* and we want to round it up to a page boundary using "PAGESIZE" as
* the alignment, we can do either
* P2ROUNDUP(x, (uint64_t)PAGESIZE)
* or
* P2ROUNDUP_TYPED(x, PAGESIZE, uint64_t)
*/
#define P2SAMEHIGHBIT_TYPED(x, y, type) \
/*
* must be pointers.
*/
/*
* Macros to declare bitfields - the order in the parameter list is
* Low to High - that is, declare bit 0 first. We only support 8-bit bitfields
* because if a field crosses a byte boundary it's not likely to be meaningful
* without reassembly in its nonnative endianness.
*/
#if defined(_BIT_FIELDS_LTOH)
#elif defined(_BIT_FIELDS_HTOL)
#else
#endif /* _BIT_FIELDS_LTOH */
/* avoid any possibility of clashing with <stddef.h> version */
#endif
#ifdef __cplusplus
}
#endif
#endif /* _SYS_SYSMACROS_H */