Karg  v0.2.0
The kernel of CargOS
list.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stddef.h>
4 #include <types.h>
5 
6 typedef struct list_node {
7  struct list_node *prev, *next;
9 
10 #define list_head_init(name) {&name, &name}
11 
12 static inline void list_init_head(list_node_t *head) {
13  head->prev = head;
14  head->next = head;
15 }
16 
17 static inline list_node_t *list_front(list_node_t *head) { return head->next; }
18 
19 static inline list_node_t *list_back(list_node_t *head) { return head->prev; }
20 
21 static inline bool list_empty(list_node_t *head) {
22  return head == list_front(head);
23 }
24 
25 static inline bool list_is_front(list_node_t *head, list_node_t *node) {
26  return head->next == node;
27 }
28 
29 static inline bool list_is_back(list_node_t *head, list_node_t *node) {
30  return head->prev == node;
31 }
32 
33 #define list_entry(node, type, member) \
34  ((type *) ((u8 *) (node) - offsetof(type, member)))
35 #define list_first_entry(head, type, member) \
36  list_entry(list_front(head), type, member)
37 #define list_last_entry(head, type, member) \
38  list_entry(list_back(head), type, member)
39 
40 static inline void list_insert(list_node_t *pos, list_node_t *node) {
41  pos->prev->next = node;
42  node->prev = pos->prev;
43  node->next = pos;
44  pos->prev = node;
45 }
46 
47 static inline void list_push_front(list_node_t *head, list_node_t *node) {
48  list_insert(list_front(head), node);
49 }
50 
51 static inline void list_push_back(list_node_t *head, list_node_t *node) {
52  list_insert(head, node);
53 }
54 
55 static inline void list_remove(list_node_t *node) {
56  node->prev->next = node->next;
57  node->next->prev = node->prev;
58 }
59 
60 static inline void list_remove_init(list_node_t *node) {
61  list_remove(node);
62  node->prev = node;
63  node->next = node;
64 }
65 
66 static inline void list_pop_front(list_node_t *head) {
67  list_remove(list_front(head));
68 }
69 
70 static inline void list_pop_back(list_node_t *head) {
71  list_remove(list_back(head));
72 }
73 
74 #define list_for_each(head, node) \
75  for (node = (head)->next; node != (head); node = node->next)
76 #define list_for_each_entry(head, member, entry) \
77  for (entry = list_first_entry(head, typeof(*entry), member); \
78  &entry->member != (head); \
79  entry = list_entry(entry->member.next, typeof(*entry), member))
struct list_node list_node_t
Definition: list.h:6
struct list_node * prev
Definition: list.h:7
struct list_node * next
Definition: list.h:7