rework external irq system, introduce hierarchical irq
[lunaix-os.git] / lunaix-os / includes / lunaix / ds / list.h
1 /**
2  * @file slist.h
3  * @author Lunaixsky
4  * @brief This singly linked list is adopted from Linux kernel
5  * <linux/llist.h>
6  * @version 0.1
7  * @date 2022-03-12
8  *
9  * @copyright Copyright (c) 2024
10  *
11  */
12 #ifndef __LUNAIX_LIST_H
13 #define __LUNAIX_LIST_H
14
15 #include <lunaix/types.h>
16
17 struct list_head {
18         struct list_node *first;
19 };
20
21 struct list_node {
22         struct list_node *next;
23 };
24
25 #define DEFINE_LIST(name)       struct list_head name = { .first = NULL }
26
27 static inline void 
28 list_head_init(struct list_head *list)
29 {
30         list->first = NULL;
31 }
32
33 static inline void 
34 list_node_init(struct list_node *node)
35 {
36         node->next = node;
37 }
38
39 #define slist_entry(ptr, type, member)          \
40         container_of(ptr, type, member)
41
42 #define member_address_is_nonnull(ptr, member)  \
43         ((ptr_t)(ptr) + offsetof(typeof(*(ptr)), member) != 0)
44
45 #define list_for_each(pos, n, node, member)                                                                     \
46         for (pos = slist_entry((node), typeof(*pos), member);                           \
47                 member_address_is_nonnull(pos, member) &&                                       \
48                         (n = slist_entry(pos->member.next, typeof(*n), member), true);  \
49              pos = n)
50
51 static inline bool      
52 __llist_add_batch(struct list_node *new_first,
53                                   struct list_node *new_last,
54                                   struct list_head *head)
55 {
56         new_last->next = head->first;
57         head->first = new_first;
58         return new_last->next == NULL;
59 }
60
61 static inline void
62 list_add(struct list_head* head, struct list_node* node)
63 {
64         __llist_add_batch(node, node, head);
65 }
66
67 #endif /* __LUNAIX_LIST_H */