aboutsummaryrefslogtreecommitdiffstats
path: root/libmaple/ring_buffer.h
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2010-08-31 17:39:46 -0400
committerbnewbold <bnewbold@robocracy.org>2010-08-31 17:39:46 -0400
commit02d7b08f0497096f21e41922e0efb54c4ef33bab (patch)
treea7e04293efcba70f37cffcd03c0fcc4c0be7858a /libmaple/ring_buffer.h
parentb2dd49c3141d8a21a4e7c7ef51dee7329f847c30 (diff)
parente03d58f4dab4176514924baa3a1ff430bf5819b8 (diff)
downloadlibrambutan-02d7b08f0497096f21e41922e0efb54c4ef33bab.tar.gz
librambutan-02d7b08f0497096f21e41922e0efb54c4ef33bab.zip
Merge maple-native changes into portable
This compiles for both maple and maple_native but is untested.
Diffstat (limited to 'libmaple/ring_buffer.h')
-rw-r--r--libmaple/ring_buffer.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/libmaple/ring_buffer.h b/libmaple/ring_buffer.h
new file mode 100644
index 0000000..95b9dd8
--- /dev/null
+++ b/libmaple/ring_buffer.h
@@ -0,0 +1,57 @@
+/**
+ * @brief simple circular buffer
+ */
+
+#ifndef _RING_BUFFER_H_
+#define _RING_BUFFER_H_
+
+#ifdef __cplusplus
+extern "C"{
+#endif
+
+/* The buffer is empty when head == tail.
+ * The buffer is full when the head is one byte in front of the tail
+ * The total buffer size must be a power of two
+ * One byte is left free to distinguish empty from full */
+typedef struct ring_buffer {
+ uint32 head;
+ uint32 tail;
+ uint8 size;
+ uint8 *buf;
+} ring_buffer;
+
+static inline void rb_init(ring_buffer *rb, uint8 size, uint8 *buf) {
+ ASSERT(IS_POWER_OF_TWO(size));
+ rb->head = 0;
+ rb->tail = 0;
+ rb->size = size;
+ rb->buf = buf;
+}
+
+static inline void rb_insert(ring_buffer *rb, uint8 element) {
+ rb->buf[(rb->tail)++] = element;
+ rb->tail &= (rb->size - 1);
+}
+
+static inline uint8 rb_remove(ring_buffer *rb) {
+ uint8 ch = rb->buf[rb->head++];
+ rb->head &= (rb->size - 1);
+ return ch;
+}
+
+static inline uint32 rb_full_count(ring_buffer *rb) {
+ return rb->tail - rb->head;
+}
+
+static inline void rb_reset(ring_buffer *rb) {
+ rb->tail = rb->head;
+}
+
+#ifdef __cplusplus
+} // extern "C"
+#endif
+
+
+
+#endif
+