从内核工具中分离出来的链表工具
从网上找到了一个详细些的注释版本。
http://hi.baidu.com/pank7/blog/item/dd864e4aff89f22009f7efb2.html
先放版权证
/*
<list.h>
Copyright (C) 2007 Li Yi (pAnk7.yArdbird) <pank7yardbird (AT) gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
list版
list
/*
* 描述: 双循环链表的实现,拷贝自Linux内核(2.6.20.1)。
* 稍作修改。头文件,使用时只需包含此头文件即可。
* 文件名: list.h
* 作者: pAnk7.yArdbird <pank7yardbird (AT) gmail.com>
* 日期 & 时间: Tue Nov 13 12:49:25 2007
*/
#ifndef _LINUX_LIST_H
#define _LINUX_LIST_H
#ifdef __cplusplus /* 使C++程序也能用。 */
extern "C" {
#endif
#if !defined(__cplusplus) && defined(WIN32)
#define inline
#endif
#define LIST_POISON1 ((void *) 0x00100100) /* 链表节点指针的特殊标志。 */
#define LIST_POISON2 ((void *) 0x00200200)
#ifndef NULL /* C语言中没有定义NULL。 */
#define NULL ((void *)0)
#endif /* NULL */
#ifndef offsetof /* 宏container_of需要使用。 */
#ifdef __compiler_offsetof /* 有的系统自带有此功能的宏。 */
#define offsetof(TYPE,MEMBER) __compiler_offsetof(TYPE,MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
#endif /* offsetof */
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
/* 并发互斥的处理,在用户空间的程序大多用不着。可考虑改善这里的功能。 */
#define smp_read_barrier_depends() do { } while(0)
#define rcu_dereference(p) ({ \
typeof(p) _________p1 = p; \
smp_read_barrier_depends(); \
(_________p1); \
})
/* 利用CPU的cache来提高效率。 */
static inline void prefetch(const void *x) { ; }
/*
* Simple doubly linked list implementation.
* 简单的双循环链表的实现。
*
* 当处理整个链表而不是单个节点的时候,有一些内部函数(“__xxx”)更有用,
* 因为有时候我们已经知道了前后节点,所以直接使用内部函数我们可以让写出
* 更好的代码。
*/
/*
* 在你需要链接起来的数据结构中加入一个此结构体(注意不能是list_head指针)
* 成员。你也可以加入多个此结构体的成员,从而可以让同一个数据结构可以加入多
* 个链表。
*/
typedef struct list_head {
struct list_head *next, *prev;
} list_head;
#define LIST_HEAD_INIT(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;
}
/*
* 在两个已知的连续的节点中插入一个新的节点。
*
* 此函数仅作为链表内部操作使用,链表内部我们已经知道了前后节点。
*/
static inline void __list_add(struct list_head *newentry,
struct list_head *prev,
struct list_head *next)
{
next->prev = newentry;
newentry->next = next;
newentry->prev = prev;
prev->next = newentry;
}
/**
* 加入一个新的节点。
* @newentry: new entry to be added
* @head: list head to add it after
*
* 在指定链表头部后面加入一个新的节点。可以方便地用来实现栈。
*/
static inline void list_add(struct list_head *newentry, struct list_head *head)
{
__list_add(newentry, head, head->next);
}
/**
* 加入一个新的节点。
* @newentry: new entry to be added
* @head: list head to add it before
*
* 在指定链表头部前面加入一个新的节点(即加在尾部)。可以方便地用来实现队列。
*/
static inline void list_add_tail(struct list_head *newentry,
struct list_head *head)
{
__list_add(newentry, head->prev, head);
}
/*
* 在两个已知的连续的节点之间插入一个新的节点。由于我的修改此函数和之前的
* __list_add没有区别,但是为后续开发留下接口。
*
* 此函数仅作为链表的内部操作使用。
*/
static inline void __list_add_rcu(struct list_head * newentry,
struct list_head * prev,
struct list_head * next)
{
newentry->next = next;
newentry->prev = prev;
next->prev = newentry;
prev->next = newentry;
}
/**
* 在一个rcu保护下的链表中加入一个新的节点。与之前的list_add没有区别,
* 但是为后续开发留下接口。
* @newentry: new entry to be added
* @head: list head to add it after
*/
static inline void list_add_rcu(struct list_head *newentry,
struct list_head *head)
{
__list_add_rcu(newentry, head, head->next);
}
/**
* 在一个rcu保护下的链表中加入一个新的节点。与之前的list_add_tail没有区别,
* 但是为后续开发留下接口。
* @newentry: new entry to be added
* @head: list head to add it before
*/
static inline void list_add_tail_rcu(struct list_head *newentry,
struct list_head *head)
{
__list_add_rcu(newentry, head->prev, head);
}
/*
* 使前后节点相互指向对方,从而删掉一个节点。但是此节点的空间还得自己释放。
*
* 此函数仅作为链表的内部操作使用。
*/
static inline void __list_del(struct list_head * prev,
struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
/**
* 从链表中删除一个节点。
* @entry: the element to delete from the list.
* 注意:调用此函数后list_empty并不返回真,节点处于一个未知状态。
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->next = (list_head *)LIST_POISON1;
entry->prev = (list_head *)LIST_POISON2;
}
/**
* 从链表中删除一个节点,无需重新初始化。
* @entry: the element to delete from the list.
*/
static inline void list_del_rcu(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
entry->prev = (list_head *)LIST_POISON2;
}
/**
* 用新的节点替换旧的节点。
* @old : the element to be replaced
* @newentry : the new element to insert
* Note: if 'old' was empty, it will be overwritten.
*/
static inline void list_replace(struct list_head *old,
struct list_head *newentry)
{
newentry->next = old->next;
newentry->next->prev = newentry;
newentry->prev = old->prev;
newentry->prev->next = newentry;
}
/* 用新的节点替换旧的节点,并将旧的节点初始化。 */
static inline void list_replace_init(struct list_head *old,
struct list_head *newentry)
{
list_replace(old, newentry);
INIT_LIST_HEAD(old);
}
/**
* list_replace_rcu - replace old entry by new one
* 用新的节点替换旧的节点。
* @old : the element to be replaced
* @newentry : the new element to insert
*
* The @old entry will be replaced with the @newentry entry atomically.
* Note: @old should not be empty.
*/
static inline void list_replace_rcu(struct list_head *old,
struct list_head *newentry)
{
newentry->next = old->next;
newentry->prev = old->prev;
newentry->next->prev = newentry;
newentry->prev->next = newentry;
old->prev = (list_head *)LIST_POISON2;
}
/**
* 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_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的节点将被剔除。 */
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;
}
/**
* list_splice - join two lists
* 将两个链表合二为一。list的节点将被剔除。
* @list: the new list to add.
* @head: the place to add it in the first list.
*/
static inline void list_splice(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head);
}
/**
* list_splice_init - join two lists and reinitialise the emptied list.
* 将两个链表合二为一,并重新初始化空链表(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);
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) \
container_of(ptr, type, member)
/**
* 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; prefetch(pos->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 differs from list_for_each() in that it's the
* simplest possible list iteration code, no prefetching is done.
* Use this for code that knows the list to be very short (empty
* or 1 entry) most of the time.
* 此宏没有使用prefetch。
*/
#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; prefetch(pos->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_entry - iterate over list of given type
* 迭代一个链表,与list_for_each不同的地方在于此宏给出的是结构体指针。
* @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); \
prefetch(pos->member.next), &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); \
prefetch(pos->member.prev), &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
* 为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的下一个节点处开始继续迭代一个链表。
* @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); \
prefetch(pos->member.next), &pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_entry_from - iterate over list of given type from the current
* point
* 从pos处开始继续迭代一个链表。
* @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 (; prefetch(pos->member.next), &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
* 顾名思义。
* @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
* 顾名思义。
* @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
* 顾名思义。
* @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_for_each_rcu - iterate over an rcu-protected list
* 顾名思义。
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* This list-traversal primitive may safely run concurrently with
* the _rcu list-mutation primitives such as list_add_rcu()
* as long as the traversal is guarded by rcu_read_lock().
*/
#define list_for_each_rcu(pos, head) \
for (pos = (head)->next; \
prefetch(rcu_dereference(pos)->next), pos != (head); \
pos = pos->next)
#define __list_for_each_rcu(pos, head) \
for (pos = (head)->next; \
rcu_dereference(pos) != (head); \
pos = pos->next)
/**
* list_for_each_safe_rcu
* 顾名思义。
* @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.
*
* Iterate over an rcu-protected list, safe against removal of list entry.
*
* This list-traversal primitive may safely run concurrently with
* the _rcu list-mutation primitives such as list_add_rcu()
* as long as the traversal is guarded by rcu_read_lock().
*/
#define list_for_each_safe_rcu(pos, n, head) \
for (pos = (head)->next; \
n = rcu_dereference(pos)->next, pos != (head); \
pos = n)
/**
* list_for_each_entry_rcu - iterate over rcu 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.
*
* This list-traversal primitive may safely run concurrently with
* the _rcu list-mutation primitives such as list_add_rcu()
* as long as the traversal is guarded by rcu_read_lock().
*/
#define list_for_each_entry_rcu(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*pos), member); \
prefetch(rcu_dereference(pos)->member.next), \
&pos->member != (head); \
pos = list_entry(pos->member.next, typeof(*pos), member))
/**
* list_for_each_continue_rcu
* 顾名思义。
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*
* Iterate over an rcu-protected list, continuing after current point.
*
* This list-traversal primitive may safely run concurrently with
* the _rcu list-mutation primitives such as list_add_rcu()
* as long as the traversal is guarded by rcu_read_lock().
*/
#define list_for_each_continue_rcu(pos, head) \
for ((pos) = (pos)->next; \
prefetch(rcu_dereference((pos))->next), (pos) != (head); \
(pos) = (pos)->next)
hlist版
View Code
/*
* 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).
*
* 头节点只有一个指针的链表(注意,不再是循环链表了)。在散列表中链表头部使用两个指
* 针显然是有点浪费空间了。
* 但是这样就无法在O(1)的时间内访问链表的尾节点了。
*/
/* 可使用此结构体的数组作为散列表。x86_32体系中只有4个字节的空间,其实就是一个指针。 */
typedef struct hlist_head {
struct hlist_node *first;
} hlist_head;
/* 散列表中的每一个数据节点使用此结构体。仔细想想为什么这里的pprev是hlist_node **
* 类型的? */
typedef struct hlist_node {
struct hlist_node *next, **pprev;
} hlist_node;
#define HLIST_HEAD_INIT { .first = NULL } /* 链表头部初始化。 */
/* 定义并初始化一个链表头部。 */
#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL) /* 初始化链表头部。 */
/* 初始化链表节点。 */
static inline void INIT_HLIST_NODE(struct hlist_node *h)
{
h->next = NULL;
h->pprev = NULL;
}
/* 判断一个节点是否已经被散列过。 */
static inline int hlist_unhashed(const struct hlist_node *h)
{
return !h->pprev;
}
/* 判断链表是否为空。 */
static inline int hlist_empty(const struct hlist_head *h)
{
return !h->first;
}
/* 删除链表中的一个节点。 */
static inline void __hlist_del(struct hlist_node *n)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
*pprev = next;
if (next)
next->pprev = pprev;
}
/* 删除链表中的一个节点,标记删除节点的指针。 */
static inline void hlist_del(struct hlist_node *n)
{
__hlist_del(n);
n->next = (hlist_node *)LIST_POISON1;
n->pprev = (hlist_node **)LIST_POISON2;
}
/**
* hlist_del_rcu - deletes entry from hash list without re-initialization
* 顾名思义。
* @n: the element to delete from the hash list.
*
* Note: list_unhashed() on entry does not return true after this,
* the entry is in an undefined state. It is useful for RCU based
* lockfree traversal.
*
* In particular, it means that we can not poison the forward
* pointers that may still be used for walking the hash list.
*
* The caller must take whatever precautions are necessary
* (such as holding appropriate locks) to avoid racing
* with another list-mutation primitive, such as hlist_add_head_rcu()
* or hlist_del_rcu(), running on this same list.
* However, it is perfectly legal to run concurrently with
* the _rcu list-traversal primitives, such as
* hlist_for_each_entry().
*/
static inline void hlist_del_rcu(struct hlist_node *n)
{
__hlist_del(n);
n->pprev = (hlist_node **)LIST_POISON2;
}
/* 顾名思义。 */
static inline void hlist_del_init(struct hlist_node *n)
{
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
/* 接下来这些函数和宏和list的都差不多,都可以顾名思义的啦^.^。 */
/**
* hlist_replace_rcu - replace old entry by new one
* @old : the element to be replaced
* @newentry : the new element to insert
*
* The @old entry will be replaced with the @newentry entry atomically.
*/
static inline void hlist_replace_rcu(struct hlist_node *old,
struct hlist_node *newentry)
{
struct hlist_node *next = old->next;
newentry->next = next;
newentry->pprev = old->pprev;
if (next)
newentry->next->pprev = &newentry->next;
*newentry->pprev = newentry;
old->pprev = (hlist_node **)LIST_POISON2;
}
static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
if (first)
first->pprev = &n->next;
h->first = n;
n->pprev = &h->first;
}
/**
* hlist_add_head_rcu
* @n: the element to add to the hash list.
* @h: the list to add to.
*
* Description:
* Adds the specified element to the specified hlist,
* while permitting racing traversals.
*
* The caller must take whatever precautions are necessary
* (such as holding appropriate locks) to avoid racing
* with another list-mutation primitive, such as hlist_add_head_rcu()
* or hlist_del_rcu(), running on this same list.
* However, it is perfectly legal to run concurrently with
* the _rcu list-traversal primitives, such as
* hlist_for_each_entry_rcu(), used to prevent memory-consistency
* problems on Alpha CPUs. Regardless of the type of CPU, the
* list-traversal primitive must be guarded by rcu_read_lock().
*/
static inline void hlist_add_head_rcu(struct hlist_node *n,
struct hlist_head *h)
{
struct hlist_node *first = h->first;
n->next = first;
n->pprev = &h->first;
if (first)
first->pprev = &n->next;
h->first = n;
}
/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n,
struct hlist_node *next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
static inline void hlist_add_after(struct hlist_node *n,
struct hlist_node *next)
{
next->next = n->next;
n->next = next;
next->pprev = &n->next;
if(next->next)
next->next->pprev = &next->next;
}
/**
* hlist_add_before_rcu
* @n: the new element to add to the hash list.
* @next: the existing element to add the new element before.
*
* Description:
* Adds the specified element to the specified hlist
* before the specified node while permitting racing traversals.
*
* The caller must take whatever precautions are necessary
* (such as holding appropriate locks) to avoid racing
* with another list-mutation primitive, such as hlist_add_head_rcu()
* or hlist_del_rcu(), running on this same list.
* However, it is perfectly legal to run concurrently with
* the _rcu list-traversal primitives, such as
* hlist_for_each_entry_rcu(), used to prevent memory-consistency
* problems on Alpha CPUs.
*/
static inline void hlist_add_before_rcu(struct hlist_node *n,
struct hlist_node *next)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
*(n->pprev) = n;
}
/**
* hlist_add_after_rcu
* @prev: the existing element to add the new element after.
* @n: the new element to add to the hash list.
*
* Description:
* Adds the specified element to the specified hlist
* after the specified node while permitting racing traversals.
*
* The caller must take whatever precautions are necessary
* (such as holding appropriate locks) to avoid racing
* with another list-mutation primitive, such as hlist_add_head_rcu()
* or hlist_del_rcu(), running on this same list.
* However, it is perfectly legal to run concurrently with
* the _rcu list-traversal primitives, such as
* hlist_for_each_entry_rcu(), used to prevent memory-consistency
* problems on Alpha CPUs.
*/
static inline void hlist_add_after_rcu(struct hlist_node *prev,
struct hlist_node *n)
{
n->next = prev->next;
n->pprev = &prev->next;
prev->next = n;
if (n->next)
n->next->pprev = &n->next;
}
#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
#define hlist_for_each(pos, head) \
for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
pos = n)
/**
* hlist_for_each_entry - iterate over list of given type
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(tpos, pos, head, member) \
for (pos = (head)->first; \
pos && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(tpos, pos, member) \
for (pos = (pos)->next; \
pos && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_from(tpos, pos, member) \
for (; pos && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
for (pos = (head)->first; \
pos && ({ n = pos->next; 1; }) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = n)
/**
* hlist_for_each_entry_rcu - iterate over rcu list of given type
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*
* This list-traversal primitive may safely run concurrently with
* the _rcu list-mutation primitives such as hlist_add_head_rcu()
* as long as the traversal is guarded by rcu_read_lock().
*/
#define hlist_for_each_entry_rcu(tpos, pos, head, member) \
for (pos = (head)->first; \
rcu_dereference(pos) && ({ prefetch(pos->next); 1;}) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
#ifdef __cplusplus
}
#endif
#endif /* _LINUX_LIST_H */
三个部分应该合在一起的。
在C/C++下测试可用。


浙公网安备 33010602011771号