# Linux内核之数据结构--队列

## 前言

Linux内核实现了以下常用的内建数据结构，主要有：
链表
队列
映射
二叉树

## kfifo

struct kfifo {
unsigned char *buffer;    //the buffer holding the data
unsigned int size;    //the size of the allocated buffer
unsigned int in;    //data is added at offset (in % size)
unsigned int out;//data is extracted from off. (out % size)
spinlock_t *lock;    //protects concurrent modifications
};  

## 创建队列

int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask);

void kfifo_init(struct kfifo *fifo, void *buffer, unsigned int size);

DECLARE_KFIFO(name, size);
INIT_KFIFO(name);

## 推入数据队列

unsigned int kfifo_in(struct kfifo *fifo, const void *from, unsigned int len)

## 摘取队列数据

unsigned int kfifo_out(struct kfifo *fifo, void *to, unsigned int len);

unsigned int kfifo_out_peek(struct kfifo *fifo, void *to, unsigned int len, unsigned offset);

## 获取队列长度

static inline unsigned int kfifo_size(struct kfifo *fifo);

static inline unsigned int kfifo_len(struct kfifo *fifo);

static inline unsigned int kfifo_avoil(struct kfifo *fifo);

static inline int kfifo_is_empty(struct kfifo *fifo);
//若为空返回非0值，否则返回0

static inline unsigned int kfifo_is_full(struct kfifo *fifo);
//若队列已满返回非0值，否则返回0

## 重置和撤销队列

static inline void kfifo_reset(struct kfifo *fifo);

void kfifo_free(struct kfifo *fifo);

08-24
01-10 1447

06-10 6259
07-08 1161
08-02 2172
12-09 2569
06-28 2551
03-25 37
11-30 950
04-16 381
02-25 5771
11-21 361
03-30 6923
09-04 1289
12-12 1418
10-23 621
10-11 2万+