Gcc 什么是';类型((fifo)和#x2B;1)和#x27;是指linux/kfifo.h文件?
我从linux内核源代码中的linux/kfifo.h文件中找到了以下代码Gcc 什么是';类型((fifo)和#x2B;1)和#x27;是指linux/kfifo.h文件?,gcc,linux-kernel,Gcc,Linux Kernel,我从linux内核源代码中的linux/kfifo.h文件中找到了以下代码 /** * kfifo_init - initialize a fifo using a preallocated buffer * @fifo: the fifo to assign the buffer * @buffer: the preallocated buffer to be used * @size: the size of the internal buffer, this have to be
/**
* kfifo_init - initialize a fifo using a preallocated buffer
* @fifo: the fifo to assign the buffer
* @buffer: the preallocated buffer to be used
* @size: the size of the internal buffer, this have to be a power of 2
*
* This macro initialize a fifo using a preallocated buffer.
*
* The numer of elements will be rounded-up to a power of 2.
* Return 0 if no error, otherwise an error code.
*/
#define kfifo_init(fifo, buffer, size) \
({ \
typeof((fifo) + 1) __tmp = (fifo); \
struct __kfifo *__kfifo = &__tmp->kfifo; \
__is_kfifo_ptr(__tmp) ? \
__kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \
-EINVAL; \
})
从这段代码中,“typeof((fifo)+1)”的含义是什么?
为什么不使用“typeof(fifo)u tmpl=(fifo);” @wonill你的第一个假设是正确的。此构造用于检查指针是否用作参数 当给出一个普通结构时,表达式将引发一个编译器错误,因为该结构用于带int的一元+中
当给出一个指针时,二进制+将向其添加1,它仍然是指向相同类型的指针,并且表达式在语法上是正确的。OK。再花5分钟,我就明白为什么了。kfifo_init和其他kfifo相关宏需要第一个参数作为kfifo结构的指针。如果这个宏的用户不小心在第一个参数中使用了纯kfifo结构,这将导致编译错误,因为typeof中有+1。“+1”告诉gcc采用“1”(int)类型或“fifo”类型,以需要升级的为准。看看这个帖子:有趣,@PeterL。林克的解释和我想的完全不同。我需要多想想。我猜原因是这样的。这种技巧是有用的,但我希望在头文件中有一个关于它的注释,因为它的意图并不明显。