cbuf.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdlib.h>
  24. #include <debug.h>
  25. #include <pow2.h>
  26. #include <string.h>
  27. #include <lib/cbuf.h>
  28. #include <kernel/event.h>
  29. #define LOCAL_TRACE 0
  30. #define INC_POINTER(cbuf, ptr, inc) \
  31. modpow2(((ptr) + (inc)), (cbuf)->len_pow2)
  32. void cbuf_initialize(cbuf_t *cbuf, size_t len)
  33. {
  34. DEBUG_ASSERT(cbuf);
  35. DEBUG_ASSERT(len > 0);
  36. DEBUG_ASSERT(ispow2(len));
  37. cbuf->head = 0;
  38. cbuf->tail = 0;
  39. cbuf->len_pow2 = log2(len);
  40. cbuf->buf = malloc(len);
  41. event_init(&cbuf->event, false, 0);
  42. LTRACEF("len %zd, len_pow2 %u\n", len, cbuf->len_pow2);
  43. }
  44. static size_t cbuf_space_avail(cbuf_t *cbuf)
  45. {
  46. return (cbuf->head + valpow2(cbuf->len_pow2) - cbuf->tail - 1);
  47. }
  48. size_t cbuf_write(cbuf_t *cbuf, const void *_buf, size_t len, bool canreschedule)
  49. {
  50. const char *buf = (const char *)_buf;
  51. LTRACEF("len %zd\n", len);
  52. DEBUG_ASSERT(cbuf);
  53. DEBUG_ASSERT(_buf);
  54. DEBUG_ASSERT(len < valpow2(cbuf->len_pow2));
  55. enter_critical_section();
  56. size_t write_len;
  57. size_t pos = 0;
  58. while (pos < len && cbuf_space_avail(cbuf) > 0) {
  59. if (cbuf->head >= cbuf->tail) {
  60. write_len = MIN(valpow2(cbuf->len_pow2) - cbuf->head, len - pos);
  61. } else {
  62. write_len = MIN(cbuf->tail - cbuf->head - 1, len - pos);
  63. }
  64. // if it's full, abort and return how much we've written
  65. if (write_len == 0) {
  66. break;
  67. }
  68. memcpy(cbuf->buf + cbuf->head, buf + pos, write_len);
  69. cbuf->head = INC_POINTER(cbuf, cbuf->head, write_len);
  70. pos += write_len;
  71. }
  72. if (cbuf->head != cbuf->tail)
  73. event_signal(&cbuf->event, canreschedule);
  74. exit_critical_section();
  75. return pos;
  76. }
  77. size_t cbuf_read(cbuf_t *cbuf, void *_buf, size_t buflen, bool block)
  78. {
  79. size_t ret;
  80. char *buf = (char *)_buf;
  81. DEBUG_ASSERT(cbuf);
  82. DEBUG_ASSERT(_buf);
  83. enter_critical_section();
  84. if (block)
  85. event_wait(&cbuf->event);
  86. // see if there's data available
  87. if (cbuf->tail != cbuf->head) {
  88. size_t pos = 0;
  89. // loop until we've read everything we need
  90. // at most this will make two passes to deal with wraparound
  91. while (pos < buflen && cbuf->tail != cbuf->head) {
  92. size_t read_len;
  93. if (cbuf->head > cbuf->tail) {
  94. // simple case where there is no wraparound
  95. read_len = MIN(cbuf->head - cbuf->tail, buflen - pos);
  96. } else {
  97. // read to the end of buffer in this pass
  98. read_len = MIN(valpow2(cbuf->len_pow2) - cbuf->tail, buflen - pos);
  99. }
  100. memcpy(buf + pos, cbuf->buf + cbuf->tail, read_len);
  101. cbuf->tail = INC_POINTER(cbuf, cbuf->tail, read_len);
  102. pos += read_len;
  103. }
  104. if (cbuf->tail == cbuf->head) {
  105. // we've emptied the buffer, unsignal the event
  106. event_unsignal(&cbuf->event);
  107. }
  108. ret = pos;
  109. } else {
  110. DEBUG_ASSERT(!block);
  111. ret = 0;
  112. }
  113. exit_critical_section();
  114. return ret;
  115. }