aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple
diff options
context:
space:
mode:
authorMarti Bolivar <mbolivar@leaflabs.com>2011-03-17 09:10:28 -0400
committerMarti Bolivar <mbolivar@leaflabs.com>2011-03-17 09:10:28 -0400
commit6245b43b26e47ece1927d28246611488c2f36e67 (patch)
tree352b6aefa9a61f7861c2622ec9083baa503a95b0 /libmaple
parent7a7dff5186fa1b96f15c82672fc94432196d7eb5 (diff)
downloadlibrambutan-6245b43b26e47ece1927d28246611488c2f36e67.tar.gz
librambutan-6245b43b26e47ece1927d28246611488c2f36e67.zip
Fixing inefficient rb_safe_remove() implementation; thanks, geoffreymbrown!
Diffstat (limited to 'libmaple')
-rw-r--r--libmaple/ring_buffer.h10
1 files changed, 6 insertions, 4 deletions
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);
}
/**