首页 服务器系统 Linux

Linux内核数据结构之链表list.h详解

#ifndef _LINUX_LIST_H

#define _LINUX_LIST_H

#include <linux/types.h>

#include <linux/stddef.h>

#include <linux/poison.h>

#include <linux/const.h>

/*

* 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) }

//头指针name初始化

#define LIST_HEAD(name) \

struct list_head name = LIST_HEAD_INIT(name)

//初始化,空链表

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!

*/

/**********************************************************************************************************/

/**************************************************插入****************************************************/

/**********************************************************************************************************/

#ifndef CONFIG_DEBUG_LIST

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;

}

#else

extern void __list_add(struct list_head *new,

struct list_head *prev,

struct list_head *next);

#endif

/*

* list_add - add a new entry //list_add - 头插

* @new: new entry to be added //@new: 要插入的新结点

* @head: list head to add it after //@head: 链表的头,插入到这个结点之后

*

* //新结点插入到指定头结点之后

* 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 //list_add_tail - 尾插

* @new: new entry to be added //@new: 要插入的新结点

* @head: list head to add it before //@head:链表的头,插入到这个结点之前

*

* //在指定的结点之前插入一个结点

* 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. //list_del - 从链表删除结点

* @entry: the element to delete from the list. //@entry: 要从链表删除的结点

* //@Note:

* Note: list_empty() on entry does not return true after this, the entry is

* in an undefined state.

*/

#ifndef CONFIG_DEBUG_LIST

static inline void __list_del_entry(struct list_head *entry)

{

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

}

/*

* LIST_POISON1和LIST_POISON2这两个变量在poison.h中定义的:

* #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)

* #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)

* prev、next指针分别被设为LIST_POSITION2和LIST_POSITION1两个特殊值

* 这样设置是为了保证不在链表中的节点项不可访问

*(对LIST_POSITION1和LIST_POSITION2的访问都将引起页故障)

*/

static inline void list_del(struct list_head *entry)

{

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

//entry置为不可用

entry->next = LIST_POISON1;

entry->prev = LIST_POISON2;

}

#else

extern void __list_del_entry(struct list_head *entry);

extern void list_del(struct list_head *entry);

#endif

/*

* list_replace - replace old entry by new one //list_replace - 结点更新

* @old : the element to be replaced //@old :要被替换的结点

* @new : the new element to insert //@new : 新结点

*

* If @old was empty, it will be overwritten. //如果旧结点是个空结点,将会被覆盖

*/

/**********************************************************************************************************/

/***********************************************替换*******************************************************/

/**********************************************************************************************************/

//old置为不可用

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);

//old置为空链表

INIT_LIST_HEAD(old);

}

/**

* //list_del_init - 从链表删除结点并初始化该结点为空链表

* list_del_init - deletes entry from list and reinitialize it.

* @entry: the element to delete from the list. //@entry : 从链表删除的结点

*/

static inline void list_del_init(struct list_head *entry)

{

__list_del_entry(entry);

//置entry为空链表

INIT_LIST_HEAD(entry);

}

/**********************************************************************************************************/

/***********************************************移动*******************************************************/

/**********************************************************************************************************/

/*********************************************移动头插*****************************************************/

/**

* list_move - delete from one list and add as another's head //list_move - 从一个链表移除并且用头插法插入到另一个链表

* @list: the entry to move //@list : 要移除的结点

* @head: the head that will precede our entry //@head : 插入到head结点之后

*/

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

{

__list_del_entry(list);

list_add(list, head);

}

/*********************************************移动尾插*****************************************************/

/**

* list_move_tail - delete from one list and add as another's tail //list_move_tail - 从一个链表移除结点,并用尾插法把该结点插入到另一个链表

* @list: the entry to move //@list : 要移除的几点

* @head: the head that will follow our entry //@head : head为该结点后继

*/

static inline void list_move_tail(struct list_head *list,

struct list_head *head)

{

__list_del_entry(list);

list_add_tail(list, head);

}



/**********************************************************************************************************/

/***************************************************检测***************************************************/

/**********************************************************************************************************/

/**

* list_is_last - tests whether @list is the last entry in list @head //list_is_last - 测试 以 head 开头的链表的list 结点

* @list: the entry to test //@list :要测试的结点

* @head: the head of the list //@head : 链表的头

*/

/*******************************************判断是否是最后一个结点******************************************/

static inline int list_is_last(const struct list_head *list,

const struct list_head *head)

