-
Notifications
You must be signed in to change notification settings - Fork 65
/
ring.c
executable file
·54 lines (44 loc) · 1.04 KB
/
ring.c
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
//
// ring.c -- Ring buffers
//
// Copyright (c) 2012-2013 Andrew Payne <[email protected]>
//
#include <freedom.h>
#include "common.h"
inline void buf_reset(RingBuffer *buf, int size)
{
buf->head = buf->tail = 0;
buf->size = size;
}
inline int buf_len(const RingBuffer *buf)
{
int len = buf->tail - buf->head;
if (len < 0)
len += buf->size;
return len;
}
inline int buf_isfull(const RingBuffer *buf)
{
return buf_len(buf) == (buf->size-1);
}
inline int buf_isempty(const RingBuffer *buf)
{
return buf->head == buf->tail;
}
inline int advance(uint16_t i, uint16_t size)
{
if (++i >= size) // Avoid modulo or divide
i = 0;
return i;
}
inline uint8_t buf_get_byte(RingBuffer *buf)
{
const uint8_t item = buf->data[buf->head];
buf->head = advance(buf->head, buf->size);
return item;
}
inline void buf_put_byte(RingBuffer *buf, uint8_t val)
{
buf->data[buf->tail] = val;
buf->tail = advance(buf->tail, buf->size);
}