linux内核数据结构之链表

代码如下:

list.h中:

/**
505  * list_entry - get the struct for this entry                                                                                       
506  * @ptr:    the &struct list_head pointer.
507  * @type:   the type of the struct this is embedded in.
508  * @member: the name of the list_head within the struct.
509  */
510 #define list_entry(ptr, type, member)
511     container_of(ptr, type, member)

container_of(ptr, type, member)在kernle.h中  

/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member. 指向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) ({
void *__mptr = (void *)(ptr);
BUILD_BUG_ON_MSG(!__same_type(*(ptr), ((type *)0)->member) &&
!__same_type(*(ptr), void),
"pointer type mismatch in container_of()");
((type *)(__mptr - offsetof(type, member))); })

其中offsetof(type, member)又在:stddef.h中:

19 #define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)

人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。
原文地址:https://www.cnblogs.com/guochaoxxl/p/15159514.html