当前位置: 首页 > news >正文

__is_constexpr(x)宏介绍---max()宏扩展

1、概述

__is_constexpr是Linux内核中一个重要的宏,用于在编译时检测一个表达式是否为常量表达式。它由Martin Uecker开发,并在2018年4月通过commit 3c8ba0d61d04引入Linux内核v4.17版本。

通过这个宏的设计,我们可以看到Linux内核开发者对编译器和C语言特性的深入理解和巧妙运用。

1.1、内核源码

/// https://elixir.bootlin.com/linux/v6.12/source/include/linux/compiler.h#L248
/** This returns a constant expression while determining if an argument is* a constant expression, most importantly without evaluating the argument.* Glory to Martin Uecker <Martin.Uecker@med.uni-goettingen.de>** Details:* - sizeof() return an integer constant expression, and does not evaluate*   the value of its operand; it only examines the type of its operand.* - The results of comparing two integer constant expressions is also*   an integer constant expression.* - The first literal "8" isn't important. It could be any literal value.* - The second literal "8" is to avoid warnings about unaligned pointers;*   this could otherwise just be "1".* - (long)(x) is used to avoid warnings about 64-bit types on 32-bit*   architectures.* - The C Standard defines "null pointer constant", "(void *)0", as*   distinct from other void pointers.* - If (x) is an integer constant expression, then the "* 0l" resolves*   it into an integer constant expression of value 0. Since it is cast to*   "void *", this makes the second operand a null pointer constant.* - If (x) is not an integer constant expression, then the second operand*   resolves to a void pointer (but not a null pointer constant: the value*   is not an integer constant 0).* - The conditional operator's third operand, "(int *)8", is an object*   pointer (to type "int").* - The behavior (including the return type) of the conditional operator*   ("operand1 ? operand2 : operand3") depends on the kind of expressions*   given for the second and third operands. This is the central mechanism*   of the macro:*   - When one operand is a null pointer constant (i.e. when x is an integer*     constant expression) and the other is an object pointer (i.e. our*     third operand), the conditional operator returns the type of the*     object pointer operand (i.e. "int *"). Here, within the sizeof(), we*     would then get:*       sizeof(*((int *)(...))  == sizeof(int)  == 4*   - When one operand is a void pointer (i.e. when x is not an integer*     constant expression) and the other is an object pointer (i.e. our*     third operand), the conditional operator returns a "void *" type.*     Here, within the sizeof(), we would then get:*       sizeof(*((void *)(...)) == sizeof(void) == 1* - The equality comparison to "sizeof(int)" therefore depends on (x):*     sizeof(int) == sizeof(int)     (x) was a constant expression*     sizeof(int) != sizeof(void)    (x) was not a constant expression*/
#define __is_constexpr(x) \(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

2、工作原理

该宏的实现主要依赖三个关键机制:

  1. C语言条件运算符的类型推导规则
  2. 空指针常量的定义
  3. GNU C扩展中的sizeof(void)特性

2.1、宏的整体构成

宏的主体如下:

(sizeof(int) == sizeof(*(8 ? ((void *)((long)(x) * 0l)) : (int *)8)))

乍一看可能较为复杂,下面逐步解析其工作原理。

2.2、解析整体常量表达式部分

首先,分析 ((void *)((long)(x) * 0l)) 这一部分。这里将 (long)(x) 进行类型转换,主要是为了允许 x 具有指针类型,同时避免在32位平台上的u64类型出现警告。

根据 C 标准 6.6.6,如果 x 是一个整型常量表达式,那么 ((long)(x) * 0l) 就是一个值为0的整型常量表达式。而根据 C 标准 6.3.2.3.3,值为0的整型常量表达式或者将其转换为 void* 类型的表达式被称为空指针常量。因此,此时 (void *)((long)(x) * 0l) 就是一个空指针常量。

如果 x 不是整型常量表达式,不管 (void *)((long)(x) * 0l) 的值是多少,它都不是空指针常量。

2.3、条件运算符的作用

