Rationale behind the container_of macro in linux/list.h

后端 未结 1 1027
悲哀的现实
悲哀的现实 2020-11-29 03:54

In the implementation of linux kernel lists in /include/linux/list.h, what is the rationale behind the first line (pasted below) of the container_of

相关标签:
1条回答
  • 2020-11-29 04:35

    It adds some type checking. With your version, this compiles fine (without warning):

    struct foo { int bar; };
    
    ....
    
    float a;
    struct foo *var = container_of(&a, foo, bar);
    

    With the kernel version, the compiler reports:

    warning: initialization from incompatible pointer type
    

    Good explanation of how the macro works: container_of by Greg Kroah-Hartman.

    0 讨论(0)
提交回复
热议问题