summaryrefslogtreecommitdiff
path: root/kernel/lib
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-04-15 23:47:25 +0200
committerAnton Kling <anton@kling.gg>2024-04-16 00:02:23 +0200
commitefe10908766d96974c008a6a8f124916fdca7a14 (patch)
tree69f90dc3c01d32d63a38ab50f61268b1ac06dc97 /kernel/lib
parent62ba2a9ab01e5f2a9b1cd325af8d3112702b7713 (diff)
Kernel: Improve "out of memory" error handling
It can now boot with fairly low memory while not crashing and instead gracefully failing the operation. Userland still does not properly handle errors.
Diffstat (limited to 'kernel/lib')
-rw-r--r--kernel/lib/list.c26
-rw-r--r--kernel/lib/list.h2
2 files changed, 19 insertions, 9 deletions
diff --git a/kernel/lib/list.c b/kernel/lib/list.c
index c663230..e4d4b23 100644
--- a/kernel/lib/list.c
+++ b/kernel/lib/list.c
@@ -2,15 +2,13 @@
#include <kmalloc.h>
#include <lib/list.h>
-int list_init(struct list *list) {
- // TODO: Make it dynamic
+void list_init(struct list *list) {
list->capacity = 5;
+ list->tail_index = -1;
list->entries = kmalloc(sizeof(void *) * list->capacity);
if (!list->entries) {
- return 0;
+ list->capacity = 0;
}
- list->tail_index = -1;
- return 1;
}
void list_reset(struct list *list) {
@@ -24,15 +22,24 @@ int list_clone(struct list *in, struct list *out) {
if (!list_get(in, i, &output)) {
break;
}
- list_add(out, output, NULL);
+ if (!list_add(out, output, NULL)) {
+ list_free(out);
+ return 0;
+ }
}
return 1;
}
int list_add(struct list *list, void *entry, int *index) {
if (list->tail_index + 1 >= list->capacity) {
- list->capacity += 25;
- list->entries = krealloc(list->entries, sizeof(void *) * list->capacity);
+ int new_capacity = list->capacity + 25;
+ void *new_allocation =
+ krealloc(list->entries, sizeof(void *) * new_capacity);
+ if (!new_allocation) {
+ return 0;
+ }
+ list->entries = new_allocation;
+ list->capacity = new_capacity;
}
list->tail_index++;
list->entries[list->tail_index] = entry;
@@ -60,5 +67,8 @@ int list_get(const struct list *list, int index, void **out) {
}
void list_free(struct list *list) {
+ list->capacity = 0;
+ list->tail_index = -1;
kfree(list->entries);
+ list->entries = NULL;
}
diff --git a/kernel/lib/list.h b/kernel/lib/list.h
index b1f0127..2d1fa0f 100644
--- a/kernel/lib/list.h
+++ b/kernel/lib/list.h
@@ -6,7 +6,7 @@ struct list {
int tail_index;
};
-int list_init(struct list *list);
+void list_init(struct list *list);
void list_reset(struct list *list);
void list_free(struct list *list);
int list_clone(struct list *in, struct list *out);