ish/util/fifo.h
Theodore Dubois a0ca767db6 Rewrite syslog buffer to use a generic fifo
Trivia: I wrote this ring buffer implementation in half an hour during a Google interview and it somehow works better than the old one which took me half a day.
2019-01-05 17:24:32 -08:00

32 lines
897 B
C

#ifndef UTIL_FIFO_H
#define UTIL_FIFO_H
#include <sys/types.h>
struct fifo {
char *buf;
size_t capacity;
size_t size;
size_t start;
};
#define FIFO_INIT(b) ((struct fifo) {.buf = b, .capacity = sizeof(b)})
void fifo_init(struct fifo *fifo, size_t capacity);
void fifo_destroy(struct fifo *fifo);
size_t fifo_capacity(struct fifo *fifo);
size_t fifo_size(struct fifo *fifo);
size_t fifo_remaining(struct fifo *fifo);
// return 0 on success, 1 if size > fifo_remaining and FIFO_OVERWRITE was not specified
#define FIFO_OVERWRITE 1
int fifo_write(struct fifo *fifo, const void *data, size_t size, int flags);
// return 0 on success, 1 if size > fifo_size
#define FIFO_PEEK 1 // don't remove the bytes
#define FIFO_LAST 2 // return bytes from the end, not the start
int fifo_read(struct fifo *fifo, void *buf, size_t size, int flags);
void fifo_flush(struct fifo *fifo);
#endif