你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

Linux内核中链表的结构 list_head

[复制链接]
gaosmile 发布时间:2020-9-2 22:42
  在Linux内核中,提供了一个用来创建双向循环链表的结构 list_head。虽然linux内核是用C语言写的,但是list_head的引入,使得内核数据结构也可以拥有面向对象的特性,通过使用操作list_head 的通用接口很容易实现代码的重用,有点类似于C++的继承机制(希望有机会写篇文章研究一下C语言的面向对象机制)。
首先找到list_head结构体定义,kernel/inclue/linux/types.h  如下:

  1. struct list_head {
  2.   struct list_head *next, *prev;
  3. };
  4. #define LIST_HEAD_INIT(name) { &(name), &(name) }
复制代码

需要注意的一点是,头结点head是不使用的,这点需要注意。
使用list_head组织的链表的结构如下图所示:
微信图片_20200902223445.jpg

      然后就开始围绕这个结构开始构建链表,然后插入、删除节点 ,遍历整个链表等等,其实内核已经提供好了现成的接口,接下来就让我们进入 kernel/include/linux/list.h中:
一. 创建链表
    内核提供了下面的这些接口来初始化链表:

  1. #define LIST_HEAD_INIT(name) { &(name), &(name) }

  2. #define LIST_HEAD(name) \
  3.   struct list_head name = LIST_HEAD_INIT(name)

  4. static inline void INIT_LIST_HEAD(struct list_head *list)
  5. {
  6.   WRITE_ONCE(list->next, list);
  7.   list->prev = list;
  8. }
复制代码

如:  可以通过 LIST_HEAD(mylist) 进行初始化一个链表,mylist的prev 和 next 指针都是指向自己。
  • structlist_head mylist = {&mylist, &mylist} ;
     
       但是如果只是利用mylist这样的结构体实现链表就没有什么实际意义了,因为正常的链表都是为了遍历结构体中的其它有意义的字段而创建的,而我们mylist中只有 prev和next指针,却没有实际有意义的字段数据,所以毫无意义。
     综上,我们可以创建一个宿主结构,然后在此结构中再嵌套mylist字段,宿主结构又有其它的字段(进程描述符 task_struct,页面管理的page结构,等就是采用这种方法创建链表的)。为简便理解,定义如下:

  1. struct  mylist{
  2.     int type;
  3.     char name[MAX_NAME_LEN];
  4.     struct list_head list;
  5. }
复制代码

创建链表,并初始化

  1. structlist_head myhead;
  2. INIT_LIST_HEAD(&myhead);
复制代码

这样我们的链表就初始化完毕,链表头的myhead就prev 和 next指针分别指向myhead自己了,如下图:
微信图片_20200902223452.jpg
二. 添加节点
内核已经提供了添加节点的接口了

1.  list_add
    如下所示。根据注释可知,是在链表头head后方插入一个新节点new。

  1. /**
  2. * list_add - add a new entry
  3. * @new: new entry to be added
  4. * @head: list head to add it after
  5. *
  6. * Insert a new entry after the specified head.
  7. * This is good for implementing stacks.
  8. */
  9. static inline void list_add(struct list_head *new, struct list_head *head)
  10. {
  11.   __list_add(new, head, head->next);
  12. }
复制代码

list_add再调用__list_add接口

  1. /*
  2. * Insert a new entry between two known consecutive entries.
  3. *
  4. * This is only for internal list manipulation where we know
  5. * the prev/next entries already!
  6. */
  7. static inline void __list_add(struct list_head *new,
  8.             struct list_head *prev,
  9.             struct list_head *next)
  10. {
  11.   if (!__list_add_valid(new, prev, next))
  12.     return;

  13.   next->prev = new;
  14.   new->next = next;
  15.   new->prev = prev;
  16.   WRITE_ONCE(prev->next, new);
  17. }
复制代码

其实就是在myhead链表头后和链表头后第一个节点之间插入一个新节点。然后这个新的节点就变成了链表头后的第一个节点了。
接着上面步骤创建1个节点然后插入到myhead之后

  1. struct mylist node1;
  2. node1.type = I2C_TYPE;
  3. strcpy(node1.name,"yikoulinux");
  4. list_add(&node1.list,&myhead);
复制代码

微信图片_20200902223457.png

然后在创建第二个节点,同样把它插入到header_task之后

  1. struct mylist node2;
  2. node2.type = I2C_TYPE;
  3. strcpy(node2.name,"yikoupeng");
  4. list_add(&node2.list,&myhead);
复制代码

微信图片_20200902223500.png
list_add
以此类推,每次插入一个新节点,都是紧靠着header节点,而之前插入的节点依次排序靠后,那最后一个节点则是第一次插入header后的那个节点。最终可得出:先来的节点靠后,而后来的节点靠前,“先进后出,后进先出”。所以此种结构类似于 stack“堆栈”, 而header_task就类似于内核stack中的栈顶指针esp,它都是紧靠着最后push到栈的元素。