{

return list->next == head;

}

/***************************************判断 head 这个链表是不是空链表***************************************/

/*

* list_empty()函数和list_empty_careful()函数都是用来检测链表是否为空的。但是稍有区别的就是第一个链表使用的检测方法是

* 判断表头的结点,的下一个结点是否为其本身,如果是则返回为1,否则返回0。第二个函数使用的检测方法是判断表头的前一个结点

* 和后一个结点是否为其本身,如果同时满足则返回0,否则返回值为1。

*

* 这主要是为了应付另一个cpu正在处理同一个链表而造成next、prev不一致的情况。但代码注释也承认,这一安全保障能力有限:

* 除非其他cpu的链表操作只有list_del_init(),否则仍然不能保证安全,也就是说,还是需要加锁保护。

*/

/**

* 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);

}

}

/********************************判断 head 这个链表是否只有一个结点(除了head)********************************/

/**

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

* @head: the list to test.

*/

//返回1,有一个结点

static inline int list_is_singular(const struct list_head *head)

{

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

}

/************************************************************************************************************/

/**************************************************分割链表**************************************************/

/************************************************************************************************************/

/*

* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

* | _ _ _ _ _ _ _ _ |

* ---| |->| |->| |->| |->| |->| |->| |->| |<--|

* ---->|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|------|

* | head entry |

* |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|

*

* _

* | |

* |_|

* list

*/

/********************************************head 与 entry 不重合*******************************************/

/*

* @list : 链表外的一个结点

* @head : 以 head 开始的链表

* @entry : 以 head 开始的链表中的一个结点

*/

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;

}

/********************************************head 与 entry 重合********************************************/

/**

* 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)

//entry 指向 head ,无法分割,初始化list为一个空链表

INIT_LIST_HEAD(list);

else

//生成两个链表,head 和 list

__list_cut_position(list, head, entry);

}

/************************************************************************************************************/

/**************************************************链表合并**************************************************/

/************************************************************************************************************/

/*

* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

* | _ _ _ _ _ _ _ _ |

* ---| |->| |->| |->| |->| |->| |->| |->| |<--|

* ---->|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|------|

* | list |

* |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|

*

* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

* | _ _ _ _ _ _ _ _ |

* ----| |->| |->| |->| |->| |->| |->| |->| |<--|

* ---->|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|------|

* | prev next |

* |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|

*/

/*************************************************链表合并算法**********************************************/

//不包括list这个结点

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链表以头插的方式插入head链表**************************************/

/**

* 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链表以尾插的方式插入head链表**************************************/

/**

* 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.

*/

// 注意:插入后,list->prev 将会是原来的 list->prev

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链表以头插的方式插入head链表,并初始化 list 为空链表**************************/

/**

* 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链表以尾插的方式插入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

*/

//注意:插入后,list->prev 将会是原来的 list->prev

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);

}

}

/************************************************************************************************************/

/*****************************************************宏*****************************************************/

/************************************************************************************************************/

/****************************************************offsetof************************************************/

/*********************************************求结构内成员地址偏移量*****************************************/

//myread_linux-3.5/include/linux/stddef.h

/*

* 1.( (TYPE *)0 ) 将零转型为TYPE类型指针;

* 2.((TYPE *)0)->MEMBER 访问结构中的数据成员;

* 3.&( ( (TYPE *)0 )->MEMBER )取出数据成员的地址;

* 4.(size_t)(&(((TYPE*)0)->MEMBER))结果转换类型。

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

*/

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

/***************************************************container_of**********************************************/

/****************************************通过指针ptr.返回结构体起始地址***************************************/

//myread_linux-3.5/drivers/staging/rtl8192e/rtllib.h

#ifndef container_of

/**

* container_of - cast a member of a structure out to the containing structure

*

* //ptr指向type成员member

* @ptr: the pointer to the member. //@ptr : 指向成员的指针

* @type: the type of the container struct this is embedded in. //@type : 结构(体)类型

* @member: the name of the member within the struct. //member : 结构(体)内成员名

*

* typeof是GNU C对标准C的扩展,它的作用是根据变量获取变量的类型。因此,下面代码中的第2行的作用是首先

* 使用typeof获取结构体域变量member的类型为 type,然后定义了一个type指针类型的临时变量__mptr,并将实

* 际结构体变量中的域变量的指针ptr的值赋给临时变量__mptr

*

* 1.(char *)__mptr转换为字节型指针。

* 2.(char *)__mptr - offsetof(type,member) )用来求出结构体起始地址(为char *型指针),

* 3.然后(type *)( (char *)__mptr - offsetof(type,member) )在(type *)作用下进行将字节型的结构体起始指针转换为type *型的结构体起始指针。

*/

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

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

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

