diff options
author | Michael Hope <michael.hope@linaro.org> | 2010-10-10 20:57:06 +1300 |
---|---|---|
committer | Michael Hope <michael.hope@linaro.org> | 2010-10-10 20:57:06 +1300 |
commit | 0caf14387c4194ed7c892a592e663803985f3751 (patch) | |
tree | 41c0254ab092fdac4e9a8800d58955c300e78ac0 | |
parent | c925e6c219e6ae29ac724ff7c2dc17872d2a64c3 (diff) | |
download | librambutan-0caf14387c4194ed7c892a592e663803985f3751.tar.gz librambutan-0caf14387c4194ed7c892a592e663803985f3751.zip |
Reduced the use of volatiles to speed up rb_insert() and rb_remove().
Added support for non-power-of-two ring buffers.
-rw-r--r-- | libmaple/ring_buffer.h | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/libmaple/ring_buffer.h b/libmaple/ring_buffer.h index b4c136f..43e0b28 100644 --- a/libmaple/ring_buffer.h +++ b/libmaple/ring_buffer.h @@ -17,43 +17,43 @@ typedef struct ring_buffer { /** Buffer items are stored into */ volatile uint8 *buf; /** Index of the next item to remove */ - volatile uint16 head; + uint16 head; /** Index where the next item will get inserted */ - volatile uint16 tail; - /** Buffer capacity */ + uint16 tail; + /** Buffer capacity minus one */ uint16 size; } ring_buffer; /** Initialise a ring buffer. - - @param rb instance to initialise - @param size number of items in the buffer - @param buf buffer to store items into + * + * @param rb instance to initialise + * @param size number of items in the buffer + * @param buf buffer to store items into */ -static inline void rb_init(ring_buffer *rb, uint8 size, uint8 *buf) { - ASSERT(IS_POWER_OF_TWO(size)); +static inline void rb_init(ring_buffer *rb, uint16 size, uint8 *buf) { rb->head = 0; rb->tail = 0; - rb->size = size; + rb->size = size - 1; rb->buf = buf; } /** Append an item onto the end of the ring buffer */ static inline void rb_insert(ring_buffer *rb, uint8 element) { - rb->buf[(rb->tail)++] = element; - rb->tail &= (rb->size - 1); + rb->buf[rb->tail] = element; + rb->tail = (rb->tail == rb->size) ? 0 : rb->tail + 1; } /** Remove and return the first item from the ring buffer */ static inline uint8 rb_remove(ring_buffer *rb) { - uint8 ch = rb->buf[rb->head++]; - rb->head &= (rb->size - 1); + uint8 ch = rb->buf[rb->head]; + rb->head = (rb->head == rb->size) ? 0 : rb->head + 1; return ch; } static inline uint32 rb_full_count(ring_buffer *rb) { /* PENDING: Broken */ - return rb->tail - rb->head; + volatile ring_buffer *arb = rb; + return arb->tail - arb->head; } /** Discard all items from the buffer */ |