]> rtime.felk.cvut.cz Git - orte.git/blob - orte/include/ul_listbase.h
Added prerelease of ORTE-0.2 (Real Time Publisher Subscriber communication protocol...
[orte.git] / orte / include / ul_listbase.h
1 #ifndef __KERNEL__
2
3 /* In RT-Linux this functions collide with Linux lists functions, so
4  * we use Linux functions instead. */
5
6 #ifndef _UL_LISTBASE_H
7 #define _UL_LISTBASE_H
8
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12
13 #define LIST_POISON1  ((void *) 0)
14 #define LIST_POISON2  ((void *) 0)
15
16 /*
17  * Simple doubly linked list implementation.
18  *
19  * Some of the internal functions ("__xxx") are useful when
20  * manipulating whole lists rather than single entries, as
21  * sometimes we already know the next/prev entries and we can
22  * generate better code by using them directly rather than
23  * using the generic single-entry routines.
24  */
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 /*
40  * Insert a new entry between two known consecutive entries. 
41  *
42  * This is only for internal list manipulation where we know
43  * the prev/next entries already!
44  */
45 static inline void __list_add(struct list_head *new,
46                               struct list_head *prev,
47                               struct list_head *next)
48 {
49         next->prev = new;
50         new->next = next;
51         new->prev = prev;
52         prev->next = new;
53 }
54
55 /**
56  * list_add - add a new entry
57  * @new: new entry to be added
58  * @head: list head to add it after
59  *
60  * Insert a new entry after the specified head.
61  * This is good for implementing stacks.
62  */
63 static inline void list_add(struct list_head *new, struct list_head *head)
64 {
65         __list_add(new, head, head->next);
66 }
67
68 /**
69  * list_add_tail - add a new entry
70  * @new: new entry to be added
71  * @head: list head to add it before
72  *
73  * Insert a new entry before the specified head.
74  * This is useful for implementing queues.
75  */
76 static inline void list_add_tail(struct list_head *new, struct list_head *head)
77 {
78         __list_add(new, head->prev, head);
79 }
80
81 /*
82  * Delete a list entry by making the prev/next entries
83  * point to each other.
84  *
85  * This is only for internal list manipulation where we know
86  * the prev/next entries already!
87  */
88 static inline void __list_del(struct list_head * prev, struct list_head * next)
89 {
90         next->prev = prev;
91         prev->next = next;
92 }
93
94 /**
95  * list_del - deletes entry from list.
96  * @entry: the element to delete from the list.
97  * Note: list_empty on entry does not return true after this, the entry is
98  * in an undefined state.
99  */
100 static inline void list_del(struct list_head *entry)
101 {
102         __list_del(entry->prev, entry->next);
103         entry->next = LIST_POISON1;
104         entry->prev = LIST_POISON2;
105 }
106
107 /**
108  * list_del_init - deletes entry from list and reinitialize it.
109  * @entry: the element to delete from the list.
110  */
111 static inline void list_del_init(struct list_head *entry)
112 {
113         __list_del(entry->prev, entry->next);
114         INIT_LIST_HEAD(entry); 
115 }
116
117 /**
118  * list_move - delete from one list and add as another's head
119  * @list: the entry to move
120  * @head: the head that will precede our entry
121  */
122 static inline void list_move(struct list_head *list, struct list_head *head)
123 {
124         __list_del(list->prev, list->next);
125         list_add(list, head);
126 }
127
128 /**
129  * list_move_tail - delete from one list and add as another's tail
130  * @list: the entry to move
131  * @head: the head that will follow our entry
132  */
133 static inline void list_move_tail(struct list_head *list,
134                                   struct list_head *head)
135 {
136         __list_del(list->prev, list->next);
137         list_add_tail(list, head);
138 }
139
140 /**
141  * list_empty - tests whether a list is empty
142  * @head: the list to test.
143  */
144 static inline int list_empty(struct list_head *head)
145 {
146         return head->next == head;
147 }
148
149 static inline void __list_splice(struct list_head *list,
150                                  struct list_head *head)
151 {
152         struct list_head *first = list->next;
153         struct list_head *last = list->prev;
154         struct list_head *at = head->next;
155
156         first->prev = head;
157         head->next = first;
158
159         last->next = at;
160         at->prev = last;
161 }
162
163 /**
164  * list_splice - join two lists
165  * @list: the new list to add.
166  * @head: the place to add it in the first list.
167  */
168 static inline void list_splice(struct list_head *list, struct list_head *head)
169 {
170         if (!list_empty(list))
171                 __list_splice(list, head);
172 }
173
174 /**
175  * list_splice_init - join two lists and reinitialise the emptied list.
176  * @list: the new list to add.
177  * @head: the place to add it in the first list.
178  *
179  * The list at @list is reinitialised
180  */
181 static inline void list_splice_init(struct list_head *list,
182                                     struct list_head *head)
183 {
184         if (!list_empty(list)) {
185                 __list_splice(list, head);
186                 INIT_LIST_HEAD(list);
187         }
188 }
189
190 /**
191  * list_entry - get the struct for this entry
192  * @ptr:        the &struct list_head pointer.
193  * @type:       the type of the struct this is embedded in.
194  * @member:     the name of the list_struct within the struct.
195  */
196 #define list_entry(ptr, type, member) \
197         container_of(ptr, type, member)
198
199 /**
200  * list_for_each        -       iterate over a list
201  * @pos:        the &struct list_head to use as a loop counter.
202  * @head:       the head for your list.
203  */
204 #define list_for_each(pos, head) \
205         for (pos = (head)->next, prefetch(pos->next); pos != (head); \
206                 pos = pos->next, prefetch(pos->next))
207
208 /**
209  * __list_for_each      -       iterate over a list
210  * @pos:        the &struct list_head to use as a loop counter.
211  * @head:       the head for your list.
212  *
213  * This variant differs from list_for_each() in that it's the
214  * simplest possible list iteration code, no prefetching is done.
215  * Use this for code that knows the list to be very short (empty
216  * or 1 entry) most of the time.
217  */
218 #define __list_for_each(pos, head) \
219         for (pos = (head)->next; pos != (head); pos = pos->next)
220
221 /**
222  * list_for_each_prev   -       iterate over a list backwards
223  * @pos:        the &struct list_head to use as a loop counter.
224  * @head:       the head for your list.
225  */
226 #define list_for_each_prev(pos, head) \
227         for (pos = (head)->prev, prefetch(pos->prev); pos != (head); \
228                 pos = pos->prev, prefetch(pos->prev))
229                 
230 /**
231  * list_for_each_safe   -       iterate over a list safe against removal of list entry
232  * @pos:        the &struct list_head to use as a loop counter.
233  * @n:          another &struct list_head to use as temporary storage
234  * @head:       the head for your list.
235  */
236 #define list_for_each_safe(pos, n, head) \
237         for (pos = (head)->next, n = pos->next; pos != (head); \
238                 pos = n, n = pos->next)
239
240 /**
241  * list_for_each_entry  -       iterate over list of given type
242  * @pos:        the type * to use as a loop counter.
243  * @head:       the head for your list.
244  * @member:     the name of the list_struct within the struct.
245  */
246 #define list_for_each_entry(pos, head, member)                          \
247         for (pos = list_entry((head)->next, typeof(*pos), member),      \
248                      prefetch(pos->member.next);                        \
249              &pos->member != (head);                                    \
250              pos = list_entry(pos->member.next, typeof(*pos), member),  \
251                      prefetch(pos->member.next))
252
253 /**
254  * list_for_each_entry_reverse - iterate backwards over list of given type.
255  * @pos:        the type * to use as a loop counter.
256  * @head:       the head for your list.
257  * @member:     the name of the list_struct within the struct.
258  */
259 #define list_for_each_entry_reverse(pos, head, member)                  \
260         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
261                      prefetch(pos->member.prev);                        \
262              &pos->member != (head);                                    \
263              pos = list_entry(pos->member.prev, typeof(*pos), member),  \
264                      prefetch(pos->member.prev))
265
266
267 /**
268  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
269  * @pos:        the type * to use as a loop counter.
270  * @n:          another type * to use as temporary storage
271  * @head:       the head for your list.
272  * @member:     the name of the list_struct within the struct.
273  */
274 #define list_for_each_entry_safe(pos, n, head, member)                  \
275         for (pos = list_entry((head)->next, typeof(*pos), member),      \
276                 n = list_entry(pos->member.next, typeof(*pos), member); \
277              &pos->member != (head);                                    \
278              pos = n, n = list_entry(n->member.next, typeof(*n), member))
279
280 #ifdef __cplusplus
281 } /* extern "C"*/
282 #endif
283
284 #endif /* _UL_LISTBASE_H */
285
286 #endif /* __RTL__ */