build: remove automake/autoconf build system
[oweals/opkg-lede.git] / libopkg / list.h
1 /* list.h - the opkg package management system
2
3    Tick Chen <tick@openmoko.com>
4
5    Copyright (C) 2008 Openmoko Inc.
6
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.
11
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.
16
17    This is modified from Linux Kernel.
18 */
19
20 #ifndef _LINUX_LIST_H
21 #define _LINUX_LIST_H
22
23 struct list_head {
24         struct list_head *next, *prev;
25 };
26
27 #define LIST_POISON1  ((struct list_head *) 0x00100100)
28 #define LIST_POISON2  ((struct list_head *) 0x00200200)
29
30 #define LIST_HEAD_INIT(name) { &(name), &(name) }
31
32 #define LIST_HEAD(name) \
33         struct list_head name = LIST_HEAD_INIT(name)
34
35 #define INIT_LIST_HEAD(ptr) do { \
36         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
37 } while (0)
38
39 static inline void __list_add(struct list_head *newitem,
40                               struct list_head *prev, struct list_head *next)
41 {
42         next->prev = newitem;
43         newitem->next = next;
44         newitem->prev = prev;
45         prev->next = newitem;
46 }
47
48 /**
49  * list_add - add a new entry
50  * @newitem: new entry to be added
51  * @head: list head to add it after
52  *
53  * Insert a new entry after the specified head.
54  * This is good for implementing stacks.
55  */
56 static inline void list_add(struct list_head *newitem, struct list_head *head)
57 {
58         __list_add(newitem, head, head->next);
59 }
60
61 /**
62  * list_add_tail - add a new entry
63  * @newitem: new entry to be added
64  * @head: list head to add it before
65  *
66  * Insert a new entry before the specified head.
67  * This is useful for implementing queues.
68  */
69 static inline void list_add_tail(struct list_head *newitem,
70                                  struct list_head *head)
71 {
72         __list_add(newitem, head->prev, head);
73 }
74
75 /*
76  * Delete a list entry by making the prev/next entries
77  * point to each other.
78  *
79  * This is only for internal list manipulation where we know
80  * the prev/next entries already!
81  */
82 static inline void __list_del(struct list_head *prev, struct list_head *next)
83 {
84         next->prev = prev;
85         prev->next = next;
86 }
87
88 /**
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.
93  */
94 static inline void list_del(struct list_head *entry)
95 {
96         __list_del(entry->prev, entry->next);
97         entry->next = LIST_POISON1;
98         entry->prev = LIST_POISON2;
99 }
100
101 /**
102  * list_del_init - deletes entry from list and reinitialize it.
103  * @entry: the element to delete from the list.
104  */
105 static inline void list_del_init(struct list_head *entry)
106 {
107         __list_del(entry->prev, entry->next);
108         INIT_LIST_HEAD(entry);
109 }
110
111 /**
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
115  */
116 static inline void list_move(struct list_head *list, struct list_head *head)
117 {
118         __list_del(list->prev, list->next);
119         list_add(list, head);
120 }
121
122 /**
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
126  */
127 static inline void list_move_tail(struct list_head *list,
128                                   struct list_head *head)
129 {
130         __list_del(list->prev, list->next);
131         list_add_tail(list, head);
132 }
133
134 /**
135  * list_empty - tests whether a list is empty
136  * @head: the list to test.
137  */
138 static inline int list_empty(const struct list_head *head)
139 {
140         return head->next == head;
141 }
142
143 /**
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
147  *
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.
152  *
153  * @head: the list to test.
154  */
155 static inline int list_empty_careful(const struct list_head *head)
156 {
157         struct list_head *next = head->next;
158         return (next == head) && (next == head->prev);
159 }
160
161 static inline void __list_splice(struct list_head *list, struct list_head *head)
162 {
163         struct list_head *first = list->next;
164         struct list_head *last = list->prev;
165         struct list_head *at = head->next;
166
167         first->prev = head;
168         head->next = first;
169
170         last->next = at;
171         at->prev = last;
172 }
173
174 /**
175  * list_splice - join two lists
176  * @list: the new list to add.
177  * @head: the place to add it in the first list.
178  */
179 static inline void list_splice(struct list_head *list, struct list_head *head)
180 {
181         if (!list_empty(list))
182                 __list_splice(list, head);
183 }
184
185 /**
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.
189  *
190  * The list at @list is reinitialised
191  */
192 static inline void list_splice_init(struct list_head *list,
193                                     struct list_head *head)
194 {
195         if (!list_empty(list)) {
196                 __list_splice(list, head);
197                 INIT_LIST_HEAD(list);
198         }
199 }
200
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) );})
205
206 /**
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.
211  */
212 #define list_entry(ptr, type, member) \
213         container_of(ptr, type, member)
214
215 /**
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.
219  */
220 #define list_for_each(pos, head) \
221         for (pos = (head)->next; pos != (head); \
222                 pos = pos->next)
223
224 /**
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.
228  *
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.
233  */
234 #define __list_for_each(pos, head) \
235         for (pos = (head)->next; pos != (head); pos = pos->next)
236
237 /**
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.
241  */
242 #define list_for_each_prev(pos, head) \
243         for (pos = (head)->prev; pos != (head); \
244                 pos = pos->prev)
245
246 /**
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.
251  */
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)
255
256 /**
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.
261  */
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))
266
267 /**
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.
272  */
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))
277
278 /**
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.
284  */
285 #define list_prepare_entry(pos, head, member) \
286         ((pos) ? : list_entry(head, typeof(*pos), member))
287
288 /**
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.
294  */
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))
299
300 /**
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.
306  */
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))
312
313 #endif