kfifo.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * A generic kernel FIFO implementation.
  3. *
  4. * Copyright (C) 2009 Stefani Seibold <stefani@seibold.net>
  5. * Copyright (C) 2004 Stelian Pop <stelian@popies.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/slab.h>
  25. #include <linux/err.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/log2.h>
  28. static void _kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  29. unsigned int size)
  30. {
  31. fifo->buffer = buffer;
  32. fifo->size = size;
  33. kfifo_reset(fifo);
  34. }
  35. /**
  36. * kfifo_init - initialize a FIFO using a preallocated buffer
  37. * @fifo: the fifo to assign the buffer
  38. * @buffer: the preallocated buffer to be used.
  39. * @size: the size of the internal buffer, this have to be a power of 2.
  40. *
  41. */
  42. void kfifo_init(struct kfifo *fifo, unsigned char *buffer, unsigned int size)
  43. {
  44. /* size must be a power of 2 */
  45. BUG_ON(!is_power_of_2(size));
  46. _kfifo_init(fifo, buffer, size);
  47. }
  48. EXPORT_SYMBOL(kfifo_init);
  49. /**
  50. * kfifo_alloc - allocates a new FIFO internal buffer
  51. * @fifo: the fifo to assign then new buffer
  52. * @size: the size of the buffer to be allocated, this have to be a power of 2.
  53. * @gfp_mask: get_free_pages mask, passed to kmalloc()
  54. *
  55. * This function dynamically allocates a new fifo internal buffer
  56. *
  57. * The size will be rounded-up to a power of 2.
  58. * The buffer will be release with kfifo_free().
  59. * Return 0 if no error, otherwise the an error code
  60. */
  61. int kfifo_alloc(struct kfifo *fifo, unsigned int size, gfp_t gfp_mask)
  62. {
  63. unsigned char *buffer;
  64. /*
  65. * round up to the next power of 2, since our 'let the indices
  66. * wrap' technique works only in this case.
  67. */
  68. if (!is_power_of_2(size)) {
  69. BUG_ON(size > 0x80000000);
  70. size = roundup_pow_of_two(size);
  71. }
  72. buffer = kmalloc(size, gfp_mask);
  73. if (!buffer) {
  74. _kfifo_init(fifo, 0, 0);
  75. return -ENOMEM;
  76. }
  77. _kfifo_init(fifo, buffer, size);
  78. return 0;
  79. }
  80. EXPORT_SYMBOL(kfifo_alloc);
  81. /**
  82. * kfifo_free - frees the FIFO internal buffer
  83. * @fifo: the fifo to be freed.
  84. */
  85. void kfifo_free(struct kfifo *fifo)
  86. {
  87. kfree(fifo->buffer);
  88. }
  89. EXPORT_SYMBOL(kfifo_free);
  90. /**
  91. * kfifo_in - puts some data into the FIFO
  92. * @fifo: the fifo to be used.
  93. * @from: the data to be added.
  94. * @len: the length of the data to be added.
  95. *
  96. * This function copies at most @len bytes from the @from buffer into
  97. * the FIFO depending on the free space, and returns the number of
  98. * bytes copied.
  99. *
  100. * Note that with only one concurrent reader and one concurrent
  101. * writer, you don't need extra locking to use these functions.
  102. */
  103. unsigned int kfifo_in(struct kfifo *fifo,
  104. const unsigned char *from, unsigned int len)
  105. {
  106. unsigned int l;
  107. len = min(len, fifo->size - fifo->in + fifo->out);
  108. /*
  109. * Ensure that we sample the fifo->out index -before- we
  110. * start putting bytes into the kfifo.
  111. */
  112. smp_mb();
  113. /* first put the data starting from fifo->in to buffer end */
  114. l = min(len, fifo->size - (fifo->in & (fifo->size - 1)));
  115. memcpy(fifo->buffer + (fifo->in & (fifo->size - 1)), from, l);
  116. /* then put the rest (if any) at the beginning of the buffer */
  117. memcpy(fifo->buffer, from + l, len - l);
  118. /*
  119. * Ensure that we add the bytes to the kfifo -before-
  120. * we update the fifo->in index.
  121. */
  122. smp_wmb();
  123. fifo->in += len;
  124. return len;
  125. }
  126. EXPORT_SYMBOL(kfifo_in);
  127. /**
  128. * kfifo_out - gets some data from the FIFO
  129. * @fifo: the fifo to be used.
  130. * @to: where the data must be copied.
  131. * @len: the size of the destination buffer.
  132. *
  133. * This function copies at most @len bytes from the FIFO into the
  134. * @to buffer and returns the number of copied bytes.
  135. *
  136. * Note that with only one concurrent reader and one concurrent
  137. * writer, you don't need extra locking to use these functions.
  138. */
  139. unsigned int kfifo_out(struct kfifo *fifo,
  140. unsigned char *to, unsigned int len)
  141. {
  142. unsigned int l;
  143. len = min(len, fifo->in - fifo->out);
  144. /*
  145. * Ensure that we sample the fifo->in index -before- we
  146. * start removing bytes from the kfifo.
  147. */
  148. smp_rmb();
  149. /* first get the data from fifo->out until the end of the buffer */
  150. l = min(len, fifo->size - (fifo->out & (fifo->size - 1)));
  151. memcpy(to, fifo->buffer + (fifo->out & (fifo->size - 1)), l);
  152. /* then get the rest (if any) from the beginning of the buffer */
  153. memcpy(to + l, fifo->buffer, len - l);
  154. /*
  155. * Ensure that we remove the bytes from the kfifo -before-
  156. * we update the fifo->out index.
  157. */
  158. smp_mb();
  159. fifo->out += len;
  160. return len;
  161. }
  162. EXPORT_SYMBOL(kfifo_out);