MOS Source Code
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include <mos/types.h>
6
14typedef struct list_node list_node_t;
15
23typedef list_node_t list_head;
24
27{
28 list_node_t *prev;
29 list_node_t *next;
30};
31
35#define as_linked_list list_node_t list_node
36
37// clang-format off
38#define LIST_HEAD_INIT(container) { .prev = &(container), .next = &(container) }
39// clang-format on
40
41#define LIST_NODE_INIT(container) LIST_HEAD_INIT(container.list_node)
42
46#define list_entry(node, type) container_of((node), type, list_node)
47
51#define list_prev_entry(item, type) list_entry(list_node(item)->prev, type)
52
56#define list_next_entry(item, type) list_entry(list_node(item)->next, type)
57
62#define list_node_next_entry(node, type) container_of((node)->next, type, list_node)
63
68#define list_node(element) (&((element)->list_node))
69
70#define list_prepend(element, item) list_node_prepend(list_node(element), list_node(item))
71#define list_append(element, item) list_node_append(list_node(element), list_node(item))
72#define list_insert_before(element, item) list_node_insert_before(list_node(element), list_node(item))
73#define list_insert_after(element, item) list_node_insert_after(list_node(element), list_node(item))
74#define list_remove(element) list_node_remove(list_node(element))
75
83#define list_foreach(t, v, h) \
84 for (__typeof(list_entry(&(h), t)) v = list_entry((h).next, t), __next = list_next_entry(v, t); list_node(v) != &(h); v = __next, __next = list_next_entry(v, t))
85
86#define list_foreach_reverse(t, v, h) \
87 for (__typeof(list_entry(&(h), t)) v = list_entry((h).prev, t), __next = list_prev_entry(v, t); list_node(v) != &(h); v = __next, __next = list_prev_entry(v, t))
88
89#define list_node_foreach(v, h) for (list_node_t *v = (h)->next, *__next = v->next; v != (h); v = __next, __next = v->next)
90#define list_node_foreach_reverse(v, h) for (list_node_t *v = (h)->prev, *__next = v->prev; v != (h); v = __next, __next = v->prev)
91
92#define list_headless_foreach(t, v, h) \
93 for (__typeof(&(h)) v = &(h), __next = list_next_entry(v, t), __head = NULL; (v) != __head; \
94 v = __next, __next = list_next_entry(v, t), __head = __head == NULL ? &(h) : __head)
95
96#define list_headless_foreach_reverse(t, v, h) \
97 for (__typeof(h) v = (h), __next = list_prev_entry(v, t), __head = NULL; (v) != __head; \
98 v = __next, __next = list_prev_entry(v, t), __head = __head == NULL ? (h) : __head)
99
100MOSAPI void linked_list_init(list_node_t *head_node);
101MOSAPI bool list_is_empty(const list_node_t *head);
102MOSAPI void list_node_remove(list_node_t *link);
103
104MOSAPI list_node_t *list_node_pop(list_node_t *head);
105MOSAPI void list_node_prepend(list_node_t *head, list_node_t *item);
106MOSAPI void list_node_append(list_node_t *head, list_node_t *item);
107MOSAPI void list_node_insert_before(list_node_t *element, list_node_t *item);
108MOSAPI void list_node_insert_after(list_node_t *element, list_node_t *item);
109
MOSAPI void linked_list_init(list_node_t *head_node)
Initialise a circular double linked list.
Definition list.c:15
MOSAPI list_node_t * list_node_pop(list_node_t *head)
Definition list.c:56
MOSAPI void list_node_append(list_node_t *head, list_node_t *item)
Definition list.c:68
MOSAPI void list_node_insert_before(list_node_t *element, list_node_t *item)
Definition list.c:74
MOSAPI void list_node_prepend(list_node_t *head, list_node_t *item)
Definition list.c:63
list_node_t list_head
A linked list head.
Definition list.h:23
MOSAPI bool list_is_empty(const list_node_t *head)
Definition list.c:21
MOSAPI void list_node_remove(list_node_t *link)
Definition list.c:26
MOSAPI void list_node_insert_after(list_node_t *element, list_node_t *item)
Definition list.c:79
#define MOSAPI
Definition mos_global.h:102
A node in a linked list.
Definition list.h:27
list_node_t * prev
Definition list.h:28
list_node_t * next
Definition list.h:29