8.4. fifo — First In First Out queuing

Source code: src/collections/fifo.h

Test code: tst/collections/fifo/main.c


Defines

FIFO_DEFINE_TEMPLATE(type)

Define the fifo structure and functions for a given type.

FIFO_DEFINE_TEMPLATE(int);

int foo()
{
    struct fifo_int_t fifo;
    int buf[4];
    int value;

    fifo_init_int(&fifo, buf, membersof(buf));

    // Put a value into the fifo.
    value = 10;
    fifo_put_int(&fifo, &value);

    // Get the value from the fifo.
    fifo_get_int(&fifo, &value);

    // Prints 'value = 10'.
    std_printf(FSTR("value=  %d\r\n", value));
}

Parameters
  • type: Type of the elements in the defined fifo.

Functions

static int fifo_init(struct fifo_t *self_p, int max)

Initialize given fifo.

Return
zero(0) or negative error code.
Parameters
  • self_p: Fifo to initialize.
  • max: Maximum number of elements in the fifo.

static int fifo_put(struct fifo_t *self_p)

Put an element in the fifo.

Return
Added element index in fifo, or -1 if there are no free positions.
Parameters
  • self_p: Initialized fifo.

static int fifo_get(struct fifo_t *self_p)

Get the next element from the fifo.

Return
The fetched element index in fifo , or -1 if the fifo is empty.
Parameters
  • self_p: Initialized fifo.

struct fifo_t
#include <fifo.h>

Public Members

int rdpos
int wrpos
void *buf_p
int max