#endif

/****************************************************list_entry***********************************************/

/**

* 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) \

container_of(ptr, type, member)

/************************************************list_first_entry********************************************/

/**

* //这里的ptr是一个链表的头节点,这个宏就是取得这个链表第一元素的所指结构体的首地址

*

* 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_for_each*********************************************/

/*****************************************************遍历链表***********************************************/

/**

* list_for_each - iterate over a list

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

* @head: the head for your list.

*/

/*

* //这个实际上就是一个for循环,从头到尾遍历链表。prefetch()用于预取以此提高效率

* static inline void prefetch(const void *ptr)

* {

* __asm__ __volatile__(

* "pld\t%a0"

* :

* : "p" (ptr)

* : "cc");

* }

* #define list_for_each(pos, head) \

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

* pos = pos->next)

*/

#define list_for_each(pos, head) \

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

/*************************************************__list_for_each********************************************/

/**

* __list_for_each - iterate over a list

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

* @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******************************************/

/*************************************************从尾到头遍历链表*******************************************/

/**

* //

* 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******************************************/

/***************************************从头到尾遍历链表,便于删除结点***************************************/

/**

* 1.这个实际上就是一个for循环,从头到尾遍历链表。这里使用了n来记录pos的下一个,这样处理完一个流程之后再

* 赋给pos,避免了删除pos结点造成的问题,由它的英文注释我们可以看书,其实这个函数是专门为删除结点是准备的

* 2.注:list_for_each(pos, head)和list_for_each_safe(pos, n, head)都是从头至尾遍历链表的,但是对于前者来

* 说当操作中没有删除结点的时候使用,但是如果操作中有删除结点 的操作的时候就使用后者,

* 对于后面代safe的一般都是这个目的

*

* 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*****************************************/

/****************************************从尾到头遍历链表,便于删除结点**************************************/

/**

* 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****************************************/

/**************************************************从头到尾遍历链表*****************************************/

/********************************head是一个链表,链表的每一个结点又是一个结构体*****************************/

/**

* //head链表的每个结点为pos所指向结构体的member

*

* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

* | pos |

* | _ _ _ _ _ _ _ |

* | | | | | | | | | | | | | | | |

* | _ |_| |_| |_| |_| |_| |_| |_| |

* ---| |->| |->| |->| |->| |->| |->| |->| |<--|

* ---->|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|<-|_|------|

* | head member |

* |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|

*

* list_for_each_entry - iterate over list of given type

* @pos: the type * to use as a loop cursor. //@pos : 用于遍历的指针,struct list_head类型

* @head: the head for your list. //@head: 链表头,list_head结构

* @member: the name of the list_struct within the struct. //@member : list_head在结构体当中的变量的名字

*/

#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**************************************/

/**************************************************从尾到头遍历链表*****************************************/

/********************************head是一个链表,链表的每一个结点又是一个结构体*****************************/

/**

* 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_for_each_entry_continue**************************************/

/*********************************************可以从指定的pos结点遍历****************************************/

/**

* 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. //@pos : 用来存储结构体首地址,不同于list_for_entry中的pos

* @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**********************************/

/********************************************可以从指定的pos结点向前遍历**************************************/

/**

* 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****************************************/

/***********************************************从当前结点开始遍历*******************************************/

/**

* 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****************************************/

/**************和list_for_each_entry的遍历类似,这个带了safe是为了防止删除节点而造成断链的发生***************/

/**

* 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 //@n : n是为了防止删除节点而造成断链的发生

* @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************************************/

/************和
list_for_each_entry_continue()遍历类似,这个带了safe是为了防止删除节点而造成断链的发生*********/

/**

* 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.

*/

/******************************************list_for_each_entry_safe_from**************************************/

#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.

*/

/******************************************list_for_each_entry_safe_reverse**************************************/

/**************************************和
list_for_each_entry_safe_from方向相反***********************************/

#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**********************************************/

/******************************************************重设******************************************************/

/**

* 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)

/*

* Double linked lists with a single pointer list head.

* Mostly useful for hash tables where the two pointer list head is

* too wasteful.

* You lose the ability to access the tail in O(1).

*/

#endif

相关推荐