From 726af8d44e3a1ac5d3d387300618ed887874ccd5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= <rene.kijewski@fu-berlin.de>
Date: Sat, 3 May 2014 19:17:29 +0200
Subject: [PATCH] ringbuffer: fix parameter types

It is a bad idea to use signed types for lengths.
Mark pointers a `restrict`, since the ringbuffer is not thread safe
anyway.
---
 sys/include/ringbuffer.h | 18 +++++++++---------
 sys/lib/ringbuffer.c     | 30 +++++++++++++-----------------
 2 files changed, 22 insertions(+), 26 deletions(-)

diff --git a/sys/include/ringbuffer.h b/sys/include/ringbuffer.h
index 93522d08f0..23755c1229 100644
--- a/sys/include/ringbuffer.h
+++ b/sys/include/ringbuffer.h
@@ -27,23 +27,23 @@ typedef struct ringbuffer {
     unsigned int        avail;
 } ringbuffer_t;
 
-void ringbuffer_init(ringbuffer_t *rb, char *buffer, unsigned int bufsize);
-void ringbuffer_add_one(ringbuffer_t *rb, char c);
-void ringbuffer_add(ringbuffer_t *rb, char *buf, int n);
-int ringbuffer_get_one(ringbuffer_t *rb);
-int ringbuffer_get(ringbuffer_t *rb, char *buf, int n);
+void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize);
+void ringbuffer_add_one(ringbuffer_t *restrict rb, char c);
+void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n);
+int ringbuffer_get_one(ringbuffer_t *restrict rb);
+unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n);
 
-static inline int ringbuffer_empty(ringbuffer_t *rb)
+static inline int ringbuffer_empty(const ringbuffer_t *restrict rb)
 {
     return rb->avail == 0;
 }
 
-static inline int ringbuffer_full(ringbuffer_t *rb)
+static inline int ringbuffer_full(const ringbuffer_t *restrict rb)
 {
     return rb->avail == rb->size;
 }
 
-int ringbuffer_peek_one(ringbuffer_t *rb);
-int ringbuffer_peek(ringbuffer_t *rb, char *buf, unsigned n);
+int ringbuffer_peek_one(const ringbuffer_t *restrict rb);
+unsigned ringbuffer_peek(const ringbuffer_t *restrict rb, char *buf, unsigned n);
 
 #endif /* __RINGBUFFER_H */
diff --git a/sys/lib/ringbuffer.c b/sys/lib/ringbuffer.c
index fc7055d8ff..b8ee266f89 100644
--- a/sys/lib/ringbuffer.c
+++ b/sys/lib/ringbuffer.c
@@ -17,7 +17,7 @@
 
 #include "ringbuffer.h"
 
-void ringbuffer_init(ringbuffer_t *rb, char *buffer, unsigned int bufsize)
+void ringbuffer_init(ringbuffer_t *restrict rb, char *buffer, unsigned bufsize)
 {
     rb->buf = buffer;
     rb->start = 0;
@@ -26,14 +26,14 @@ void ringbuffer_init(ringbuffer_t *rb, char *buffer, unsigned int bufsize)
     rb->avail = 0;
 }
 
-void ringbuffer_add(ringbuffer_t *rb, char *buf, int n)
+void ringbuffer_add(ringbuffer_t *restrict rb, const char *buf, unsigned n)
 {
-    for (int i = 0; i < n; i++) {
+    for (unsigned i = 0; i < n; i++) {
         ringbuffer_add_one(rb, buf[i]);
     }
 }
 
-void ringbuffer_add_one(ringbuffer_t *rb, char c)
+void ringbuffer_add_one(ringbuffer_t *restrict rb, char c)
 {
     if (rb->avail == rb->size) {
         ringbuffer_get_one(rb);
@@ -48,7 +48,7 @@ void ringbuffer_add_one(ringbuffer_t *rb, char c)
     rb->avail++;
 }
 
-int ringbuffer_get_one(ringbuffer_t *rb)
+int ringbuffer_get_one(ringbuffer_t *restrict rb)
 {
     if (rb->avail == 0) {
         return -1;
@@ -65,9 +65,9 @@ int ringbuffer_get_one(ringbuffer_t *rb)
     return c;
 }
 
-int ringbuffer_get(ringbuffer_t *rb, char *buf, int n)
+unsigned ringbuffer_get(ringbuffer_t *restrict rb, char *buf, unsigned n)
 {
-    int count = 0;
+    unsigned count = 0;
 
     while (rb->avail && (count < n)) {
         buf[count++] = ringbuffer_get_one(rb);
@@ -76,18 +76,14 @@ int ringbuffer_get(ringbuffer_t *rb, char *buf, int n)
     return count;
 }
 
-int ringbuffer_peek_one(ringbuffer_t *rb)
+int ringbuffer_peek_one(const ringbuffer_t *restrict rb_)
 {
-    unsigned end = rb->end, start = rb->start, avail = rb->avail;
-    int result = ringbuffer_get_one(rb);
-    rb->end = end, rb->start = start, rb->avail = avail;
-    return result;
+    ringbuffer_t rb = *rb_;
+    return ringbuffer_get_one(&rb);
 }
 
-int ringbuffer_peek(ringbuffer_t *rb, char *buf, unsigned n)
+unsigned ringbuffer_peek(const ringbuffer_t *restrict rb_, char *buf, unsigned n)
 {
-    unsigned end = rb->end, start = rb->start, avail = rb->avail;
-    int result = ringbuffer_get(rb, buf, n);
-    rb->end = end, rb->start = start, rb->avail = avail;
-    return result;
+    ringbuffer_t rb = *rb_;
+    return ringbuffer_get(&rb, buf, n);
 }
-- 
GitLab