fix errors in slides
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / llist.h
1 /**
2  * @file llist.h
3  * @author Lunaixsky
4  * @brief This doubly linked cyclic list is adopted from Linux kernel <linux/list.h>
5  * @version 0.1
6  * @date 2022-03-12
7  * 
8  * @copyright Copyright (c) 2022
9  * 
10  */
11 #ifndef __LUNAIX_LLIST_H
12 #define __LUNAIX_LLIST_H
13
14 #include <lunaix/common.h>
15
16 struct llist_header
17 {
18     struct llist_header* prev;
19     struct llist_header* next;
20 };
21
22 static inline void
23 __llist_add(struct llist_header* elem,
24             struct llist_header* prev,
25             struct llist_header* next)
26 {
27     next->prev = elem;
28     elem->next = next;
29     elem->prev = prev;
30     prev->next = elem;
31 }
32
33 static inline void
34 llist_init_head(struct llist_header* head) {
35     head->next = head;
36     head->prev = head;
37 }
38
39 static inline void
40 llist_append(struct llist_header* head, struct llist_header* elem)
41 {
42     __llist_add(elem, head, head->next);
43 }
44
45 static inline void
46 llist_prepend(struct llist_header* head, struct llist_header* elem)
47 {
48     __llist_add(elem, head->prev, head);
49 }
50
51 static inline void
52 llist_delete(struct llist_header* elem) {
53     elem->prev->next = elem->next;
54     elem->next->prev = elem->next;
55     
56     // make elem orphaned
57     elem->prev = elem;
58     elem->next = elem;
59 }
60
61 /**
62  * list_entry - get the struct for this entry
63  * @ptr:        the &struct list_head pointer.
64  * @type:       the type of the struct this is embedded in.
65  * @member:     the name of the list_struct within the struct.
66  */
67 #define list_entry(ptr, type, member) \
68         container_of(ptr, type, member)
69
70 /**
71  * list_for_each_entry  -       iterate over list of given type
72  * @pos:        the type * to use as a loop counter.
73  * @head:       the head for your list.
74  * @member:     the name of the list_struct within the struct.
75  */
76 #define llist_for_each(pos, n, head, member)                                \
77         for (pos = list_entry((head)->next, typeof(*pos), member),      \
78                 n = list_entry(pos->member.next, typeof(*pos), member); \
79              &pos->member != (head);                                                \
80              pos = n, n = list_entry(n->member.next, typeof(*n), member))
81
82 #endif /* __LUNAIX_LLIST_H */