1.INIT_LIST_HEAD: 创建链表
创建链表的时候需要一个头链表所以要先
struct list_head list;
然后调用
INIT_LIST_HEAD(& list); 初始化链表头
2.list_add: 在链表头插入节点
首先是list_add(struct list_head *new, struct list_head *head);
传入要插入的指针域,和头部,然后又进入了__list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
就是把头部next指向,新的指针域. 把原来next->prev指向新的指针域. 然后把新的指针域指向各自的头部和原来的next;
于是就可以在head后不断插入新的指针域(个人理解,不知对错)
3.list_add_tail:在链表尾插入节点
这个原理和上面的有点类似;调用了__list_add, 但是传入的值有所不同.
感觉就是在head->prev上 一个一个的插入,head不是头吗.看着像是尾巴
4.list_del: 删除节点
这里调用了__list_del(entry->prev, entry->next); 把指针指向了自己.
后面的就看不懂了,这里传进去的都是指针吧,那么后面是什么意思呢,求大神啊POISON_POINTER_DELTA是0
entry - > next = LIST_POISON1 ;
entry - > prev = LIST_POISON2 ;
这段来自http://hi.baidu.com/fan_ying_fei/item/5010e140cbe837efbdf4515b
此操作删除一个链表中的指定元素。调用该函数后,指定元素从链表中消失,它的前仆后继成为彼此新的前仆后继。
其中LIST_POISON1、LIST_POISON2是因为删除的元素其next和prev变量仍然是指向它以前的前仆后继,也就是说将会导致从一个
非链表中的对象可以访问链表对象,会引起严重的安全问题
LIST_POISON1和LIST_POISON定义在include/linux/poison.h中:
#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
将该被抛弃的元素的内部指针指向两个内核空间不会用到的内存位置,当试图访问该指针时会出现错误
所以被删除的节点的两个指针被指向一个固定的位置( LIST_POISON1 和 LIST_POISON2 是内核空间的两个地址) -----《linux操作系统与应用》
5.list_entry: 去除节点
调用了container_of(ptr, type, member);用来获得指针域上面的结构体开头地址
ptr:list_head头指针位置 type:结构体类型吧 member:这个结构体里的list_head名字
ptr把list_head的位置放在mptr;然后又offsetof,把TYPE强制转换成0地址开始到MEMBER就是从struct开始到list_head的距离
mptr的地址减去这个距离就是struct的位置,从而能使用struct的变量
6.list_for_each: 遍历链表
list_for_each是个循环遍历链表头,更看不懂了..先睡了
这段来自http://hi.baidu.com/fan_ying_fei/item/5010e140cbe837efbdf4515b
它实际上是一个for循环,利用传入的pos作为循环变量,从表头head开始,逐项向后(next方向)移动pos,直至又回到head,停止循环,该宏使用两个list_head类型的参数,第一个参数用来指向当前项,第二个参数指向需要检索的链表
当然使用该遍历链表只能得指向list_head结构体的指针,但是无法访问其成员变量,为此linux提供了
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
假设设有一个list_head的链表,且每个链表包含在另外一个结构体的对象里,这些外部结构体的对象通过list_head联系起来。如果ptr指向链表中一个已知的list_head对象,那么list_entry宏会返回包含这个对象的外部结构体对象的指针,从而实现访问其成员变量的遍历
自己写的:
阅读(267) | 评论(2) | 转发(0) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
点击(此处)折叠或打开
- /*
- * 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.
- */
-
- #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;
- }
struct list_head list;
然后调用
INIT_LIST_HEAD(& list); 初始化链表头
2.list_add: 在链表头插入节点
点击(此处)折叠或打开
- /**
- * 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);
- }
-
- /*
- * Insert a new entry between two known consecutive entries.
- *
- * This is only for internal list manipulation where we know
- * the prev/next entries
- */
- #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(struct list_head *new, struct list_head *prev, struct list_head *next)
就是把头部next指向,新的指针域. 把原来next->prev指向新的指针域. 然后把新的指针域指向各自的头部和原来的next;
于是就可以在head后不断插入新的指针域(个人理解,不知对错)
3.list_add_tail:在链表尾插入节点
点击(此处)折叠或打开
- /**
- * 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);
- }
-
- /*
- * Insert a new entry between two known consecutive entries.
- *
- * This is only for internal list manipulation where we know
- * the prev/next entries
- */
- #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
感觉就是在head->prev上 一个一个的插入,head不是头吗.看着像是尾巴
4.list_del: 删除节点
点击(此处)折叠或打开
- /**
- * list_del - deletes entry from list.
- * @entry: the element to delete from the list.
- * 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);
- }
-
- static inline void list_del(struct list_head *entry)
- {
- __list_del(entry->prev, entry->next);
- 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
-
- /*
- * 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
- */
- static inline void __list_del(struct list_head * prev, struct list_head * next)
- {
- next->prev = prev;
- prev->next = next;
- }
-
- /*
- * These are non-NULL pointers that will result in page faults
- * under normal circumstances, used to verify that nobody uses
- * non-initialized list entries.
- */
- #define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
- #define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
后面的就看不懂了,这里传进去的都是指针吧,那么后面是什么意思呢,求大神啊POISON_POINTER_DELTA是0
entry - > next = LIST_POISON1 ;
entry - > prev = LIST_POISON2 ;
这段来自http://hi.baidu.com/fan_ying_fei/item/5010e140cbe837efbdf4515b
此操作删除一个链表中的指定元素。调用该函数后,指定元素从链表中消失,它的前仆后继成为彼此新的前仆后继。
其中LIST_POISON1、LIST_POISON2是因为删除的元素其next和prev变量仍然是指向它以前的前仆后继,也就是说将会导致从一个
非链表中的对象可以访问链表对象,会引起严重的安全问题
LIST_POISON1和LIST_POISON定义在include/linux/poison.h中:
#define LIST_POISON1 ((void *) 0x00100100 + POISON_POINTER_DELTA)
#define LIST_POISON2 ((void *) 0x00200200 + POISON_POINTER_DELTA)
将该被抛弃的元素的内部指针指向两个内核空间不会用到的内存位置,当试图访问该指针时会出现错误
所以被删除的节点的两个指针被指向一个固定的位置( LIST_POISON1 和 LIST_POISON2 是内核空间的两个地址) -----《linux操作系统与应用》
5.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)
-
- #ifndef container_of
- /**
- * container_of - cast a member of a structure out to the containing structure
- * @ptr: the pointer to the member.
- * @type: the type of the container struct this is embedded in.
- * @member: the name of the member within the struct.
- *
- */
- #define container_of(ptr, type, member) ({ \
- const typeof(((type *)0)->member) * __mptr = (ptr); \
- (type *)((char *)__mptr - offsetof(type, member)); })
- #endif
-
- #ifndef offsetof
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
- #endif
ptr:list_head头指针位置 type:结构体类型吧 member:这个结构体里的list_head名字
ptr把list_head的位置放在mptr;然后又offsetof,把TYPE强制转换成0地址开始到MEMBER就是从struct开始到list_head的距离
mptr的地址减去这个距离就是struct的位置,从而能使用struct的变量
6.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.
- */
- #define list_for_each(pos, head) \
- for (pos = (head)->next; prefetch(pos->next), pos != (head); \
- pos = pos->next)
-
- #ifndef PERF_LINUX_PREFETCH_H
- #define PERF_LINUX_PREFETCH_H
-
- static inline void prefetch(void *a __attribute__((unused))) { }
-
- #endif
-
- static inline __attribute__((pure)) int phys_to_nid(unsigned long addr)
- {
- unsigned nid;
- VIRTUAL_BUG_ON(!memnodemap);
- nid = memnodemap[addr >> memnode_shift];
- VIRTUAL_BUG_ON(nid >= MAX_NUMNODES || !node_data[nid]);
- return nid;
- }
这段来自http://hi.baidu.com/fan_ying_fei/item/5010e140cbe837efbdf4515b
它实际上是一个for循环,利用传入的pos作为循环变量,从表头head开始,逐项向后(next方向)移动pos,直至又回到head,停止循环,该宏使用两个list_head类型的参数,第一个参数用来指向当前项,第二个参数指向需要检索的链表
当然使用该遍历链表只能得指向list_head结构体的指针,但是无法访问其成员变量,为此linux提供了
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
假设设有一个list_head的链表,且每个链表包含在另外一个结构体的对象里,这些外部结构体的对象通过list_head联系起来。如果ptr指向链表中一个已知的list_head对象,那么list_entry宏会返回包含这个对象的外部结构体对象的指针,从而实现访问其成员变量的遍历
自己写的:
- #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/list.h>
-
-
- struct score
- {
- int num;
- int english;
- int math;
- struct list_head list;
- };
-
-
- struct list_head score_head;
- struct score stu1,stu2,stu3;
- struct list_head *pos;
- struct score *tmp;
-
-
-
-
- int mylist_init()
- {
- INIT_LIST_HEAD(&score_head);
-
- stu1.num = 1;
- stu1.english = 90;
- stu1.math =98;
- list_add_tail(&(stu1.list),&score_head);
-
- stu2.num = 2;
- stu2.english = 91;
- stu2.math =96;
- list_add_tail(&(stu2.list),&score_head);
-
-
- stu3.num = 3;
- stu3.english = 92;
- stu3.math = 99;
- list_add_tail(&(stu3.list),&score_head);
-
- list_for_each(pos,&score_head)
- {
- tmp = list_entry(pos,struct score,list);
- printk("No. %d, english is %d, math is %d\n",tmp->num,tmp->english,tmp->math);
- }
-
- return 0;
- }
-
-
- void mylist_exit()
- {
- list_del(&(stu1.list));
- list_del(&(stu2.list));
- }
-
-
- module_init(mylist_init);
-
-
- module_exit(mylist_exit);
相关热门文章
给主人留下些什么吧!~~
评论热议