Chinaunix首页 | 论坛 | 博客
  • 博客访问: 130748
  • 博文数量: 12
  • 博客积分: 102
  • 博客等级: 入伍新兵
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-25 09:03
个人简介

80后程序员

文章分类

全部博文(12)

文章存档

2020年(1)

2018年(3)

2013年(5)

2011年(1)

2010年(2)

我的朋友

分类: 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) );})
下面给出一个例子的示意图,方便理解

阅读(2215) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~