-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_buffer.c
81 lines (62 loc) · 1.26 KB
/
circular_buffer.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "circular_buffer.h"
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
struct cb_t {
uint16_t head; // head is is
uint16_t tail;
size_t capacity;
bool full;
uint8_t *buffer;
};
cb_handle *cb_create(uint16_t capacity) {
cb_handle *cb = (cb_handle *)malloc(sizeof(struct cb_t));
uint8_t *buffer = malloc(capacity);
assert(cb && buffer);
cb->head = 0;
cb->tail = 0;
cb->full = false;
cb->capacity = capacity;
cb->buffer = buffer;
return cb;
}
void cb_destroy(cb_handle *cb) {
assert(cb);
free(cb);
}
void cb_reset(cb_handle *cb) {
assert(cb);
cb->head = 0;
cb->tail = 0;
cb->full = false;
}
bool cb_full(cb_handle *cb) {
assert(cb);
return cb->full;
}
void cb_write(cb_handle *cb, uint8_t value) {
// write to tail
cb->buffer[cb->head] = value;
if (cb->full) {
cb->tail = (cb->tail + 1) % cb->capacity;
}
cb->head = (cb->head + 1) % cb->capacity;
if (cb->tail == cb->head) {
cb->full = true;
}
}
bool cb_read(cb_handle *cb, uint8_t *read) {
assert(cb);
assert(read);
// cb is empty!
if (cb->head == cb->tail && !cb->full) {
return false;
}
cb->full = false;
*read = cb->buffer[cb->tail];
cb->tail++;
if (cb->tail == cb->capacity) {
cb->tail = 0;
}
return true;
}