]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - include/list.h
2 * Copyright (c) 2006 Silicon Graphics, Inc.
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.
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.
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
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.
26 * As OS X is kind of a derivate of BSD, this affects OS X too.
28 * To use our own LIST_ macros (copied from kernel code), we have to
29 * at first undefine the conflicting system macros.
36 * Simple, generic doubly-linked list implementation.
40 struct list_head
*next
;
41 struct list_head
*prev
;
44 #define LIST_HEAD_INIT(name) { &(name), &(name) }
46 #define LIST_HEAD(name) \
47 struct list_head name = LIST_HEAD_INIT(name)
49 #define INIT_LIST_HEAD(list) list_head_init(list)
50 static inline void list_head_init(struct list_head
*list
)
52 list
->next
= list
->prev
= list
;
55 static inline void list_head_destroy(struct list_head
*list
)
57 list
->next
= list
->prev
= NULL
;
60 static inline void __list_add(struct list_head
*add
,
61 struct list_head
*prev
, struct list_head
*next
)
69 static inline void list_add(struct list_head
*add
, struct list_head
*head
)
71 __list_add(add
, head
, head
->next
);
74 static inline void list_add_tail(struct list_head
*add
, struct list_head
*head
)
76 __list_add(add
, head
->prev
, head
);
79 static inline void __list_del(struct list_head
*prev
, struct list_head
*next
)
85 static inline void list_del_init(struct list_head
*entry
)
87 __list_del(entry
->prev
, entry
->next
);
88 list_head_init(entry
);
91 static inline void list_del(struct list_head
*entry
)
93 __list_del(entry
->prev
, entry
->next
);
96 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
98 __list_del(list
->prev
, list
->next
);
102 static inline void list_move_tail(struct list_head
*list
, struct list_head
*head
)
104 __list_del(list
->prev
, list
->next
);
105 list_add_tail(list
, head
);
108 static inline int list_empty(const struct list_head
*head
)
110 return head
->next
== head
;
113 static inline void __list_splice(struct list_head
*list
,
114 struct list_head
*prev
,
115 struct list_head
*next
)
117 struct list_head
*first
= list
->next
;
118 struct list_head
*last
= list
->prev
;
127 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
129 if (!list_empty(list
))
130 __list_splice(list
, head
, head
->next
);
133 static inline void list_splice_init(struct list_head
*list
,
134 struct list_head
*head
)
136 if (!list_empty(list
)) {
137 __list_splice(list
, head
, head
->next
);
138 list_head_init(list
);
142 #define list_entry(ptr, type, member) ({ \
143 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
144 (type *)( (char *)__mptr - offsetof(type,member) );})
146 #define list_for_each(pos, head) \
147 for (pos = (head)->next; pos != (head); pos = pos->next)
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)
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))
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))
164 #define list_first_entry(ptr, type, member) \
165 list_entry((ptr)->next, type, member)
167 #define container_of(ptr, type, member) ({ \
168 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
169 (type *)( (char *)__mptr - offsetof(type,member) );})
171 void list_sort(void *priv
, struct list_head
*head
,
172 int (*cmp
)(void *priv
, struct list_head
*a
,
173 struct list_head
*b
));
175 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
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.
182 * Each of the lists is a queue.
183 * The list at @list is reinitialised
185 static inline void list_splice_tail_init(struct list_head
*list
,
186 struct list_head
*head
)
188 if (!list_empty(list
)) {
189 __list_splice(list
, head
->prev
, head
);
190 INIT_LIST_HEAD(list
);
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.
200 * Note, that list is expected to be not empty.
202 #define list_last_entry(ptr, type, member) \
203 list_entry((ptr)->prev, type, member)
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.
210 #define list_prev_entry(pos, member) \
211 list_entry((pos)->member.prev, typeof(*(pos)), member)
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.
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))
224 #endif /* __LIST_H__ */