6---内核数据结构

关于链表,以下说法正确的是 ( )

A 需经常修改线性表L中的结点值适合采用链式结构

B 需不断对线性表L进行删除插入适合采用链式结构

C 带头结点的单链表,在表的第一个元素之前插入一个新元素与链表长度无关

D 删除单链表中的最后一个元素,与链表长度无关

分析:A:如果修改时插入删除的话,链式当然可行,但如果只是修改数值没什么必然关系。B:链式结构插入删除只需要修改指针 指向。C:在头结点前插入节点,只需要该节点的指针指向头结点就行了; D:删除尾节点,要知道指向尾节点指针,链表遍历到尾结点肯定与其长度有关

链表的特点是:

  • 动态创建的、非连续内存。
  • 更适用于频繁的(任何位置)插入、写入情况。但不是适用于随机访问。

链表类基本操作:

1.遍历访问:只依靠指针从头到尾一个个访问,而不能像数组一样支持下标随机访问。

2.删除和插入:也只能依靠指针遍历到删除或插入位置,

 

链表在Linux内核的实现------环形链表,可以把链表中任何一个位置的结点当做头结点

在目录\include\linux下定义了list.h文件,关于链表的函数直接引用该模板

//链表的数据结构
struct list_head{
    struct list_head *next;
    struct list_head *prev;
}
//插入
void INIT_LIST_HEAD(struct list_head *list){
	list->next = list;
	list->prev = list;
}
//其他位置插入
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_tail(struct list_head *new, struct list_head *head)
{
	__list_add(new, head->prev, head);
}
//删除一个结点
static inline void __list_del(struct list_head * prev, struct list_head * next)
{
	next->prev = prev;
	prev->next = next;
}

队列也称为FIFO,第一个进入队列的一定是第一个离开队列的。

Linux的Kfifo提供两个操作函数enqueue(入队)和dequeue(出队)。

  • enqueue拷贝数据到队列的入口偏移位置,入口偏移随之加上元素数目
  • dequeue从队列出口偏移位置拷贝数据,出口偏移随之减去元素数目

维护两个偏移变量入口偏移和出口偏移,入口偏移指下一次入队列的位置,出口偏移指下一次出队列的位置。

  • 出口偏移总是小于等于入口偏移
  • 当出口偏移等于入口偏移时说明队列空了
  • 当入口偏移等于队列长度时说明队列满了

在目录\include\linux下定义了Kfifo.h文件,在lib下定义了Kfifo.c文件,关于队列的函数直接引用该模板。

/*****************创建和释放队列***************/
int __kfifo_alloc(struct __kfifo *fifo, unsigned int size,size_t esize, gfp_t gfp_mask{
    size = roundup_pow_of_two(size);    
    //初始化fifo
	fifo->in = 0;
	fifo->out = 0;
	fifo->esize = esize;
	if (size < 2) {
		fifo->data = NULL;
		fifo->mask = 0;
		return -EINVAL;
	}
    //分配内存
	fifo->data = kmalloc(size * esize, gfp_mask);//gfp_mask分配队列内存的标志
	if (!fifo->data) {
		fifo->mask = 0;
		return -ENOMEM;
	}
	fifo->mask = size - 1;
	return 0;
}

void __kfifo_free(struct __kfifo *fifo)
{
	kfree(fifo->data);
	fifo->in = 0;
	fifo->out = 0;
	fifo->esize = 0;
	fifo->data = NULL;
	fifo->mask = 0;
}



int __kfifo_init(struct __kfifo *fifo, void *buffer,unsigned int size, size_t esize){
	size /= esize;
	size = roundup_pow_of_two(size);
	//初始化fifo
	fifo->in = 0;
	fifo->out = 0;
	fifo->esize = esize;    
    //内存是buffer指向的size大小的内存
	fifo->data = buffer;
	if (size < 2) {
		fifo->mask = 0;
		return -EINVAL;
	}
	fifo->mask = size - 1;
	return 0;
}
/*****************向队列添加数据*********************/
unsigned int __kfifo_in(struct __kfifo *fifo,const void *buf, unsigned int len){
	unsigned int l;
	l = kfifo_unused(fifo);
	if (len > l)
		len = l;
	kfifo_copy_in(fifo, buf, len, fifo->in);
	fifo->in += len;
	return len;
}

static void kfifo_copy_in(struct __kfifo *fifo, const void *src,unsigned int len,unsigned int off){
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;
	unsigned int l;
	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);
	memcpy(fifo->data + off, src, l);
	memcpy(fifo->data, src + l, len - l);
	/*
	 * make sure that the data in the fifo is up to date before
	 * incrementing the fifo->in index counter
	 */
	smp_wmb();
}
/******************从队列取数据************************/
unsigned int __kfifo_out(struct __kfifo *fifo,void *buf, unsigned int len){
	len = __kfifo_out_peek(fifo, buf, len);
	fifo->out += len;
	return len;
}
unsigned int __kfifo_out_peek(struct __kfifo *fifo,void *buf, unsigned int len){
	unsigned int l;
	l = fifo->in - fifo->out;
	if (len > l)
		len = l;
	kfifo_copy_out(fifo, buf, len, fifo->out);
	return len;
}
static void kfifo_copy_out(struct __kfifo *fifo, void *dst,unsigned int len, unsigned int off){
	unsigned int size = fifo->mask + 1;
	unsigned int esize = fifo->esize;
	unsigned int l;
	off &= fifo->mask;
	if (esize != 1) {
		off *= esize;
		size *= esize;
		len *= esize;
	}
	l = min(len, size - off);
	memcpy(dst, fifo->data + off, l);
	memcpy(dst + l, fifo->data, len - l);
/*make sure that the data is copied before incrementing the fifo->out index counter */
	smp_wmb();
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值