Sun, 10 Aug 2025 20:51:10 +0200
3 files changed,
77 insertions(+),
0 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -94,6 +94,7 @@ list(APPEND COMMON_SRC
common/memory.c common/string.c common/fat12.c + common/list.c ) list(APPEND BOOT_SRC
A
common/list.c
@@ -0,0 +1,37 @@
+#include "list.h" + +void list_append(struct list *list, struct list_node *node) { + struct list_node *new_stl; + + if (list == NULL || node == NULL) { return; } + + new_stl = list->last; + list->last = node; + + node->next = NULL; + node->prev = new_stl; + + if (new_stl == NULL) { + list->first = node; + } else { + new_stl->next = node; + } +} + +void list_prepend(struct list *list, struct list_node *node) { + struct list_node *new_second; + + if (list == NULL || node == NULL) { return; } + + new_second = list->first; + list->first = node; + + node->prev = NULL; + node->next = new_second; + + if (new_second == NULL) { + list->last = node; + } else { + new_second->prev = node; + } +}
A
common/list.h
@@ -0,0 +1,39 @@
+#ifndef INCLUDE_LIST_H +#define INCLUDE_LIST_H + +#include <stddef.h> + +#include "utils.h" + +struct list_node { + struct list_node *next; + struct list_node *prev; +}; + +struct list { + struct list_node *first; + struct list_node *last; +}; + +#define LIST_INIT() { NULL, NULL } + +void list_append(struct list *list, struct list_node *node); +void list_prepend(struct list *list, struct list_node *node); + +static inline struct list_node *list_next(struct list_node *node) { + if (node == NULL) { + return NULL; + } + + return node->next; +} + +#define LIST_FOR_EACH(listptr, type, nodeptr) \ + for ((nodeptr) = (listptr)->first; (nodeptr) != NULL; (nodeptr) = (nodeptr)->next) +#define LIST_FOR_EACH_ENTRY(listptr, type, member, entry) \ + for (struct list_node *nodeptr = (listptr)->first; \ + nodeptr != NULL \ + && ((entry) = containerof(nodeptr, type, member)) != NULL; \ + nodeptr = nodeptr->next) + +#endif