接着,分析 8? ((void *)((long)(x) * 0l) : (int *)8 这部分。这里使用8作为条件判断的一部分,其中第二个8是为了避免编译器在创建指向未对齐地址的指针时发出警告,第一个8可以是1。

C 标准 6.5.15-6规定条件运算符的特性,当其中一个操作数是空指针常量时,结果将具有另一个操作数的类型;否则,如果一个操作数是指向 void 的指针或 void 的限定版本,结果类型会是指向 void 的适当限定版本的指针。

因此,如果 x 是一个整型常量表达式,那么 ((void *)((long)(x) * 0l) 是一个空指针常量,此时整个条件表达式的结果就是第三个操作数 (int *)8 的类型,即指向 int 的指针。反之,如果 x 不是整型常量表达式,结果就是指向 void 的指针。

2.4、最终的判断依据

根据上述情况,有两种可能的情况:

sizeof(int) == sizeof(*((int *) (NULL)))    // (x)是常量表达式
sizeof(int) == sizeof(*((void *)(....)))    // (x)不是常量表达式
// 最终结果为:
sizeof(int) == sizeof(int)                  // (x)是常量表达式
sizeof(int) != sizeof(void)                 // (x)不是常量表达式

根据 GNU C 扩展,sizeof(void) == 1。因此,如果 x 是整数常量表达式,宏的结果为1;否则为0。由于比较的是两个 sizeof 表达式,最终结果本身也是一个整数常量表达式。

需要注意的是,常量表达式不能包含赋值、增量、减量、函数调用或逗号运算符,除非它们在未计算的子表达式中。同时,整型常量表达式只能包含整型常量、枚举常量、字符常量、结果为整型常量的 sizeof 表达式以及作为强制转换直接操作数的浮点常量,并且整型常量表达式中的强制转换运算符只能将算术类型转换为整型,作为 sizeof 运算符操作数的一部分除外。

简而言之,如果输入的参数是整型常量表达式,__is_constexpr(x) 宏会返回值为1的整型常量表达式;否则,返回值为0的整型常量表达式。

3、引入该宏的原因

3.1、与变量长度数组(VLAs)的关系

该宏的引入与从 Linux 内核中删除所有变量长度数组(Variable Length Arrays,简称 VLAs)的工作密切相关。为了实现这一目标,开发人员需要启用 GCC 的 -Wvla 警告,以标记出所有的 VLAs 实例。

当 -Wvla 警告被激活后,GCC 会报告许多数组被判定为 VLAs 的情况。例如在 fs/btrfs/tree-checker.c 中:

#define BTRFS_NAME_LEN 255
#define XATTR_NAME_MAX 255char namebuf[max(BTRFS_NAME_LEN, XATTR_NAME_MAX)];

开发人员期望 max(BTRFS_NAME_LEN, XATTR_NAME_MAX) 被解析为255,即希望它被视为标准数组(非 VLA),但这取决于 max(x, y) 宏的扩展结果。

如果数组的大小不是 C 标准定义的(整数)常量表达式,GCC 会生成 VLA 代码。例如:

#define not_really_constexpr ((void)0, 100)int a[not_really_constexpr];

根据 C90 标准,因为使用了逗号运算符,((void)0, 100) 不是常量表达式。此时,GCC 会生成 VLA 代码,即便它知道这个大小是一个编译时的常量。

由于内核中的 max(x, y) 宏不是常量表达式,GCC 会触发警告并生成开发人员不希望的 VLA 代码。因此,一些内核开发人员尝试寻找 max 宏和其他宏的替代版本,以避免出现警告和 VLA 代码。有些开发人员尝试利用 GCC 的 __builtin_constant_p 内置函数,但未能完美适用于当时内核支持的所有 GCC 版本(GCC >= 4.4)。

后来,Martin Uecker 提出了一种聪明的方法:

#define ICE_P(x) (sizeof(int) == sizeof(*(1? ((void*)((x) * 0l)) : (int*)1)))

该方法虽然使用了 GCC 扩展,但备受好评,成为 __is_constexpr(x) 宏的关键思路。经过几次迭代,最终 __is_constexpr(x) 宏出现在内核中。该宏用于实现 max 宏和其他宏,使其必须是常量表达式,从而避免 GCC 生成 VLA 代码。

4、实验验证

为了更好地理解 __is_constexpr(x) 宏,可以进行以下实验:

#define multiply_zero(x) ((long)(x) * 0l)
int main(void)
{const int a = 2;int arr1[multiply_zero(a)];int arr2[multiply_zero(2)];return 0;
}

打开编译器的 -Wvla 选项时,如果 x 输入的是变量 a,在编译时期,multiply_zero(a) 无法计算,因此不是常量表达式。而 multiply_zero(2) 则是常量表达式。

$ gcc -Wvla test_vla.c
test_vla.c: In function ‘main’:
test_vla.c:5:2: warning: ISO C90 forbids array ‘arr1’ whose size can’t be evaluated [-Wvla]5 |  int arr1[multiply_zero(a)];|  ^~~

5、参考资料

Linux内核中max()宏的奥妙何在?(一)

Linux内核中max()宏的奥妙何在?(二)——大神Linus对这个宏怎么看?

http://www.dtcms.com/a/282776.html

相关文章:

  • Linux 常用指令
  • 信而泰×DeepSeek:AI推理引擎驱动网络智能诊断迈向 “自愈”时代
  • Java基础语法补充v2
  • C# --- 单例类错误初始化 + 没有释放资源导致线程泄漏
  • The 2024 ICPC Asia Shenyang Regional Contest B. Magical Palette
  • Docker容器技术讲解
  • Liunx练习项目6-创建dns服务器
  • 主机安全---开源wazuh安装
  • 深入理解概率图模型:贝叶斯网络因子分解、d-分离与马尔可夫毯
  • 基于用户空间操作IIC接口调试云台电机
  • 7.16 Java基础 | 集合框架(上)
  • 微服务架构中实现跨服务的字段级权限统一控制
  • PyTorch深度学习框架入门案例实战
  • 第一章 【vue】基础(超详细)
  • 【动归解题套路框架】【带备忘录的递归】【最优子结构】【自下而上DP table】
  • Spring核心注解@RequestMapping详解
  • Java 二维数组详解:从基础语法到实战应用,彻底掌握多维数据结构
  • 边缘计算革命:AWS Snowcone在智慧工厂的落地实践(2025工业4.0实战指南)
  • 笔试——Day10
  • 【AI交叉】天文学:人工智能如何赋能星辰大海的探索
  • 如何关闭Elasticsearch的安全认证的解决方法
  • Maven入门指南:生命周期、阶段和执行顺序详解
  • 基于深度学习的情感分析模型:从文本数据到模型部署
  • leetcode:990.等式方程的可满足性[图]
  • 推荐《Python 编程:从入门到实践》之Python编程的基础知识
  • 经典算法之基数排序
  • 算法精讲--正则表达式(二):分组、引用与高级匹配技术
  • 基站前传卡 加速卡 EU
  • 一个项目的完整一生 --- 一 窗口大小设置
  • NW956NW961美光固态闪存NW964NW968