Linux内核循环链表经典分析和移植

  为什么说这个链表做的经典呢,哥哥我从Linux内核里边儿扣出来的,要么怎么说内核不是一般人能写的,这代码太TM优美了!

  这里有一篇参考文章:http://isis.poly.edu/kulesh/stuff/src/klist/,下面的分析来自其他人的分析这里做了整理,使得它便于阅读。

  在linux内核中,有大量的数据结构需要用到双循环链表,例如进程、文件、模块、页面等。若采用双循环链表的传统实现方式,需要为这些数据结构维护各自的链表,并且为每个链表都要设计插入、删除等操作函数。因为用来维持链表的next和prev指针指向对应类型的对象,因此一种数据结构的链表操作函数不能用于操作其它数据结构的链表。在Linux源代码树的include/linux/list.h文件中,采用了一种类型无关的双循环链表实现方式。其思想是将指针prev和next从具体的数据结构中提取出来构成一种通用的"双链表"数据结构list_head。如果需要构造某类对象的特定链表,则在其结构(被称为宿主数据结构)中定义一个类型为list_head类型的成员,通过这个成员将这类对象连接起来,形成所需链表,并通过通用链表函数对其进行操作。其优点是只需编写通用链表函数,即可构造和操作不同对象的链表,而无需为每类对象的每种列表编写专用函数,实现了代码的重用。

  在Linux内核中的双循环链表实现方式下: 

  1.  list_head类型的变量作为一个成员嵌入到宿主数据结构内; 
  2.  可以将链表结构放在宿主结构内的任何地方; 
  3.  可以为链表结构取任何名字; 
  4.  宿主结构可以有多个链表结构; 
  5.  用list_head中的成员和相对应的处理函数来对链表进行遍历; 
  6.  如果想得到宿主结构的指针,使用list_entry可以算出来。

  在上面的体系设计下,所有链表(包括添加、删除、移动和拼接等)操作都是针对数据结构list_head进行的。

  

  添加操作

  提供给用户的的添加链表的操作有两种,表头添加和表尾添加。注意到,Linux双循环链表中有一个链表头,表头添加是指添加到链表头之后,而表尾添加则是添加到链表头的prev所指链表节点之后。相关函数 __list_add(), list_add(), list_add_tail()。

static inline void __list_add(struct list_head *new, struct list_head *prev,  struct list_head *next) 

{ 

    next->prev = new;

    new->next = next;        

    new->prev = prev;        

    prev->next = new; 

} 

  普通的在两个非空结点中插入一个结点,注意new、prev、next都不能是空值。 Prev可以等于next,此时在只含头节点的链表中插入新节点。

