1 /* SPDX-License-Identifier: GPL-2.0+ OR Apache-2.0 */
2 /*
3 * Copyright (C) 2018 HUAWEI, Inc.
4 * http://www.huawei.com/
5 * Created by Li Guifu <[email protected]>
6 */
7 #ifndef __EROFS_LIST_HEAD_H
8 #define __EROFS_LIST_HEAD_H
9
10 #ifdef __cplusplus
11 extern "C"
12 {
13 #endif
14
15 #include "defs.h"
16
17 struct list_head {
18 struct list_head *prev;
19 struct list_head *next;
20 };
21
22 #define LIST_HEAD_INIT(name) \
23 { \
24 &(name), &(name) \
25 }
26
27 #define LIST_HEAD(name) struct list_head name = LIST_HEAD_INIT(name)
28
init_list_head(struct list_head * list)29 static inline void init_list_head(struct list_head *list)
30 {
31 list->prev = list;
32 list->next = list;
33 }
34
__list_add(struct list_head * entry,struct list_head * prev,struct list_head * next)35 static inline void __list_add(struct list_head *entry,
36 struct list_head *prev,
37 struct list_head *next)
38 {
39 entry->prev = prev;
40 entry->next = next;
41 prev->next = entry;
42 next->prev = entry;
43 }
44
list_add(struct list_head * entry,struct list_head * head)45 static inline void list_add(struct list_head *entry, struct list_head *head)
46 {
47 __list_add(entry, head, head->next);
48 }
49
list_add_tail(struct list_head * entry,struct list_head * head)50 static inline void list_add_tail(struct list_head *entry,
51 struct list_head *head)
52 {
53 __list_add(entry, head->prev, head);
54 }
55
__list_del(struct list_head * prev,struct list_head * next)56 static inline void __list_del(struct list_head *prev, struct list_head *next)
57 {
58 prev->next = next;
59 next->prev = prev;
60 }
61
list_del(struct list_head * entry)62 static inline void list_del(struct list_head *entry)
63 {
64 __list_del(entry->prev, entry->next);
65 entry->prev = entry->next = NULL;
66 }
67
list_empty(struct list_head * head)68 static inline int list_empty(struct list_head *head)
69 {
70 return head->next == head;
71 }
72
__list_splice(struct list_head * list,struct list_head * prev,struct list_head * next)73 static inline void __list_splice(struct list_head *list,
74 struct list_head *prev, struct list_head *next)
75 {
76 struct list_head *first = list->next;
77 struct list_head *last = list->prev;
78
79 first->prev = prev;
80 prev->next = first;
81
82 last->next = next;
83 next->prev = last;
84 }
85
list_splice_tail(struct list_head * list,struct list_head * head)86 static inline void list_splice_tail(struct list_head *list,
87 struct list_head *head)
88 {
89 if (!list_empty(list))
90 __list_splice(list, head->prev, head);
91 }
92
93 #define list_entry(ptr, type, member) container_of(ptr, type, member)
94
95 #define list_first_entry(ptr, type, member) \
96 list_entry((ptr)->next, type, member)
97
98 #define list_last_entry(ptr, type, member) \
99 list_entry((ptr)->prev, type, member)
100
101 #define list_next_entry(pos, member) \
102 list_entry((pos)->member.next, typeof(*(pos)), member)
103
104 #define list_prev_entry(pos, member) \
105 list_entry((pos)->member.prev, typeof(*(pos)), member)
106
107 #define list_for_each(pos, head) \
108 for (pos = (head)->next; pos != (head); pos = pos->next)
109
110 #define list_for_each_safe(pos, n, head) \
111 for (pos = (head)->next, n = pos->next; pos != (head); \
112 pos = n, n = pos->next)
113
114 #define list_for_each_entry(pos, head, member) \
115 for (pos = list_first_entry(head, typeof(*pos), member); \
116 &pos->member != (head); \
117 pos = list_next_entry(pos, member))
118
119 #define list_for_each_entry_reverse(pos, head, member) \
120 for (pos = list_last_entry(head, typeof(*pos), member); \
121 &pos->member != (head); \
122 pos = list_prev_entry(pos, member))
123
124 #define list_for_each_entry_from(pos, head, member) \
125 for (; &pos->member != (head); pos = list_next_entry(pos, member))
126
127 #define list_for_each_entry_safe(pos, n, head, member) \
128 for (pos = list_first_entry(head, typeof(*pos), member), \
129 n = list_next_entry(pos, member); \
130 &pos->member != (head); \
131 pos = n, n = list_next_entry(n, member))
132
133 #ifdef __cplusplus
134 }
135 #endif
136
137 #endif
138