内核链表的分析
一。 链表结构
struct list_head {
struct list_head *next, *prev;
}; //链表结构体,两个指针域
二。链表操作
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
} //链表的初始化
================================================
static inline void list_add(struct list_head *new, struct list_head *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);
} //链表操作,插入到尾结点
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;
}
================================================
static inline void list_del(struct list_head *entry)//删除一个结点
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1; //指到一个无效的地址
entry->prev = LIST_POISON2;
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
================================================
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); //重新初始化旧链表
}
================================================
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next); //删除LIST结点
list_add(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_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; //判断链表是否为空
}
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
} //安全的测试链表是否为空
=============================================
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)
{
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);
} //如果条件成立,将以entry为分隔线,以前接到list,以后接到head
=========================================================================================
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.
*/ //合并两个链表,将list添加到head的第一个成员位置
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
} //这个同理,添加到尾
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);
}
} //合并两个链表并添加到第一个成员,并初始化一个链表
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);
}
} //合并两个链表并添加到尾部,并初始化一个链表
===================================================================
链表中,关键的部件,也是核心
//////////////////////////////////////////////////////////
#define list_entry(ptr, type, member) \
container_of(ptr, type, member) //获取指针指向结构体成员的结构体指针
#define container_of(ptr, type, member) ({ \ (type*)0将0转换为type类型的指针
const typeof( ((type *)0)->member ) *__mptr = (ptr); \ //得到指向成员指针的值
(type *)( (char *)__mptr - offsetof(type,member) );}) //得到member相对结构体的偏移量
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
=============================================================
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member) //得到第一个成员。结构体指针
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next) //遍历链表通常和list_entry合用(网上解释prefetch为提高速度,预取)
===============================================================
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next) //没有预取,速度较慢,可以用来读成员较少的,一个或零个
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
pos = pos->prev) //反向遍历
================================================================
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next) //为了避免在遍历结点的时候,发生删除操作,以缓冲下一个,来避免这个情况
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
prefetch(pos->prev), pos != (head); \
pos = n, n = pos->prev) //同理
===================================================================
#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有什么区别,
//应该可以确定仅读某一类结构,因为有结构指针约定,没有搞清楚
#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)) //反向遍历
===================================================================
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member)) //如果不为空,则不变,如为空则返回一个结构体指针
#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)) //可以用做读链表中的成员
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
prefetch(pos->member.prev), &pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member)) //反向读取
到此内枋链表函数源码就分析到这里了,如果有什么错误望大家指出来,共同学习
一。 链表结构
struct list_head {
struct list_head *next, *prev;
}; //链表结构体,两个指针域
二。链表操作
static inline void INIT_LIST_HEAD(struct list_head *list)
{
list->next = list;
list->prev = list;
} //链表的初始化
================================================
static inline void list_add(struct list_head *new, struct list_head *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);
} //链表操作,插入到尾结点
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;
}
================================================
static inline void list_del(struct list_head *entry)//删除一个结点
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1; //指到一个无效的地址
entry->prev = LIST_POISON2;
}
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
next->prev = prev;
prev->next = next;
}
================================================
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); //重新初始化旧链表
}
================================================
static inline void list_move(struct list_head *list, struct list_head *head)
{
__list_del(list->prev, list->next); //删除LIST结点
list_add(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_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; //判断链表是否为空
}
static inline int list_empty_careful(const struct list_head *head)
{
struct list_head *next = head->next;
return (next == head) && (next == head->prev);
} //安全的测试链表是否为空
=============================================
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)
{
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);
} //如果条件成立,将以entry为分隔线,以前接到list,以后接到head
=========================================================================================
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.
*/ //合并两个链表,将list添加到head的第一个成员位置
static inline void list_splice(const struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head, head->next);
}
static inline void list_splice_tail(struct list_head *list,
struct list_head *head)
{
if (!list_empty(list))
__list_splice(list, head->prev, head);
} //这个同理,添加到尾
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);
}
} //合并两个链表并添加到第一个成员,并初始化一个链表
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);
}
} //合并两个链表并添加到尾部,并初始化一个链表
===================================================================
链表中,关键的部件,也是核心
//////////////////////////////////////////////////////////
#define list_entry(ptr, type, member) \
container_of(ptr, type, member) //获取指针指向结构体成员的结构体指针
#define container_of(ptr, type, member) ({ \ (type*)0将0转换为type类型的指针
const typeof( ((type *)0)->member ) *__mptr = (ptr); \ //得到指向成员指针的值
(type *)( (char *)__mptr - offsetof(type,member) );}) //得到member相对结构体的偏移量
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
=============================================================
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member) //得到第一个成员。结构体指针
#define list_for_each(pos, head) \
for (pos = (head)->next; prefetch(pos->next), pos != (head); \
pos = pos->next) //遍历链表通常和list_entry合用(网上解释prefetch为提高速度,预取)
===============================================================
#define __list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next) //没有预取,速度较慢,可以用来读成员较少的,一个或零个
#define list_for_each_prev(pos, head) \
for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
pos = pos->prev) //反向遍历
================================================================
#define list_for_each_safe(pos, n, head) \
for (pos = (head)->next, n = pos->next; pos != (head); \
pos = n, n = pos->next) //为了避免在遍历结点的时候,发生删除操作,以缓冲下一个,来避免这个情况
#define list_for_each_prev_safe(pos, n, head) \
for (pos = (head)->prev, n = pos->prev; \
prefetch(pos->prev), pos != (head); \
pos = n, n = pos->prev) //同理
===================================================================
#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有什么区别,
//应该可以确定仅读某一类结构,因为有结构指针约定,没有搞清楚
#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)) //反向遍历
===================================================================
#define list_prepare_entry(pos, head, member) \
((pos) ? : list_entry(head, typeof(*pos), member)) //如果不为空,则不变,如为空则返回一个结构体指针
#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)) //可以用做读链表中的成员
#define list_for_each_entry_continue_reverse(pos, head, member) \
for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
prefetch(pos->member.prev), &pos->member != (head); \
pos = list_entry(pos->member.prev, typeof(*pos), member)) //反向读取
到此内枋链表函数源码就分析到这里了,如果有什么错误望大家指出来,共同学习