aboutsummaryrefslogtreecommitdiff
path: root/kernel/collection/include/collection/rbuffer.h
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/collection/include/collection/rbuffer.h')
-rw-r--r--kernel/collection/include/collection/rbuffer.h84
1 files changed, 31 insertions, 53 deletions
diff --git a/kernel/collection/include/collection/rbuffer.h b/kernel/collection/include/collection/rbuffer.h
index 01d5418..dceaeb0 100644
--- a/kernel/collection/include/collection/rbuffer.h
+++ b/kernel/collection/include/collection/rbuffer.h
@@ -4,67 +4,45 @@
#include <stddef.h>
struct rbuffer_t {
- char* address_begin;
- char* address_end;
- char* volatile head;
- char* volatile tail;
+ char* address_begin;
+ char* address_end;
+ char* volatile head;
+ char* volatile tail;
};
-#define RBUFFER_ARRAY_INIT(array, length) \
- { \
- .address_begin = array, \
- .address_end = array + length, \
- .head=array, \
- .tail=array \
- }
+#define RBUFFER_INIT(array, length) \
+ { \
+ .address_begin = array, \
+ .address_end = array + length, \
+ .head = array, \
+ .tail = array \
+ }
+
+#define INIT_RBUFFER(buffer, array, length) \
+ (buffer)->address_begin = (array); \
+ (buffer)->address_end = (array) + (length); \
+ (buffer)->head = (array); \
+ (buffer)->tail = (array)
static inline int rbuffer_empty(struct rbuffer_t* const rb) {
- return rb->head == rb->tail;
+ return rb->head == rb->tail;
}
-#define __rbuffer_overlap(rb, ptr) \
- if (ptr >= rb->address_end) ptr = rb->address_begin
-
-static inline int rbuffer_write_char(struct rbuffer_t* const rb, char data) {
- char* next_head = rb->head + 1;
- __rbuffer_overlap(rb, next_head);
- if (rb->tail == next_head) return 0;
- *rb->head = data;
- rb->head = next_head;
- return 1;
+static inline int rbuffer_write(struct rbuffer_t* const rb, char data) {
+ char* next_head = rb->head + 1;
+ if(next_head >= rb->address_end) next_head = rb->address_begin;
+ if (rb->tail == next_head) return -1;
+ *rb->head = data;
+ rb->head = next_head;
+ return 0;
}
-static inline int rbuffer_read_char(struct rbuffer_t* const rb, char* const data) {
- if (rbuffer_empty(rb)) return 0;
- *data = *rb->tail;
- rb->tail += 1;
- __rbuffer_overlap(rb, rb->tail);
- return 1;
+static inline int rbuffer_read(struct rbuffer_t* const rb, char* const data) {
+ if (rbuffer_empty(rb)) return -1;
+ *data = *rb->tail;
+ rb->tail += 1;
+ if(rb->tail >= rb->address_end) rb->tail = rb->address_begin;
+ return 0;
}
-static inline size_t rbuffer_write(struct rbuffer_t* const rb, const char* const data, size_t size) {
- size_t wrote = 0;
- for (size_t i = 0; i < size; ++i) {
- if (rbuffer_write_char(rb, data[i])) {
- wrote += 1;
- } else {
- return wrote;
- }
- }
- return wrote;
-}
-
-static inline size_t rbuffer_read(struct rbuffer_t* const rb, char* const data, size_t size) {
- size_t read = 0;
- for (size_t i = 0; i < size; ++i) {
- if (rbuffer_read_char(rb, &data[i])) {
- read += 1;
- } else {
- return read;
- }
- }
- return read;
-}
-
-
#endif