kfifo.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef _LINUX_KFIFO_H
  23. #define _LINUX_KFIFO_H
  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. };
  32. extern void kfifo_init(struct kfifo *fifo, unsigned char *buffer,
  33. unsigned int size);
  34. extern __must_check int kfifo_alloc(struct kfifo *fifo, unsigned int size,
  35. gfp_t gfp_mask);
  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
  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_len - returns the number of used bytes in the FIFO
  51. * @fifo: the fifo to be used.
  52. */
  53. static inline unsigned int kfifo_len(struct kfifo *fifo)
  54. {
  55. register unsigned int out;
  56. out = fifo->out;
  57. smp_rmb();
  58. return fifo->in - out;
  59. }
  60. /**
  61. * kfifo_put_locked - puts some data into the FIFO using a spinlock for locking
  62. * @fifo: the fifo to be used.
  63. * @from: the data to be added.
  64. * @n: the length of the data to be added.
  65. * @lock: pointer to the spinlock to use for locking.
  66. *
  67. * This function copies at most @len bytes from the @from buffer into
  68. * the FIFO depending on the free space, and returns the number of
  69. * bytes copied.
  70. */
  71. static inline __must_check unsigned int kfifo_put_locked(struct kfifo *fifo,
  72. const unsigned char *from, unsigned int n, spinlock_t *lock)
  73. {
  74. unsigned long flags;
  75. unsigned int ret;
  76. spin_lock_irqsave(lock, flags);
  77. ret = kfifo_put(fifo, from, n);
  78. spin_unlock_irqrestore(lock, flags);
  79. return ret;
  80. }
  81. /**
  82. * kfifo_get_locked - gets some data from the FIFO using a spinlock for locking
  83. * @fifo: the fifo to be used.
  84. * @to: where the data must be copied.
  85. * @n: the size of the destination buffer.
  86. * @lock: pointer to the spinlock to use for locking.
  87. *
  88. * This function copies at most @len bytes from the FIFO into the
  89. * @to buffer and returns the number of copied bytes.
  90. */
  91. static inline __must_check unsigned int kfifo_get_locked(struct kfifo *fifo,
  92. unsigned char *to, unsigned int n, spinlock_t *lock)
  93. {
  94. unsigned long flags;
  95. unsigned int ret;
  96. spin_lock_irqsave(lock, flags);
  97. ret = kfifo_get(fifo, to, n);
  98. /*
  99. * optimization: if the FIFO is empty, set the indices to 0
  100. * so we don't wrap the next time
  101. */
  102. if (fifo->in == fifo->out)
  103. fifo->in = fifo->out = 0;
  104. spin_unlock_irqrestore(lock, flags);
  105. return ret;
  106. }
  107. #endif