summaryrefslogtreecommitdiff
path: root/include/list.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/list.h')
-rw-r--r--include/list.h58
1 files changed, 58 insertions, 0 deletions
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 <stddef.h>
+
+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 */