kfifo.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifdef __KERNEL__
  24. #include <linux/kernel.h>
  25. #include <linux/spinlock.h>
  26. struct kfifo {
  27. unsigned char *buffer; /* the buffer holding the data */
  28. unsigned int size; /* the size of the allocated buffer */
  29. unsigned int in; /* data is added at offset (in % size) */
  30. unsigned int out; /* data is extracted from off. (out % size) */
  31. spinlock_t *lock; /* protects concurrent modifications */
  32. };
  33. extern struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
  34. gfp_t gfp_mask, spinlock_t *lock);
  35. extern struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask,
  36. spinlock_t *lock);
  37. extern void kfifo_free(struct kfifo *fifo);
  38. extern unsigned int __kfifo_put(struct kfifo *fifo,
  39. unsigned char *buffer, unsigned int len);
  40. extern unsigned int __kfifo_get(struct kfifo *fifo,
  41. unsigned char *buffer, unsigned int len);
  42. /**
  43. * __kfifo_reset - removes the entire FIFO contents, no locking version
  44. * @fifo: the fifo to be emptied.
  45. */
  46. static inline void __kfifo_reset(struct kfifo *fifo)
  47. {
  48. fifo->in = fifo->out = 0;
  49. }
  50. /**
  51. * kfifo_reset - removes the entire FIFO contents
  52. * @fifo: the fifo to be emptied.
  53. */
  54. static inline void kfifo_reset(struct kfifo *fifo)
  55. {
  56. unsigned long flags;
  57. spin_lock_irqsave(fifo->lock, flags);
  58. __kfifo_reset(fifo);
  59. spin_unlock_irqrestore(fifo->lock, flags);
  60. }
  61. /**
  62. * kfifo_put - puts some data into the FIFO
  63. * @fifo: the fifo to be used.
  64. * @buffer: the data to be added.
  65. * @len: the length of the data to be added.
  66. *
  67. * This function copies at most @len bytes from the @buffer into
  68. * the FIFO depending on the free space, and returns the number of
  69. * bytes copied.
  70. */
  71. static inline unsigned int kfifo_put(struct kfifo *fifo,
  72. unsigned char *buffer, unsigned int len)
  73. {
  74. unsigned long flags;
  75. unsigned int ret;
  76. spin_lock_irqsave(fifo->lock, flags);
  77. ret = __kfifo_put(fifo, buffer, len);
  78. spin_unlock_irqrestore(fifo->lock, flags);
  79. return ret;
  80. }
  81. /**
  82. * kfifo_get - gets some data from the FIFO
  83. * @fifo: the fifo to be used.
  84. * @buffer: where the data must be copied.
  85. * @len: the size of the destination buffer.
  86. *
  87. * This function copies at most @len bytes from the FIFO into the
  88. * @buffer and returns the number of copied bytes.
  89. */
  90. static inline unsigned int kfifo_get(struct kfifo *fifo,
  91. unsigned char *buffer, unsigned int len)
  92. {
  93. unsigned long flags;
  94. unsigned int ret;
  95. spin_lock_irqsave(fifo->lock, flags);
  96. ret = __kfifo_get(fifo, buffer, len);
  97. /*
  98. * optimization: if the FIFO is empty, set the indices to 0
  99. * so we don't wrap the next time
  100. */
  101. if (fifo->in == fifo->out)
  102. fifo->in = fifo->out = 0;
  103. spin_unlock_irqrestore(fifo->lock, flags);
  104. return ret;
  105. }
  106. /**
  107. * __kfifo_len - returns the number of bytes available in the FIFO, no locking version
  108. * @fifo: the fifo to be used.
  109. */
  110. static inline unsigned int __kfifo_len(struct kfifo *fifo)
  111. {
  112. return fifo->in - fifo->out;
  113. }
  114. /**
  115. * kfifo_len - returns the number of bytes available in the FIFO
  116. * @fifo: the fifo to be used.
  117. */
  118. static inline unsigned int kfifo_len(struct kfifo *fifo)
  119. {
  120. unsigned long flags;
  121. unsigned int ret;
  122. spin_lock_irqsave(fifo->lock, flags);
  123. ret = __kfifo_len(fifo);
  124. spin_unlock_irqrestore(fifo->lock, flags);
  125. return ret;
  126. }
  127. #else
  128. #warning "don't include kernel headers in userspace"
  129. #endif /* __KERNEL__ */
  130. #endif