2. list_add_tail 接口
上面所讲的list_add接口是从链表头header后添加的节点。同样,内核也提供了从链表尾处向前添加节点的接口list_add_tail.让我们来看一下它的具体实现。

  1. /**
  2. * list_add_tail - add a new entry
  3. * @new: new entry to be added
  4. * @head: list head to add it before
  5. *
  6. * Insert a new entry before the specified head.
  7. * This is useful for implementing queues.
  8. */
  9. static inline void list_add_tail(struct list_head *new, struct list_head *head)
  10. {
  11.   __list_add(new, head->prev, head);
  12. }
复制代码

从注释可得出:(1)在一个特定的链表头前面插入一个节点
(2)这个方法很适用于队列的实现(why?)
进一步把__list_add()展开如下:

  1. /*
  2. * Insert a new entry between two known consecutive entries.
  3. *
  4. * This is only for internal list manipulation where we know
  5. * the prev/next entries already!
  6. */
  7. static inline void __list_add(struct list_head *new,
  8.             struct list_head *prev,
  9.             struct list_head *next)
  10. {
  11.   if (!__list_add_valid(new, prev, next))
  12.     return;

  13.   next->prev = new;
  14.   new->next = next;
  15.   new->prev = prev;
  16.   WRITE_ONCE(prev->next, new);
  17. }
复制代码

    所以,很清楚明了, list_add_tail就相当于在链表头前方依次插入新的节点(也可理解为在链表尾部开始插入节点,此时,header节点既是为节点,保持不变)
    利用上面分析list_add接口的方法可画出数据结构图形如下。
(1)创建一个链表头(实际上应该是表尾)代码参考第一节;   
微信图片_20200902223505.jpg
  
(2)插入第一个节点 node1.list , 调用

  1.   struct mylist node1;
  2. node1.type = I2C_TYPE;
  3. strcpy(node1.name,"yikoulinux");
  4. list_add_tail(&node1.list,&myhead);
复制代码

微信图片_20200902223510.png
   
(3) 插入第二个节点node2.list,调用

  1. struct mylist node2;
  2. node2.type = I2C_TYPE;
  3. strcpy(node2.name,"yikoupeng");
  4. list_add_tail(&node2.list,&myhead);
复制代码

微信图片_20200902223518.png
list_add_tail
依此类推,每次插入的新节点都是紧挨着 header_task表尾,而插入的第一个节点my_first_task排在了第一位,my_second_task排在了第二位,可得出:先插入的节点排在前面,后插入的节点排在后面,“先进先出,后进后出”,这不正是队列的特点吗(First in First out)!
三. 删除节点
     内核同样在list.h文件中提供了删除节点的接口 list_del(), 让我们看一下它的实现流程

  1. static inline void list_del(struct list_head *entry)
  2. {
  3.   __list_del_entry(entry);
  4.   entry->next = LIST_POISON1;
  5.   entry->prev = LIST_POISON2;
  6. }
  7. /*
  8. * Delete a list entry by making the prev/next entries
  9. * point to each other.
  10. *
  11. * This is only for internal list manipulation where we know
  12. * the prev/next entries already!
  13. */
  14. static inline void __list_del(struct list_head * prev, struct list_head * next)
  15. {
  16.   next->prev = prev;
  17.   WRITE_ONCE(prev->next, next);
  18. }

  19. /**
  20. * list_del - deletes entry from list.
  21. * @entry: the element to delete from the list.
  22. * Note: list_empty() on entry does not return true after this, the entry is
  23. * in an undefined state.
  24. */
  25. static inline void __list_del_entry(struct list_head *entry)
  26. {
  27.   if (!__list_del_entry_valid(entry))
  28.     return;

  29.   __list_del(entry->prev, entry->next);
  30. }
复制代码

    利用list_del(struct list_head *entry) 接口就可以删除链表中的任意节点了,但需注意,前提条件是这个节点是已知的,既在链表中真实存在,切prev,next指针都不为NULL。
   
四. 链表遍历
    内核是同过下面这个宏定义来完成对list_head链表进行遍历的,如下 :

  1. /**
  2. * list_for_each  -  iterate over a list
  3. * @pos:  the &struct list_head to use as a loop cursor.
  4. * @head:  the head for your list.
  5. */
  6. #define list_for_each(pos, head) \
  7.   for (pos = (head)->next; pos != (head); pos = pos->next)
复制代码

   上面这种方式是从前向后遍历的,同样也可以使用下面的宏反向遍历:

  1. /**
  2. * list_for_each_prev  -  iterate over a list backwards
  3. * @pos:  the &struct list_head to use as a loop cursor.
  4. * @head:  the head for your list.
  5. */
  6. #define list_for_each_prev(pos, head) \
  7.   for (pos = (head)->prev; pos != (head); pos = pos->prev)
