fifo.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. len = func(src, wptr, len);
  116. if (len <= 0)
  117. break;
  118. } else {
  119. memcpy(wptr, src, len);
  120. src = (uint8_t *)src + len;
  121. }
  122. // Write memory barrier needed for SMP here in theory
  123. wptr += len;
  124. if (wptr >= f->end)
  125. wptr = f->buffer;
  126. wndx += len;
  127. size -= len;
  128. } while (size > 0);
  129. f->wndx= wndx;
  130. f->wptr= wptr;
  131. return total - size;
  132. }
  133. int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
  134. void (*func)(void *, void *, int))
  135. {
  136. // Read memory barrier needed for SMP here in theory
  137. do {
  138. int len = FFMIN(f->end - f->rptr, buf_size);
  139. if (func)
  140. func(dest, f->rptr, len);
  141. else {
  142. memcpy(dest, f->rptr, len);
  143. dest = (uint8_t *)dest + len;
  144. }
  145. // memory barrier needed for SMP here in theory
  146. av_fifo_drain(f, len);
  147. buf_size -= len;
  148. } while (buf_size > 0);
  149. return 0;
  150. }
  151. /** Discard data from the FIFO. */
  152. void av_fifo_drain(AVFifoBuffer *f, int size)
  153. {
  154. av_assert2(av_fifo_size(f) >= size);
  155. f->rptr += size;
  156. if (f->rptr >= f->end)
  157. f->rptr -= f->end - f->buffer;
  158. f->rndx += size;
  159. }
  160. #ifdef TEST
  161. int main(void)
  162. {
  163. /* create a FIFO buffer */
  164. AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
  165. int i, j, n;
  166. /* fill data */
  167. for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
  168. av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
  169. /* peek at FIFO */
  170. n = av_fifo_size(fifo) / sizeof(int);
  171. for (i = -n + 1; i < n; i++) {
  172. int *v = (int *)av_fifo_peek2(fifo, i * sizeof(int));
  173. printf("%d: %d\n", i, *v);
  174. }
  175. printf("\n");
  176. /* read data */
  177. for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
  178. av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
  179. printf("%d ", j);
  180. }
  181. printf("\n");
  182. av_fifo_free(fifo);
  183. return 0;
  184. }
  185. #endif