From 6245b43b26e47ece1927d28246611488c2f36e67 Mon Sep 17 00:00:00 2001 From: Marti Bolivar Date: Thu, 17 Mar 2011 09:10:28 -0400 Subject: Fixing inefficient rb_safe_remove() implementation; thanks, geoffreymbrown! --- libmaple/ring_buffer.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'libmaple') diff --git a/libmaple/ring_buffer.h b/libmaple/ring_buffer.h index 6e9b250..3b5d4e7 100644 --- a/libmaple/ring_buffer.h +++ b/libmaple/ring_buffer.h @@ -65,6 +65,11 @@ static inline int rb_is_full(ring_buffer *rb) { (rb->tail == rb->size && rb->head == 0); } +/** Return true if and only if the ring buffer is empty. */ +static inline int rb_is_empty(ring_buffer *rb) { + return rb->head == rb->tail; +} + /** Append element onto the end of the ring buffer. */ static inline void rb_insert(ring_buffer *rb, uint8 element) { rb->buf[rb->tail] = element; @@ -83,10 +88,7 @@ static inline uint8 rb_remove(ring_buffer *rb) { * If it is empty, does nothing and returns a negative value. */ static inline int16 rb_safe_remove(ring_buffer *rb) { - if (rb_full_count(rb) == 0) { - return -1; - } - return rb_remove(rb); + return rb_is_empty(rb) ? -1 : rb_remove(rb); } /** -- cgit v1.2.3