复制代码

  而且,list.h 中也提供了list_replace(节点替换)  list_move(节点移位) ,翻转,查找等接口,这里就不在一一分析了。
五. 宿主结构1.找出宿主结构  list_entry(ptr, type, member)
   上面的所有操作都是基于list_head这个链表进行的,涉及的结构体也都是:

  1. struct list_head {
  2.   struct list_head *next, *prev;
  3. };
复制代码

        其实,正如文章一开始所说,我们真正更关心的是包含list_head这个结构体字段的宿主结构体,因为只有定位到了宿主结构体的起始地址,我们才能对对宿主结构体中的其它有意义的字段进行操作。

  1. struct mylist
  2. {
  3.     int type;
  4.     char name[MAX_NAME_LEN];
  5.   struct list_head list;  
  6. };
复制代码

   那我们如何根据list这个字段的地址而找到宿主结构node1的位置呢?
list.h中定义如下:

  1. /**
  2. * list_entry - get the struct for this entry
  3. * @ptr:  the &struct list_head pointer.
  4. * @type:  the type of the struct this is embedded in.
  5. * @member:  the name of the list_head within the struct.
  6. */
  7. #define list_entry(ptr, type, member) \
  8.   container_of(ptr, type, member)
复制代码

   list.h中提供了list_entry宏来实现对应地址的转换,但最终还是调用了container_of宏,所以container_of宏的伟大之处不言而喻。
2 container_of
     做linux驱动开发的同学是不是想到了LDD3这本书中经常使用的一个非常经典的宏定义!
  • container_of(ptr, type, member)
在LDD3这本书中的第三章字符设备驱动,以及第十四章驱动设备模型中多次提到,我觉得这个宏应该是内核最经典的宏之一。那接下来让我们揭开她的面纱:
   此宏在内核代码 kernel/include/linux/kernel.h中定义(此处kernel版本为3.10;新版本4.13之后此宏定义改变,但实现思想保持一致)
微信图片_20200902223825.png
而offsetof定义在 kernel/include/linux/stddef.h ,如下:
微信图片_20200902223830.png
举个例子,来简单分析一下container_of内部实现机制。
例如:

  1. struct   test
  2. {
  3.     int       a;
  4.     short   b;
  5.     char    c;
  6. };
  7. struct  test  *p = (struct test *)malloc(sizeof(struct  test));
  8. test_function(&(p->b));

  9. int  test_function(short *addr_b)
  10. {
  11.       //获取struct test结构体空间的首地址
  12.      struct  test *addr;
  13.      addr =   container_of(addr_b,struct test,b);
  14. }
复制代码

展开container_of宏,探究内部的实现:

  1. typeof (  ( (struct test   *)0 )->b ) ;                        (1)
  2. typeof (  ( (struct test   *)0 )->b )   *__mptr =  addr_b ;    (2)
  3. (struct test *)( (char *)__mptr  -  offsetof(struct test,b))   (3)
复制代码

(1) 获取成员变量b的类型 ,这里获取的就是short 类型。这是GNU_C的扩展语法。
(2) 用获取的变量类型,定义了一个指针变量 __mptr ,并且将成员变量 b的首地址赋值给它
(3) 这里的offsetof(struct test,b)是用来计算成员b在这个struct test 结构体的偏移。__mptr
是成员b的首地址, 现在 减去成员b在结构体里面的偏移值,算出来的是不是这个结构体的
首地址呀 。
3. 宿主结构的遍历
        我们可以根据结构体中成员变量的地址找到宿主结构的地址,并且我们可以对成员变量所建立的链表进行遍历,那我们是不是也可以通过某种方法对宿主结构进行遍历呢?   
       答案肯定是可以的,内核在list.h中提供了下面的宏:

  1. /**
  2. * list_for_each_entry  -  iterate over list of given type
  3. * @pos:  the type * to use as a loop cursor.
  4. * @head:  the head for your list.
  5. * @member:  the name of the list_head within the struct.
  6. */
  7. #define list_for_each_entry(pos, head, member)        \
  8.   for (pos = list_first_entry(head, typeof(*pos), member);  \
  9.        &pos->member != (head);          \
  10.        pos = list_next_entry(pos, member))
复制代码

其中,list_first_entry 和  list_next_entry宏都定义在list.h中,分别代表:获取第一个真正的宿主结构的地址;获取下一个宿主结构的地址。它们的实现都是利用list_entry宏。

  1. /**
  2. * list_first_entry - get the first element from a list
  3. * @ptr:  the list head to take the element from.
  4. * @type:  the type of the struct this is embedded in.
  5. * @member:  the name of the list_head within the struct.
  6. *
  7. * Note, that list is expected to be not empty.
  8. */
  9. #define list_first_entry(ptr, type, member) \
  10.   list_entry((ptr)->next, type, member)

  11. /**
  12. * list_next_entry - get the next element in list
  13. * @pos:  the type * to cursor
  14. * @member:  the name of the list_head within the struct.
  15. */
  16. #define list_next_entry(pos, member) \
  17.   list_entry((pos)->member.next, typeof(*(pos)), member)
