zoukankan      html  css  js  c++  java
  • 内核代码之container_of

     1 /*****************************
     2  * container_of - cast a member of a structure out to the containing structure
      功能:通过结构体成员找到成员所属结构体的首地址
    3 * @ptr: the pointer to the member. 4 * @type: the type of the container struct this is embedded in. 5 * @member: the name of the member within the struct. 6 * 7 ************************************/ 8 #define container_of(ptr, type, member) ({ 9 const typeof(((type *)0)->member) * __mptr = (ptr); 10 (type *)((char *)__mptr - offsetof(type, member)); })
    typeof(((type *)0)->member) * __mptr = (ptr);
    定义了一个type*类型的结构体子成员_mptr,并将ptr赋给它。(注:((type *)0)获得的是type的首地址)
    (type *)((char *)__mptr - offsetof(type, member));
      #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER);
    offsetof函数实现计算member相对于结构体type的偏移。
    然后通过_mptr与其相减,即可获得mptr成员所属结构体的首地址。
     
  • 相关阅读:
    C#windows向窗体传递泛型类
    phoenix与spark整合
    phoenix创建二级索引
    git
    socket详解
    切片
    通过串口读写数据
    Python 跳出多重循环
    Python从json中提取数据
    Python 字典
  • 原文地址:https://www.cnblogs.com/embeded-linux/p/10828169.html
Copyright © 2011-2022 走看看