diff options
author | Anton Kling <anton@kling.gg> | 2024-04-15 23:47:25 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-04-16 00:02:23 +0200 |
commit | efe10908766d96974c008a6a8f124916fdca7a14 (patch) | |
tree | 69f90dc3c01d32d63a38ab50f61268b1ac06dc97 /kernel/lib/list.c | |
parent | 62ba2a9ab01e5f2a9b1cd325af8d3112702b7713 (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/list.c')
-rw-r--r-- | kernel/lib/list.c | 26 |
1 files changed, 18 insertions, 8 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; } |