复制代码

最终实现了宿主结构的遍历

  1. #define list_for_each_entry(pos, head, member)      \
  2.   for (pos = list_first_entry(head, typeof(*pos), member);  \
  3.        &pos->member != (head);          \
  4.        pos = list_next_entry(pos, member))
复制代码

首先pos定位到第一个宿主结构地址,然后循环获取下一个宿主结构地址,如果查到宿主结构中的member成员变量(宿主结构中struct list_head定义的字段)地址为head,则退出,从而实现了宿主结构的遍历。如果要循环对宿主结构中的其它成员变量进行操作,这个遍历操作就显得特别有意义了。
       我们用上面的 nod结构举个例子:

  1. struct my_list *pos_ptr = NULL ;
  2. list_for_each_entry (pos_ptr, &myhead, list )
  3. {
  4.          printk ("val =  %d\n" , pos_ptr->val);
  5. }
复制代码


实例1 一个简单的链表的实现
为方便起见,本例把内核的list.h文件单独拷贝出来,这样就可以独立于内核来编译测试。
功能描述:
本例比较简单,仅仅实现了单链表节点的创建、删除、遍历。

  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <string.h>

  4. #define MAX_NAME_LEN 32
  5. #define MAX_ID_LEN 10

  6. struct list_head myhead;

  7. #define I2C_TYPE 1
  8. #define SPI_TYPE 2

  9. char *dev_name[]={
  10.   "none",
  11.   "I2C",
  12.   "SPI"
  13. };

  14. struct mylist
  15. {
  16.     int type;
  17.     char name[MAX_NAME_LEN];
  18.   struct list_head list;  
  19. };

  20. void display_list(struct list_head *list_head)
  21. {
  22.   int i=0;
  23.   struct list_head *p;
  24.   struct mylist *entry;
  25.   printf("-------list---------\n");
  26.   list_for_each(p,list_head)
  27.   {
  28.     printf("node[%d]\n",i++);
  29.     entry=list_entry(p,struct mylist,list);
  30.     printf("\ttype: %s\n",dev_name[entry->type]);
  31.     printf("\tname: %s\n",entry->name);
  32.   }
  33.   printf("-------end----------\n");
  34. }

  35. int main(void)
  36. {

  37.   struct mylist node1;
  38.   struct mylist node2;

  39.   INIT_LIST_HEAD(&myhead);

  40.   node1.type = I2C_TYPE;
  41.   strcpy(node1.name,"yikoulinux");

  42.   node2.type = I2C_TYPE;
  43.   strcpy(node2.name,"yikoupeng");

  44.   list_add(&node1.list,&myhead);
  45.   list_add(&node2.list,&myhead);

  46.   display_list(&myhead);

  47.   list_del(&node1.list);

  48.   display_list(&myhead);
  49.   return 0;
  50. }
复制代码


    运行结果
微信图片_20200902223529.jpg
实例2  如何在一个链表上管理不同类型的节点
功能描述:
本实例主要实现在同一个链表上管理两个不同类型的节点,实现增删改查的操作。
结构体定义
一个链表要想区分节点的不同类型,那么节点中必须要有信息能够区分该节点类型,为了方便节点扩展,我们参考Linux内核,定义一个统一类型的结构体:

  1. struct device
  2. {
  3.     int type;
  4.     char name[MAX_NAME_LEN];
  5.     struct list_head list;
  6. };
复制代码

其中成员type表示该节点的类型:

  1. #defineI2C_TYPE 1
  2. #define SPI_TYPE 2
复制代码

有了该结构体,我们要定义其他类型的结构体只需要包含该结构体即可,这个思想有点像面向对象语言的基类,后续派生出新的属性叫子类,说到这,一口君又忍不住想挖个坑,写一篇如何用C语言实现面向对象思想的继承、多态、interface。
下面我们定义2种类型的结构体:
i2c这种类型设备的专用结构体:

  1. struct i2c_node
  2. {
  3.   int data;
  4.   unsigned int reg;
  5.   struct device dev;
  6. };
复制代码

spi这种类型设备的专用结构体:

  1. struct spi_node
  2. {  
  3.   unsigned int reg;
  4.   struct device dev;
  5. };
复制代码

我特意让两个结构体大小类型不一致。
结构类型

链表头结点定义
  • structlist_head device_list;
根据之前我们讲解的思想,这个链表链接起来后,应该是以下这种结构:

微信图片_20200902223538.jpg
节点的插入
我们定义的节点要插入链表仍然是要依赖list_add(),既然我们定义了struct device这个结构体,那么我们完全可以参考linux内核,针对不同的节点封装函数,要注册到这个链表只需要调用该函数即可。
实现如下:
设备i2c的注册函数如下:

  1. void i2c_register_device(struct device*dev)
  2. {
  3.   dev.type = I2C_TYPE;
  4.   strcpy(dev.name,"yikoulinux");
  5.   list_add(&dev->list,&device_list);  
  6. }
