相关博文:
linux中的list.h(1) ——>内核链表简介、list_entry()方法。
linux中的list.h(2) ——>内核链表的创建、LIST_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;
}
头插法:在head和head->next之间插入一个新的节点。有利于实现栈。
/**
* 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);
}
尾插法:在head->prev(双向循环链表中的最后一个节点)和head之间插入一个新的节点。有利于实现队列。
/**
* 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);
}
假设我们创建一个新的struct fox f,并把它加入到fox_list:
list_add(&f->list,&fox_list);
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.
*/
static inline void list_del(struct list_head *entry)
{
__list_del(entry->prev, entry->next);
}
注意:该函数从链表中删除entry元素,但并不会释放entry或释放包含entry的宿主结构体所占用的内存,该函数仅仅是将entry从链表中移走,所以该函数被调用后,通常还需要再撤销包含entry的数据结构和其中的entry项。
删除我们刚才新添加的struct fox f:
list_del(&f->list)
我们发现这个函数并没有接受fox_list作为输入参数。该函数只是接受一个特定的节点,并修改其前后节点的指针,这样给定的节点就从链表中删除了。
list_for_each()
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
该宏使用两个list_head类型的参数,第一个参数用来指向当前项(充当临时变量),第二个参数是需要遍历的链表的以头节点形式存在的list_head。每次遍历时,第一个参数在链表中不断移动指向下一个元素,直到链表中的所有元素都被访问为止。
示例:
struct list_head *p;
list_for_each(p,&fox_list){
//...
}
不过其实一个指向链表结构(list_head)的指针通常是无用的。我们是将链表塞入了我们的数据结构,即把list_head嵌入对象的数据结构。所有的链表操作是针对链表结构(list_head),而链表的设计者并不知道链表宿主结构体的任何情况。
我们所需要的是一个指向包含list_head的结构体(链表宿主结构体)的指针。
拿前面的struct fox举例子来说明:
我们需要的是指向每个fox的指针,而不是指向fox结构体中list成员的指针。
我们可以使用list_entry()宏来获得包含给定list_head的数据结构。
list_for_each_entry()
/**
* 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_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
该宏的三个参数:
pos = list_first_entry(head, typeof(*pos), member)
相当于
pos = list_entry((head)->next, typeof(*pos), member)
利用list_entry宏和链表头指针head的next指针域head->next,反推出宿主节点头指针pos。
然后判断pos->member
的地址是否等于指向链表头指针head,如果不等,则表示还没有遍历完。
在当前宿主节点遍历完后,将pos指向链表中下一个宿主节点。
pos = list_next_entry(pos, member)
就相当于
pos = list_entry((pos)->member.next, typeof(*(pos)), member)
示例:
struct fox *f;
list_for_each_entry(f, &fox_list, list){
//...
}
/**
* 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_head 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_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
list.h
#ifndef _LINUX_LIST_H
#define _LINUX_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)
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 *prev,
struct list_head *next)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
static inline void list_add(struct list_head *new, struct list_head *head)
{
__list_add(new, head, head->next);
}
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);
}
#ifdef __compiler_offsetof
#define offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
#else
#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)
#endif
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
#endif
list_study.c
#include
#include
#include "list.h"
struct fox{
unsigned int weight;
int is_cute;
struct list_head list;
};
int main()
{
struct fox *red_fox;
red_fox=(struct fox*)malloc(sizeof(struct fox));
red_fox->weight=6;
red_fox->is_cute=0;
INIT_LIST_HEAD(&red_fox->list);
printf("red_fox:\t%p\t%p\t%p\n",&red_fox->list,
(red_fox->list).next,
(red_fox->list).prev);
printf("\t\t%p\t%d\t%d\n",&red_fox->list, red_fox->weight, red_fox->is_cute);
struct fox *write_fox;
write_fox=(struct fox*)malloc(sizeof(struct fox));
write_fox->weight=7;
write_fox->is_cute=-1;
INIT_LIST_HEAD(&write_fox->list);
printf("write_fox:\t%p\t%p\t%p\n",&write_fox->list,
(write_fox->list).next,
(write_fox->list).prev);
printf("\t\t%p\t%d\t%d\n",&write_fox->list, write_fox->weight, write_fox->is_cute);
//链表头
LIST_HEAD(fox_list);
//添加
list_add(&(red_fox->list),&fox_list);
list_add(&(write_fox->list),&fox_list);
struct fox *pos;
//遍历
list_for_each_entry(pos,&fox_list,list){
printf("%p\t%d\t%d\n",&pos->list, pos->weight, pos->is_cute);
}
//删除
list_del(&(red_fox->list));
free(red_fox);
list_del(&(write_fox->list));
free(write_fox);
list_for_each_entry(pos,&fox_list,list){
printf("%p\t%d\t%d\n",&pos->list, pos->weight, pos->is_cute);
}
return 0;
}