I am new to the Linux kernel. I am reading the file ioctl.h
, there I encountered a macro
_IOC_TYPECHECK(t)
, which looks like this:
This is used to check the validity of the third parameter to the _IOR
/_IOW
/_IOWR
macros, which is supposed to be a type. It checks that the parameter is actually a type (and not a variable or a number), and causes a compiler or linker error otherwise.
If t
is a type, then t[1]
is the type "an array of 1 t
". This type has the same size as t
, and therefore sizeof(t) == sizeof(t[1])
is true.
If t
is a number, sizeof(t)
will fail to compile.
If t
is a simple (non-array) variable, then t[1]
will cause a compiler error.
If t
is an array variable, sizeof(t) == sizeof(t[1])
will be false, and a linker error will be caused (because __invalid_size_argument_for_IOC
is not defined).
The expression sizeof(t) < (1 << _IOC_SIZEBITS)
checks that the size of the type t
does not exceed the maximum allowed for ioctl
, and causes the same linker error otherwise.
There are still some invalid cases which will not be caught by this macro - for example, when t
is a pointer to a pointer.