The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in |
| 12 | * the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 | * SUCH DAMAGE. |
| 27 | */ |
Elliott Hughes | 203e13d | 2016-07-22 14:56:18 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | dd6763a | 2018-10-04 16:35:13 -0700 | [diff] [blame] | 29 | #pragma once |
| 30 | |
| 31 | /** |
| 32 | * @file sys/param.h |
| 33 | * @brief Various macros. |
| 34 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | |
Elliott Hughes | 414dd2d | 2024-10-16 14:48:30 +0000 | [diff] [blame] | 36 | #include <sys/cdefs.h> |
| 37 | |
Elliott Hughes | 00dda09 | 2018-10-05 14:54:48 -0700 | [diff] [blame] | 38 | #include <endian.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 39 | #include <limits.h> |
Yabin Cui | 2d8f9b5 | 2015-02-09 13:58:28 -0800 | [diff] [blame] | 40 | #include <linux/param.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | |
Elliott Hughes | dd6763a | 2018-10-04 16:35:13 -0700 | [diff] [blame] | 42 | /** The unit of `st_blocks` in `struct stat`. */ |
Elliott Hughes | 1f1a51a | 2016-03-31 17:05:30 -0700 | [diff] [blame] | 43 | #define DEV_BSIZE 512 |
| 44 | |
Elliott Hughes | e5bd239 | 2024-04-09 16:48:18 +0000 | [diff] [blame] | 45 | /** A historical name for PATH_MAX. Use PATH_MAX in new code. */ |
| 46 | #define MAXPATHLEN PATH_MAX |
| 47 | |
| 48 | /** A historical name for NGROUPS_MAX. Use NGROUPS_MAX in new code. */ |
| 49 | #define NGROUPS NGROUPS_MAX |
Elliott Hughes | dd6763a | 2018-10-04 16:35:13 -0700 | [diff] [blame] | 50 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | #define MAXSYMLINKS 8 |
| 52 | |
Elliott Hughes | c2f082f | 2013-12-12 15:31:35 -0800 | [diff] [blame] | 53 | #ifndef howmany |
| 54 | #define howmany(x, y) (((x)+((y)-1))/(y)) |
| 55 | #endif |
| 56 | #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) |
Elliott Hughes | c2f082f | 2013-12-12 15:31:35 -0800 | [diff] [blame] | 57 | |
Nick Kralevich | 3cfedf4 | 2019-03-27 10:20:06 -0700 | [diff] [blame] | 58 | /** |
Nick Kralevich | c50b6a2 | 2019-03-21 14:04:33 -0700 | [diff] [blame] | 59 | * Returns true if the binary representation of the argument is all zeros |
| 60 | * or has exactly one bit set. Contrary to the macro name, this macro |
| 61 | * DOES NOT determine if the provided value is a power of 2. In particular, |
| 62 | * this function falsely returns true for powerof2(0) and some negative |
| 63 | * numbers. |
| 64 | */ |
Nick Kralevich | 3cfedf4 | 2019-03-27 10:20:06 -0700 | [diff] [blame] | 65 | #define powerof2(x) \ |
| 66 | ({ \ |
| 67 | __typeof__(x) _x = (x); \ |
| 68 | __typeof__(x) _x2; \ |
| 69 | __builtin_add_overflow(_x, -1, &_x2) ? 1 : ((_x2 & _x) == 0); \ |
| 70 | }) |
Elliott Hughes | dd6763a | 2018-10-04 16:35:13 -0700 | [diff] [blame] | 71 | |
| 72 | /** Returns the lesser of its two arguments. */ |
Elliott Hughes | c2f082f | 2013-12-12 15:31:35 -0800 | [diff] [blame] | 73 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
Elliott Hughes | dd6763a | 2018-10-04 16:35:13 -0700 | [diff] [blame] | 74 | /** Returns the greater of its two arguments. */ |
Elliott Hughes | c2f082f | 2013-12-12 15:31:35 -0800 | [diff] [blame] | 75 | #define MAX(a,b) (((a)>(b))?(a):(b)) |