summaryrefslogtreecommitdiff
path: root/kernel/lib/list.c
diff options
context:
space:
mode:
Diffstat (limited to 'kernel/lib/list.c')
-rw-r--r--kernel/lib/list.c26
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;
}