c语言内核笔记

一、宏函数container_of(ptr,type,member) 这个宏的作用就是利用member成员的地址计算其所在结构体的首地址。

#define container_of(ptr, type, member) ({ \

const typeof( ((type *)0)->member ) *__mptr = (ptr); \

(type *)( (char *)__mptr - offsetof(type,member) );})

offserof 函数原型:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)这个宏的含义就是TYPE类型的结构体的首地址到MEMBER成员的首地址的偏移量。

__mptr地址减去offsetof的偏移量就是type结构体的首地址

二、typeof()

typeof(b)  a;    //相当于获取变量b的类型,定义一个变量a,即a和b的变量类型一致。

如上面的  const typeof( ((type *)0)->member ) *__mptr = (ptr);这样可以确保传入参数ptr的类型是(type *)0)->member,否则编译会报警告。体现内核的严谨。

VFS文件系统结构分析

http://blog.jobbole.com/105537/

猜你喜欢

转载自blog.csdn.net/lzg_zone/article/details/81563157
今日推荐