ARM学习笔记之驱动程序篇五----内核链表

1.8 linux内核链表

1.8.1 内核链表简介

    链表是一种常用的数据结构,它通过指针将一系列数据节点连接成一条数据链。相对于数组,链表具有更好的动态性,建立链表时无需预先知道数据总量,可以随机分配空间,可以高效地在链表中的任意位置实时插入或删除数据。链表的开销主要是访问的顺序性和组织链的空间损失。

1.8.2 内核链表结构

struct list_head{
    struct list_head *next,*prev;
};

    list_head结构包含两个指向list_head结构的指针prev和next,由此可见,内核的链表具备双链表功能,实际上,通常它都组织成双向循环链表。 

1.8.3 内核链表函数

//初始化链表
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;
}

//删除list结点
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}

//删除entry结点
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_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;
}

//遍历结点信息
#define list_for_each_entry(pos, head, member)				\
	for (pos = list_entry((head)->next, typeof(*pos), member);	\
	     &pos->member != (head); 	\
	     pos = list_entry(pos->member.next, typeof(*pos), member))

//计算结构信息结点的偏移量
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

//将结构的成员强制转化为包含结构
#define container_of(ptr, type, member) ({          \
	const typeof(((type *)0)->member)*__mptr = (ptr);    \
		     (type *)((char *)__mptr - offsetof(type, member)); })

内核链表具有通用性,就是在于它自定义了一个struct list_head类型的结构体,作为链表的指针指向,然后,将用户定义的链表结点包含该结构体,用户链表的连接只需要使用struct list_head类型的结点指针进行连接,就有效的避免了不同类型链表结点导致,每个不同类型的结点,都必须针对其类型生成特定的链表处理函数。在内核链表中通过struct list_head类型的指针来连接链表中的各个结点,通过计算结点中有效信息部分的偏移量,再通过list_for_each_entry()这个函数达到访问结点信息的目的。因此,即可实现只需要一套链表处理函数,就可以生成不同结点类型的链表。

1.8.4通过内核链表函数处理特定类型结点

#include<linux/module.h>
#include<linux/init.h>
#include<linux/list.h>

//特定信息链表结点类型
struct socre{
    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 list_head *tmp;

int mylist_init(){
    //链表初始化
    INIT_LIST_HEAD(&score_head);
    
    stu1.num=1;
    stu1.english=98;
    stu1.math=90;
    //添加结点
    list_add_tail(&(stu1.list),&score_head);
    
    stu2.num=2;
    stu2.english=91;
    stu2.math=93;
    list_add_tail(&(stu2.list),&score_head);
    
    stu3.num=3;
    stu3.english=96;
    stu3.math=95;
    list_add_tail(&(stu3.list),&score_head);
    //遍历结点信息
    list_for_each(pos,&score_head){
        tmp = list_entry(pos,struct score,list);
        printk("Num is %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);

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值