static inline void list_add(struct list_head *newstruct  list_head *head) 
{         __list_add(
new, head, head->next); 

  在head和head->next两指针所指向的结点之间插入new所指向的结点。 即:在head指针后面插入new所指向的结点。Head并非一定为头结点。当现有链表只含有一个头节点时,上述__list_add(new, head, head->next)仍然成立。

static inline void list_add_tail(struct list_head *new, struct list_head *head) 

{ 

       __list_add(new, head->prev, head);

} 

  在结点指针head所指向结点的前面插入new所指向的结点。当head指向头节点时,也相当于在尾结点后面增加一个new所指向的结点。 注意: 
  head->prev不能为空,即若head为头结点,其head->prev当指向一个数值,一般为指向尾结点,构成循环链表。

  上述三个函数实现了添加一个节点的任务,其中__list_add()为底层函数,“__”通常表示该函数是底层函数,供其他模块调用,此处实现了较好的代码复用,list_add和list_add_tail虽然原型一样,但调用底层函数__list_add时传递了不同的参数,从而实现了在head指向节点之前或之后添加新的对象。

  删除节点:

  如果要从链表中删除某个链表节点,则可以调用list_del或list_del_init。 需要注意的是,上述操作均仅仅是把节点从双循环链表中拿掉,用户需要自己负责释放该节点对应的数据结构所占用的空间,而这个空间本来就是用户分配的。相关函数:__list_del(),list_del(),list_del_init()。

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

  在prev和next指针所指向的结点之间,两者互相所指。在后面会看到:prev为待删除的结点的前面一个结点,next为待删除的结点的后面一个结点。

static inline void list_del(struct list_head *entry)
{ __list_del(entry
->prev, entry->next);
    entry->next = LIST_POISON1;
    entry->prev = LIST_POISON2;
}

  删除entry所指的结点,同时将entry所指向的结点指针域封死。 对LIST_POISON1,LIST_POISON2的解释说明: 

  Linux 内核中解释:These are non-NULL pointers that will result in page faults under normal circumstances, used to verify that nobody uses  non-initialized list entries. 

  #define LIST_POISON1  ((void *) 0x00100100) #define LIST_POISON2  ((void *) 0x00200200)

  常规思想是:entry->next = NULL; entry->prev = NULL; 保证不可通过该节点进行访问。

static inline void list_del_init(struct list_head *entry)
{ __list_del(entry
->prev, entry->next);
    INIT_LIST_HEAD(entry);
}

  删除entry所指向的结点,同时调用LIST_INIT_HEAD()把被删除节点为作为链表头构建一个新的空双循环链表。

  

  移动节点:linux还提供了连个移动操作,list_move和list_move_tail

  

static inline void list_move(struct list_head *list, struct list_head *head)
{ __list_del(list
->prev, list->next);
    list_add(list, head);
}

  将list结点前后两个结点互相指向彼此,删除list指针所指向的结点,再将此结点插入head,和head->next两个指针所指向的结点之间。 即:将list所指向的结点移动到head所指向的结点的后面。

  

static inline void list_move_tail(struct list_head *list,    struct list_head *head) 
{ __list_del(list
->prev, list->next);
     list_add_tail(list, head);
}

  删除了list所指向的结点,将其插入到head所指向的结点的前面,如果head->prev指向链表的尾结点的话,就是将list所指向的结点插入到链表的结尾。

  

  链表判空 
  由list-head构成的双向循环链表中,通常有一个头节点,其不含有有效信息,初始化时prev和next都指向自身。判空操作是判断除了头节点外是否有其他节点。

static inline int list_empty(const struct list_head *head) 
{
return head->next == head;
}

  测试链表是否为空,如果是只有一个结点,head,head->next,head->prev都指向同一个结点,则这里会返回1,表示空;但这个空不是没有任何结点,而是只有一个头结点,因为头节点只是纯粹的list节点,没有有效信息,故认为为空。

  

static inline int list_empty_careful(const struct list_head *head) 
{
struct list_head *next = head->next; return (next == head) && (next == head->prev);
}

  分析: 

  1. 只有一个头结点head,这时head指向这个头结点,head->next,head->prev指向head,即:head==head->next==head->prev,这时候list_empty_careful()函数返回1。 
  2. 有两个结点,head指向头结点,head->next,head->prev均指向后面那个结点,即:head->next==head->prev,而head!=head->next,head!=head->prev.所以函数将返回0 
  3. 有三个及三个以上的结点,这是一般的情况,自己容易分析了。 

  注意:这里empty list是指只有一个空的头结点,而不是毫无任何结点。并且该头结点必须其head->next==head->prev==head

  

  链表合并:

  Linux还支持两个链表的拼接,提供给用户的具体函数是list_splice和list_splice_init

static inline void __list_splice(struct list_head *list, struct list_head *head)
{
struct list_head *first = list->next;
    struct list_head *last = list->prev;
    struct list_head *at = head->next; first->prev = head;
    head->next = first; last->next = at;
    at->prev = last;
}

  将一个非空链表插入到另外一个链表中。不作链表是否为空的检查,由调用者默认保证。因为每个链表只有一个头节点,将空链表插入到另外一个链表中是没有意义的。但被插入的链表可以是空的。

static inline void list_splice(struct list_head *list, struct list_head *head) 
{
if (!list_empty(list)) __list_splice(list, head);
}

  这种情况会丢弃list所指向的头结点,这是特意设计的,因为两个链表有两个头结点,要去掉一个头结点。只要list非空链,head无任何限制,该程序都可以实现链表合并。

  获取宿主对象指针
  如果需要有某种数据结构的队列,就在这种数据结构定义内部放上一个list_head数据结构。例如,建立数据结构foo链表的方式是,在foo的定义中,嵌入了一个list_head成员list。这里foo就是所指的"宿主"。 

typedef struct foo 

{    … 

    struct list_head list;    

 … 

};   

  但是,如何通过list_head成员访问到宿主结构项呢?毕竟list_head不过是个连接件,而我们需要的是一个"特定"的数据结构链表。 先介绍几个基本宏:offsetof、typeof、containerof 

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 

  一共4步 

  1.  ( (TYPE *)0 ) 0地址强制 "转换" 为 TYPE结构的指针; 
  2. ((TYPE *)0)->MEMBER   访问结构中的数据成员; 
  3.  &( ( (TYPE *)0 )->MEMBER)取出数据成员的地址; 
  4. (size_t)(&(((TYPE*)0)->MEMBER))结果转换类型.

  巧妙之处在于将0转换成(TYPE*),结构以内存空间首地址0作为起始地址,则成员地址自然为偏移地址;

  这里使用的是一个利用编译器技术的小技巧(编译器自动算出成员的偏移量),即先求得结构成员变量在结构体中的相对于结构体的首地址的偏移地址,然后根据结构体的首地址为0,从而得出该偏移地址就是该结构体变量在该结构体中的偏移,即:该结构体成员变量距离结构体首的距离。在offsetof()中,这个member成员的地址实际上就是type数据结构中member成员相对于结构变量的偏移量。对于给定一个结构,offsetof(type,member)是一个常量,list_entry()正是利用这个不变的偏移量来求得链表数据项的变量地址。

  typeof() 是 gcc 的扩展,和 sizeof() 类似。

  在 container_of 宏中,它用来给 typeof() 提供参数,以获得 member 成员的数据类型;

  ist_entry()宏,获取当前list_head链表节点所在的宿主结构项。第一个参数为当前list_head节点的指针,即指向宿主结构项的list_head成员。第二个参数是宿主数据结构的定义类型。第三个参数为宿主结构类型定义中list_head成员名。

  #define list_entry(ptr, type, member) \        container_of(ptr, type, member)

   扩展替换即为: 

  #define list_entry(ptr, type, member) \ 

      ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) 

  例如,我们要访问foo链表(链表头为head)中首个元素,则如此调用:list_entry(head->next, struct foo, list); 

  经过C预处理的文字替换,这一行的内容就成为: 
  ((struct foo *)((char *)(head->next) - (unsigned long)(&((struct foo *)0)->list))) 

  需要重申的是,链表头没有被嵌入到宿主对象中,因此对链表头执行宿主对象指针获取操作是没有意义的。

 

  链表的遍历

  遍历是双循环链表的基本操作,为此Linux定义了一些宏。list_for_each对遍历链表中的所有list_head节点,不涉及到对宿主结构的处理。list_for_each实际是一个 for 循环,利用传入的指向list_head结构的指针作为循环变量,从链表头开始(并跳过链表头),逐项向后移动指针,直至又回到链表头。

