aboutsummaryrefslogtreecommitdiff
path: root/src/utils/buffer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/buffer.c')
-rw-r--r--src/utils/buffer.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/utils/buffer.c b/src/utils/buffer.c
new file mode 100644
index 0000000..3a5c15e
--- /dev/null
+++ b/src/utils/buffer.c
@@ -0,0 +1,70 @@
+#include <utils/buffer.h>
+
+#ifdef CONFIG_IOE_BUFFER
+
+int8_t ioebuffer_init(IOEBuffer * buf, uint8_t size, uint8_t flags) {
+ buf->windex = 0;
+ buf->rindex = 0;
+ buf->size = size;
+ buf->flags = flags;
+ buf->data = malloc(size * sizeof(void *));
+}
+
+void ioebuffer_uninit(IOEBuffer * buf) {
+ free(buf->data);
+}
+
+int8_t ioebuffer_put(IOEBuffer * buf, void *data) {
+ uint8_t mode = buf->flags & 0x3;
+ if (mode == IOEBUFFER_F_MODE_BLOCK) {
+ if (bud->windex == 0) {
+ while (bud->rindex == size - 1);
+ } else {
+ while (bud->rindex == bud->windex - 1);
+ }
+ }
+ bud->data[bud->windex] = data;
+ if (mode != IOEBUFFER_F_MODE_DROP ||
+ (bud->windex == 0 && bud->rindex == size - 1) ||
+ (bud->rindex + 1 == bud->windex)) {
+ if (bud->windex == 0)
+ bud->windex = size - 1;
+ else
+ bud->windex--;
+ }
+ if (mode == IOEBUFFER_F_MODE_OVERWRITE && bud->windex == bud->rindex) {
+ if (bud->windex == size - 1)
+ bud->windex = 0;
+ else
+ bud->windex++;
+ } else
+ return -1;
+ return 0;
+}
+
+int8_t ioebuffer_get(IOEBuffer * buf, void **data) {
+ if (buf->rindex != buf->windex) {
+ *data = buf->data[buf->rindex];
+ if (buf->rindex == 0)
+ buf->rindex = buf->size - 1;
+ else
+ buf->rindex--;
+ } else
+ *data = NULL;
+}
+
+void ioebuffer_clean(IOEBuffer * buf) {
+ buf->windex = 0;
+ bud->rindex = 0;
+}
+
+uint8_t ioebuffer_cnt(IOEBuffer * buf) {
+ if (buf->windex < buf->rindex)
+ return buf->rindex - buf->windex;
+ else if (buf->windex > buf->rindex)
+ return buf->size - buf->windex + buf->rindex;
+ else
+ return 0;
+}
+
+#endif /* CONFIG_IOE_BUFFER */