circbuff.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef CIRCBUFF_H_
  2. #define CIRCBUFF_H_
  3. /*
  4. * Copyright (C) 2012 by egnite GmbH
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the name of the copyright holders nor the names of
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  31. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * For additional information see http://www.ethernut.de/
  35. */
  36. #include <toolchain.h>
  37. #include <sys/event.h>
  38. #include <stdint.h>
  39. #include <stdlib.h>
  40. /*!
  41. * \defgroup xgCircBuff Circular Buffer
  42. * \ingroup xgDevSerial
  43. * \anchor xrCircBuff
  44. * \brief Generic circular buffer routines.
  45. */
  46. /*@{*/
  47. #ifndef CIRCBUFF_MAX
  48. /*!
  49. * \brief Maximum circular buffer size.
  50. *
  51. * On 8-bit targets this may be configured to less than 256,
  52. * in which case the buffer indices will fit into one byte.
  53. *
  54. * See \ref _CIRCBUFF.
  55. */
  56. #define CIRCBUFF_MAX 16383
  57. #endif
  58. #if CIRCBUFF_MAX <= 255
  59. typedef uint_fast8_t cb_size_t;
  60. #elif CIRCBUFF_MAX <= 16383
  61. typedef uint_fast16_t cb_size_t;
  62. #else
  63. typedef uint_fast32_t cb_size_t;
  64. #endif
  65. /*!
  66. * \brief Circular buffer structure type.
  67. *
  68. * See \ref _CIRCBUFF.
  69. */
  70. typedef struct _CIRCBUFF CIRCBUFF;
  71. /*!
  72. * \brief Circular buffer structure.
  73. *
  74. * This circular buffer implementation is intended to be used by
  75. * device drivers, specifically UART drivers.
  76. */
  77. struct _CIRCBUFF {
  78. /*! \brief Pointer to the buffer. */
  79. uint8_t *cb_buff;
  80. /*! \brief Buffer size. */
  81. cb_size_t cb_size;
  82. /*! \brief Next position to read from. */
  83. cb_size_t cb_ridx;
  84. /*! \brief Next position to write to. */
  85. cb_size_t cb_widx;
  86. };
  87. /*!
  88. * \brief Get the number of consecutive bytes available for writing.
  89. *
  90. * The caller must make sure, that this function has exclusive access
  91. * to the buffer.
  92. *
  93. * \param cb Pointer to a \ref CIRCBUFF structure.
  94. *
  95. * \return The number of bytes available.
  96. */
  97. static NUT_INLINE_FUNC size_t CircBuffWriteSize(CIRCBUFF *cb)
  98. {
  99. size_t rc;
  100. if (cb->cb_ridx <= cb->cb_widx) {
  101. rc = cb->cb_size - cb->cb_widx;
  102. rc += cb->cb_ridx != 0;
  103. } else {
  104. rc = cb->cb_ridx - cb->cb_widx - 1;
  105. }
  106. return rc;
  107. }
  108. /*!
  109. * \brief Get the number of consecutive bytes available for reading.
  110. *
  111. * \param cb Pointer to a \ref CIRCBUFF structure.
  112. *
  113. * \return The number of bytes available.
  114. */
  115. static NUT_INLINE_FUNC size_t CircBuffReadSize(CIRCBUFF *cb)
  116. {
  117. size_t rc;
  118. if (cb->cb_ridx > cb->cb_widx) {
  119. rc = cb->cb_size - cb->cb_ridx + 1;
  120. } else {
  121. rc = cb->cb_widx - cb->cb_ridx;
  122. }
  123. return rc;
  124. }
  125. /*!
  126. * \brief Reset a circular buffer.
  127. *
  128. * The caller must make sure, that this function has exclusive access
  129. * to the buffer.
  130. *
  131. * \param cb Pointer to a \ref CIRCBUFF structure.
  132. * \param size Requested size of the buffer. The actual buffer size
  133. * will be 2^n - 1, where n = 5 to 32. However, the maximum
  134. * size is globally limited by \ref CIRCBUFF_MAX, which is
  135. * 16383 by default.
  136. *
  137. * \return If not enough memory is available, -1 is returned. Otherwise
  138. * the return value will be 0, which indicates, that the buffer
  139. * can be used. In this case it might be possible, that the buffer
  140. * is actually smaller than requested.
  141. */
  142. extern int CircBuffReset(CIRCBUFF *cb, size_t size);
  143. /*@}*/
  144. #endif