tizen 2.0
[external/module-init-tools.git] / list.h
1 /* Stolen from Linux Kernel Source's list.h -- GPL. */
2 #ifndef _MODINITTOOLS_LIST_H
3 #define _MODINITTOOLS_LIST_H
4
5 #undef offsetof
6 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
7
8 /**
9  * container_of - cast a member of a structure out to the containing structure
10  *
11  * @ptr:        the pointer to the member.
12  * @type:       the type of the container struct this is embedded in.
13  * @member:     the name of the member within the struct.
14  *
15  */
16 #define container_of(ptr, type, member) ({                      \
17         const typeof( ((type *)0)->member ) *__mptr = (ptr);    \
18         (type *)( (char *)__mptr - offsetof(type,member) );})
19
20 /*
21  * Simple doubly linked list implementation.
22  *
23  * Some of the internal functions ("__xxx") are useful when
24  * manipulating whole lists rather than single entries, as
25  * sometimes we already know the next/prev entries and we can
26  * generate better code by using them directly rather than
27  * using the generic single-entry routines.
28  */
29
30 struct list_head {
31         struct list_head *next, *prev;
32 };
33
34 #define LIST_HEAD_INIT(name) { &(name), &(name) }
35
36 #define LIST_HEAD(name) \
37         struct list_head name = LIST_HEAD_INIT(name)
38
39 #define INIT_LIST_HEAD(ptr) do { \
40         (ptr)->next = (ptr); (ptr)->prev = (ptr); \
41 } while (0)
42
43 /*
44  * Insert a new entry between two known consecutive entries. 
45  *
46  * This is only for internal list manipulation where we know
47  * the prev/next entries already!
48  */
49 static inline void __list_add(struct list_head *new,
50                               struct list_head *prev,
51                               struct list_head *next)
52 {
53         next->prev = new;
54         new->next = next;
55         new->prev = prev;
56         prev->next = new;
57 }
58
59 /**
60  * list_add - add a new entry
61  * @new: new entry to be added
62  * @head: list head to add it after
63  *
64  * Insert a new entry after the specified head.
65  * This is good for implementing stacks.
66  */
67 static inline void list_add(struct list_head *new, struct list_head *head)
68 {
69         __list_add(new, head, head->next);
70 }
71
72 /**
73  * list_add_tail - add a new entry
74  * @new: new entry to be added
75  * @head: list head to add it before
76  *
77  * Insert a new entry before the specified head.
78  * This is useful for implementing queues.
79  */
80 static inline void list_add_tail(struct list_head *new, struct list_head *head)
81 {
82         __list_add(new, head->prev, head);
83 }
84
85 /*
86  * Delete a list entry by making the prev/next entries
87  * point to each other.
88  *
89  * This is only for internal list manipulation where we know
90  * the prev/next entries already!
91  */
92 static inline void __list_del(struct list_head * prev, struct list_head * next)
93 {
94         next->prev = prev;
95         prev->next = next;
96 }
97
98 /**
99  * list_del - deletes entry from list.
100  * @entry: the element to delete from the list.
101  * Note: list_empty on entry does not return true after this, the entry is
102  * in an undefined state.
103  */
104 static inline void list_del(struct list_head *entry)
105 {
106         __list_del(entry->prev, entry->next);
107 }
108
109 /**
110  * list_del_init - deletes entry from list and reinitialize it.
111  * @entry: the element to delete from the list.
112  */
113 static inline void list_del_init(struct list_head *entry)
114 {
115         __list_del(entry->prev, entry->next);
116         INIT_LIST_HEAD(entry); 
117 }
118
119 /**
120  * list_move - delete from one list and add as another's head
121  * @list: the entry to move
122  * @head: the head that will precede our entry
123  */
124 static inline void list_move(struct list_head *list, struct list_head *head)
125 {
126         __list_del(list->prev, list->next);
127         list_add(list, head);
128 }
129
130 /**
131  * list_move_tail - delete from one list and add as another's tail
132  * @list: the entry to move
133  * @head: the head that will follow our entry
134  */
135 static inline void list_move_tail(struct list_head *list,
136                                   struct list_head *head)
137 {
138         __list_del(list->prev, list->next);
139         list_add_tail(list, head);
140 }
141
142 /**
143  * list_empty - tests whether a list is empty
144  * @head: the list to test.
145  */
146 static inline int list_empty(struct list_head *head)
147 {
148         return head->next == head;
149 }
150
151 static inline void __list_splice(struct list_head *list,
152                                  struct list_head *head)
153 {
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 {
172         if (!list_empty(list))
173                 __list_splice(list, head);
174 }
175
176 /**
177  * list_splice_init - join two lists and reinitialise the emptied list.
178  * @list: the new list to add.
179  * @head: the place to add it in the first list.
180  *
181  * The list at @list is reinitialised
182  */
183 static inline void list_splice_init(struct list_head *list,
184                                     struct list_head *head)
185 {
186         if (!list_empty(list)) {
187                 __list_splice(list, head);
188                 INIT_LIST_HEAD(list);
189         }
190 }
191
192 /**
193  * list_entry - get the struct for this entry
194  * @ptr:        the &struct list_head pointer.
195  * @type:       the type of the struct this is embedded in.
196  * @member:     the name of the list_struct within the struct.
197  */
198 #define list_entry(ptr, type, member) \
199         container_of(ptr, type, member)
200
201 /**
202  * list_for_each        -       iterate over a list
203  * @pos:        the &struct list_head to use as a loop counter.
204  * @head:       the head for your list.
205  */
206 #define list_for_each(pos, head) \
207         for (pos = (head)->next; pos != (head); pos = pos->next)
208
209 /**
210  * list_for_each_prev   -       iterate over a list backwards
211  * @pos:        the &struct list_head to use as a loop counter.
212  * @head:       the head for your list.
213  */
214 #define list_for_each_prev(pos, head) \
215         for (pos = (head)->prev; pos != (head); pos = pos->prev)
216                 
217 /**
218  * list_for_each_safe   -       iterate over a list safe against removal of list entry
219  * @pos:        the &struct list_head to use as a loop counter.
220  * @n:          another &struct list_head to use as temporary storage
221  * @head:       the head for your list.
222  */
223 #define list_for_each_safe(pos, n, head) \
224         for (pos = (head)->next, n = pos->next; pos != (head); \
225                 pos = n, n = pos->next)
226
227 /**
228  * list_for_each_entry  -       iterate over list of given type
229  * @pos:        the type * to use as a loop counter.
230  * @head:       the head for your list.
231  * @member:     the name of the list_struct within the struct.
232  */
233 #define list_for_each_entry(pos, head, member)                          \
234         for (pos = list_entry((head)->next, typeof(*pos), member);      \
235              &pos->member != (head);                                    \
236              pos = list_entry(pos->member.next, typeof(*pos), member))
237
238 /**
239  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
240  * @pos:        the type * to use as a loop cursor.
241  * @n:          another type * to use as temporary storage
242  * @head:       the head for your list.
243  * @member:     the name of the list_struct within the struct.
244  */
245 #define list_for_each_entry_safe(pos, n, head, member)                  \
246         for (pos = list_entry((head)->next, typeof(*pos), member),      \
247                 n = list_entry(pos->member.next, typeof(*pos), member); \
248              &pos->member != (head);                                    \
249              pos = n, n = list_entry(n->member.next, typeof(*n), member))
250
251 #endif