复制代码

设备spi的注册函数如下:

  1. void spi_register_device(struct device*dev)
  2. {
  3.   dev.type = SPI_TYPE;
  4.   strcpy(dev.name,"yikoupeng");
  5.   list_add(&dev->list,&device_list);  
  6. }
复制代码


  我们可以看到注册函数功能是填充了struct device 的type和name成员,然后再调用list_add()注册到链表中。这个思想很重要,因为Linux内核中许许多多的设备节点也是这样添加到其他的链表中的。要想让自己的C语言编程能力得到质的提升,一定要多读内核代码,即使看不懂也要坚持看,古人有云:代码读百遍其义自见。
节点的删除
同理,节点的删除,我们也统一封装成函数,同样只传递参数device即可:

  1. void i2c_unregister_device(struct device *device)
  2. {
  3. //  struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);
  4.   list_del(&device->list);
  5. }
  6. void spi_unregister_device(struct device *device)
  7. {
  8. //  struct spi_node *spi_device=container_of(dev, struct spi_node, dev);
  9.   list_del(&device->list);
  10. }
复制代码

在函数中,可以用container_of提取出了设备节点的首地址,实际使用中可以根据设备的不同释放不同的资源。
宿主结构的遍历
节点的遍历,在这里我们通过设备链表device_list开始遍历,假设该节点名是node,通过list_for_each()可以得到node->dev->list的地址,然后利用container_of 可以得到node->dev、node的地址。

  1. void display_list(struct list_head *list_head)
  2. {
  3.   int i=0;
  4.   struct list_head *p;
  5.   struct device *entry;

  6.   printf("-------list---------\n");
  7.   list_for_each(p,list_head)
  8.   {
  9.     printf("node[%d]\n",i++);
  10.     entry=list_entry(p,struct device,list);

  11.     switch(entry->type)
  12.     {
  13.       case I2C_TYPE:
  14.                            display_i2c_device(entry);
  15.         break;
  16.       case SPI_TYPE:
  17.         display_spi_device(entry);
  18.         break;
  19.       default:
  20.         printf("unknown device type!\n");
  21.         break;
  22.     }
  23.     display_device(entry);
  24.   }
  25.   printf("-------end----------\n");
  26. }
复制代码

由以上代码可知,利用内核链表的统一接口,找个每一个节点的list成员,然后再利用container_of 得到我们定义的标准结构体struct device,进而解析出节点的类型,调用对应节点显示函数,这个地方其实还可以优化,就是我们可以在struct device中添加一个函数指针,在xxx_unregister_device()函数中可以将该函数指针直接注册进来,那么此处代码会更精简高效一些。如果在做项目的过程中,写出这种面向对象思想的代码,那么你的地址是肯定不一样的。读者有兴趣可以自己尝试一下。

  1. void display_i2c_device(struct device *device)
  2. {
  3.   struct i2c_node *i2c_device=container_of(device, struct i2c_node, dev);
  4.   printf("\t  i2c_device->data: %d\n",i2c_device->data);
  5.   printf("\t  i2c_device->reg: %#x\n",i2c_device->reg);
  6. }
复制代码

  1. void display_spi_device(struct device *device)
  2. {
  3.   struct spi_node *spi_device=container_of(device, struct spi_node, dev);
  4.   printf("\t  spi_device->reg: %#x\n",spi_device->reg);
  5. }
复制代码

