Linux kernel ———— queues
Linux kernel--queues
Defined:
struct __kfifo{
unsigned int in; The team is offset, write index
unsigned int out; Out team offset, read index
unsigned int mask;
unsigned int esize;
void *data;
}
Use:
Creates a queue that creates and initializes a kfifo of size:
__kfifo_alloc int (struct __KFIFO *fifo, unsigned int size, M size_t esize, gfp_t gfp_mask)
{
45
size = roundup_pow_of_two (size); 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);
The if (!fifo->data) { fifo->mask = 0; Return-enomem; fifo->mask = size-1; 0;
66}
To determine whether an integer is a power of 2 integers
static inline int is_power_of_2 (unsigned long long n)
{return
(n!=0 && (n& (n-1) ==0))
}
The way to push data to the queue is the kfifo_in () function
102 static void kfifo_copy_in (struct __kfifo *fifo, const void *SRC, unsigned int len, unsigned int o FF) {unsigned int size = fifo->mask + 1; unsigned int esize = fifo->esize; 107
unsigned int l; 108 109 off &= fifo->mask; The operation from the mathematical point of view will be the fifo->mask of the length of the modulo operation (esize!= 1) {*= esize; 112 si
Ze *= esize;
113 Len *= esize; 114} L = min (len, size-off); Size-off represents the current in to the end of the buffer size, 116/* First copy the L bytes from the buffer to the remaining space, L<=len, also <= from the real_in to the end of the buffer space so this copy may not be finished, but not
will cause buffer crossings/117 memcpy (Fifo->data + off, SRC, l); * len > L, the remaining contents of the copy buffer, in fact, the address of course is buffer + L, and the remaining size for len-l when len = = L, the following memcpy nothing dry * * * 118 memcpy (FIF
O->data, SRC + L, len-l);
123 SMP_WMB (); 124} 126 unsigned int __kfifo_in (struct __kfifo-*fifo, 127 const void *BUF, unsigned iNT Len)//buf points to a request for a queued buffer, Len represents the requested write size 128 {129 unsigned int l; 131 L = kfifo_unused (FIFO);//Calculate the remaining space in the queue
The size of fifo->size-(fifo->in-fifo->out) 132 if (Len > L) (len = l);
135 kfifo_copy_in (FIFO, buf, Len, fifo->in);
136 fifo->in = Len;
137 return len; 138}
The ingenious thing about Kfifo is that in and out are defined as unsigned types, in and out when put and get are incremented, and when the maximum is reached, an overflow occurs, starting at 0, for recycling