Home > OS >  About # define macros in Linux container_of (PTR, type, member) definition of doubt
About # define macros in Linux container_of (PTR, type, member) definition of doubt

Time:03-07

First of all in Linux has the following definition
 
# define container_of (PTR, type, member) ({\
Const typeof ((0) (type *) - & gt; Member) * __mptr=(PTR); \
(type *) ((char *) __mptr - offsetof (type, member)); })

My question is, why not just use PTR - offsetof (type, member), but to the other __mptr=(PTR), and then use __mptr - offsetof (type, member),

CodePudding user response:

__mptr=(PTR) can be used for type testing, if the incoming PTR type error, can be detected in the compilation phase directly,