1 /* list.h - the opkg package management system
3 Tick Chen <tick@openmoko.com>
5 Copyright (C) 2008 Openmoko Inc.
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 This is modified from Linux Kernel.
24 struct list_head
*next
, *prev
;
27 #define LIST_POISON1 ((struct list_head *) 0x00100100)
28 #define LIST_POISON2 ((struct list_head *) 0x00200200)
30 #define LIST_HEAD_INIT(name) { &(name), &(name) }
32 #define LIST_HEAD(name) \
33 struct list_head name = LIST_HEAD_INIT(name)
35 #define INIT_LIST_HEAD(ptr) do { \
36 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
39 static inline void __list_add(struct list_head
*newitem
,
40 struct list_head
*prev
, struct list_head
*next
)
49 * list_add - add a new entry
50 * @newitem: new entry to be added
51 * @head: list head to add it after
53 * Insert a new entry after the specified head.
54 * This is good for implementing stacks.
56 static inline void list_add(struct list_head
*newitem
, struct list_head
*head
)
58 __list_add(newitem
, head
, head
->next
);
62 * list_add_tail - add a new entry
63 * @newitem: new entry to be added
64 * @head: list head to add it before
66 * Insert a new entry before the specified head.
67 * This is useful for implementing queues.
69 static inline void list_add_tail(struct list_head
*newitem
,
70 struct list_head
*head
)
72 __list_add(newitem
, head
->prev
, head
);
76 * Delete a list entry by making the prev/next entries
77 * point to each other.
79 * This is only for internal list manipulation where we know
80 * the prev/next entries already!
82 static inline void __list_del(struct list_head
*prev
, struct list_head
*next
)
89 * list_del - deletes entry from list.
90 * @entry: the element to delete from the list.
91 * Note: list_empty on entry does not return true after this, the entry is
92 * in an undefined state.
94 static inline void list_del(struct list_head
*entry
)
96 __list_del(entry
->prev
, entry
->next
);
97 entry
->next
= LIST_POISON1
;
98 entry
->prev
= LIST_POISON2
;
102 * list_del_init - deletes entry from list and reinitialize it.
103 * @entry: the element to delete from the list.
105 static inline void list_del_init(struct list_head
*entry
)
107 __list_del(entry
->prev
, entry
->next
);
108 INIT_LIST_HEAD(entry
);
112 * list_move - delete from one list and add as another's head
113 * @list: the entry to move
114 * @head: the head that will precede our entry
116 static inline void list_move(struct list_head
*list
, struct list_head
*head
)
118 __list_del(list
->prev
, list
->next
);
119 list_add(list
, head
);
123 * list_move_tail - delete from one list and add as another's tail
124 * @list: the entry to move
125 * @head: the head that will follow our entry
127 static inline void list_move_tail(struct list_head
*list
,
128 struct list_head
*head
)
130 __list_del(list
->prev
, list
->next
);
131 list_add_tail(list
, head
);
135 * list_empty - tests whether a list is empty
136 * @head: the list to test.
138 static inline int list_empty(const struct list_head
*head
)
140 return head
->next
== head
;
144 * list_empty_careful - tests whether a list is
145 * empty _and_ checks that no other CPU might be
146 * in the process of still modifying either member
148 * NOTE: using list_empty_careful() without synchronization
149 * can only be safe if the only activity that can happen
150 * to the list entry is list_del_init(). Eg. it cannot be used
151 * if another CPU could re-list_add() it.
153 * @head: the list to test.
155 static inline int list_empty_careful(const struct list_head
*head
)
157 struct list_head
*next
= head
->next
;
158 return (next
== head
) && (next
== head
->prev
);
161 static inline void __list_splice(struct list_head
*list
, struct list_head
*head
)
163 struct list_head
*first
= list
->next
;
164 struct list_head
*last
= list
->prev
;
165 struct list_head
*at
= head
->next
;
175 * list_splice - join two lists
176 * @list: the new list to add.
177 * @head: the place to add it in the first list.
179 static inline void list_splice(struct list_head
*list
, struct list_head
*head
)
181 if (!list_empty(list
))
182 __list_splice(list
, head
);
186 * list_splice_init - join two lists and reinitialise the emptied list.
187 * @list: the new list to add.
188 * @head: the place to add it in the first list.
190 * The list at @list is reinitialised
192 static inline void list_splice_init(struct list_head
*list
,
193 struct list_head
*head
)
195 if (!list_empty(list
)) {
196 __list_splice(list
, head
);
197 INIT_LIST_HEAD(list
);
201 #define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
202 #define container_of(ptr, type, member) ({ \
203 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
204 (type *)( (char *)__mptr - _offsetof(type,member) );})
207 * list_entry - get the struct for this entry
208 * @ptr: the &struct list_head pointer.
209 * @type: the type of the struct this is embedded in.
210 * @member: the name of the list_struct within the struct.
212 #define list_entry(ptr, type, member) \
213 container_of(ptr, type, member)
216 * list_for_each - iterate over a list
217 * @pos: the &struct list_head to use as a loop counter.
218 * @head: the head for your list.
220 #define list_for_each(pos, head) \
221 for (pos = (head)->next; pos != (head); \
225 * __list_for_each - iterate over a list
226 * @pos: the &struct list_head to use as a loop counter.
227 * @head: the head for your list.
229 * This variant differs from list_for_each() in that it's the
230 * simplest possible list iteration code, no prefetching is done.
231 * Use this for code that knows the list to be very short (empty
232 * or 1 entry) most of the time.
234 #define __list_for_each(pos, head) \
235 for (pos = (head)->next; pos != (head); pos = pos->next)
238 * list_for_each_prev - iterate over a list backwards
239 * @pos: the &struct list_head to use as a loop counter.
240 * @head: the head for your list.
242 #define list_for_each_prev(pos, head) \
243 for (pos = (head)->prev; pos != (head); \
247 * list_for_each_safe - iterate over a list safe against removal of list entry
248 * @pos: the &struct list_head to use as a loop counter.
249 * @n: another &struct list_head to use as temporary storage
250 * @head: the head for your list.
252 #define list_for_each_safe(pos, n, head) \
253 for (pos = (head)->next, n = pos->next; pos != (head); \
254 pos = n, n = pos->next)
257 * list_for_each_entry - iterate over list of given type
258 * @pos: the type * to use as a loop counter.
259 * @head: the head for your list.
260 * @member: the name of the list_struct within the struct.
262 #define list_for_each_entry(pos, head, member) \
263 for (pos = list_entry((head)->next, typeof(*pos), member); \
264 &pos->member != (head); \
265 pos = list_entry(pos->member.next, typeof(*pos), member))
268 * list_for_each_entry_reverse - iterate backwards over list of given type.
269 * @pos: the type * to use as a loop counter.
270 * @head: the head for your list.
271 * @member: the name of the list_struct within the struct.
273 #define list_for_each_entry_reverse(pos, head, member) \
274 for (pos = list_entry((head)->prev, typeof(*pos), member); \
275 &pos->member != (head); \
276 pos = list_entry(pos->member.prev, typeof(*pos), member))
279 * list_prepare_entry - prepare a pos entry for use as a start point in
280 * list_for_each_entry_continue
281 * @pos: the type * to use as a start point
282 * @head: the head of the list
283 * @member: the name of the list_struct within the struct.
285 #define list_prepare_entry(pos, head, member) \
286 ((pos) ? : list_entry(head, typeof(*pos), member))
289 * list_for_each_entry_continue - iterate over list of given type
290 * continuing after existing point
291 * @pos: the type * to use as a loop counter.
292 * @head: the head for your list.
293 * @member: the name of the list_struct within the struct.
295 #define list_for_each_entry_continue(pos, head, member) \
296 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
297 &pos->member != (head); \
298 pos = list_entry(pos->member.next, typeof(*pos), member))
301 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
302 * @pos: the type * to use as a loop counter.
303 * @n: another type * to use as temporary storage
304 * @head: the head for your list.
305 * @member: the name of the list_struct within the struct.
307 #define list_for_each_entry_safe(pos, n, head, member) \
308 for (pos = list_entry((head)->next, typeof(*pos), member), \
309 n = list_entry(pos->member.next, typeof(*pos), member); \
310 &pos->member != (head); \
311 pos = n, n = list_entry(n->member.next, typeof(*n), member))