#define list_for_each(pos, head) \ 

       for (pos = (head)->next; prefetch(pos->next), pos != (head); \                pos = pos->next) 

  head为头节点,遍历过程中首先从(head)->next开始,当pos==head时退出,故head节点并没有访问,这和list结构设计有关,通常头节点就是纯粹的list结构,不含有其他有效信息,或者头节点含有其他信息,如内核PCB链表中的头节点为idle任务,但其不参予比较优先级,因此此时头节点只是作为双向链表遍历一遍的检测标志。

  为提高遍历速度,还使用了预取。 

-----asm-x86_64\processor.h---prefetch()---------

static inline void prefetch(void *x) 
{ asm
volatile("prefetcht0 %0" :: "m" (*(unsigned long *)x));
}

  将x指针作强制类型转换为unsigned long *型,然后取出该内存操作数,送入高速缓存。

#define __list_for_each(pos, head) \ 

       for (pos = (head)->next; pos != (head); pos = pos->next) 

  list_for_each()有prefetch()用于复杂的表的遍历,而__list_for_each()无prefetch()用于简单的表的遍历,此时表项比较少,无需缓存。

  如果在遍历过程中,包含有删除或移动当前链接节点的操作,由于这些操作会修改遍历指针,这样会导致遍历的中断。这种情况下,必须使用list_for_each_safe宏,在操作之前将遍历指针缓存下来: 内核中解释的精华部分: /* 
  

/ * list_for_each_safe     -      iterate over a list safe against removal of list entry   */ 

