Wonil
Wonil

Reputation: 6727

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

I found the below code from linux/kfifo.h file in Linux kernel source.

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

From this code, what is the meaning of "typeof((fifo) + 1)" ? Why not using 'typeof(fifo) __tmpl = (fifo);'

Upvotes: 4

Views: 1368

Answers (1)

krase
krase

Reputation: 1044

@WonilYour first assumption is correct. 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.

Upvotes: 4

Related Questions