vlc_fifo_helper.h 4.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/*****************************************************************************
 * vlc_fifo_helper.h: Basic FIFO helper functions
 *****************************************************************************
 * Copyright (C) 2017 VLC authors and VideoLAN
 *
 * Authors: Steve Lhomme <robux4@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * 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 License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

#ifndef VLC_FIFO_HELPER_H
#define VLC_FIFO_HELPER_H 1

typedef struct FIFO_ITEM {
    struct FIFO_ITEM  *p_next;
} fifo_item_t;

typedef struct
{
    fifo_item_t  *p_first;
    fifo_item_t  **pp_last;
    size_t     i_depth;
} fifo_t;

static inline void fifo_Init(fifo_t *p_fifo)
{
    p_fifo->p_first = NULL;
    p_fifo->pp_last = &p_fifo->p_first;
    p_fifo->i_depth = 0;
}

static inline void fifo_Release(fifo_t *fifo)
{
    assert(fifo->i_depth == 0); /* we should have poped all the items */
}

static inline size_t fifo_GetCount(fifo_t *fifo)
{
    return fifo->i_depth;
}

static inline fifo_item_t *fifo_Show(fifo_t *p_fifo)
{
    fifo_item_t *b;

    assert(p_fifo->p_first != NULL);
    b = p_fifo->p_first;

    return b;
}

static inline fifo_item_t *fifo_Get(fifo_t *fifo)
{
    fifo_item_t *block = fifo->p_first;

    if (block == NULL)
        return NULL; /* Nothing to do */

    fifo->p_first = block->p_next;
    if (block->p_next == NULL)
        fifo->pp_last = &fifo->p_first;
    block->p_next = NULL;

    assert(fifo->i_depth > 0);
    fifo->i_depth--;

    return block;
}

static inline void fifo_Put(fifo_t *fifo, fifo_item_t *p_item)
{
    *(fifo->pp_last) = p_item;

    while (p_item != NULL)
    {
        fifo->pp_last = &p_item->p_next;
        fifo->i_depth++;

        p_item = p_item->p_next;
    }
}

/* macro to get the proper item type in/out of the FIFO
 * The item type must have a field fifo_item_t named fifo
 */
#define TYPED_FIFO(type, prefix)                                      \
static inline void prefix ## _fifo_Init(fifo_t *p_fifo)               \
{                                                                     \
    fifo_Init(p_fifo);                                                \
}                                                                     \
static inline void prefix ## _fifo_Release(fifo_t *p_fifo)            \
{                                                                     \
    fifo_Release(p_fifo);                                             \
}                                                                     \
static inline void prefix ## _fifo_Put(fifo_t *p_fifo, type *p_item)  \
{                                                                     \
    fifo_Put(p_fifo, &p_item->fifo);                                  \
}                                                                     \
static inline type *prefix ## _fifo_Get(fifo_t *p_fifo)               \
{                                                                     \
    return (type*)fifo_Get(p_fifo);                                   \
}                                                                     \
static inline type *prefix ## _fifo_Show(fifo_t *p_fifo)              \
{                                                                     \
    return (type*)fifo_Show(p_fifo);                                  \
}                                                                     \
static inline size_t prefix ## _fifo_GetCount(fifo_t *fifo)           \
{                                                                     \
    return fifo->i_depth;                                             \
}


#endif /* VLC_FIFO_HELPER_H */