In the implementation of linux kernel lists in /include/linux/list.h
, what is the rationale behind the first line (pasted below) of the container_of
macro?
const typeof( ((type *)0)->member ) *__mptr = (ptr);
In a sample code of mine, I removed this line and changed the definition to
#define container_of(ptr, type, member) ({
(type *)( (char *)ptr - offsetof(type,member) );})
and my code still showed expected results. Is the first line redundant then? Or does it have some hidden trap that I am not aware of?
The code I found at Faq/LinkedLists
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({
const typeof( ((type *)0)->member ) *__mptr = (ptr);
(type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…