StreamBuffer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /**
  2. * @file StreamBuffer.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <misc/balloc.h>
  30. #include <misc/minmax.h>
  31. #include "StreamBuffer.h"
  32. // called when receive operation is complete
  33. static void input_handler_done (void *vo, int data_len)
  34. {
  35. StreamBuffer *o = (StreamBuffer *)vo;
  36. ASSERT(data_len > 0)
  37. ASSERT(data_len <= o->buf_size - (o->buf_start + o->buf_used))
  38. // remember if buffer was empty
  39. int was_empty = (o->buf_used == 0);
  40. // increment buf_used by the amount that was received
  41. o->buf_used += data_len;
  42. // start another receive operation unless buffer is full
  43. if (o->buf_used < o->buf_size - o->buf_start) {
  44. int end = o->buf_start + o->buf_used;
  45. StreamRecvInterface_Receiver_Recv(o->input, o->buf + end, o->buf_size - end);
  46. }
  47. else if (o->buf_used < o->buf_size) {
  48. // wrap around
  49. StreamRecvInterface_Receiver_Recv(o->input, o->buf, o->buf_start);
  50. }
  51. // if buffer was empty before, start send operation
  52. if (was_empty) {
  53. StreamPassInterface_Sender_Send(o->output, o->buf + o->buf_start, o->buf_used);
  54. }
  55. }
  56. // called when send operation is complete
  57. static void output_handler_done (void *vo, int data_len)
  58. {
  59. StreamBuffer *o = (StreamBuffer *)vo;
  60. ASSERT(data_len > 0)
  61. ASSERT(data_len <= o->buf_used)
  62. ASSERT(data_len <= o->buf_size - o->buf_start)
  63. // remember if buffer was full
  64. int was_full = (o->buf_used == o->buf_size);
  65. // increment buf_start and decrement buf_used by the
  66. // amount that was sent
  67. o->buf_start += data_len;
  68. o->buf_used -= data_len;
  69. // wrap around buf_start
  70. if (o->buf_start == o->buf_size) {
  71. o->buf_start = 0;
  72. }
  73. // start receive operation if buffer was full
  74. if (was_full) {
  75. int end;
  76. int avail;
  77. if (o->buf_used >= o->buf_size - o->buf_start) {
  78. end = o->buf_used - (o->buf_size - o->buf_start);
  79. avail = o->buf_start - end;
  80. } else {
  81. end = o->buf_start + o->buf_used;
  82. avail = o->buf_size - end;
  83. }
  84. StreamRecvInterface_Receiver_Recv(o->input, o->buf + end, avail);
  85. }
  86. // start another receive send unless buffer is empty
  87. if (o->buf_used > 0) {
  88. int to_send = bmin_int(o->buf_used, o->buf_size - o->buf_start);
  89. StreamPassInterface_Sender_Send(o->output, o->buf + o->buf_start, to_send);
  90. }
  91. }
  92. int StreamBuffer_Init (StreamBuffer *o, int buf_size, StreamRecvInterface *input, StreamPassInterface *output)
  93. {
  94. ASSERT(buf_size > 0)
  95. ASSERT(input)
  96. ASSERT(output)
  97. // remember arguments
  98. o->buf_size = buf_size;
  99. o->input = input;
  100. o->output = output;
  101. // allocate buffer memory
  102. o->buf = (uint8_t *)BAllocSize(bsize_fromint(o->buf_size));
  103. if (!o->buf) {
  104. goto fail0;
  105. }
  106. // set initial buffer state
  107. o->buf_start = 0;
  108. o->buf_used = 0;
  109. // set receive and send done callbacks
  110. StreamRecvInterface_Receiver_Init(o->input, input_handler_done, o);
  111. StreamPassInterface_Sender_Init(o->output, output_handler_done, o);
  112. // start receive operation
  113. StreamRecvInterface_Receiver_Recv(o->input, o->buf, o->buf_size);
  114. DebugObject_Init(&o->d_obj);
  115. return 1;
  116. fail0:
  117. return 0;
  118. }
  119. void StreamBuffer_Free (StreamBuffer *o)
  120. {
  121. DebugObject_Free(&o->d_obj);
  122. // free buffer memory
  123. BFree(o->buf);
  124. }