#define list_for_each_safe(pos, n, head) \ 

       for (pos = (head)->next, n = pos->next; pos != (head); \              
       pos = n, n = pos->next)

  在for循环中n暂存pos下一个节点的地址,避免因pos节点被释放而造成的断链。也就是说你可以遍历完当前节点后将其删除,同时可以接着访问下一个节点,遍历完毕后就只剩下一个头节点。这就叫safe。十分精彩。典型用途是多个进程等待在同一个等待队列上,若事件发生时唤醒所有进程,则可以唤醒后将其依次从等待队列中删除。

  遍历宿主对象 

  如果只提供对list_head结构的遍历操作是远远不够的,我们希望实现的是对宿主结构的遍历,即在遍历时直接获得当前链表节点所在的宿主结构项,而不是每次要同时调用list_for_each和list_entry。对此,Linux提供了list_for_each_entry()宏,第一个参数为传入的遍历指针,指向宿主数据结构,第二个参数为链表头,为list_head结构,第三个参数为list_head结构在宿主结构中的成员名。

#define list_for_each_entry(pos, head, member)                       \ 

       for (pos = list_entry((head)->next, typeof(*pos), member);       \ 
            prefetch(pos->member.next), &pos->member != (head);        \ 
            pos = list_entry(pos->member.next, typeof(*pos), member)) 

  如果遍历不是从链表头开始,而是从已知的某个pos结点开始,则可以使用list_for_each_entry_continue(pos,head,member)。但为了确保pos的初始值有效,Linux专门提供了一个list_prepare_entry(pos,head,member)宏,如果pos有值,则其不变;如果没有,则从链表头强制扩展一个虚pos指针。将它的返回值作为list_for_each_entry_continue()的pos参数,就可以满足这一要求。

  内核中的list_prepare_entry()的代码: 

#define list_prepare_entry(pos, head, member) \ 

       ((pos) ? : list_entry(head, typeof(*pos), member)) 

  分析: 
  前面是个空值,即:若pos不为空,则pos为其自身。等效于: (pos)? (pos): list_entry(head,typeof(*pos),member) 注意内核格式::前后都加了空格。

  内核中的list_for_each_entry_continue()的代码:

#define list_for_each_entry_continue(pos, head, member)         \ 

       for (pos = list_entry(pos->member.next, typeof(*pos), member);      \ 
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))

  此时不是从头节点开始遍历的,但仍然是以头节点为结束点的,即没有遍历完整个链表。 
     要注意并不是从pos开始的,而是从其下一个节点开始的,因为第一个有效pos是从pos->member.next扩展得到的。

  list_for_each_entry_safe(),它们要求调用者另外提供一个与pos同类型的指针n,在for循环中暂存pos下一个节点的地址,避免因pos节点被释放而造成的断链。

#define list_for_each_entry_safe(pos, n, head, member)                   \
for (pos = list_entry((head)->next, typeof(*pos), member), \
n = list_entry(pos->member.next, typeof(*pos), member); \
&pos->member != (head); \ pos = n, n = list_entry(n->member.next, typeof(*n), member))
#define list_for_each_entry_safe_continue(pos, n, head, member)           \
        for (pos = list_entry(pos->member.next, typeof(*pos), member),    \
             n = list_entry(pos->member.next, typeof(*pos), member);     \
             &pos->member != (head);                                      \               pos = n, n = list_entry(n->member.next, typeof(*n), member))

  分析类似上面。容易明白。

--------------------------------------------------------------------------------------------------------------------

  下面给出整理之后的 list.h,可以用在自己的程序之中。

  

#ifndef __LIST_H

#define __LIST_H



/* This file is from Linux Kernel (include/linux/list.h) 

 * and modified by simply removing hardware prefetching of list items. 

 * Here by copyright, credits attributed to wherever they belong.

 * Kulesh Shanmugasundaram (kulesh [squiggly] isis.poly.edu)

 */



#define NULL (void *)0



#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)



#define container_of(ptr, type, member) ({                      \

    const typeof( ((type *)0)->member ) *__mptr = (ptr);    \

    (type *)( (char *)__mptr - offsetof(type,member) );})



/*

 * Simple doubly linked list implementation.

 *

 * Some of the internal functions ("__xxx") are useful when

 * manipulating whole lists rather than single entries, as

 * sometimes we already know the next/prev entries and we can

 * generate better code by using them directly rather than

 * using the generic single-entry routines.

 */



