aboutsummaryrefslogtreecommitdiff
path: root/include/fifo.h
blob: 1af51ec55f9e3b2c3c4b3f9dc8f630a5c9fc99ac (about) (plain)
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
#ifndef FIFO_H
#define FIFO_H

#include <zeta.h>

#define FIFO_LEN 32

struct fifo {
	u8 head;
	u8 tail;
	u8 data[FIFO_LEN];
};

static inline u8
fifo_pop(struct fifo *fifo)
{
	u8 ret = fifo->data[fifo->head];
	if (++fifo->head >= LENGTH(fifo->data))
		fifo->head = 0;
	return ret;
}

static inline void
fifo_push(struct fifo *fifo, u8 v)
{
	fifo->data[fifo->tail] = v;

	if (++fifo->tail >= LENGTH(fifo->data))
		fifo->tail = 0;
}

static inline bool
fifo_empty(const struct fifo *fifo)
{
	return (fifo->head == fifo->tail);
}

static inline void
fifo_clear(struct fifo *fifo)
{
	fifo->head = 0;
	fifo->tail = 0;
}

#endif // FIFO_H