当前位置:  技术问答>linux和unix

关于container_of源码

    来源: 互联网  发布时间:2017-04-19

    本文导语:  #define container_of(ptr, type, member) ({                               const typeof( ((type *)0)->member ) *__mptr = (ptr);             (type *)( (char *)__mptr - offsetof(type,member) );}) 感觉这container_of的第二行...

#define container_of(ptr, type, member) ({                      
        const typeof( ((type *)0)->member ) *__mptr = (ptr);    
        (type *)( (char *)__mptr - offsetof(type,member) );})

感觉这container_of的第二行有点多余,把ptr赋给__mptr,第三行又用__mptr来做减法。
能不能直接改成:
#define container_of(ptr, type, member) ({                      
        (type *)( (char *)(ptr) - offsetof(type,member) );})

第二行加一下__mptr实在是没有意义啊

|
其实可以不用,只是做个类型检查而已,防止程序员错误,当ptr传非法的时候报个警告,这样程序员就知道了。

    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • 宏container_of
  • container_of疑问
  • Linux内核的 container_of宏的疑问?
  • container_of() 的实现代码为什么这么写
  • 請教關於container_of的實現原理
  • 求解释,container_of()宏


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,