From 12287ff0a55f929bf840dcb4780d3f77b862c434 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 2 Nov 2014 18:17:44 +0000 Subject: fish --- app/ring.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 app/ring.c (limited to 'app/ring.c') diff --git a/app/ring.c b/app/ring.c new file mode 100644 index 0000000..4f38e44 --- /dev/null +++ b/app/ring.c @@ -0,0 +1,61 @@ +#include "project.h" + + +static inline size_t +ring_next (ring_t * r, size_t p) +{ + p++; + if (p >= r->size) + p -= r->size; + return p; +} + +void +ring_init (ring_t * r, uint8_t * buf, size_t len) +{ + r->data = buf; + r->size = len; + r->write = 0; + r->read = 0; +} + +int +ring_write_byte (ring_t * r, uint8_t c) +{ + size_t n = ring_next (r, r->write); + + if (n == r->read) + return -1; + + r->data[r->write] = c; + r->write = n; + + return 0; +} + + +int +ring_read_byte (ring_t * r, uint8_t * c) +{ + size_t n = ring_next (r, r->read); + + if (r->read == r->write) + return -1; + + *c = r->data[r->read]; + r->read = n; + + return 0; +} + +int +ring_write (ring_t * r, uint8_t * buf, size_t len) +{ + while (len--) + { + if (ring_write_byte (r, *(buf++))) + return -1; + } + + return 0; +} -- cgit v1.2.3