fifo.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. static AVFifoBuffer *fifo_alloc_common(void *buffer, size_t size)
  26. {
  27. AVFifoBuffer *f;
  28. if (!buffer)
  29. return NULL;
  30. f = av_mallocz(sizeof(AVFifoBuffer));
  31. if (!f) {
  32. av_free(buffer);
  33. return NULL;
  34. }
  35. f->buffer = buffer;
  36. f->end = f->buffer + size;
  37. av_fifo_reset(f);
  38. return f;
  39. }
  40. AVFifoBuffer *av_fifo_alloc(unsigned int size)
  41. {
  42. void *buffer = av_malloc(size);
  43. return fifo_alloc_common(buffer, size);
  44. }
  45. AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
  46. {
  47. void *buffer = av_malloc_array(nmemb, size);
  48. return fifo_alloc_common(buffer, nmemb * size);
  49. }
  50. void av_fifo_free(AVFifoBuffer *f)
  51. {
  52. if (f) {
  53. av_freep(&f->buffer);
  54. av_free(f);
  55. }
  56. }
  57. void av_fifo_freep(AVFifoBuffer **f)
  58. {
  59. if (f) {
  60. av_fifo_free(*f);
  61. *f = NULL;
  62. }
  63. }
  64. void av_fifo_reset(AVFifoBuffer *f)
  65. {
  66. f->wptr = f->rptr = f->buffer;
  67. f->wndx = f->rndx = 0;
  68. }
  69. int av_fifo_size(FF_CONST_AVUTIL53 AVFifoBuffer *f)
  70. {
  71. return (uint32_t)(f->wndx - f->rndx);
  72. }
  73. int av_fifo_space(FF_CONST_AVUTIL53 AVFifoBuffer *f)
  74. {
  75. return f->end - f->buffer - av_fifo_size(f);
  76. }
  77. int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
  78. {
  79. unsigned int old_size = f->end - f->buffer;
  80. if (old_size < new_size) {
  81. int len = av_fifo_size(f);
  82. AVFifoBuffer *f2 = av_fifo_alloc(new_size);
  83. if (!f2)
  84. return AVERROR(ENOMEM);
  85. av_fifo_generic_read(f, f2->buffer, len, NULL);
  86. f2->wptr += len;
  87. f2->wndx += len;
  88. av_free(f->buffer);
  89. *f = *f2;
  90. av_free(f2);
  91. }
  92. return 0;
  93. }
  94. int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
  95. {
  96. unsigned int old_size = f->end - f->buffer;
  97. if(size + (unsigned)av_fifo_size(f) < size)
  98. return AVERROR(EINVAL);
  99. size += av_fifo_size(f);
  100. if (old_size < size)
  101. return av_fifo_realloc2(f, FFMAX(size, 2*size));
  102. return 0;
  103. }
  104. /* src must NOT be const as it can be a context for func that may need
  105. * updating (like a pointer or byte counter) */
  106. int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
  107. int (*func)(void *, void *, int))
  108. {
  109. int total = size;
  110. uint32_t wndx= f->wndx;
  111. uint8_t *wptr= f->wptr;
  112. do {
  113. int len = FFMIN(f->end - wptr, size);
  114. if (func) {
  115. if (func(src, wptr, len) <= 0)
  116. break;
  117. } else {
  118. memcpy(wptr, src, len);
  119. src = (uint8_t *)src + len;
  120. }
  121. // Write memory barrier needed for SMP here in theory
  122. wptr += len;
  123. if (wptr >= f->end)
  124. wptr = f->buffer;
  125. wndx += len;
  126. size -= len;
  127. } while (size > 0);
  128. f->wndx= wndx;
  129. f->wptr= wptr;
  130. return total - size;
  131. }
  132. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
  133. void (*func)(void *, void *, int))
  134. {
  135. // Read memory barrier needed for SMP here in theory
  136. do {
  137. int len = FFMIN(f->end - f->rptr, buf_size);
  138. if (func)
  139. func(dest, f->rptr, len);
  140. else {
  141. memcpy(dest, f->rptr, len);
  142. dest = (uint8_t *)dest + len;
  143. }
  144. // memory barrier needed for SMP here in theory
  145. av_fifo_drain(f, len);
  146. buf_size -= len;
  147. } while (buf_size > 0);
  148. return 0;
  149. }
  150. /** Discard data from the FIFO. */
  151. void av_fifo_drain(AVFifoBuffer *f, int size)
  152. {
  153. av_assert2(av_fifo_size(f) >= size);
  154. f->rptr += size;
  155. if (f->rptr >= f->end)
  156. f->rptr -= f->end - f->buffer;
  157. f->rndx += size;
  158. }
  159. #ifdef TEST
  160. int main(void)
  161. {
  162. /* create a FIFO buffer */
  163. AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
  164. int i, j, n;
  165. /* fill data */
  166. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  167. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  168. /* peek at FIFO */
  169. n = av_fifo_size(fifo) / sizeof(int);
  170. for (i = -n + 1; i < n; i++) {
  171. int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int));
  172. printf("%d: %d\n", i, *v);
  173. }
  174. printf("\n");
  175. /* read data */
  176. for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
  177. av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
  178. printf("%d ", j);
  179. }
  180. printf("\n");
  181. av_fifo_free(fifo);
  182. return 0;
  183. }
  184. #endif