上述代码提取出来宿主节点的信息。
实例代码

  1. #include "list.h"
  2. #include <stdio.h>
  3. #include <string.h>

  4. #define MAX_NAME_LEN 32
  5. #define MAX_ID_LEN 10

  6. struct list_head device_list;

  7. #define I2C_TYPE 1
  8. #define SPI_TYPE 2

  9. char *dev_name[]={
  10.   "none",
  11.   "I2C",
  12.   "SPI"
  13. };

  14. struct device
  15. {
  16.   int type;
  17.   char name[MAX_NAME_LEN];
  18.   struct list_head list;
  19. };

  20. struct i2c_node
  21. {
  22.   int data;
  23.   unsigned int reg;
  24.   struct device dev;
  25. };
  26. struct spi_node
  27. {  
  28.   unsigned int reg;
  29.   struct device dev;
  30. };
  31. void display_i2c_device(struct device *device)
  32. {
  33.   struct i2c_node *i2c_device=container_of(device, struct i2c_node, dev);

  34.   printf("\t  i2c_device->data: %d\n",i2c_device->data);
  35.   printf("\t  i2c_device->reg: %#x\n",i2c_device->reg);
  36. }
  37. void display_spi_device(struct device *device)
  38. {
  39.   struct spi_node *spi_device=container_of(device, struct spi_node, dev);

  40.   printf("\t  spi_device->reg: %#x\n",spi_device->reg);
  41. }
  42. void display_device(struct device *device)
  43. {
  44.     printf("\t  dev.type: %d\n",device->type);
  45.     printf("\t  dev.type: %s\n",dev_name[device->type]);
  46.     printf("\t  dev.name: %s\n",device->name);
  47. }
  48. void display_list(struct list_head *list_head)
  49. {
  50.   int i=0;
  51.   struct list_head *p;
  52.   struct device *entry;

  53.   printf("-------list---------\n");
  54.   list_for_each(p,list_head)
  55.   {
  56.     printf("node[%d]\n",i++);
  57.     entry=list_entry(p,struct device,list);

  58.     switch(entry->type)
  59.     {
  60.       case I2C_TYPE:
  61.         display_i2c_device(entry);
  62.         break;
  63.       case SPI_TYPE:
  64.         display_spi_device(entry);
  65.         break;
  66.       default:
  67.         printf("unknown device type!\n");
  68.         break;
  69.     }
  70.     display_device(entry);
  71.   }
  72.   printf("-------end----------\n");
  73. }
  74. void i2c_register_device(struct device*dev)
  75. {
  76.   struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);

  77.   i2c_device->dev.type = I2C_TYPE;
  78.   strcpy(i2c_device->dev.name,"yikoulinux");

  79.   list_add(&dev->list,&device_list);  
  80. }
  81. void spi_register_device(struct device*dev)
  82. {
  83.   struct spi_node *spi_device=container_of(dev, struct spi_node, dev);

  84.   spi_device->dev.type = SPI_TYPE;
  85.   strcpy(spi_device->dev.name,"yikoupeng");

  86.   list_add(&dev->list,&device_list);  
  87. }
  88. void i2c_unregister_device(struct device *device)
  89. {
  90.   struct i2c_node *i2c_device=container_of(dev, struct i2c_node, dev);

  91.   list_del(&device->list);
  92. }
  93. void spi_unregister_device(struct device *device)
  94. {
  95.   struct spi_node *spi_device=container_of(dev, struct spi_node, dev);

  96.   list_del(&device->list);
  97. }
  98. int main(void)
  99. {

  100.   struct i2c_node dev1;
  101.   struct spi_node dev2;

  102.   INIT_LIST_HEAD(&device_list);
  103.   dev1.data = 1;
  104.   dev1.reg = 0x40009000;
  105.   i2c_register_device(&dev1.dev);
  106.   dev2.reg  = 0x40008000;
  107.   spi_register_device(&dev2.dev);  
  108.   display_list(&device_list);
  109.   unregister_device(&dev1.dev);
  110.   display_list(&device_list);  
  111.   return 0;
  112. }
复制代码
代码主要功能:
  • 117-118          :定义两个不同类型的节点dev1,dev2;
  • 120                 :初始化设备链表;
  • 121-122、124:初始化节点数据;
  • 123/125          :向链表device_list注册这两个节点;
  • 126                :显示该链表;
  • 127                :删除节点dev1;
  • 128                 :显示该链表。

程序运行截图

微信图片_20200902223549.jpg
读者可以试试如何管理更多类型的节点。

实例3  实现节点在两个链表上自由移动
功能描述:
初始化两个链表,实现两个链表上节点的插入和移动。每个节点维护大量的临时内存数据。
节点创建
节点结构体创建如下:

  1. struct mylist{
  2.   int number;
  3.   char type;
  4.     char *pmem;  //内存存放地址,需要malloc
  5.   struct list_head list;
  6. };
复制代码

需要注意成员pmem,因为要维护大量的内存,我们最好不要直定义个很大的数组,因为定义的变量位于栈中,而一般的系统给栈的空间是有限的,如果定义的变量占用空间太大,会导致栈溢出,一口君曾经就遇到过这个bug。
链表定义和初始化
链表定义如下:

  1. structlist_head active_head;
  2. struct list_head free_head;
复制代码
初始化

  1. INIT_LIST_HEAD(&free_head);
  2. INIT_LIST_HEAD(&active_head);
复制代码

这两个链表如下:

微信图片_20200902223557.jpg
关于节点,因为该实例是从实际项目中剥离出来,节点启示是起到一个缓冲去的作用,数量不是无限的,所以在此我们默认最多10个节点。
我们不再动态创建节点,而是先全局创建指针数组,存放这10个节点的地址,然后将这10个节点插入到对应的队列中。
数组定义:
  • structmylist*list_array[BUFFER_NUM];
这个数组只用于存放指针,所以定义之后实际情况如下:

微信图片_20200902223608.jpg


初始化这个数组对应的节点:

  1. static ssize_t buffer_ring_init()
  2. {
  3.   int i=0;
  4.   for(i=0;i<BUFFER_NUM;i++){
  5.     list_array[i]=malloc(sizeof(struct mylist));
  6.     INIT_LIST_HEAD(&list_array[i]->list);
  7.     list_array[i]->pmem=kzalloc(DATA_BUFFER_SIZE,GFP_KERNEL);
  8.   }
  9.   return 0;
  10. }
