opkg: fix nullpointer dereference
[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 #define LIST_POISON1  ((void *) 0x00100100)
24 #define LIST_POISON2  ((void *) 0x00200200)
25
26 struct list_head {
27     struct list_head *next, *prev;
28 };
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 *new,
40                               struct list_head *prev,
41                               struct list_head *next) {
42     next->prev = new;
43     new->next = next;
44     new->prev = prev;
45     prev->next = new;
46 }
47
48 /**
49  * list_add - add a new entry
50  * @new: 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 *new, struct list_head *head) {
57     __list_add(new, head, head->next);
58 }
59
60 /**
61  * list_add_tail - add a new entry
62  * @new: new entry to be added
63  * @head: list head to add it before
64  *
65  * Insert a new entry before the specified head.
66  * This is useful for implementing queues.
67  */
68 static inline void list_add_tail(struct list_head *new, struct list_head *head) {
69     __list_add(new, head->prev, head);
70 }
71
72
73 /*
74  * Delete a list entry by making the prev/next entries
75  * point to each other.
76  *
77  * This is only for internal list manipulation where we know
78  * the prev/next entries already!
79  */
80 static inline void __list_del(struct list_head * prev, struct list_head * next) {
81     next->prev = prev;
82     prev->next = next;
83 }
84
85 /**
86  * list_del - deletes entry from list.
87  * @entry: the element to delete from the list.
88  * Note: list_empty on entry does not return true after this, the entry is
89  * in an undefined state.
90  */
91 static inline void list_del(struct list_head *entry) {
92     __list_del(entry->prev, entry->next);
93     entry->next = LIST_POISON1;
94     entry->prev = LIST_POISON2;
95 }
96
97 /**
98  * list_del_init - deletes entry from list and reinitialize it.
99  * @entry: the element to delete from the list.
100  */
101 static inline void list_del_init(struct list_head *entry) {
102     __list_del(entry->prev, entry->next);
103     INIT_LIST_HEAD(entry);
104 }
105
106 /**
107  * list_move - delete from one list and add as another's head
108  * @list: the entry to move
109  * @head: the head that will precede our entry
110  */
111 static inline void list_move(struct list_head *list, struct list_head *head) {
112     __list_del(list->prev, list->next);
113     list_add(list, head);
114 }
115
116 /**
117  * list_move_tail - delete from one list and add as another's tail
118  * @list: the entry to move
119  * @head: the head that will follow our entry
120  */
121 static inline void list_move_tail(struct list_head *list,
122                                   struct list_head *head) {
123     __list_del(list->prev, list->next);
124     list_add_tail(list, head);
125 }
126
127 /**
128  * list_empty - tests whether a list is empty
129  * @head: the list to test.
130  */
131 static inline int list_empty(const struct list_head *head) {
132     return head->next == head;
133 }
134
135 /**
136  * list_empty_careful - tests whether a list is
137  * empty _and_ checks that no other CPU might be
138  * in the process of still modifying either member
139  *
140  * NOTE: using list_empty_careful() without synchronization
141  * can only be safe if the only activity that can happen
142  * to the list entry is list_del_init(). Eg. it cannot be used
143  * if another CPU could re-list_add() it.
144  *
145  * @head: the list to test.
146  */
147 static inline int list_empty_careful(const struct list_head *head) {
148     struct list_head *next = head->next;
149     return (next == head) && (next == head->prev);
150 }
151
152 static inline void __list_splice(struct list_head *list,
153                                  struct list_head *head) {
154     struct list_head *first = list->next;
155     struct list_head *last = list->prev;
156     struct list_head *at = head->next;
157
158     first->prev = head;
159     head->next = first;
160
161     last->next = at;
162     at->prev = last;
163 }
164
165 /**
166  * list_splice - join two lists
167  * @list: the new list to add.
168  * @head: the place to add it in the first list.
169  */
170 static inline void list_splice(struct list_head *list, struct list_head *head) {
171     if (!list_empty(list))
172         __list_splice(list, head);
173 }
174
175 /**
176  * list_splice_init - join two lists and reinitialise the emptied list.
177  * @list: the new list to add.
178  * @head: the place to add it in the first list.
179  *
180  * The list at @list is reinitialised
181  */
182 static inline void list_splice_init(struct list_head *list,
183                                     struct list_head *head) {
184     if (!list_empty(list)) {
185         __list_splice(list, head);
186         INIT_LIST_HEAD(list);
187     }
188 }
189
190
191
192 #define _offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
193 #define container_of(ptr, type, member) ({                      \
194         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
195         (type *)( (char *)__mptr - _offsetof(type,member) );})
196
197 /**
198  * list_entry - get the struct for this entry
199  * @ptr:        the &struct list_head pointer.
200  * @type:       the type of the struct this is embedded in.
201  * @member:     the name of the list_struct within the struct.
202  */
203 #define list_entry(ptr, type, member) \
204         container_of(ptr, type, member)
205
206 /**
207  * list_for_each        -       iterate over a list
208  * @pos:        the &struct list_head to use as a loop counter.
209  * @head:       the head for your list.
210  */
211 #define list_for_each(pos, head) \
212         for (pos = (head)->next; pos != (head); \
213                 pos = pos->next)
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  * This variant differs from list_for_each() in that it's the
221  * simplest possible list iteration code, no prefetching is done.
222  * Use this for code that knows the list to be very short (empty
223  * or 1 entry) most of the time.
224  */
225 #define __list_for_each(pos, head) \
226         for (pos = (head)->next; pos != (head); pos = pos->next)
227
228 /**
229  * list_for_each_prev   -       iterate over a list backwards
230  * @pos:        the &struct list_head to use as a loop counter.
231  * @head:       the head for your list.
232  */
233 #define list_for_each_prev(pos, head) \
234         for (pos = (head)->prev; pos != (head); \
235                 pos = pos->prev)
236
237 /**
238  * list_for_each_safe   -       iterate over a list safe against removal of list entry
239  * @pos:        the &struct list_head to use as a loop counter.
240  * @n:          another &struct list_head to use as temporary storage
241  * @head:       the head for your list.
242  */
243 #define list_for_each_safe(pos, n, head) \
244         for (pos = (head)->next, n = pos->next; pos != (head); \
245                 pos = n, n = pos->next)
246
247 /**
248  * list_for_each_entry  -       iterate over list of given type
249  * @pos:        the type * to use as a loop counter.
250  * @head:       the head for your list.
251  * @member:     the name of the list_struct within the struct.
252  */
253 #define list_for_each_entry(pos, head, member)                          \
254         for (pos = list_entry((head)->next, typeof(*pos), member);      \
255              &pos->member != (head);    \
256              pos = list_entry(pos->member.next, typeof(*pos), member))
257
258 /**
259  * list_for_each_entry_reverse - iterate backwards over list of given type.
260  * @pos:        the type * to use as a loop counter.
261  * @head:       the head for your list.
262  * @member:     the name of the list_struct within the struct.
263  */
264 #define list_for_each_entry_reverse(pos, head, member)                  \
265         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
266              &pos->member != (head);    \
267              pos = list_entry(pos->member.prev, typeof(*pos), member))
268
269 /**
270  * list_prepare_entry - prepare a pos entry for use as a start point in
271  *                      list_for_each_entry_continue
272  * @pos:        the type * to use as a start point
273  * @head:       the head of the list
274  * @member:     the name of the list_struct within the struct.
275  */
276 #define list_prepare_entry(pos, head, member) \
277         ((pos) ? : list_entry(head, typeof(*pos), member))
278
279 /**
280  * list_for_each_entry_continue -       iterate over list of given type
281  *                      continuing after existing point
282  * @pos:        the type * to use as a loop counter.
283  * @head:       the head for your list.
284  * @member:     the name of the list_struct within the struct.
285  */
286 #define list_for_each_entry_continue(pos, head, member)                 \
287         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
288              &pos->member != (head);    \
289              pos = list_entry(pos->member.next, typeof(*pos), member))
290
291 /**
292  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
293  * @pos:        the type * to use as a loop counter.
294  * @n:          another type * to use as temporary storage
295  * @head:       the head for your list.
296  * @member:     the name of the list_struct within the struct.
297  */
298 #define list_for_each_entry_safe(pos, n, head, member)                  \
299         for (pos = list_entry((head)->next, typeof(*pos), member),      \
300                 n = list_entry(pos->member.next, typeof(*pos), member); \
301              &pos->member != (head);                                    \
302              pos = n, n = list_entry(n->member.next, typeof(*n), member))
303
304 #endif