1

I have this code in linux kernel:

#define task_cred_xxx(task, xxx)                        
({                                                      
    __typeof__(((struct cred *)NULL)->xxx) ___val;  
    rcu_read_lock();                               
    ___val = __task_cred((task))->xxx;              
    rcu_read_unlock();                              
    ___val;                                         
})

I never saw macro defined like this before, does that mean this is task_cred_xxx(task, xxx) returns ___val?

Thanks!

Rachel
  • 251
  • 1
  • 2
  • 12
  • Also, `((struct cred *)NULL)->xxx` is (I think) undefined behavior. – Chris Lutz Sep 30 '11 at 01:06
  • @ChrisLutz: Normally it is, but since the linux kernel targets only gcc, and gcc allows it, it's accepted. – Bill Lynch Sep 30 '11 at 01:56
  • @Chris Lutz: As long as only type or size of such expressions are needed, this is OK (at least for GCC), the expressions themselves are never evaluated. Take a look at the implementation of commonly used `container_of` macro in `` for another example of this trick. – Eugene Oct 04 '11 at 07:57

1 Answers1

4

Correct. It will return ___val. However, block expressions like these are a GNU extension and isn't actually part of the C standard.

http://www.toofishes.net/blog/gcc-compound-statement-expressions/

Mysticial
  • 464,885
  • 45
  • 335
  • 332