复制代码

5:为下标为i的节点分配实际大小为sizeof(structmylist)的内存
6:初始化该节点的链表
7:为pmem成员从堆中分配一块内存
初始化完毕,链表实际情况如下:
微信图片_20200902223613.jpg
节点插入

  1. static ssize_t insert_free_list_all()
  2. {
  3.   int i=0;

  4.   for(i=0;i<BUFFER_NUM;i++){
  5.     list_add(&list_array[i]->list,&free_head);
  6.   }
  7.   return 0;
  8. }
复制代码


8:用头插法将所有节点插入到free_head链表中
所有节点全部插入free链表后,结构图如下:

微信图片_20200902223617.jpg

遍历链表
虽然可以通过数组遍历链表,但是实际在操作过程中,在链表中各个节点的位置是错乱的。所以最好从借助list节点来查找各个节点。
  1. how_list(&free_head);
  2. show_list(&active_head);
复制代码

代码实现如下:

  1. void show_list(struct list_head *list_head)
  2. {
  3.   int i=0;
  4.   struct mylist*entry,*tmp;
  5.   //判断节点是否为空
  6.   if(list_empty(list_head)==true)
  7.   {
  8.     return;
  9.   }
  10.   list_for_each_entry_safe(entry,tmp,list_head,list)
  11.   {
  12.     printf("[%d]=%d\t",i++,entry->number);
  13.     if(i%4==0)
  14.     {
  15.       printf("\n");
  16.     }
  17.   }  
  18. }
复制代码


节点移动
将节点从active_head链表移动到free_head链表,有点像生产者消费者模型中的消费者,吃掉资源后,就要把这个节点放置到空闲链表,让生产者能够继续生产数据,所以这两个函数我起名eat、spit,意为吃掉和吐,希望你们不要觉得很怪异。

  1. int eat_node()
  2. {
  3.   struct mylist*entry=NULL;

  4.   if(list_empty(&active_head)==true)
  5.   {
  6.     printf("list active_head is empty!-----------\n");
  7.   }
  8.   entry=list_first_entry(&active_head,struct mylist,list);
  9.   printf("\t eat node=%d\n",entry->number);
  10.   list_move_tail(&entry->list,&free_head);
  11. }
复制代码


节点移动的思路是:
1. 利用list_empty判断该链表是否为空
2. 利用list_first_entry从active_head链表中查找到一个节点,并用指针entry指向该节点
3. 利用list_move_tail将该节点移入到free_head链表,注意此处不能用list_add,因为这个节点我要从原链表把他删除掉,然后插入到新链表。
将节点从free_head链表移动到active_head链表。

  1. spit_node()
  2. {
  3.   struct mylist*entry=NULL;

  4.   if(list_empty(&free_head)==true)
  5.   {
  6.     printf("list free_head is empty!-----------\n");
  7.   }
  8.   entry=list_first_entry(&free_head,struct mylist,list);
  9.   printf("\t spit node=%d\n",entry->number);
  10.   list_move_tail(&entry->list,&active_head);
  11. }
复制代码

