赞
踩
队列:队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。
循环队列:在实际使用队列时,为了使队列空间能重复使用,往往对队列的使用方法稍加改进:无论插入或删除,一旦rear指针增1或front指针增1 时超出了所分配的队列空间,就让它指向这片连续空间的起始位置。自己真从MaxSize-1增1变到0,可用取余运算rear%MaxSize和front%MaxSize来实现。这实际上是把队列空间想象成一个环形空间,环形空间中的存储单元循环使用,用这种方法管理的队列也就称为循环队列。内核使用的队列结构便是循环队列。
Linux的kfifo提供了两个主要操作:enqueue(入队)和dequeue(出队)。kfifo维护了两个偏移量:入口偏移(下次入队位置)和出口偏移(下次出队位置)。
内核关于kfifo的源码位于:
include/linux/kfifo.h
lib/kfifo.c
内核队列的设计精妙之处在于:
理解宏定义形式建议首先从之前版本的函数开始,能够有更清晰的认识,具体详情见文末补充链接。
struct __kfifo {
unsigned int in; // 入列
unsigned int out; // 出列
unsigned int mask; // 巧妙的mask设计,同时包含了数据的个数信息
unsigned int esize; // 元素大小
void *data; // 数据
};
新版本内核中,kfifo中大量使用了宏定义方式,在使用kfifo前需要对其进行定义和初始化,最常用的是动态方法。
/** * kfifo_alloc - dynamically allocates a new fifo buffer * @fifo: pointer to the fifo * @size: the number of elements in the fifo, this must be a power of 2 * @gfp_mask: get_free_pages mask, passed to kmalloc() * * This macro dynamically allocates a new fifo buffer. * * The number of elements will be rounded-up to a power of 2. * The fifo will be release with kfifo_free(). * Return 0 if no error, otherwise an error code. */ #define kfifo_alloc(fifo, size, gfp_mask) \ __kfifo_int_must_check_helper( \ ({ \ typeof((fifo) + 1) __tmp = (fifo); \ struct __kfifo *__kfifo = &__tmp->kfifo; \ __is_kfifo_ptr(__tmp) ? \ __kfifo_alloc(__kfifo, size, sizeof(*__tmp->type), gfp_mask) : \ -EINVAL; \ }) \ )
该函数创建并初始化一个大小为size的kfifo。内核使用gfp_mask标志分配队列,若成功返回0,错误则返回负数错误码。(size必须是2的幂)
若想自己分配缓冲,可以使用该函数。
/** * kfifo_init - initialize a fifo using a preallocated buffer * @fifo: the fifo to assign the buffer * @buffer: the preallocated buffer to be used * @size: the size of the internal buffer, this have to be a power of 2 * * This macro initializes a fifo using a preallocated buffer. * * The number of elements will be rounded-up to a power of 2. * Return 0 if no error, otherwise an error code. */ #define kfifo_init(fifo, buffer, size) \ ({ \ typeof((fifo) + 1) __tmp = (fifo); \ struct __kfifo *__kfifo = &__tmp->kfifo; \ __is_kfifo_ptr(__tmp) ? \ __kfifo_init(__kfifo, buffer, size, sizeof(*__tmp->type)) : \ -EINVAL; \ })
该函数创建并初始化一个kfifo对象,它将使用由buffer指向的size字节大小的内存。(size必须是2的幂)使用该方法需要自己释放相关缓冲。
/** * kfifo_in - put data into the fifo * @fifo: address of the fifo to be used * @buf: the data to be added * @n: number of elements to be added * * This macro copies the given buffer into the fifo and returns the * number of copied elements. * * Note that with only one concurrent reader and one concurrent * writer, you don't need extra locking to use these macro. */ #define kfifo_in(fifo, buf, n) \ ({ \ typeof((fifo) + 1) __tmp = (fifo); \ typeof(__tmp->ptr_const) __buf = (buf); \ unsigned long __n = (n); \ const size_t __recsize = sizeof(*__tmp->rectype); \ struct __kfifo *__kfifo = &__tmp->kfifo; \ (__recsize) ?\ __kfifo_in_r(__kfifo, __buf, __n, __recsize) : \ __kfifo_in(__kfifo, __buf, __n); \ })
该函数将buf内的n字节数据拷贝到fifo所指队列中
/** * kfifo_out - get data from the fifo * @fifo: address of the fifo to be used * @buf: pointer to the storage buffer * @n: max. number of elements to get * * This macro get some data from the fifo and return the numbers of elements * copied. * * Note that with only one concurrent reader and one concurrent * writer, you don't need extra locking to use these macro. */ #define kfifo_out(fifo, buf, n) \ __kfifo_uint_must_check_helper( \ ({ \ typeof((fifo) + 1) __tmp = (fifo); \ typeof(__tmp->ptr) __buf = (buf); \ unsigned long __n = (n); \ const size_t __recsize = sizeof(*__tmp->rectype); \ struct __kfifo *__kfifo = &__tmp->kfifo; \ (__recsize) ?\ __kfifo_out_r(__kfifo, __buf, __n, __recsize) : \ __kfifo_out(__kfifo, __buf, __n); \ }) \ )
该函数将fifo内的n字节数据拷贝到buf中,此时队列中的数据将被删除。
/**
* kfifo_size - returns the size of the fifo in elements
* @fifo: address of the fifo to be used
*/
#define kfifo_size(fifo) ((fifo)->kfifo.mask + 1)
/**
* kfifo_len - returns the number of used elements in the fifo
* @fifo: address of the fifo to be used
*/
#define kfifo_len(fifo) \
({ \
typeof((fifo) + 1) __tmpl = (fifo); \
__tmpl->kfifo.in - __tmpl->kfifo.out; \
})
/**
* kfifo_avail - returns the number of unused elements in the fifo
* @fifo: address of the fifo to be used
*/
#define kfifo_avail(fifo) \
__kfifo_uint_must_check_helper( \
({ \
typeof((fifo) + 1) __tmpq = (fifo); \
const size_t __recsize = sizeof(*__tmpq->rectype); \
unsigned int __avail = kfifo_size(__tmpq) - kfifo_len(__tmpq); \
(__recsize) ? ((__avail <= __recsize) ? 0 : \
__kfifo_max_r(__avail - __recsize, __recsize)) : \
__avail; \
}) \
)
/** * kfifo_is_empty - returns true if the fifo is empty * @fifo: address of the fifo to be used */ #define kfifo_is_empty(fifo) \ ({ \ typeof((fifo) + 1) __tmpq = (fifo); \ __tmpq->kfifo.in == __tmpq->kfifo.out; \ }) /** * kfifo_is_full - returns true if the fifo is full * @fifo: address of the fifo to be used */ #define kfifo_is_full(fifo) \ ({ \ typeof((fifo) + 1) __tmpq = (fifo); \ kfifo_len(__tmpq) > __tmpq->kfifo.mask; \ })
/**
* kfifo_reset - removes the entire fifo content
* @fifo: address of the fifo to be used
*
* Note: usage of kfifo_reset() is dangerous. It should be only called when the
* fifo is exclusived locked or when it is secured that no other thread is
* accessing the fifo.
*/
#define kfifo_reset(fifo) \
(void)({ \
typeof((fifo) + 1) __tmp = (fifo); \
__tmp->kfifo.in = __tmp->kfifo.out = 0; \
})
/**
* kfifo_free - frees the fifo
* @fifo: the fifo to be freed
*/
#define kfifo_free(fifo) \
({ \
typeof((fifo) + 1) __tmp = (fifo); \
struct __kfifo *__kfifo = &__tmp->kfifo; \
if (__is_kfifo_ptr(__tmp)) \
__kfifo_free(__kfifo); \
})
《Linux内核设计与实现(第三版)》
从2.6.36开始,kfifo修改为如下形式,与之前内核kfifo结构对比见:
https://blog.csdn.net/zh1074/article/details/50962697
理解宏定义形式建议首先从之前版本的函数开始,能够有更清晰的认识:
https://blog.csdn.net/zhoutaopower/article/details/86491852
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。