-------------------------------------------
本文系作者原创, 欢迎大家转载!
转载请注明出处:netwalker.blog.chinaunix.net
-------------------------------------------
尽管在大多数时候只需关心代码运行的正确性,但是很多时候需要在编译期间就发现这些潜在的致命错误。内核提供了两个有力的宏定义:
- include/linux/kernel.h
- /* Force a compilation error if condition is true */
- #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
- /* Force a compilation error if condition is true, but also produce a
- result (of value 0 and type size_t), so the expression can be used
- e.g. in a structure initializer (or where-ever else comma expressions
- aren't permitted). */
- #define BUILD_BUG_ON_ZERO(e) (sizeof(char[1 - 2 * !!(e)]) - 1)
注意核心表达式sizeof(char[1 - 2*!!(condition)])的作用,首先对条件表达式进行两次取反,这可以保证进行1 - 2*!!(condition)的结果只有两种值:条件为0时结果为1或者不为0则结果为-1,显然char[-1]将会导致编译器报错。
- BUILD_BUG_ON:只有条件condition为0时可编译,但不返回值,否则编译器报错。
- BUILD_BUG_ON_ZERO:只有条件e为0时返回0,否则编译器报错。
- xxx.c:42: error: size of array ‘type name’ is negative
内核对BUILD_BUG_ON的使用则要普遍的多,它被用来做编译期间的参数检查,广泛存在于内核的源码文件中。某些情况下需要一个数据结构满足特定的大小,比如jffs2文件系统中的jffs2_raw_inode结构的大小必须为68。另外可能需要为了兼容性考虑,可能需要定义一些别名,比如:
- include/linux/net.h
- #define SOCK_CLOEXEC O_CLOEXEC
- net/socket.c
- BUILD_BUG_ON(SOCK_CLOEXEC != O_CLOEXEC);
- struct map
- {
- int used[2]; /* 8 */
- int empty[5]; /* 20 */
-
- char pad[32 - 28];
- } men = {{1, 2}, {0, 3, 4, 5, 6}};
- int main()
- {
- BUILD_BUG_ON(sizeof(men) != 32);
- printf("BUILD_BUG_ON_ZERO(0):%d, %d\n", BUILD_BUG_ON_ZERO(0), sizeof(men));
-
- return 0;
- }