What is 'typeof((fifo) + 1)' means from linux/kfifo.h file?

参考:https://stackoverflow.com/questions/16196440/what-is-typeoffifo-1-means-from-linux-kfifo-h-file

/**
 * 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; \
})

解释:

This construct is used to check if a pointer was used as argument.

When a plain struct is given, the expression will raise a compiler
error, because a struct is used in a unary + with an int.

When a pointer is given, the binary + will add 1 to it which will
still be a pointer to the same type and the expression is syntactically right.
posted @ 2022-07-25 18:40  dolinux  阅读(38)  评论(0)    收藏  举报