Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

c - Rationale behind the container_of macro in linux/list.h

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It adds some type checking. With your version, this compiles fine (without warning):

struct foo { int bar; };

....

float a;
struct foo *var = container_of(&a, foo, bar);

With the kernel version, the compiler reports:

warning: initialization from incompatible pointer type

Good explanation of how the macro works: container_of by Greg Kroah-Hartman.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...