2
0

buffer.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. #define _GNU_SOURCE
  23. #include "squeezelite.h"
  24. // _* called with muxtex locked
  25. inline unsigned _buf_used(struct buffer *buf) {
  26. return buf->writep >= buf->readp ? buf->writep - buf->readp : buf->size - (buf->readp - buf->writep);
  27. }
  28. unsigned _buf_space(struct buffer *buf) {
  29. return buf->size - _buf_used(buf) - 1; // reduce by one as full same as empty otherwise
  30. }
  31. unsigned _buf_cont_read(struct buffer *buf) {
  32. return buf->writep >= buf->readp ? buf->writep - buf->readp : buf->wrap - buf->readp;
  33. }
  34. unsigned _buf_cont_write(struct buffer *buf) {
  35. return buf->writep >= buf->readp ? buf->wrap - buf->writep : buf->readp - buf->writep;
  36. }
  37. void _buf_inc_readp(struct buffer *buf, unsigned by) {
  38. buf->readp += by;
  39. if (buf->readp >= buf->wrap) {
  40. buf->readp -= buf->size;
  41. }
  42. }
  43. void _buf_inc_writep(struct buffer *buf, unsigned by) {
  44. buf->writep += by;
  45. if (buf->writep >= buf->wrap) {
  46. buf->writep -= buf->size;
  47. }
  48. }
  49. void buf_flush(struct buffer *buf) {
  50. mutex_lock(buf->mutex);
  51. buf->readp = buf->buf;
  52. buf->writep = buf->buf;
  53. mutex_unlock(buf->mutex);
  54. }
  55. // adjust buffer to multiple of mod bytes so reading in multiple always wraps on frame boundary
  56. void buf_adjust(struct buffer *buf, size_t mod) {
  57. size_t size;
  58. mutex_lock(buf->mutex);
  59. size = ((unsigned)(buf->base_size / mod)) * mod;
  60. buf->readp = buf->buf;
  61. buf->writep = buf->buf;
  62. buf->wrap = buf->buf + size;
  63. buf->size = size;
  64. mutex_unlock(buf->mutex);
  65. }
  66. // called with mutex locked to resize, does not retain contents, reverts to original size if fails
  67. void _buf_resize(struct buffer *buf, size_t size) {
  68. free(buf->buf);
  69. buf->buf = malloc(size);
  70. if (!buf->buf) {
  71. size = buf->size;
  72. buf->buf= malloc(size);
  73. if (!buf->buf) {
  74. size = 0;
  75. }
  76. }
  77. buf->readp = buf->buf;
  78. buf->writep = buf->buf;
  79. buf->wrap = buf->buf + size;
  80. buf->size = size;
  81. buf->base_size = size;
  82. }
  83. void buf_init(struct buffer *buf, size_t size) {
  84. buf->buf = malloc(size);
  85. buf->readp = buf->buf;
  86. buf->writep = buf->buf;
  87. buf->wrap = buf->buf + size;
  88. buf->size = size;
  89. buf->base_size = size;
  90. mutex_create_p(buf->mutex);
  91. }
  92. void buf_destroy(struct buffer *buf) {
  93. if (buf->buf) {
  94. free(buf->buf);
  95. buf->buf = NULL;
  96. buf->size = 0;
  97. buf->base_size = 0;
  98. mutex_destroy(buf->mutex);
  99. }
  100. }