[Linux kernel] [Linux kernel Programming] Learning notes (ii)

Source: Internet
Author: User
Tags int size

Linux kernel ———— queue

Linux kernel--queue

Defined:

[CPP]View Plaincopy
    1. struct __kfifo{
    2. unsigned int in; //Queue offset, write index
    3. unsigned int out; //Queue offset, read index
    4. unsigned int mask;
    5. unsigned int esize;
    6. void *data;
    7. }


Use:

Create a queue that creates and initializes a kfifo of size:

[CPP]View Plaincopy
  1. __kfifo_alloc Int (struct __kfifo *fifo, unsigned int size,
  2. size_t esize, gfp_t gfp_mask)
  3. 40 {
  4. Size = roundup_pow_of_two (size);
  5. 46
  6. fifo->in = 0;
  7. fifo->out = 0;
  8. Fifo->esize = esize;
  9. 50
  10. if (Size < 2) {
  11. Fifo->data = NULL;
  12. Fifo->mask = 0;
  13. Return-einval;
  14. 55}
  15. 56
  16. Fifo->data = kmalloc (Size * esize, gfp_mask);
  17. 58
  18. if (!fifo->data) {
  19. Fifo->mask = 0;
  20. Return-enomem;
  21. 62}
  22. Fifo->mask = size-1;
  23. 64
  24. 0;
  25. 66}


Determines whether an integer is a power of 2 for the whole number of times

[CPP]View Plaincopy
    1. static inline int is_power_of_2 (unsigned long long N)
    2. {
    3. Return (n!=0 && (n& (n-1) ==0))
    4. }


The method to push data into the queue is the kfifo_in () function

[CPP]View Plaincopy
  1. 102 static void kfifo_copy_in (struct __KFIFO *fifo, const void *src,
  2. 103 unsigned int len, unsigned int off)
  3. 104 {
  4. unsigned int size = fifo->mask + 1;
  5. 106 unsigned int esize = fifo->esize;
  6. 107 unsigned int l;
  7. 108
  8. 109 Off &= fifo->mask; //The operation will be mathematically based on the modulus of length fifo->mask
  9. if (esize! = 1) {
  10. 111 off *= Esize;
  11. The size of *= esize;
  12. 113 Len *= esize;
  13. 114}
  14. L = min (len, size-off); the meaning of the//size-off represents the size of the current in to the end of the buffer,
  15. * /
  16. Copies the L bytes from buffer to the remaining space of the buffers, L<=len, and <= the space from real_in to the end of the buffer.
  17. So this copy may not have been copied, but it will not cause the buffer to cross out
  18. */
  19. 117 memcpy (Fifo->data + off, SRC, l);
  20. /* 
  21. Len > L, copy the remaining contents of buffer, in fact the address is buffer + L, and the remaining size is Len-l
  22. When len = = L, the following memcpy do nothing.
  23. */
  24. 118 memcpy (Fifo->data, src + L, len-l);
  25. 123 SMP_WMB ();
  26. 124}
  27. 125
  28. 126 unsigned int __kfifo_in (struct __kfifo *fifo,
  29. 127 const void *buf, unsigned int len)//buf points to the buffer in which the request was queued, Len represents the size of the requested write /c2>
  30. 128 {
  31. 129 unsigned int l;
  32. 131 L = kfifo_unused (FIFO); //Calculate the size of the remaining space in the queue, fifo->size-(fifo->in-fifo->out)
  33. if (len > L)
  34. 133 len = l;
  35. 135 kfifo_copy_in (FIFO, buf, Len, fifo->in);
  36. 136 Fifo->in + = Len;
  37. 137 return Len;
  38. 138}


The clever thing about Kfifo is that in and out are defined as unsigned types, both in and out are incremented when put and get, and overflow when maximum is reached, so that it is recycled from 0 onwards

[Linux kernel] [Linux kernel Programming] Learning notes (ii)

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.