www.pudn.com > nat.rar > list.h
#ifndef _LIST_H
#define _LIST_H
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)
/**
* list_entry - get the struct for this entry 如果type结构中member的地址是ptr,则返回type结构的地址
* @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)))
static inline void __list_add(struct list_head * one,
struct list_head * prev,struct list_head * next)
{
next->prev = one;
one->next = next;
one->prev = prev;
prev->next = one;
}
//插入新的列表到head的前面
static inline void list_add(struct list_head *one, struct list_head *head)
{
__list_add(one, head, head->next);
}
//插入新的节点到head的后面
static inline void list_add_tail(struct list_head *one, struct list_head *head)
{
__list_add(one, head->prev, head);
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
//节点删除
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
static inline void list_del_init(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
//列表是否空
static inline int list_empty(struct list_head *head)
{
return head->next == head;
}
//将两个环形链表合成一个大表
static inline void list_splice(struct list_head *list, struct list_head *head)
{
struct list_head *first = list->next;
if (first != list) {
struct list_head *last = list->prev;
struct list_head *at = head->next;
first->prev = head;
head->next = first;
last->next = at;
at->prev = last;
}
}
/*/
// Return pointer to first true entry, if any, or NULL. A macro
// required to allow inlining of cmpfn.
#define LIST_FIND(head, cmpfn, type, args...) \
({ \
const struct list_head *__i, *__j = NULL; \
\
list_for_each(__i, (head)) \
if (cmpfn((const type)__i , ## args)) { \
__j = __i; \
break; \
} \
(type)__j; \
})
//*/
#define LIST_FIND(head, cmpfn, type, args) \
({ \
const struct list_head *__i = (head); \
do { \
__i = __i->next; \
if (__i == (head)) { \
__i = NULL; \
break; \
} \
} while (!cmpfn((const type)__i , args)); \
(type)__i; \
})
static inline int
__list_cmp_same(const void *p1, const void *p2)
{
return (p1 == p2);
}
#define LIST_DELETE(head, oldentry) list_del((struct list_head *)oldentry)
//将entry添加到head之后,用于构造堆栈
static inline void
list_append(struct list_head *head, void *one)
{
list_add((struct list_head *)one, (head)->prev);
}
//将entry添加到head之前,用于构造队列
static inline void
list_prepend(struct list_head *head, void *one)
{
list_add((struct list_head *)one, head);
}
// 顺序插入,在比较函数成立时插入
#define LIST_INSERT(head, one, cmpfn) \
do { \
struct list_head *__i; \
for (__i = (head)->next; \
!cmpfn((one), (typeof (one))__i) && __i != (head); \
__i = __i->next); \
list_add((struct list_head *)(one), __i->prev); \
} while(0)
#endif