From a515dfb3b8f8e999362db7a6b52b3104c03b750a Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Fri, 1 Nov 2024 23:46:08 -0400 Subject: Import quark sources Signed-off-by: Ian Moffett --- include/list.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 include/list.h (limited to 'include/list.h') diff --git a/include/list.h b/include/list.h new file mode 100644 index 0000000..0364f7c --- /dev/null +++ b/include/list.h @@ -0,0 +1,58 @@ +/* + * 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 */ -- cgit v1.2.3