Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this kernel code mean?

I found this in an old kernel code :

#define hlist_for_each_entry(tpos, pos, head, member)                    \
        for (pos = (head)->first;                                        \
             pos &&                                                      \
                ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
             pos = pos->next)

but I don't understand how I should interpret the meaning of this line:

({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});

How does the compiler undersand the meaning of more than one expression inside braces/parentheses (the ({...;...;}) construct)?

like image 355
Mayank Avatar asked Feb 18 '26 02:02

Mayank


1 Answers

It seems that this construction

({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;});

is based on its own language extension of the compiler and allows to use compound statements in expressions. It is similar to lambda expressions in C++.

I think that the result of evaluation of the construction has value 1.

In my opinion it is simply a bad code because the same can be written using the comma operator like

pos && ( ( tpos = hlist_entry(pos, typeof(*tpos), member) ), 1 ); \
             pos = pos->next)
like image 146
Vlad from Moscow Avatar answered Feb 19 '26 20:02

Vlad from Moscow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!