From efe10908766d96974c008a6a8f124916fdca7a14 Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Mon, 15 Apr 2024 23:47:25 +0200 Subject: 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. --- kernel/lib/list.c | 26 ++++++++++++++++++-------- kernel/lib/list.h | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) (limited to 'kernel/lib') 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 #include -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); -- cgit v1.2.3