2013-06-22 17:58:19 +02:00
|
|
|
/**
|
|
|
|
* Ringbuffer implementation
|
|
|
|
*
|
|
|
|
* Copyright (C) 2013 Freie Universität Berlin
|
|
|
|
* Copyright (C) 2013 INRIA
|
|
|
|
*
|
|
|
|
* This file subject to the terms and conditions of the GNU Lesser General
|
|
|
|
* Public License. See the file LICENSE in the top level directory for more
|
|
|
|
* details.
|
|
|
|
*
|
|
|
|
* @ingroup sys_lib
|
|
|
|
* @{
|
|
|
|
* @file ringbuffer.c
|
|
|
|
* @author Kaspar Schleiser <kaspar.schleiser@fu-berlin.de>
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2013-06-03 13:23:57 +02:00
|
|
|
#if (defined(__MACH__) || defined(__FreeBSD__))
|
2013-05-15 17:45:43 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#else
|
|
|
|
#include "malloc.h"
|
|
|
|
#endif
|
|
|
|
|
2013-10-10 17:06:14 +02:00
|
|
|
#define ENABLE_DEBUG (0)
|
|
|
|
#include "debug.h"
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2013-10-10 17:06:14 +02:00
|
|
|
#include "ringbuffer.h"
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void ringbuffer_init(ringbuffer_t *rb, char *buffer, unsigned int bufsize)
|
|
|
|
{
|
2010-09-22 17:25:19 +02:00
|
|
|
rb->buf = buffer;
|
|
|
|
rb->start = 0;
|
|
|
|
rb->end = 0;
|
|
|
|
rb->size = bufsize;
|
|
|
|
rb->avail = 0;
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void rb_add_elements(ringbuffer_t *rb, char *buf, int n)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
for (int i = 0; i < n; i++) {
|
2010-09-24 13:53:22 +02:00
|
|
|
rb_add_element(rb, buf[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
void rb_add_element(ringbuffer_t *rb, char c)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rb->avail == rb->size) {
|
2013-06-22 17:58:19 +02:00
|
|
|
rb_get_element(rb);
|
|
|
|
}
|
2010-09-22 17:25:19 +02:00
|
|
|
|
2010-09-24 13:53:22 +02:00
|
|
|
rb->buf[rb->end++] = c;
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rb->end >= rb->size) {
|
2013-06-22 17:58:19 +02:00
|
|
|
rb->end = 0;
|
|
|
|
}
|
2010-09-24 13:53:22 +02:00
|
|
|
|
|
|
|
rb->avail++;
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
int rb_get_element(ringbuffer_t *rb)
|
|
|
|
{
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rb->avail == 0) {
|
2013-06-22 17:58:19 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
rb->avail--;
|
|
|
|
|
|
|
|
int c = (char)rb->buf[rb->start++];
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
if (rb->start >= rb->size) {
|
2013-06-22 17:58:19 +02:00
|
|
|
rb->start = 0;
|
|
|
|
}
|
2010-09-24 13:53:22 +02:00
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2013-06-22 17:58:19 +02:00
|
|
|
int rb_get_elements(ringbuffer_t *rb, char *buf, int n)
|
|
|
|
{
|
2010-09-22 17:25:19 +02:00
|
|
|
int count = 0;
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2013-06-24 22:37:35 +02:00
|
|
|
while (rb->avail && (count < n)) {
|
2010-09-24 13:53:22 +02:00
|
|
|
buf[count++] = rb_get_element(rb);
|
2010-09-22 17:25:19 +02:00
|
|
|
}
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2010-09-24 13:53:22 +02:00
|
|
|
/*
|
|
|
|
int main(int argc, char *argv[] ){
|
2010-09-22 17:25:19 +02:00
|
|
|
ringbuffer r;
|
2010-09-24 13:53:22 +02:00
|
|
|
char buffer[5];
|
2010-09-22 17:25:19 +02:00
|
|
|
ringbuffer_init(&r, buffer, sizeof(buffer));
|
|
|
|
|
2010-09-24 13:53:22 +02:00
|
|
|
rb_add_element(&r, 1);
|
|
|
|
rb_add_element(&r, 2);
|
|
|
|
rb_add_element(&r, 3);
|
|
|
|
rb_add_element(&r, 4);
|
|
|
|
rb_add_element(&r, 5);
|
|
|
|
rb_add_element(&r, 6);
|
|
|
|
rb_add_element(&r, 7);
|
|
|
|
rb_add_element(&r, 8);
|
|
|
|
rb_add_element(&r, 9);
|
|
|
|
rb_add_element(&r, 10);
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2010-09-22 17:25:19 +02:00
|
|
|
int c;
|
2010-09-24 13:53:22 +02:00
|
|
|
while ( r.avail ) {
|
|
|
|
c = rb_get_element(&r);
|
|
|
|
if (c == -1) break;
|
|
|
|
printf("c=%i\n", (int)c);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_add_element(&r, 1);
|
|
|
|
rb_add_element(&r, 2);
|
|
|
|
rb_add_element(&r, 3);
|
|
|
|
rb_add_element(&r, 4);
|
|
|
|
rb_add_element(&r, 5);
|
|
|
|
|
|
|
|
char buffer2[10];
|
|
|
|
|
|
|
|
int n = rb_get_elements(&r, buffer2, sizeof(buffer2));
|
|
|
|
|
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
printf("%i\n", buffer2[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_add_element(&r, 1);
|
|
|
|
rb_add_element(&r, 2);
|
|
|
|
rb_add_element(&r, 3);
|
|
|
|
rb_add_element(&r, 4);
|
|
|
|
rb_add_element(&r, 5);
|
|
|
|
rb_add_element(&r, 6);
|
|
|
|
rb_add_element(&r, 7);
|
|
|
|
rb_add_element(&r, 8);
|
|
|
|
rb_add_element(&r, 9);
|
|
|
|
rb_add_element(&r, 10);
|
2013-06-22 17:58:19 +02:00
|
|
|
|
2010-09-24 13:53:22 +02:00
|
|
|
while ( r.avail ) {
|
2010-09-22 17:25:19 +02:00
|
|
|
c = rb_get_element(&r);
|
|
|
|
if (c == -1) break;
|
|
|
|
printf("c=%i\n", (int)c);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}*/
|