fifo.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * a very simple circular buffer FIFO implementation
  3. * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  4. * Copyright (c) 2006 Roman Shaposhnik
  5. *
  6. * This file is part of FFmpeg.
  7. *
  8. * FFmpeg is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * FFmpeg is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with FFmpeg; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. #include "avassert.h"
  23. #include "common.h"
  24. #include "fifo.h"
  25. AVFifoBuffer *av_fifo_alloc(unsigned int size)
  26. {
  27. AVFifoBuffer *f = av_mallocz(sizeof(AVFifoBuffer));
  28. if (!f)
  29. return NULL;
  30. f->buffer = av_malloc(size);
  31. f->end = f->buffer + size;
  32. av_fifo_reset(f);
  33. if (!f->buffer)
  34. av_freep(&f);
  35. return f;
  36. }
  37. void av_fifo_free(AVFifoBuffer *f)
  38. {
  39. if (f) {
  40. av_freep(&f->buffer);
  41. av_free(f);
  42. }
  43. }
  44. void av_fifo_reset(AVFifoBuffer *f)
  45. {
  46. f->wptr = f->rptr = f->buffer;
  47. f->wndx = f->rndx = 0;
  48. }
  49. int av_fifo_size(AVFifoBuffer *f)
  50. {
  51. return (uint32_t)(f->wndx - f->rndx);
  52. }
  53. int av_fifo_space(AVFifoBuffer *f)
  54. {
  55. return f->end - f->buffer - av_fifo_size(f);
  56. }
  57. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
  58. {
  59. unsigned int old_size = f->end - f->buffer;
  60. if (old_size < new_size) {
  61. int len = av_fifo_size(f);
  62. AVFifoBuffer *f2 = av_fifo_alloc(new_size);
  63. if (!f2)
  64. return AVERROR(ENOMEM);
  65. av_fifo_generic_read(f, f2->buffer, len, NULL);
  66. f2->wptr += len;
  67. f2->wndx += len;
  68. av_free(f->buffer);
  69. *f = *f2;
  70. av_free(f2);
  71. }
  72. return 0;
  73. }
  74. int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
  75. {
  76. unsigned int old_size = f->end - f->buffer;
  77. if(size + (unsigned)av_fifo_size(f) < size)
  78. return AVERROR(EINVAL);
  79. size += av_fifo_size(f);
  80. if (old_size < size)
  81. return av_fifo_realloc2(f, FFMAX(size, 2*size));
  82. return 0;
  83. }
  84. /* src must NOT be const as it can be a context for func that may need
  85. * updating (like a pointer or byte counter) */
  86. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
  87. int (*func)(void *, void *, int))
  88. {
  89. int total = size;
  90. uint32_t wndx= f->wndx;
  91. uint8_t *wptr= f->wptr;
  92. do {
  93. int len = FFMIN(f->end - wptr, size);
  94. if (func) {
  95. if (func(src, wptr, len) <= 0)
  96. break;
  97. } else {
  98. memcpy(wptr, src, len);
  99. src = (uint8_t *)src + len;
  100. }
  101. // Write memory barrier needed for SMP here in theory
  102. wptr += len;
  103. if (wptr >= f->end)
  104. wptr = f->buffer;
  105. wndx += len;
  106. size -= len;
  107. } while (size > 0);
  108. f->wndx= wndx;
  109. f->wptr= wptr;
  110. return total - size;
  111. }
  112. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
  113. void (*func)(void *, void *, int))
  114. {
  115. // Read memory barrier needed for SMP here in theory
  116. do {
  117. int len = FFMIN(f->end - f->rptr, buf_size);
  118. if (func)
  119. func(dest, f->rptr, len);
  120. else {
  121. memcpy(dest, f->rptr, len);
  122. dest = (uint8_t *)dest + len;
  123. }
  124. // memory barrier needed for SMP here in theory
  125. av_fifo_drain(f, len);
  126. buf_size -= len;
  127. } while (buf_size > 0);
  128. return 0;
  129. }
  130. /** Discard data from the FIFO. */
  131. void av_fifo_drain(AVFifoBuffer *f, int size)
  132. {
  133. av_assert2(av_fifo_size(f) >= size);
  134. f->rptr += size;
  135. if (f->rptr >= f->end)
  136. f->rptr -= f->end - f->buffer;
  137. f->rndx += size;
  138. }
  139. #ifdef TEST
  140. int main(void)
  141. {
  142. /* create a FIFO buffer */
  143. AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
  144. int i, j, n;
  145. /* fill data */
  146. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  147. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  148. /* peek at FIFO */
  149. n = av_fifo_size(fifo) / sizeof(int);
  150. for (i = -n + 1; i < n; i++) {
  151. int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int));
  152. printf("%d: %d\n", i, *v);
  153. }
  154. printf("\n");
  155. /* read data */
  156. for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
  157. av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
  158. printf("%d ", j);
  159. }
  160. printf("\n");
  161. av_fifo_free(fifo);
  162. return 0;
  163. }
  164. #endif