Chinaunix首页 | 论坛 | 博客

flagcugb

鸟窝

分类: LINUX

2018-01-03 17:32:01

在linux内核中有这么一个宏定义,使用非常频繁。

点击(此处)折叠或打开

  1. /**
  2.  * list_entry - get the struct for this entry
  3.  * @ptr:    the &struct list_head pointer.
  4.  * @type:    the type of the struct this is embedded in.
  5.  * @member:    the name of the list_struct within the struct.
  6.  */
  7. #define list_entry(ptr, type, member) \
  8.     container_of(ptr, type, member)

  9. /**
  10.  * container_of - cast a member of a structure out to the containing structure
  11.  *
  12.  * @ptr: the pointer to the member.
  13.  * @type: the type of the container struct this is embedded in.
  14.  * @member: the name of the member within the struct.
  15.  *
  16.  */
  17. #define container_of(ptr, type, member) ({ \
  18.         const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  19.         (type *)( (char *)__mptr - offsetof(type,member) );})
下面给出一个例子的示意图,方便理解

阅读(180) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册