struct list_head {

    struct list_head *next, *prev;

};



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



#define LIST_HEAD(name) \

    struct list_head name = LIST_HEAD_INIT(name)



/*#define INIT_LIST_HEAD(ptr) do { \

    (ptr)->next = (ptr); (ptr)->prev = (ptr); \

} while (0)*/



static inline void INIT_LIST_HEAD(struct list_head *list)

{

    list->next = list;

    list->prev = list;

}



/*

 * Insert a new entry between two known consecutive entries. 

 *

 * This is only for internal list manipulation where we know

 * the prev/next entries already!

 */

static inline void __list_add(struct list_head *new,

                  struct list_head *prev,

                  struct list_head *next)

{

    next->prev = new;

    new->next = next;

    new->prev = prev;

    prev->next = new;

}



/**

 * list_add - add a new entry

 * @new: new entry to be added

 * @head: list head to add it after

 *

 * Insert a new entry after the specified head.

 * This is good for implementing stacks.

 */

static inline void list_add(struct list_head *new, struct list_head *head)

{

    __list_add(new, head, head->next);

}



/**

 * list_add_tail - add a new entry

 * @new: new entry to be added

 * @head: list head to add it before

 *

 * Insert a new entry before the specified head.

 * This is useful for implementing queues.

 */

static inline void list_add_tail(struct list_head *new, struct list_head *head)

{

    __list_add(new, head->prev, head);

}



/*

 * Delete a list entry by making the prev/next entries

 * point to each other.

 *

 * This is only for internal list manipulation where we know

 * the prev/next entries already!

 */

static inline void __list_del(struct list_head *prev, struct list_head *next)

{

    next->prev = prev;

    prev->next = next;

}



/**

 * list_del - deletes entry from list.

 * @entry: the element to delete from the list.

 * Note: list_empty on entry does not return true after this, the entry is in an undefined state.

 */

static inline void list_del(struct list_head *entry)

{

    __list_del(entry->prev, entry->next);

    entry->next = (void *) 0;

    entry->prev = (void *) 0;

}



/**

 * list_del_init - deletes entry from list and reinitialize it.

 * @entry: the element to delete from the list.

 */

static inline void list_del_init(struct list_head *entry)

{

    __list_del(entry->prev, entry->next);

    INIT_LIST_HEAD(entry); 

}



/**

 * list_replace - replace old entry by new one

 * @old : the element to be replaced

 * @new : the new element to insert

 *

 * If @old was empty, it will be overwritten.

 */

static inline void list_replace(struct list_head *old,

                struct list_head *new)

{

    new->next = old->next;

    new->next->prev = new;

    new->prev = old->prev;

    new->prev->next = new;

}



static inline void list_replace_init(struct list_head *old,

                    struct list_head *new)

{

    list_replace(old, new);

    INIT_LIST_HEAD(old);

}



/**

 * list_move - delete from one list and add as another's head

 * @list: the entry to move

 * @head: the head that will precede our entry

 */

static inline void list_move(struct list_head *list, struct list_head *head)

{

        __list_del(list->prev, list->next);

        list_add(list, head);

}



/**

 * list_move_tail - delete from one list and add as another's tail

 * @list: the entry to move

 * @head: the head that will follow our entry

 */

static inline void list_move_tail(struct list_head *list,

                  struct list_head *head)

{

        __list_del(list->prev, list->next);

        list_add_tail(list, head);

}



/**

 * list_is_last - tests whether @list is the last entry in list @head

 * @list: the entry to test

 * @head: the head of the list

 */

static inline int list_is_last(const struct list_head *list,

                const struct list_head *head)

{

    return list->next == head;

}



/**

 * list_empty - tests whether a list is empty

 * @head: the list to test.

 */

static inline int list_empty(const struct list_head *head)

{

    return head->next == head;

}



/**

 * list_empty_careful - tests whether a list is empty and not being modified

 * @head: the list to test

 *

 * Description:

 * tests whether a list is empty _and_ checks that no other CPU might be

 * in the process of modifying either member (next or prev)

 *

 * NOTE: using list_empty_careful() without synchronization

 * can only be safe if the only activity that can happen

 * to the list entry is list_del_init(). Eg. it cannot be used

 * if another CPU could re-list_add() it.

 */

