summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Braun <rbraun@sceen.net>2017-05-28 17:05:16 +0200
committerRichard Braun <rbraun@sceen.net>2017-05-28 17:11:49 +0200
commitdf6e8f87ad6800ecb348f11b01a42db48237b371 (patch)
tree50432b64e6b28aca10cef8263b0455325986e4d2
parenta72801f115413cd3eab46857545ab527f31abb2b (diff)
kern/cbuf: new module
-rw-r--r--Makefrag.am2
-rw-r--r--kern/cbuf.c77
-rw-r--r--kern/cbuf.h101
3 files changed, 180 insertions, 0 deletions
diff --git a/Makefrag.am b/Makefrag.am
index 7f74690b..243c2245 100644
--- a/Makefrag.am
+++ b/Makefrag.am
@@ -15,6 +15,8 @@ x15_SOURCES += \
kern/bitmap.c \
kern/bitmap.h \
kern/bitmap_i.h \
+ kern/cbuf.c \
+ kern/cbuf.h \
kern/condition.c \
kern/condition.h \
kern/condition_types.h \
diff --git a/kern/cbuf.c b/kern/cbuf.c
new file mode 100644
index 00000000..17369afe
--- /dev/null
+++ b/kern/cbuf.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2015 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <kern/assert.h>
+#include <kern/cbuf.h>
+#include <kern/error.h>
+#include <kern/macros.h>
+
+/* Negative close to 0 so that an overflow occurs early */
+#define CBUF_INIT_INDEX ((unsigned long)-500)
+
+void
+cbuf_init(struct cbuf *cbuf, char *buf, unsigned long capacity)
+{
+ assert(ISP2(capacity));
+
+ cbuf->buf = buf;
+ cbuf->capacity = capacity;
+ cbuf->start = CBUF_INIT_INDEX;
+ cbuf->end = cbuf->start;
+}
+
+static unsigned long
+cbuf_index(const struct cbuf *cbuf, unsigned long abs_index)
+{
+ return abs_index & (cbuf->capacity - 1);
+}
+
+void
+cbuf_push(struct cbuf *cbuf, char byte)
+{
+ cbuf->buf[cbuf_index(cbuf, cbuf->end)] = byte;
+ cbuf->end++;
+
+ /* Mind integer overflows */
+ if (cbuf_size(cbuf) > cbuf->capacity) {
+ cbuf->start = cbuf->end - cbuf->capacity;
+ }
+}
+
+int
+cbuf_pop(struct cbuf *cbuf, char *bytep)
+{
+ if (cbuf_size(cbuf) == 0) {
+ return ERROR_AGAIN;
+ }
+
+ *bytep = cbuf->buf[cbuf_index(cbuf, cbuf->start)];
+ cbuf->start++;
+ return 0;
+}
+
+int
+cbuf_read(const struct cbuf *cbuf, unsigned long index, char *bytep)
+{
+ /* Mind integer overflows */
+ if ((cbuf->end - index - 1) >= cbuf_size(cbuf)) {
+ return ERROR_INVAL;
+ }
+
+ *bytep = cbuf->buf[cbuf_index(cbuf, index)];
+ return 0;
+}
diff --git a/kern/cbuf.h b/kern/cbuf.h
new file mode 100644
index 00000000..3ea38418
--- /dev/null
+++ b/kern/cbuf.h
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2015 Richard Braun.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ *
+ * Circular character buffer.
+ */
+
+#ifndef _KERN_CBUF_H
+#define _KERN_CBUF_H
+
+/*
+ * Circular buffer descriptor.
+ *
+ * The buffer capacity must be a power-of-two. Indexes are absolute values
+ * which can overflow. Their difference cannot exceed the capacity.
+ */
+struct cbuf {
+ char *buf;
+ unsigned long capacity;
+ unsigned long start;
+ unsigned long end;
+};
+
+static inline unsigned long
+cbuf_capacity(const struct cbuf *cbuf)
+{
+ return cbuf->capacity;
+}
+
+static inline unsigned long
+cbuf_start(const struct cbuf *cbuf)
+{
+ return cbuf->start;
+}
+
+static inline unsigned long
+cbuf_end(const struct cbuf *cbuf)
+{
+ return cbuf->end;
+}
+
+static inline unsigned long
+cbuf_size(const struct cbuf *cbuf)
+{
+ return cbuf->end - cbuf->start;
+}
+
+static inline void
+cbuf_clear(struct cbuf *cbuf)
+{
+ cbuf->start = cbuf->end;
+}
+
+/*
+ * Initialize a circular buffer.
+ *
+ * The descriptor is set to use the given buffer for storage. Capacity
+ * must be a power-of-two.
+ */
+void cbuf_init(struct cbuf *cbuf, char *buf, unsigned long capacity);
+
+/*
+ * Append a byte to a circular buffer.
+ *
+ * The end index is incremented. If the buffer is full, the oldest byte
+ * is overwritten and the start index is updated accordingly.
+ */
+void cbuf_push(struct cbuf *cbuf, char byte);
+
+/*
+ * Read a byte from a circular buffer.
+ *
+ * If the buffer is empty, ERROR_AGAIN is returned. Otherwise, the oldest
+ * byte is stored at the bytep address, the start index is incremented,
+ * and 0 is returned.
+ */
+int cbuf_pop(struct cbuf *cbuf, char *bytep);
+
+/*
+ * Read a byte at a specific location.
+ *
+ * If the given index is outside buffer boundaries, ERROR_INVAL is returned.
+ * Otherwise the byte is stored at the bytep address and 0 is returned.
+ * The buffer isn't changed by this operation.
+ */
+int cbuf_read(const struct cbuf *cbuf, unsigned long index, char *bytep);
+
+#endif /* _KERN_CBUF_H */