-------------------------------------------This article is the author of the original, Welcome to reprint!Reprint Please specify Source: Netwalker.blog.chinaunix.net-------------------------------------------Although most of the time you only need to be concerned about the correctness of the code, there are many times when these potentially fatal errors are found during compilation. The kernel provides two powerful macro definitions:
1include/linux/kernel.h2 /*Force A compilation error if condition is true*/3 #defineBUILD_BUG_ON (condition) (void) sizeof (char[1-2*!! (condition)]))4 5 /*Force A compilation error if condition are true, but also produce a6 result (of value 0 and type size_t), so the expression can used7 e.g. in a structure initializer (or where-ever else comma expressions8 aren ' t permitted). */9 #defineBuild_bug_on_zero (e) (sizeof (CHAR[1-2 *!!) (e)])-1)
Note the core expression sizeof (char[1-2*!! (condition)]) , the first two times the conditional expression is reversed, which can guarantee the 1-2*!! There are only two values for the result (condition): The condition is 0 o'clock results of 1 or not 0 and the result is-1, obviously char[-1] will cause the compiler to give an error. Note: The purpose of two-time inversion is to convert the value of an expression to a logical value.
- BUILD_BUG_ON: Compile only if the condition condition is 0, but do not return a value, otherwise the compiler will give an error.
- Build_bug_on_zero: Only the condition E is 0 o'clock return 0, otherwise the compiler error.
Summary: build_bug_on and Build_bug_on_zero function similar, are in the parameter is not 0 o'clock compile error, but Build_bug_on_zero can return 0 value, build_bug_on not. The compiler warning format is as follows, which is consistent with the error definition of char[-1]. If you are unfamiliar with it, it is difficult to find the true location of the error based on the warning:
1 xxx.c: Negative
There are currently two uses of the kernel for Build_bug_on_zero: One is the __must_be_array macro used to determine the legal row of the pointer in the array size calculation. The other is the __module_param_call macro when a permission check is made on a module parameter.
Kernel to the use of build_bug_on is more common, it is used to compile the parameters of the check, widely exist in the kernel source files. In some cases, a data structure is required to meet a specific size, such as the size of the JFFS2_RAW_INODE structure in the JFFS2 file system must be 68. In addition, you may need to define some aliases for compatibility reasons, such as:
1 include/linux/net.h2#define sock_cloexec o_cloexec
You can detect if the definition is correct at the time of encoding:
1 net/socket.c2 build_bug_on (sock_cloexec! = o_cloexec);
In most cases, they are debugging information that needs to be removed after debugging, unless they can indicate specific information, such as the first case where the size of the data structure is emphasized as a fixed value. For example, if the size of the struct map struct is not 32, the compile times are wrong.
1 structMap2 {3 intused[2];/*8*/4 intempty[5];/* -*/ 5 6 Charpad[ +- -];7} men = {{1,2}, {0,3,4,5,6}};8 9 intMain ()Ten { OneBUILD_BUG_ON (sizeof(men)! = +); Aprintf"Build_bug_on_zero (0):%d,%d\n", Build_bug_on_zero (0),sizeof(men)); - - return 0; the}
The Build_bug_on_zero of Linux tricks.