static inline int list_empty_careful(const struct list_head *head)

{

    struct list_head *next = head->next;

    return (next == head) && (next == head->prev);

}



/**

 * list_rotate_left - rotate the list to the left

 * @head: the head of the list

 */

static inline void list_rotate_left(struct list_head *head)

{

    struct list_head *first;



    if (!list_empty(head)) {

        first = head->next;

        list_move_tail(first, head);

    }

}



/**

 * list_is_singular - tests whether a list has just one entry.

 * @head: the list to test.

 */

static inline int list_is_singular(const struct list_head *head)

{

    return !list_empty(head) && (head->next == head->prev);

}



static inline void __list_cut_position(struct list_head *list,

        struct list_head *head, struct list_head *entry)

{

    struct list_head *new_first = entry->next;

    list->next = head->next;

    list->next->prev = list;

    list->prev = entry;

    entry->next = list;

    head->next = new_first;

    new_first->prev = head;

}



/**

 * list_cut_position - cut a list into two

 * @list: a new list to add all removed entries

 * @head: a list with entries

 * @entry: an entry within head, could be the head itself

 *    and if so we won't cut the list

 *

 * This helper moves the initial part of @head, up to and

 * including @entry, from @head to @list. You should

 * pass on @entry an element you know is on @head. @list

 * should be an empty list or a list you do not care about

 * losing its data.

 *

 */

static inline void list_cut_position(struct list_head *list,

        struct list_head *head, struct list_head *entry)

{

    if (list_empty(head))

        return;

    if (list_is_singular(head) &&

        (head->next != entry && head != entry))

        return;

    if (entry == head)

        INIT_LIST_HEAD(list);

    else

        __list_cut_position(list, head, entry);

}



static inline void __list_splice(const struct list_head *list,

                 struct list_head *prev,

                 struct list_head *next)

{

    struct list_head *first = list->next;

    struct list_head *last = list->prev;



    first->prev = prev;

    prev->next = first;



    last->next = next;

    next->prev = last;

}



/**

 * list_splice - join two lists, this is designed for stacks

 * @list: the new list to add.

 * @head: the place to add it in the first list.

 */

static inline void list_splice(const struct list_head *list,

                struct list_head *head)

{

    if (!list_empty(list))

        __list_splice(list, head, head->next);

}



/**

 * list_splice_tail - join two lists, each list being a queue

 * @list: the new list to add.

 * @head: the place to add it in the first list.

 */

static inline void list_splice_tail(struct list_head *list,

                struct list_head *head)

{

    if (!list_empty(list))

        __list_splice(list, head->prev, head);

}



/**

 * list_splice_init - join two lists and reinitialise the emptied list.

 * @list: the new list to add.

 * @head: the place to add it in the first list.

 *

 * The list at @list is reinitialised

 */

static inline void list_splice_init(struct list_head *list,

                    struct list_head *head)

{

    if (!list_empty(list)) {

        __list_splice(list, head, head->next);

        INIT_LIST_HEAD(list);

    }

}



/**

 * list_splice_tail_init - join two lists and reinitialise the emptied list

 * @list: the new list to add.

 * @head: the place to add it in the first list.

 *

 * Each of the lists is a queue.

 * The list at @list is reinitialised

 */

static inline void list_splice_tail_init(struct list_head *list,

                     struct list_head *head)

{

    if (!list_empty(list)) {

        __list_splice(list, head->prev, head);

        INIT_LIST_HEAD(list);

    }

}





/**

 * list_entry - get the struct for this entry

 * @ptr:    the &struct list_head pointer.

 * @type:    the type of the struct this is embedded in.

 * @member:    the name of the list_struct within the struct.

 */

#define list_entry(ptr, type, member) \

    ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))



/**

 * list_first_entry - get the first element from a list

 * @ptr:    the list head to take the element from.

 * @type:    the type of the struct this is embedded in.

 * @member:    the name of the list_struct within the struct.

 *

 * Note, that list is expected to be not empty.

 */

#define list_first_entry(ptr, type, member) \

    list_entry((ptr)->next, type, member)



/**

 * list_first_entry_or_null - get the first element from a list

 * @ptr:    the list head to take the element from.

 * @type:    the type of the struct this is embedded in.

 * @member:    the name of the list_struct within the struct.

 *

 * Note that if the list is empty, it returns NULL.

 */

