summaryrefslogtreecommitdiff
path: root/kernel/lib
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-04-29 18:00:20 +0200
committerAnton Kling <anton@kling.gg>2024-04-29 18:00:20 +0200
commit7d2ab3a71f4bda9d8ee997764d98b29e13a902c5 (patch)
tree6dcdc7a674a8047a2dba3e95d3be91b4e3161bd5 /kernel/lib
parentd60fa1fc01e600c4a163bbe82fab3f1986cb1476 (diff)
Kernel/Socket: Move sockets back to being file descriptors
Diffstat (limited to 'kernel/lib')
-rw-r--r--kernel/lib/ringbuffer.c17
-rw-r--r--kernel/lib/ringbuffer.h3
-rw-r--r--kernel/lib/stack.c4
-rw-r--r--kernel/lib/stack.h1
4 files changed, 25 insertions, 0 deletions
diff --git a/kernel/lib/ringbuffer.c b/kernel/lib/ringbuffer.c
index 66dd17e..9caf555 100644
--- a/kernel/lib/ringbuffer.c
+++ b/kernel/lib/ringbuffer.c
@@ -16,6 +16,23 @@ int ringbuffer_init(struct ringbuffer *rb, u32 buffer_size) {
return 1;
}
+u32 ringbuffer_used(const struct ringbuffer *rb) {
+ if (rb->write_ptr < rb->read_ptr) {
+ u32 c = rb->buffer_size - rb->read_ptr;
+ c += rb->write_ptr;
+ return c;
+ }
+ return rb->write_ptr - rb->read_ptr;
+}
+
+u32 ringbuffer_capacity(const struct ringbuffer *rb) {
+ return rb->buffer_size - 1;
+}
+
+u32 ringbuffer_unused(const struct ringbuffer *rb) {
+ return ringbuffer_capacity(rb) - ringbuffer_used(rb);
+}
+
u32 ringbuffer_write(struct ringbuffer *rb, const u8 *buffer, u32 len) {
const u32 orig_len = len;
for (; len > 0;) {
diff --git a/kernel/lib/ringbuffer.h b/kernel/lib/ringbuffer.h
index 3ebc507..d6e699a 100644
--- a/kernel/lib/ringbuffer.h
+++ b/kernel/lib/ringbuffer.h
@@ -14,6 +14,9 @@ u32 ringbuffer_write(struct ringbuffer *rb, const u8 *buffer, u32 len);
u32 ringbuffer_read(struct ringbuffer *rb, u8 *buffer, u32 len);
int ringbuffer_isempty(const struct ringbuffer *rb);
void ringbuffer_free(struct ringbuffer *rb);
+u32 ringbuffer_used(const struct ringbuffer *rb);
+u32 ringbuffer_capacity(const struct ringbuffer *rb);
+u32 ringbuffer_unused(const struct ringbuffer *rb);
#ifdef KERNEL_TEST
void ringbuffer_test(void);
#endif // KERNEL_TEST
diff --git a/kernel/lib/stack.c b/kernel/lib/stack.c
index 7283b35..7c44193 100644
--- a/kernel/lib/stack.c
+++ b/kernel/lib/stack.c
@@ -7,6 +7,10 @@ void stack_init(struct stack *s) {
s->head = NULL;
}
+int stack_isempty(const struct stack *s) {
+ return (NULL == s->head);
+}
+
// 1 = Success
// 0 = Failure
int stack_push(struct stack *s, void *data) {
diff --git a/kernel/lib/stack.h b/kernel/lib/stack.h
index f47fe66..09fff66 100644
--- a/kernel/lib/stack.h
+++ b/kernel/lib/stack.h
@@ -13,6 +13,7 @@ struct stack {
};
void stack_init(struct stack *s);
+int stack_isempty(const struct stack *s);
int stack_push(struct stack *s, void *data);
void *stack_pop(struct stack *s);
#endif