Linux kernel Common queue usage notes (read Linux kernel design and implementation)

Source: Internet
Author: User
Tags int size

Linux kernel universal queue implementation Kfifo
Location: kernel/kififo.c

Use required include header file # include <kernel/kififo>

1. Create queue (dynamically created)
int Kfifo_alloc (struct kififo *fifo, unsigned int size, gfp_t gfp_mask);
The function creates and initializes a FIFO of size, and the kernel uses the Gfp_mask identity to allocate the queue.
Successful return 0
Ep:
struct Kfifo FIFO;
int ret;
Create a queue of size page_size that is allocated by the kernel for memory
ret = Kfifo_allo (&kifo, Page_size, Gfp_kernel);
if (ret)
return ret;
To allocate buffers yourself, you can call:
void Kfifo_init (struct kfifo *kfifo, void *buffer, unsigned int size);
Creates and initializes a Kfifo object that will make a size byte-sized memory pointed by buffer
For the above two functions, size must be a power of 2.
Static declaration:
Declare_kfifo (name, size);
Init_kfifo (name);
2. Stack queue data
unsigned int kfifo_in (struct kfifo *fifo, const void *from, unsigned int len);
The function copies the data from the Len Byte referred to by the from pointer to the queue pointed to by the FIFO and returns the data byte size successfully.
3. Extracting Queue data
unsigned int kfifo_out_peek (struct kfifo *fifo, void *to, unsigned int len, unsigned offset);;
Similar to Kfifo_out, if offset is 0, the read queue header, parameter offset, points to the index position in the queue.
4. Get Queue Length
Returns the overall size of the space where the Kfifo queue is stored
static inline unsigned int kififo_size (struct kfifo *fifo);
Returns the size of the heap-in data in the queue
static inline unsigned int kfifo_len (struct kfifo *fifo);
How much free space in the Kfifo queue is desired
static inline unsigned int kfifo_avail (struct kififo *fifo);
Determines whether the queue is empty, returns a value other than 0, and returns 0 instead
static inline int kfifo_is_empty (struct kfifo *fifo);
Determines whether the queue is full, returns a value other than 0, and returns 0 instead
static inline int kfifo_is_full (struct kfifo *fifo);
5. Resetting and revoking queues
Discard all the contents of the queue, call Kfifo_reset ();
static inline void Kfifo_reset (struct kfifo *fifo);
Revoke one or the queue allocated with Kfifo_alloc (), call Kfifo_free ();


Examples of Use:

unsigned int i;//Will 0, 31 in Kfifo named FIFO for (i = 0; i <; i++) kfifo_in (FIFO, &i, sizeof (i)); unsigned int val; int ret; ret = Kfifo_out_peek (FIFO, &val, sizeof (Val), 0); if (ret! = sizeof (val)) Return-einval; PRINTK (kern_info "%u\n", Val);//should output 0//extract and print all the elements in Kfifo, you can call Kfifo_out (); When there is data in the queue, print from 0 to 31 sequentially while (Kfifo_avail (FIFO)) {unsigned  int ret; int ret; ret = kfifo_out (fifo,&val, sizeof (Val)); if (ret! = sizeof (val)) Return-einval; PRINTK (kern_info "%u\n", Val);}


Linux kernel Common queue usage notes (read Linux kernel design and implementation)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.