#define list_first_entry_or_null(ptr, type, member) \

    (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)



/**

 * list_for_each    -    iterate over a list

 * @pos:    the &struct list_head to use as a loop cursor.

 * @head:    the head for your list.

 */

#define list_for_each(pos, head) \

    for (pos = (head)->next; pos != (head); pos = pos->next)



/**

 * __list_for_each    -    iterate over a list

 * @pos:    the &struct list_head to use as a loop cursor.

 * @head:    the head for your list.

 *

 * This variant doesn't differ from list_for_each() any more.

 * We don't do prefetching in either case.

 */

#define __list_for_each(pos, head) \

    for (pos = (head)->next; pos != (head); pos = pos->next)



/**

 * list_for_each_prev    -    iterate over a list backwards

 * @pos:    the &struct list_head to use as a loop cursor.

 * @head:    the head for your list.

 */

#define list_for_each_prev(pos, head) \

    for (pos = (head)->prev; pos != (head); pos = pos->prev)



/**

 * list_for_each_safe - iterate over a list safe against removal of list entry

 * @pos:    the &struct list_head to use as a loop cursor.

 * @n:        another &struct list_head to use as temporary storage

 * @head:    the head for your list.

 */

#define list_for_each_safe(pos, n, head) \

    for (pos = (head)->next, n = pos->next; pos != (head); \

        pos = n, n = pos->next)



/**

 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry

 * @pos:    the &struct list_head to use as a loop cursor.

 * @n:        another &struct list_head to use as temporary storage

 * @head:    the head for your list.

 */

#define list_for_each_prev_safe(pos, n, head) \

    for (pos = (head)->prev, n = pos->prev; \

         pos != (head); \

         pos = n, n = pos->prev)



/**

 * list_for_each_entry    -    iterate over list of given type

 * @pos:    the type * to use as a loop cursor.

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 */

#define list_for_each_entry(pos, head, member)                \

    for (pos = list_entry((head)->next, typeof(*pos), member);    \

         &pos->member != (head);     \

         pos = list_entry(pos->member.next, typeof(*pos), member))



/**

 * list_for_each_entry_reverse - iterate backwards over list of given type.

 * @pos:    the type * to use as a loop cursor.

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 */

#define list_for_each_entry_reverse(pos, head, member)            \

    for (pos = list_entry((head)->prev, typeof(*pos), member);    \

         &pos->member != (head);     \

         pos = list_entry(pos->member.prev, typeof(*pos), member))



/**

 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()

 * @pos:    the type * to use as a start point

 * @head:    the head of the list

 * @member:    the name of the list_struct within the struct.

 *

 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().

 */

#define list_prepare_entry(pos, head, member) \

    ((pos) ? : list_entry(head, typeof(*pos), member))



/**

 * list_for_each_entry_continue - continue iteration over list of given type

 * @pos:    the type * to use as a loop cursor.

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 *

 * Continue to iterate over list of given type, continuing after

 * the current position.

 */

#define list_for_each_entry_continue(pos, head, member)         \

    for (pos = list_entry(pos->member.next, typeof(*pos), member);    \

         &pos->member != (head);    \

         pos = list_entry(pos->member.next, typeof(*pos), member))



/**

 * list_for_each_entry_continue_reverse - iterate backwards from the given point

 * @pos:    the type * to use as a loop cursor.

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 *

 * Start to iterate over list of given type backwards, continuing after

 * the current position.

 */

#define list_for_each_entry_continue_reverse(pos, head, member)        \

    for (pos = list_entry(pos->member.prev, typeof(*pos), member);    \

         &pos->member != (head);    \

         pos = list_entry(pos->member.prev, typeof(*pos), member))



/**

 * list_for_each_entry_from - iterate over list of given type from the current point

 * @pos:    the type * to use as a loop cursor.

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 *

 * Iterate over list of given type, continuing from current position.

 */

#define list_for_each_entry_from(pos, head, member)             \

    for (; &pos->member != (head);    \

         pos = list_entry(pos->member.next, typeof(*pos), member))



/**

 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry

 * @pos:    the type * to use as a loop cursor.

 * @n:        another type * to use as temporary storage

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 */