大部分功能讲解完了,下面我们贴下完整代码。
代码实例


  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <byteswap.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <signal.h>
  9. #include <fcntl.h>
  10. #include <ctype.h>
  11. #include <termios.h>
  12. #include <sys/types.h>
  13. #include <sys/mman.h>
  14. #include <sys/ioctl.h>
  15. #include "list.h"     //  linux-3.14/scripts/kconfig/list.h

  16. #undef NULL
  17. #define NULL ((void *)0)

  18. enum {
  19.   false  = 0,
  20.   true  = 1
  21. };

  22. #define DATA_TYPE 0x14
  23. #define SIG_TYPE 0x15

  24. struct mylist{
  25.   int number;
  26.   char type;
  27.   char *pmem;
  28.   struct list_head list;
  29. };
  30. #define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n",__LINE_,__FILE__,errno,strerror(errno));exit(1);}while(0)
  31. struct list_head active_head;
  32. struct list_head free_head;
  33. #define BUFFER_NUM 10
  34. #define DATA_BUFFER_SIZE 512
  35. struct mylist*list_array[BUFFER_NUM];


  36. int born_number(int number)
  37. {
  38.   struct mylist *entry=NULL;

  39.   if(list_empty(&free_head)==true)
  40.   {
  41.     printf("list free_head is empty!----------------\n");
  42.   }
  43.   entry = list_first_entry(&free_head,struct mylist,list);
  44.   entry->type = DATA_TYPE;
  45.   entry->number=number;
  46.   list_move_tail(&entry->list,&active_head);  
  47. }

  48. int eat_node()
  49. {
  50.   struct mylist*entry=NULL;

  51.   if(list_empty(&active_head)==true)
  52.   {
  53.     printf("list active_head is empty!-----------\n");
  54.   }
  55.   entry=list_first_entry(&active_head,struct mylist,list);
  56.   printf("\t eat node=%d\n",entry->number);
  57.   list_move_tail(&entry->list,&free_head);
  58. }
  59. spit_node()
  60. {
  61.   struct mylist*entry=NULL;

  62.   if(list_empty(&free_head)==true)
  63.   {
  64.     printf("list free_head is empty!-----------\n");
  65.   }
  66.   entry=list_first_entry(&free_head,struct mylist,list);
  67.   printf("\t spit node=%d\n",entry->number);
  68.   list_move_tail(&entry->list,&active_head);
  69. }

  70. void show_list(struct list_head *list_head)
  71. {
  72.   int i=0;
  73.   struct mylist*entry,*tmp;

  74.   if(list_empty(list_head)==true)
  75.   {
  76.     return;
  77.   }
  78.   list_for_each_entry_safe(entry,tmp,list_head,list)
  79.   {
  80.     printf("[%d]=%d\t",i++,entry->number);
  81.     if(i%4==0)
  82.     {
  83.       printf("\n");
  84.     }
  85.   }  
  86. }
  87. int list_num(struct list_head *list_head)
  88. {
  89.   int i=0;
  90.   struct mylist *entry,*tmp;
  91. //  printf("----------show free list-------------\n");
  92.   list_for_each_entry_safe(entry,tmp,list_head,list)
  93.   {
  94.     i++;
  95.   }
  96.   return i;
  97. }
  98. static ssize_t buffer_ring_init()
  99. {
  100.   int i=0;

  101.   for(i=0;i<BUFFER_NUM;i++){
  102.     list_array[i]=malloc(sizeof(struct mylist));
  103.     INIT_LIST_HEAD(&list_array[i]->list);
  104.     list_array[i]->pmem=kzalloc(DATA_BUFFER_SIZE,GFP_KERNEL);
  105.     list_add_tail(&list_array[i]->list,&free_head);
  106.   }
  107.   return 0;
  108. }
  109. static ssize_t insert_free_list_all()
  110. {
  111.   int i=0;

  112.   for(i=0;i<BUFFER_NUM;i++){
  113.     list_add_tail(&list_array[i]->list,&free_head);
  114.   }
  115.   return 0;
  116. }
  117. static ssize_t buffer_ring_free()
  118. {
  119.   int buffer_count=0;
  120.   struct mylist*entry=NULL;
  121.   for(;buffer_count<BUFFER_NUM;buffer_count++)
  122.   {
  123.     free(list_array[buffer_count]->pmem);
  124.     free(list_array[buffer_count]);
  125.   }
  126.   return 0;
  127. }

  128. int main(int argc,char**argv)
  129. {
  130.   INIT_LIST_HEAD(&free_head);
  131.   INIT_LIST_HEAD(&active_head);
  132.   buffer_ring_init();
  133.   insert_free_list_all();
  134.   born_number(1);
  135.   born_number(2);
  136.   born_number(3);
  137.   born_number(4);
  138.   born_number(5);
  139.   born_number(6);
  140.   born_number(7);
  141.   born_number(8);
  142.   born_number(9);
  143.   born_number(10);

  144.   printf("\n----------active list[%d]------------\n",list_num(&active_head));
  145.   show_list(&active_head);
  146.   printf("\n--------------end-----------------\n");

  147.   printf("\n----------free list[%d]------------\n",list_num(&free_head));  
  148.   show_list(&free_head);
  149.   printf("\n--------------end-----------------\n");

  150.   printf("\n\n    active list----------> free list \n");

  151.   eat_node();
  152.   eat_node();
  153.   eat_node();

  154.   printf("\n----------active list[%d]------------\n",list_num(&active_head));
  155.   show_list(&active_head);
  156.   printf("\n--------------end-----------------\n");

  157.   printf("\n----------free list[%d]------------\n",list_num(&free_head));  
  158.   show_list(&free_head);
  159.   printf("\n--------------end-----------------\n");


  160.   printf("\n\n    free list----------> active list \n");

  161.   spit_node();
  162.   spit_node();

  163.   printf("\n----------active list[%d]------------\n",list_num(&active_head));
  164.   show_list(&active_head);
  165.   printf("\n--------------end-----------------\n");

  166.   printf("\n----------free list[%d]------------\n",list_num(&free_head));  
  167.   show_list(&free_head);
  168.   printf("\n--------------end-----------------\n");

  169. }
复制代码

运行结果如下:
微信图片_20200902223626.jpg
微信图片_20200902223630.jpg
list_head短小精悍,读者可以借鉴此文实现其他功能。


收藏 评论1 发布时间:2020-9-2 22:42

举报

1个回答
sincomaster 回答时间:2020-9-3 11:46:23
头大

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版