fifo — first-in, first-out queue.


#include <fifo.h>
fifo_t * fifo_new ( size_t size,
  size_t initial,
  size_t maximum);
void fifo_destroy ( fifo_t * fifo);
int fifo_enqueue ( fifo_t * fifo,
  const void * item);
int fifo_dequeue ( fifo_t * fifo,
  void * item);
size_t fifo_peak ( const fifo_t * fifo);


An enqueue/dequeue FIFO implemented on a dynamic ring-buffer, the data being items of a fixed size passed to the constructor, fifo_create along with the initial size of the stack (in items) and the maximum size of the stack (zero for unlimited).

The fifo_enqueue function copies item onto the end of the queue, and likewise fifo_dequeue copies from the head to item.

The fifo_peak function returns the peak size of the fifo (in number of items).


Enqueue ten integers, then dequeue and print them

fifo_t *f = fifo_new(sizeof(int), 0, 0);

for (int i = 0 ; i < 10 ; i++)
  fifo_enqueue(f, &i);

int k;

while (fifo_dequeue(f, &k) == FIFO_OK)
  printf("%d ", k);


Functions returning an integer return zero on success and non-zero on error. For fifo_dequeue the return value is the value FIFO_EMPTY if the FIFO is empty, FIFO_USERMAX if the maximum specified in the constructor is exceeded, or FIFO_ERROR in the case of other error.


J.J. Green.