linux 内核 中链表list

这个结构从list.h 移到了types.h, 可见内核对循环链表的重视

include/linux/types.h中定义

struct list_head {
        struct list_head *next, *prev;
};

include/linux/list.h 中的宏

初始化 一个叫name的链表节点

#define LIST_HEAD_INIT(name) { &(name), &(name) }

#define LIST_HEAD(name)
        struct list_head name = LIST_HEAD_INIT(name)

eg:

typdef struct  list_tag {

  struct list_head _list;

  int element;

  int other;

} xxx_list_t;

int main( void )

{

  xxx_list_t l = {

    ._list = LIST_HEAD_INIT( l._list ),

    .element = 1,

    .other = 2,

  };

}

 #define INIT_LIST_HEAD(ptr) do {
 (ptr)->next = (ptr); (ptr)->prev = (ptr);
 }

变成了

 static inline void INIT_LIST_HEAD(struct list_head *list)
 {  
   list->next = list;
   list->prev = list;
 }

不知还在哪里使用:

 #define LIST_HEAD(name)  
          struct list_head name = LIST_HEAD_INIT(name) 
定义一个头, 没有其他数据部分
xxxx_head
  next ------------------------>
  prev ------------------------>



#define list_entry(ptr, type, member)  
         container_of(ptr, type, member)
的作用是:

返回指针
---->struct {

   ptr----> list
    ...
}

即根据ptr 是那个结构struct 中的 那个成员可以得出这个包含它的结构体指针
这其中使用到了 typeof (根据一个变量返回一个 ‘类型‘)


原文地址:https://www.cnblogs.com/kwingmei/p/3759965.html