buffer.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  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 3 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, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. // fifo bufffers
  22. #ifndef _GNU_SOURCE
  23. #define _GNU_SOURCE
  24. #endif
  25. #include "squeezelite.h"
  26. // _* called with muxtex locked
  27. inline unsigned _buf_used(struct buffer *buf) {
  28. return buf->writep >= buf->readp ? buf->writep - buf->readp : buf->size - (buf->readp - buf->writep);
  29. }
  30. unsigned _buf_space(struct buffer *buf) {
  31. return buf->size - _buf_used(buf) - 1; // reduce by one as full same as empty otherwise
  32. }
  33. unsigned _buf_cont_read(struct buffer *buf) {
  34. return buf->writep >= buf->readp ? buf->writep - buf->readp : buf->wrap - buf->readp;
  35. }
  36. unsigned _buf_cont_write(struct buffer *buf) {
  37. return buf->writep >= buf->readp ? buf->wrap - buf->writep : buf->readp - buf->writep;
  38. }
  39. void _buf_inc_readp(struct buffer *buf, unsigned by) {
  40. buf->readp += by;
  41. if (buf->readp >= buf->wrap) {
  42. buf->readp -= buf->size;
  43. }
  44. }
  45. void _buf_inc_writep(struct buffer *buf, unsigned by) {
  46. buf->writep += by;
  47. if (buf->writep >= buf->wrap) {
  48. buf->writep -= buf->size;
  49. }
  50. }
  51. void buf_flush(struct buffer *buf) {
  52. mutex_lock(buf->mutex);
  53. buf->readp = buf->buf;
  54. buf->writep = buf->buf;
  55. mutex_unlock(buf->mutex);
  56. }
  57. void _buf_flush(struct buffer *buf) {
  58. buf->readp = buf->buf;
  59. buf->writep = buf->buf;
  60. }
  61. // adjust buffer to multiple of mod bytes so reading in multiple always wraps on frame boundary
  62. void buf_adjust(struct buffer *buf, size_t mod) {
  63. size_t size;
  64. mutex_lock(buf->mutex);
  65. size = ((unsigned)(buf->base_size / mod)) * mod;
  66. buf->readp = buf->buf;
  67. buf->writep = buf->buf;
  68. buf->wrap = buf->buf + size;
  69. buf->size = size;
  70. mutex_unlock(buf->mutex);
  71. }
  72. // called with mutex locked to resize, does not retain contents, reverts to original size if fails
  73. void _buf_resize(struct buffer *buf, size_t size) {
  74. if (size == buf->size) return;
  75. free(buf->buf);
  76. buf->buf = malloc(size);
  77. if (!buf->buf) {
  78. size = buf->size;
  79. buf->buf= malloc(size);
  80. if (!buf->buf) {
  81. size = 0;
  82. }
  83. }
  84. buf->readp = buf->buf;
  85. buf->writep = buf->buf;
  86. buf->wrap = buf->buf + size;
  87. buf->size = size;
  88. buf->base_size = size;
  89. }
  90. void _buf_unwrap(struct buffer *buf, size_t cont) {
  91. ssize_t len, by = cont - (buf->wrap - buf->readp);
  92. size_t size;
  93. u8_t *scratch;
  94. // do nothing if we have enough space
  95. if (by <= 0 || cont >= buf->size) return;
  96. // buffer already unwrapped, just move it up
  97. if (buf->writep >= buf->readp) {
  98. memmove(buf->readp - by, buf->readp, buf->writep - buf->readp);
  99. buf->readp -= by;
  100. buf->writep -= by;
  101. return;
  102. }
  103. // how much is overlapping
  104. size = by - (buf->readp - buf->writep);
  105. len = buf->writep - buf->buf;
  106. // buffer is wrapped and enough free space to move data up directly
  107. if (size <= 0) {
  108. memmove(buf->readp - by, buf->readp, buf->wrap - buf->readp);
  109. buf->readp -= by;
  110. memcpy(buf->wrap - by, buf->buf, min(len, by));
  111. if (len > by) {
  112. memmove(buf->buf, buf->buf + by, len - by);
  113. buf->writep -= by;
  114. } else buf->writep += buf->size - by;
  115. return;
  116. }
  117. scratch = malloc(size);
  118. // buffer is wrapped but not enough free room => use scratch zone
  119. if (scratch) {
  120. memcpy(scratch, buf->writep - size, size);
  121. memmove(buf->readp - by, buf->readp, buf->wrap - buf->readp);
  122. buf->readp -= by;
  123. memcpy(buf->wrap - by, buf->buf, by);
  124. memmove(buf->buf, buf->buf + by, len - by - size);
  125. buf->writep -= by;
  126. memcpy(buf->writep - size, scratch, size);
  127. free(scratch);
  128. } else {
  129. _buf_unwrap(buf, cont / 2);
  130. _buf_unwrap(buf, cont - cont / 2);
  131. }
  132. }
  133. void buf_init(struct buffer *buf, size_t size) {
  134. buf->buf = malloc(size);
  135. buf->readp = buf->buf;
  136. buf->writep = buf->buf;
  137. buf->wrap = buf->buf + size;
  138. buf->size = size;
  139. buf->base_size = size;
  140. mutex_create_p(buf->mutex);
  141. }
  142. void buf_destroy(struct buffer *buf) {
  143. if (buf->buf) {
  144. free(buf->buf);
  145. buf->buf = NULL;
  146. buf->size = 0;
  147. buf->base_size = 0;
  148. mutex_destroy(buf->mutex);
  149. }
  150. }