In kernel.h min is defined as:
#define min(x, y) ({ \\
typeof(x) _min1 = (x); \\
typeof(y) _min2 = (y); \\
(void
The Linux kernel is full of stuff like this (gratuitous gcc-specific hacks for the sake of "type safety" and other similar considerations), and I would consider it very bad practice and urge you not to follow it unless someone requires you to.
pmg is right about the purpose of the hack, but any sane person would define min
as ((x)<(y)?(x):(y))
.
Note that the kernel definition precludes many correct usages, e.g. where one argument is int
and another is long
. I suspect what they really wanted to preclude is signedness mismatches, where for example min(-1,1U)
is 1. A better way to assert this would be to use a compile-time assertion for ((1?-1:(x))<0)==((1?-1:(y))<0)
. Note that this does not require any gcc-specific hacks.