/* * Tiny doubly-linked list implementation. * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team. * Provided under the BSD 3-Clause license. */ #ifndef _LIST_H #define _LIST_H #include struct list_entry { struct list_entry *prev; struct list_entry *next; }; struct list { struct list_entry *tail; struct list_entry *head; size_t length; }; static inline void list_remove(struct list_entry *entry) { entry->prev->next = entry->next; entry->next->prev = entry->prev; } static inline void list_append(struct list *list, struct list_entry *entry) { entry->prev = list->tail; entry->next = (struct list_entry*)list; entry->prev->next = entry; list->tail = entry; list->length++; } static inline void list_prepend(struct list *list, struct list_entry *entry) { entry->next = list->head; entry->prev = (struct list_entry*)list; entry->next->prev = entry; list->head = entry; list->length++; } static inline void list_init(struct list *list) { list->tail = (struct list_entry*)list; list->head = (struct list_entry*)list; list->length = 0; } #endif /* !_LIST_H */