#define list_for_each_entry_safe(pos, n, head, member)            \

    for (pos = list_entry((head)->next, typeof(*pos), member),    \

        n = list_entry(pos->member.next, typeof(*pos), member);    \

         &pos->member != (head);                     \

         pos = n, n = list_entry(n->member.next, typeof(*n), member))



/**

 * list_for_each_entry_safe_continue - continue list iteration safe against removal

 * @pos:    the type * to use as a loop cursor.

 * @n:        another type * to use as temporary storage

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 *

 * Iterate over list of given type, continuing after current point,

 * safe against removal of list entry.

 */

#define list_for_each_entry_safe_continue(pos, n, head, member)         \

    for (pos = list_entry(pos->member.next, typeof(*pos), member),         \

        n = list_entry(pos->member.next, typeof(*pos), member);        \

         &pos->member != (head);                        \

         pos = n, n = list_entry(n->member.next, typeof(*n), member))



/**

 * list_for_each_entry_safe_from - iterate over list from current point safe against removal

 * @pos:    the type * to use as a loop cursor.

 * @n:        another type * to use as temporary storage

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 *

 * Iterate over list of given type from current point, safe against

 * removal of list entry.

 */

#define list_for_each_entry_safe_from(pos, n, head, member)             \

    for (n = list_entry(pos->member.next, typeof(*pos), member);        \

         &pos->member != (head);                        \

         pos = n, n = list_entry(n->member.next, typeof(*n), member))



/**

 * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal

 * @pos:    the type * to use as a loop cursor.

 * @n:        another type * to use as temporary storage

 * @head:    the head for your list.

 * @member:    the name of the list_struct within the struct.

 *

 * Iterate backwards over list of given type, safe against removal

 * of list entry.

 */

#define list_for_each_entry_safe_reverse(pos, n, head, member)        \

    for (pos = list_entry((head)->prev, typeof(*pos), member),    \

        n = list_entry(pos->member.prev, typeof(*pos), member);    \

         &pos->member != (head);                     \

         pos = n, n = list_entry(n->member.prev, typeof(*n), member))



/**

 * list_safe_reset_next - reset a stale list_for_each_entry_safe loop

 * @pos:    the loop cursor used in the list_for_each_entry_safe loop

 * @n:        temporary storage used in list_for_each_entry_safe

 * @member:    the name of the list_struct within the struct.

 *

 * list_safe_reset_next is not safe to use in general if the list may be

 * modified concurrently (eg. the lock is dropped in the loop body). An

 * exception to this is if the cursor element (pos) is pinned in the list,

 * and list_safe_reset_next is called after re-taking the lock and before

 * completing the current iteration of the loop body.

 */

#define list_safe_reset_next(pos, n, member)                \

    n = list_entry(pos->member.next, typeof(*pos), member)



#endif

   测试程序,请用GCC编译器编译,windows上用MinGW或者 Cygwin来编译

#include "list.h" 

#include <stdio.h> 

#include <string.h>



#define MAX_NAME_LEN 32

#define MAX_ID_LEN 10



typedef struct stud

{

    struct list_head list;

    char name[MAX_NAME_LEN];

    char stu_number[MAX_ID_LEN];

}num_n_stu;



int main(void)

{

    struct list_head head;

    num_n_stu stu_1;

    num_n_stu stu_2;

    num_n_stu *entry;



    struct list_head *p;

    INIT_LIST_HEAD(&head);

    strcpy(stu_1.name,"lisi");

    strcpy(stu_1.stu_number,"10000000");



    strcpy(stu_2.name,"zhangsan");

    strcpy(stu_2.stu_number,"10000001");

    list_add(&stu_1.list,&head);

    list_add(&stu_2.list,&head);

    

    printf("before we del \n");

    list_for_each(p,&head){

        entry=list_entry(p,struct stud,list);

        printf("name: %s\n",entry->name);

        printf("stu_number: %s\n",entry->stu_number);

    }

    

    list_del(&stu_2.list);

    printf("\nafter we del\n");

    list_for_each(p,&head)

    {

        entry=list_entry(p,struct stud,list);

        printf("name: %s\n",entry->name);

        printf("stu_number: %s\n",entry->stu_number);

    }

    list_del(&stu_1.list);

    

    return 0;

}

 

你可能感兴趣的:(linux)