2013-06-22 17:58:19 +02:00
|
|
|
/**
|
|
|
|
* Ringbuffer implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
|
|
|
* Copyright (C) 2013 INRIA
|
|
|
|
*
|
2013-11-22 20:47:05 +01:00
|
|
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
2013-06-22 17:58:19 +02:00
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @ingroup sys_lib
|
|
|
|
* @{
|
|
|
|
* @file ringbuffer.c
|
2013-12-04 15:07:56 +01:00
|
|
|
* @author Kaspar Schleiser <kaspar@schleiser.de>
|
2014-05-03 18:49:55 +02:00
|
|
|
* @author René Kijewski <rene.kijewski@fu-berlin.de>
|
2013-06-22 17:58:19 +02:00
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2013-10-10 17:06:14 +02:00
|
|
|
#include "ringbuffer.h"
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
2010-09-22 17:25:19 +02:00
|
|
|
rb->buf = buffer;
|
|
|
|
rb->size = bufsize;
|
2014-05-03 18:49:55 +02:00
|
|
|
rb->start = 0;
|
2010-09-22 17:25:19 +02:00
|
|
|
rb->avail = 0;
|
|
|
|
}
|
|
|
|
|
2014-05-03 18:49:55 +02:00
|
|
|
static void add_tail(ringbuffer_t *restrict rb, char c)
|
|
|
|
{
|
|
|
|
unsigned pos = rb->start + rb->avail++;
|
|
|
|
if (pos >= rb->size) {
|
|
|
|
pos -= rb->size;
|
|
|
|
}
|
|
|
|
rb->buf[pos] = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char get_head(ringbuffer_t *restrict rb)
|
|
|
|
{
|
|
|
|
char result = rb->buf[rb->start];
|
|
|
|
if ((--rb->avail == 0) || (++rb->start == rb->size)) {
|
|
|
|
rb->start = 0;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
2014-05-03 19:17:29 +02:00
|
|
|
for (unsigned i = 0; i < n; i++) {
|
2014-05-03 16:39:39 +02:00
|
|
|
ringbuffer_add_one(rb, buf[i]);
|
2010-09-24 13:53:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
void ringbuffer_add_one(ringbuffer_t *restrict rb, char c)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
2014-05-03 18:49:55 +02:00
|
|
|
if (ringbuffer_full(rb)) {
|
|
|
|
get_head(rb);
|
2013-06-22 17:58:19 +02:00
|
|
|
}
|
2014-05-03 18:49:55 +02:00
|
|
|
add_tail(rb, c);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
int ringbuffer_get_one(ringbuffer_t *restrict rb)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
2014-05-03 18:49:55 +02:00
|
|
|
if (!ringbuffer_empty(rb)) {
|
|
|
|
return (unsigned char) get_head(rb);
|
2013-06-22 17:58:19 +02:00
|
|
|
}
|
2014-05-03 18:49:55 +02:00
|
|
|
else {
|
|
|
|
return -1;
|
2013-06-22 17:58:19 +02:00
|
|
|
}
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n)
|
2013-06-22 17:58:19 +02:00
|
|
|
{
|
2014-05-03 18:49:55 +02:00
|
|
|
if (n > rb->avail) {
|
|
|
|
n = rb->avail;
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2014-05-03 18:49:55 +02:00
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
|
|
buf[i] = get_head(rb);
|
|
|
|
}
|
|
|
|
return n;
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
2014-05-03 18:56:48 +02:00
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
int ringbuffer_peek_one(const ringbuffer_t *restrict rb_)
|
2014-05-03 18:56:48 +02:00
|
|
|
{
|
2014-05-03 19:17:29 +02:00
|
|
|
ringbuffer_t rb = *rb_;
|
|
|
|
return ringbuffer_get_one(&rb);
|
2014-05-03 18:56:48 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 19:17:29 +02:00
|
|
|
unsigned ringbuffer_peek(const ringbuffer_t *restrict rb_, char *buf, unsigned n)
|
2014-05-03 18:56:48 +02:00
|
|
|
{
|
2014-05-03 19:17:29 +02:00
|
|
|
ringbuffer_t rb = *rb_;
|
|
|
|
return ringbuffer_get(&rb, buf, n);
|
2014-05-03 18:56:48 +02:00
|
|
|
}
|