diff --git a/sys/include/ringbuffer.h b/sys/include/ringbuffer.h index 93522d08f052cdf9db96713c44a3470dbe12fa5b..23755c122921e53e554183e87c4e0ac1e7c57568 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 fc7055d8ff4e70e419b973932406f1633ae4c28d..b8ee266f89f8d21db9377a614eac19a11a81c634 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); }