当前位置: 技术问答>linux和unix
求 linux/list.h 部分解读list_entry()
来源: 互联网 发布时间:2017-02-20
本文导语: #define list_entry(ptr, type, member) ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next) ((type *)((char *)(ptr)...
#define list_entry(ptr, type, member)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
#define list_for_each(pos, head)
for (pos = (head)->next; pos != (head);
pos = pos->next)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))听说这一句子要给出type与member就能找到对应的节点,求具体解释
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
#define list_for_each(pos, head)
for (pos = (head)->next; pos != (head);
pos = pos->next)
((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))听说这一句子要给出type与member就能找到对应的节点,求具体解释
|
(unsigned long)(&((type *)0)->member))
得到0地址处变量type对应member的地址,就是member对应type的偏移值
然后ptr值减去member偏移值就得到 对应type变量的首地址了。
得到0地址处变量type对应member的地址,就是member对应type的偏移值
然后ptr值减去member偏移值就得到 对应type变量的首地址了。