AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
list.h
Go to the documentation of this file.
1#ifndef _DS_LIST_H
2#define _DS_LIST_H
3
4#include <stddef.h>
5#include <core/types.h>
6
7/*
8 * A doubly linked list whose links are inside the things it lists. A
9 * structure that can be on a list has a `struct list_node` member for it,
10 * and list_entry() gets the structure back from its node. Putting
11 * something on a list, or taking it off, allocates nothing, so it can't
12 * fail. BSD's TAILQ and Linux's list_head work the same way.
13 *
14 * struct thread {
15 * ...
16 * struct list_node run_link; // on the ready list, or a waitq
17 * };
18 *
19 * list_append(&ready, &thread->run_link);
20 * struct thread *t = list_entry(list_pop(&ready), struct thread, run_link);
21 *
22 * A node is on at most one list at a time, and its `list` field says
23 * which one (NULL when it's on none). A structure that can be on several
24 * lists at once has one node for each. A list or a node that is all
25 * zeros is empty, or on no list, so `{0}` initializes both.
26 */
27
28struct list;
29
31struct list_node {
32 struct list_node *prev, *next;
33 struct list *list; /* the list it's on, or NULL */
34};
35
37struct list {
38 struct list_node *head, *tail;
39 size_t count;
40};
41
42/* The structure of type `type` whose member `member` is the node `node`. */
43#define list_entry(node, type, member) \
44 ((type *) ((char *) (node) - offsetof(type, member)))
45
46/* Loops over each node `n` of list `l`. The loop body may remove `n`
47 * from the list: the next node is taken before the body runs. */
48#define list_for(n, l) \
49 for (struct list_node *(n) = (l)->head, *__next_##n = (n) ? (n)->next : NULL; \
50 (n); (n) = __next_##n, __next_##n = (n) ? (n)->next : NULL)
51
53static inline int list_empty(struct list *list)
54{
55 return !list->head;
56}
57
59static inline int list_linked(struct list_node *node)
60{
61 return node->list != NULL;
62}
63
65static inline void list_append(struct list *list, struct list_node *node)
66{
67 node->list = list;
68 node->next = NULL;
69 node->prev = list->tail;
70
71 if (list->tail)
72 list->tail->next = node;
73 else
74 list->head = node;
75
76 list->tail = node;
77 list->count++;
78}
79
81static inline void list_prepend(struct list *list, struct list_node *node)
82{
83 node->list = list;
84 node->prev = NULL;
85 node->next = list->head;
86
87 if (list->head)
88 list->head->prev = node;
89 else
90 list->tail = node;
91
92 list->head = node;
93 list->count++;
94}
95
97static inline void list_remove(struct list_node *node)
98{
99 struct list *list = node->list;
100
101 if (!list)
102 return;
103
104 if (node->prev)
105 node->prev->next = node->next;
106 else
107 list->head = node->next;
108
109 if (node->next)
110 node->next->prev = node->prev;
111 else
112 list->tail = node->prev;
113
114 node->prev = node->next = NULL;
115 node->list = NULL;
116 list->count--;
117}
118
120static inline struct list_node *list_pop(struct list *list)
121{
122 struct list_node *node = list->head;
123
124 if (node)
125 list_remove(node);
126
127 return node;
128}
129
130#endif /* ! _DS_LIST_H */
static int list_empty(struct list *list)
Returns whether list has no nodes.
Definition list.h:53
static void list_remove(struct list_node *node)
Takes node off the list it is on.
Definition list.h:97
static void list_append(struct list *list, struct list_node *node)
Adds node at the end of list.
Definition list.h:65
static struct list_node * list_pop(struct list *list)
Takes the first node off list and returns it, or NULL if it's empty.
Definition list.h:120
static void list_prepend(struct list *list, struct list_node *node)
Adds node at the head of list, so that list_pop() returns it next.
Definition list.h:81
static int list_linked(struct list_node *node)
Returns whether node is on a list (node->list says which).
Definition list.h:59
#define NULL
Definition stddef.h:27
A link in a list, inside the structure that is listed.
Definition list.h:31
struct list_node * prev
Definition list.h:32
struct list * list
Definition list.h:33
struct list_node * next
Definition list.h:32
A list: its first and last nodes, and how many there are.
Definition list.h:37
struct list_node * head
Definition list.h:38
size_t count
Definition list.h:39
struct list_node * tail
Definition list.h:38