代码如下:
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)