]> git.proxmox.com Git - efi-boot-shim.git/blob - include/list.h
Force usage of newest revocations at build time
[efi-boot-shim.git] / include / list.h
1 // SPDX-License-Identifier: BSD-2-Clause-Patent
2 /*
3 * list.h - simple list primitives
4 */
5
6 #ifndef LIST_H_
7 #define LIST_H_
8
9 #define container_of(ptr, type, member) \
10 ({ \
11 void *__mptr = (void *)(ptr); \
12 ((type *)(__mptr - offsetof(type, member))); \
13 })
14
15 struct list_head {
16 struct list_head *next;
17 struct list_head *prev;
18 };
19
20 typedef struct list_head list_t;
21
22 #define LIST_HEAD_INIT(name) \
23 { \
24 .next = &(name), .prev = &(name) \
25 }
26
27 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
28
29 #define INIT_LIST_HEAD(ptr) \
30 ({ \
31 (ptr)->next = (ptr); \
32 (ptr)->prev = (ptr); \
33 })
34
35 static inline int
36 list_empty(const struct list_head *head)
37 {
38 return head->next == head;
39 }
40
41 static inline void
42 __list_add(struct list_head *new, struct list_head *prev,
43 struct list_head *next)
44 {
45 next->prev = new;
46 new->next = next;
47 new->prev = prev;
48 prev->next = new;
49 }
50
51 static inline void
52 list_add(struct list_head *new, struct list_head *head)
53 {
54 __list_add(new, head, head->next);
55 }
56
57 static inline void
58 list_add_tail(struct list_head *new, struct list_head *head)
59 {
60 __list_add(new, head->prev, head);
61 }
62
63 static inline void
64 __list_del(struct list_head *prev, struct list_head *next)
65 {
66 next->prev = prev;
67 prev->next = next;
68 }
69
70 static inline void
71 __list_del_entry(struct list_head *entry)
72 {
73 __list_del(entry->prev, entry->next);
74 }
75
76 static inline void
77 list_del(struct list_head *entry)
78 {
79 __list_del_entry(entry);
80 entry->next = NULL;
81 entry->prev = NULL;
82 }
83
84 #define list_entry(ptr, type, member) container_of(ptr, type, member)
85
86 #define list_first_entry(ptr, type, member) \
87 list_entry((ptr)->next, type, member)
88
89 #define list_last_entry(ptr, type, member) list_entry((ptr)->prev, type, member)
90
91 #define list_for_each(pos, head) \
92 for (pos = (head)->next; pos != (head); pos = pos->next)
93
94 #define list_for_each_safe(pos, n, head) \
95 for (pos = (head)->next, n = pos->next; pos != (head); \
96 pos = n, n = pos->next)
97
98 #define list_for_each_prev(pos, head) \
99 for (pos = (head)->prev; pos != (head); pos = pos->prev)
100
101 #define list_for_each_prev_safe(pos, n, head) \
102 for (pos = (head)->prev, n = pos->prev; pos != (head); \
103 pos = n, n = pos->prev)
104
105 static inline size_t
106 list_size(struct list_head *entry)
107 {
108 list_t *pos;
109 size_t i = 0;
110 list_for_each(pos, entry) {
111 i++;
112 }
113 return i;
114 }
115
116 #endif /* !LIST_H_ */
117 // vim:fenc=utf-8:tw=75:noet