libavutil/fifo.c
f5a478f6
 /*
89c9ff50
  * a very simple circular buffer FIFO implementation
f5a478f6
  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
  * Copyright (c) 2006 Roman Shaposhnik
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
f5a478f6
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
f5a478f6
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
f5a478f6
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
f5a478f6
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 #include "common.h"
 #include "fifo.h"
 
41dd680d
 AVFifoBuffer *av_fifo_alloc(unsigned int size)
f5a478f6
 {
41dd680d
     AVFifoBuffer *f= av_mallocz(sizeof(AVFifoBuffer));
f1b29223
     if (!f)
41dd680d
         return NULL;
f5a478f6
     f->buffer = av_malloc(size);
4497712f
     f->end = f->buffer + size;
32b936d0
     av_fifo_reset(f);
f5a478f6
     if (!f->buffer)
41dd680d
         av_freep(&f);
     return f;
f5a478f6
 }
 
 void av_fifo_free(AVFifoBuffer *f)
 {
f1b29223
     if (f) {
17354407
         av_freep(&f->buffer);
c900635f
         av_free(f);
41dd680d
     }
f5a478f6
 }
 
32b936d0
 void av_fifo_reset(AVFifoBuffer *f)
 {
     f->wptr = f->rptr = f->buffer;
     f->wndx = f->rndx = 0;
 }
 
f5a478f6
 int av_fifo_size(AVFifoBuffer *f)
 {
0a71e78c
     return (uint32_t)(f->wndx - f->rndx);
f5a478f6
 }
 
7b09db35
 int av_fifo_space(AVFifoBuffer *f)
 {
     return f->end - f->buffer - av_fifo_size(f);
 }
 
f1b29223
 int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
 {
     unsigned int old_size = f->end - f->buffer;
f5a478f6
 
f1b29223
     if (old_size < new_size) {
         int len = av_fifo_size(f);
         AVFifoBuffer *f2 = av_fifo_alloc(new_size);
f5a478f6
 
41dd680d
         if (!f2)
2e81bb5e
             return AVERROR(ENOMEM);
3898eed8
         av_fifo_generic_read(f, f2->buffer, len, NULL);
41dd680d
         f2->wptr += len;
         f2->wndx += len;
96e39edc
         av_free(f->buffer);
f1b29223
         *f = *f2;
41dd680d
         av_free(f2);
f5a478f6
     }
8257b835
     return 0;
f5a478f6
 }
 
f8196759
 int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
 {
     unsigned int old_size = f->end - f->buffer;
     if(size + (unsigned)av_fifo_size(f) < size)
         return AVERROR(EINVAL);
 
     size += av_fifo_size(f);
 
     if (old_size < size)
         return av_fifo_realloc2(f, FFMAX(size, 2*size));
     return 0;
 }
 
fc323a54
 // src must NOT be const as it can be a context for func that may need updating (like a pointer or byte counter)
95c76e11
 int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int))
49cec199
 {
     int total = size;
9eb0d8ba
     uint32_t wndx= f->wndx;
     uint8_t *wptr= f->wptr;
 
50b44685
     do {
9eb0d8ba
         int len = FFMIN(f->end - wptr, size);
f1b29223
         if (func) {
9eb0d8ba
             if (func(src, wptr, len) <= 0)
49cec199
                 break;
         } else {
9eb0d8ba
             memcpy(wptr, src, len);
95c76e11
             src = (uint8_t*)src + len;
49cec199
         }
a9364759
 // Write memory barrier needed for SMP here in theory
9eb0d8ba
         wptr += len;
         if (wptr >= f->end)
             wptr = f->buffer;
         wndx += len;
f5a478f6
         size -= len;
50b44685
     } while (size > 0);
9eb0d8ba
     f->wndx= wndx;
     f->wptr= wptr;
49cec199
     return total - size;
f5a478f6
 }
 
 
3898eed8
 int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int))
f5a478f6
 {
a9364759
 // Read memory barrier needed for SMP here in theory
50b44685
     do {
870a12d1
         int len = FFMIN(f->end - f->rptr, buf_size);
765d4f3b
         if(func) func(dest, f->rptr, len);
         else{
             memcpy(dest, f->rptr, len);
             dest = (uint8_t*)dest + len;
         }
a9364759
 // memory barrier needed for SMP here in theory
3da97cfd
         av_fifo_drain(f, len);
f5a478f6
         buf_size -= len;
50b44685
     } while (buf_size > 0);
f5a478f6
     return 0;
 }
 
89c9ff50
 /** Discard data from the FIFO. */
f5a478f6
 void av_fifo_drain(AVFifoBuffer *f, int size)
 {
     f->rptr += size;
     if (f->rptr >= f->end)
         f->rptr -= f->end - f->buffer;
0a71e78c
     f->rndx += size;
f5a478f6
 }
1717ba0c
 
 #ifdef TEST
 
 #undef printf
 
 int main(void)
 {
     /* create a FIFO buffer */
     AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
     int i, j, n;
 
     /* fill data */
     for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
         av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
 
     /* peek at FIFO */
     n = av_fifo_size(fifo)/sizeof(int);
     for (i = -n+1; i < n; i++) {
         int *v = (int *)av_fifo_peek2(fifo, i*sizeof(int));
         printf("%d: %d\n", i, *v);
     }
     printf("\n");
 
     /* read data */
     for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
         av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
         printf("%d ", j);
     }
     printf("\n");
 
     av_fifo_free(fifo);
 
     return 0;
 }
 
 #endif