]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - include/list.h
xfs_io: Add support for pwritev2()
[thirdparty/xfsprogs-dev.git] / include / list.h
1 /*
2 * Copyright (c) 2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #ifndef __LIST_H__
19 #define __LIST_H__
20
21 /*
22 * This undef is here because BSD 4.4 added some LIST_ macros into system
23 * header file sys/queue.h. This header is included in many other system
24 * headers and thus causes "macro redefined" warnings.
25 *
26 * As OS X is kind of a derivate of BSD, this affects OS X too.
27 *
28 * To use our own LIST_ macros (copied from kernel code), we have to
29 * at first undefine the conflicting system macros.
30 *
31 */
32 #undef LIST_HEAD
33 #undef LIST_HEAD_INIT
34
35 /*
36 * Simple, generic doubly-linked list implementation.
37 */
38
39 struct list_head {
40 struct list_head *next;
41 struct list_head *prev;
42 };
43
44 #define LIST_HEAD_INIT(name) { &(name), &(name) }
45
46 #define LIST_HEAD(name) \
47 struct list_head name = LIST_HEAD_INIT(name)
48
49 #define INIT_LIST_HEAD(list) list_head_init(list)
50 static inline void list_head_init(struct list_head *list)
51 {
52 list->next = list->prev = list;
53 }
54
55 static inline void list_head_destroy(struct list_head *list)
56 {
57 list->next = list->prev = NULL;
58 }
59
60 static inline void __list_add(struct list_head *add,
61 struct list_head *prev, struct list_head *next)
62 {
63 next->prev = add;
64 add->next = next;
65 add->prev = prev;
66 prev->next = add;
67 }
68
69 static inline void list_add(struct list_head *add, struct list_head *head)
70 {
71 __list_add(add, head, head->next);
72 }
73
74 static inline void list_add_tail(struct list_head *add, struct list_head *head)
75 {
76 __list_add(add, head->prev, head);
77 }
78
79 static inline void __list_del(struct list_head *prev, struct list_head *next)
80 {
81 next->prev = prev;
82 prev->next = next;
83 }
84
85 static inline void list_del_init(struct list_head *entry)
86 {
87 __list_del(entry->prev, entry->next);
88 list_head_init(entry);
89 }
90
91 static inline void list_del(struct list_head *entry)
92 {
93 __list_del(entry->prev, entry->next);
94 }
95
96 static inline void list_move(struct list_head *list, struct list_head *head)
97 {
98 __list_del(list->prev, list->next);
99 list_add(list, head);
100 }
101
102 static inline void list_move_tail(struct list_head *list, struct list_head *head)
103 {
104 __list_del(list->prev, list->next);
105 list_add_tail(list, head);
106 }
107
108 static inline int list_empty(const struct list_head *head)
109 {
110 return head->next == head;
111 }
112
113 static inline void __list_splice(struct list_head *list,
114 struct list_head *prev,
115 struct list_head *next)
116 {
117 struct list_head *first = list->next;
118 struct list_head *last = list->prev;
119
120 first->prev = prev;
121 prev->next = first;
122
123 last->next = next;
124 next->prev = last;
125 }
126
127 static inline void list_splice(struct list_head *list, struct list_head *head)
128 {
129 if (!list_empty(list))
130 __list_splice(list, head, head->next);
131 }
132
133 static inline void list_splice_init(struct list_head *list,
134 struct list_head *head)
135 {
136 if (!list_empty(list)) {
137 __list_splice(list, head, head->next);
138 list_head_init(list);
139 }
140 }
141
142 #define list_entry(ptr, type, member) ({ \
143 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
144 (type *)( (char *)__mptr - offsetof(type,member) );})
145
146 #define list_for_each(pos, head) \
147 for (pos = (head)->next; pos != (head); pos = pos->next)
148
149 #define list_for_each_safe(pos, n, head) \
150 for (pos = (head)->next, n = pos->next; pos != (head); \
151 pos = n, n = pos->next)
152
153 #define list_for_each_entry(pos, head, member) \
154 for (pos = list_entry((head)->next, typeof(*pos), member); \
155 &pos->member != (head); \
156 pos = list_entry(pos->member.next, typeof(*pos), member))
157
158 #define list_for_each_entry_safe(pos, n, head, member) \
159 for (pos = list_entry((head)->next, typeof(*pos), member), \
160 n = list_entry(pos->member.next, typeof(*pos), member); \
161 &pos->member != (head); \
162 pos = n, n = list_entry(n->member.next, typeof(*n), member))
163
164 #define list_first_entry(ptr, type, member) \
165 list_entry((ptr)->next, type, member)
166
167 #define container_of(ptr, type, member) ({ \
168 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
169 (type *)( (char *)__mptr - offsetof(type,member) );})
170
171 void list_sort(void *priv, struct list_head *head,
172 int (*cmp)(void *priv, struct list_head *a,
173 struct list_head *b));
174
175 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
176
177 /**
178 * list_splice_tail_init - join two lists and reinitialise the emptied list
179 * @list: the new list to add.
180 * @head: the place to add it in the first list.
181 *
182 * Each of the lists is a queue.
183 * The list at @list is reinitialised
184 */
185 static inline void list_splice_tail_init(struct list_head *list,
186 struct list_head *head)
187 {
188 if (!list_empty(list)) {
189 __list_splice(list, head->prev, head);
190 INIT_LIST_HEAD(list);
191 }
192 }
193
194 /**
195 * list_last_entry - get the last element from a list
196 * @ptr: the list head to take the element from.
197 * @type: the type of the struct this is embedded in.
198 * @member: the name of the list_head within the struct.
199 *
200 * Note, that list is expected to be not empty.
201 */
202 #define list_last_entry(ptr, type, member) \
203 list_entry((ptr)->prev, type, member)
204
205 /**
206 * list_prev_entry - get the prev element in list
207 * @pos: the type * to cursor
208 * @member: the name of the list_head within the struct.
209 */
210 #define list_prev_entry(pos, member) \
211 list_entry((pos)->member.prev, typeof(*(pos)), member)
212
213 /**
214 * list_for_each_entry_reverse - iterate backwards over list of given type.
215 * @pos: the type * to use as a loop cursor.
216 * @head: the head for your list.
217 * @member: the name of the list_head within the struct.
218 */
219 #define list_for_each_entry_reverse(pos, head, member) \
220 for (pos = list_last_entry(head, typeof(*pos), member); \
221 &pos->member != (head); \
222 pos = list_prev_entry(pos, member))
223
224 #endif /* __LIST_H__ */