kfifo.c 5.1 KB

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