kfifo.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * A simple kernel FIFO implementation.
  3. *
  4. * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. */
  21. #ifndef _LINUX_KFIFO_H
  22. #define _LINUX_KFIFO_H
  23. #include <linux/kernel.h>
  24. #include <linux/spinlock.h>
  25. struct kfifo {
  26. unsigned char *buffer; /* the buffer holding the data */
  27. unsigned int size; /* the size of the allocated buffer */
  28. unsigned int in; /* data is added at offset (in % size) */
  29. unsigned int out; /* data is extracted from off. (out % size) */
  30. spinlock_t *lock; /* protects concurrent modifications */
  31. };
  32. extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
  33. gfp_t gfp_mask, spinlock_t *lock);
  34. extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,
  35. spinlock_t *lock);
  36. extern void kfifo_free(struct kfifo *fifo);
  37. extern unsigned int __kfifo_put(struct kfifo *fifo,
  38. const unsigned char *buffer, unsigned int len);
  39. extern unsigned int __kfifo_get(struct kfifo *fifo,
  40. unsigned char *buffer, unsigned int len);
  41. /**
  42. * __kfifo_reset - removes the entire FIFO contents, no locking version
  43. * @fifo: the fifo to be emptied.
  44. */
  45. static inline void __kfifo_reset(struct kfifo *fifo)
  46. {
  47. fifo->in = fifo->out = 0;
  48. }
  49. /**
  50. * kfifo_reset - removes the entire FIFO contents
  51. * @fifo: the fifo to be emptied.
  52. */
  53. static inline void kfifo_reset(struct kfifo *fifo)
  54. {
  55. unsigned long flags;
  56. spin_lock_irqsave(fifo->lock, flags);
  57. __kfifo_reset(fifo);
  58. spin_unlock_irqrestore(fifo->lock, flags);
  59. }
  60. /**
  61. * kfifo_put - puts some data into the FIFO
  62. * @fifo: the fifo to be used.
  63. * @buffer: the data to be added.
  64. * @len: the length of the data to be added.
  65. *
  66. * This function copies at most @len bytes from the @buffer into
  67. * the FIFO depending on the free space, and returns the number of
  68. * bytes copied.
  69. */
  70. static inline unsigned int kfifo_put(struct kfifo *fifo,
  71. const unsigned char *buffer, unsigned int len)
  72. {
  73. unsigned long flags;
  74. unsigned int ret;
  75. spin_lock_irqsave(fifo->lock, flags);
  76. ret = __kfifo_put(fifo, buffer, len);
  77. spin_unlock_irqrestore(fifo->lock, flags);
  78. return ret;
  79. }
  80. /**
  81. * kfifo_get - gets some data from the FIFO
  82. * @fifo: the fifo to be used.
  83. * @buffer: where the data must be copied.
  84. * @len: the size of the destination buffer.
  85. *
  86. * This function copies at most @len bytes from the FIFO into the
  87. * @buffer and returns the number of copied bytes.
  88. */
  89. static inline unsigned int kfifo_get(struct kfifo *fifo,
  90. unsigned char *buffer, unsigned int len)
  91. {
  92. unsigned long flags;
  93. unsigned int ret;
  94. spin_lock_irqsave(fifo->lock, flags);
  95. ret = __kfifo_get(fifo, buffer, len);
  96. /*
  97. * optimization: if the FIFO is empty, set the indices to 0
  98. * so we don't wrap the next time
  99. */
  100. if (fifo->in == fifo->out)
  101. fifo->in = fifo->out = 0;
  102. spin_unlock_irqrestore(fifo->lock, flags);
  103. return ret;
  104. }
  105. /**
  106. * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
  107. * @fifo: the fifo to be used.
  108. */
  109. static inline unsigned int __kfifo_len(struct kfifo *fifo)
  110. {
  111. return fifo->in - fifo->out;
  112. }
  113. /**
  114. * kfifo_len - returns the number of bytes available in the FIFO
  115. * @fifo: the fifo to be used.
  116. */
  117. static inline unsigned int kfifo_len(struct kfifo *fifo)
  118. {
  119. unsigned long flags;
  120. unsigned int ret;
  121. spin_lock_irqsave(fifo->lock, flags);
  122. ret = __kfifo_len(fifo);
  123. spin_unlock_irqrestore(fifo->lock, flags);
  124. return ret;
  125. }
  126. #endif