kfifo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/err.h>
  25. #include <linux/kfifo.h>
  26. #include <linux/log2.h>
  27. /**
  28. * kfifo_init - allocates a new FIFO using a preallocated buffer
  29. * @buffer: the preallocated buffer to be used.
  30. * @size: the size of the internal buffer, this have to be a power of 2.
  31. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  32. * @lock: the lock to be used to protect the fifo buffer
  33. *
  34. * Do NOT pass the kfifo to kfifo_free() after use! Simply free the
  35. * &struct kfifo with kfree().
  36. */
  37. struct kfifo *kfifo_init(unsigned char *buffer, unsigned int size,
  38. gfp_t gfp_mask, spinlock_t *lock)
  39. {
  40. struct kfifo *fifo;
  41. /* size must be a power of 2 */
  42. BUG_ON(!is_power_of_2(size));
  43. fifo = kmalloc(sizeof(struct kfifo), gfp_mask);
  44. if (!fifo)
  45. return ERR_PTR(-ENOMEM);
  46. fifo->buffer = buffer;
  47. fifo->size = size;
  48. fifo->in = fifo->out = 0;
  49. fifo->lock = lock;
  50. return fifo;
  51. }
  52. EXPORT_SYMBOL(kfifo_init);
  53. /**
  54. * kfifo_alloc - allocates a new FIFO and its internal buffer
  55. * @size: the size of the internal buffer to be allocated.
  56. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  57. * @lock: the lock to be used to protect the fifo buffer
  58. *
  59. * The size will be rounded-up to a power of 2.
  60. */
  61. struct kfifo *kfifo_alloc(unsigned int size, gfp_t gfp_mask, spinlock_t *lock)
  62. {
  63. unsigned char *buffer;
  64. struct kfifo *ret;
  65. /*
  66. * round up to the next power of 2, since our 'let the indices
  67. * wrap' technique works only in this case.
  68. */
  69. if (!is_power_of_2(size)) {
  70. BUG_ON(size > 0x80000000);
  71. size = roundup_pow_of_two(size);
  72. }
  73. buffer = kmalloc(size, gfp_mask);
  74. if (!buffer)
  75. return ERR_PTR(-ENOMEM);
  76. ret = kfifo_init(buffer, size, gfp_mask, lock);
  77. if (IS_ERR(ret))
  78. kfree(buffer);
  79. return ret;
  80. }
  81. EXPORT_SYMBOL(kfifo_alloc);
  82. /**
  83. * kfifo_free - frees the FIFO
  84. * @fifo: the fifo to be freed.
  85. */
  86. void kfifo_free(struct kfifo *fifo)
  87. {
  88. kfree(fifo->buffer);
  89. kfree(fifo);
  90. }
  91. EXPORT_SYMBOL(kfifo_free);
  92. /**
  93. * __kfifo_put - puts some data into the FIFO, no locking version
  94. * @fifo: the fifo to be used.
  95. * @buffer: the data to be added.
  96. * @len: the length of the data to be added.
  97. *
  98. * This function copies at most @len bytes from the @buffer into
  99. * the FIFO depending on the free space, and returns the number of
  100. * bytes copied.
  101. *
  102. * Note that with only one concurrent reader and one concurrent
  103. * writer, you don't need extra locking to use these functions.
  104. */
  105. unsigned int __kfifo_put(struct kfifo *fifo,
  106. unsigned char *buffer, unsigned int len)
  107. {
  108. unsigned int l;
  109. len = min(len, fifo->size - fifo->in + fifo->out);
  110. /*
  111. * Ensure that we sample the fifo->out index -before- we
  112. * start putting bytes into the kfifo.
  113. */
  114. smp_mb();
  115. /* first put the data starting from fifo->in to buffer end */
  116. l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
  117. memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), buffer, l);
  118. /* then put the rest (if any) at the beginning of the buffer */
  119. memcpy(fifo->buffer, buffer + l, len - l);
  120. /*
  121. * Ensure that we add the bytes to the kfifo -before-
  122. * we update the fifo->in index.
  123. */
  124. smp_wmb();
  125. fifo->in += len;
  126. return len;
  127. }
  128. EXPORT_SYMBOL(__kfifo_put);
  129. /**
  130. * __kfifo_get - gets some data from the FIFO, no locking version
  131. * @fifo: the fifo to be used.
  132. * @buffer: where the data must be copied.
  133. * @len: the size of the destination buffer.
  134. *
  135. * This function copies at most @len bytes from the FIFO into the
  136. * @buffer and returns the number of copied bytes.
  137. *
  138. * Note that with only one concurrent reader and one concurrent
  139. * writer, you don't need extra locking to use these functions.
  140. */
  141. unsigned int __kfifo_get(struct kfifo *fifo,
  142. unsigned char *buffer, unsigned int len)
  143. {
  144. unsigned int l;
  145. len = min(len, fifo->in - fifo->out);
  146. /*
  147. * Ensure that we sample the fifo->in index -before- we
  148. * start removing bytes from the kfifo.
  149. */
  150. smp_rmb();
  151. /* first get the data from fifo->out until the end of the buffer */
  152. l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
  153. memcpy(buffer, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
  154. /* then get the rest (if any) from the beginning of the buffer */
  155. memcpy(buffer + l, fifo->buffer, len - l);
  156. /*
  157. * Ensure that we remove the bytes from the kfifo -before-
  158. * we update the fifo->out index.
  159. */
  160. smp_mb();
  161. fifo->out += len;
  162. return len;
  163. }
  164. EXPORT_SYMBOL(__kfifo_get);