Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Merge branch 'devel'
[palacios.git] / kitten / include / lwk / list.h
1 #ifndef _LWK_LIST_H
2 #define _LWK_LIST_H
3
4 #ifdef __KERNEL__
5
6 #include <lwk/stddef.h>
7 #include <lwk/prefetch.h>
8
9 /*
10  * These are non-NULL pointers that will result in page faults
11  * under normal circumstances, used to verify that nobody uses
12  * non-initialized list entries.
13  */
14 #define LIST_POISON1  ((void *) 0x00100100)
15 #define LIST_POISON2  ((void *) 0x00200200)
16
17 /*
18  * Simple doubly linked list implementation.
19  *
20  * Some of the internal functions ("__xxx") are useful when
21  * manipulating whole lists rather than single entries, as
22  * sometimes we already know the next/prev entries and we can
23  * generate better code by using them directly rather than
24  * using the generic single-entry routines.
25  */
26
27 struct list_head {
28         struct list_head *next, *prev;
29 };
30
31 #define LIST_HEAD_INIT(name) { &(name), &(name) }
32
33 #define LIST_HEAD(name) \
34         struct list_head name = LIST_HEAD_INIT(name)
35
36 static inline void INIT_LIST_HEAD(struct list_head *list)
37 {
38         list->next = list;
39         list->prev = list;
40 }
41
42 static inline void list_head_init(struct list_head *list)
43 {
44         INIT_LIST_HEAD(list);
45 }
46
47 /*
48  * Insert a new entry between two known consecutive entries.
49  *
50  * This is only for internal list manipulation where we know
51  * the prev/next entries already!
52  */
53 static inline void __list_add(struct list_head *new,
54                               struct list_head *prev,
55                               struct list_head *next)
56 {
57         next->prev = new;
58         new->next = next;
59         new->prev = prev;
60         prev->next = new;
61 }
62
63 /**
64  * list_add - add a new entry
65  * @new: new entry to be added
66  * @head: list head to add it after
67  *
68  * Insert a new entry after the specified head.
69  * This is good for implementing stacks.
70  */
71 static inline void list_add(struct list_head *new, struct list_head *head)
72 {
73         __list_add(new, head, head->next);
74 }
75
76 /**
77  * list_add_tail - add a new entry
78  * @new: new entry to be added
79  * @head: list head to add it before
80  *
81  * Insert a new entry before the specified head.
82  * This is useful for implementing queues.
83  */
84 static inline void list_add_tail(struct list_head *new, struct list_head *head)
85 {
86         __list_add(new, head->prev, head);
87 }
88
89 /*
90  * Delete a list entry by making the prev/next entries
91  * point to each other.
92  *
93  * This is only for internal list manipulation where we know
94  * the prev/next entries already!
95  */
96 static inline void __list_del(struct list_head * prev, struct list_head * next)
97 {
98         next->prev = prev;
99         prev->next = next;
100 }
101
102 /**
103  * list_del - deletes entry from list.
104  * @entry: the element to delete from the list.
105  * Note: list_empty on entry does not return true after this, the entry is
106  * in an undefined state.
107  */
108 static inline void list_del(struct list_head *entry)
109 {
110         __list_del(entry->prev, entry->next);
111         entry->next = LIST_POISON1;
112         entry->prev = LIST_POISON2;
113 }
114
115 /**
116  * list_del_init - deletes entry from list and reinitialize it.
117  * @entry: the element to delete from the list.
118  */
119 static inline void list_del_init(struct list_head *entry)
120 {
121         __list_del(entry->prev, entry->next);
122         INIT_LIST_HEAD(entry);
123 }
124
125 /**
126  * list_move - delete from one list and add as another's head
127  * @list: the entry to move
128  * @head: the head that will precede our entry
129  */
130 static inline void list_move(struct list_head *list, struct list_head *head)
131 {
132         __list_del(list->prev, list->next);
133         list_add(list, head);
134 }
135
136 /**
137  * list_move_tail - delete from one list and add as another's tail
138  * @list: the entry to move
139  * @head: the head that will follow our entry
140  */
141 static inline void list_move_tail(struct list_head *list,
142                                   struct list_head *head)
143 {
144         __list_del(list->prev, list->next);
145         list_add_tail(list, head);
146 }
147
148 /**
149  * list_empty - tests whether a list is empty
150  * @head: the list to test.
151  */
152 static inline int list_empty(const struct list_head *head)
153 {
154         return head->next == head;
155 }
156
157 /**
158  * list_empty_careful - tests whether a list is
159  * empty _and_ checks that no other CPU might be
160  * in the process of still modifying either member
161  *
162  * NOTE: using list_empty_careful() without synchronization
163  * can only be safe if the only activity that can happen
164  * to the list entry is list_del_init(). Eg. it cannot be used
165  * if another CPU could re-list_add() it.
166  *
167  * @head: the list to test.
168  */
169 static inline int list_empty_careful(const struct list_head *head)
170 {
171         struct list_head *next = head->next;
172         return (next == head) && (next == head->prev);
173 }
174
175 static inline void __list_splice(struct list_head *list,
176                                  struct list_head *head)
177 {
178         struct list_head *first = list->next;
179         struct list_head *last = list->prev;
180         struct list_head *at = head->next;
181
182         first->prev = head;
183         head->next = first;
184
185         last->next = at;
186         at->prev = last;
187 }
188
189 /**
190  * list_splice - join two lists
191  * @list: the new list to add.
192  * @head: the place to add it in the first list.
193  */
194 static inline void list_splice(struct list_head *list, struct list_head *head)
195 {
196         if (!list_empty(list))
197                 __list_splice(list, head);
198 }
199
200 /**
201  * list_splice_init - join two lists and reinitialise the emptied list.
202  * @list: the new list to add.
203  * @head: the place to add it in the first list.
204  *
205  * The list at @list is reinitialised
206  */
207 static inline void list_splice_init(struct list_head *list,
208                                     struct list_head *head)
209 {
210         if (!list_empty(list)) {
211                 __list_splice(list, head);
212                 INIT_LIST_HEAD(list);
213         }
214 }
215
216 /**
217  * list_entry - get the struct for this entry
218  * @ptr:        the &struct list_head pointer.
219  * @type:       the type of the struct this is embedded in.
220  * @member:     the name of the list_struct within the struct.
221  */
222 #define list_entry(ptr, type, member) \
223         container_of(ptr, type, member)
224
225 /**
226  * list_for_each        -       iterate over a list
227  * @pos:        the &struct list_head to use as a loop counter.
228  * @head:       the head for your list.
229  */
230 #define list_for_each(pos, head) \
231         for (pos = (head)->next; prefetch(pos->next), pos != (head); \
232                 pos = pos->next)
233
234 /**
235  * __list_for_each      -       iterate over a list
236  * @pos:        the &struct list_head to use as a loop counter.
237  * @head:       the head for your list.
238  *
239  * This variant differs from list_for_each() in that it's the
240  * simplest possible list iteration code, no prefetching is done.
241  * Use this for code that knows the list to be very short (empty
242  * or 1 entry) most of the time.
243  */
244 #define __list_for_each(pos, head) \
245         for (pos = (head)->next; pos != (head); pos = pos->next)
246
247 /**
248  * list_for_each_prev   -       iterate over a list backwards
249  * @pos:        the &struct list_head to use as a loop counter.
250  * @head:       the head for your list.
251  */
252 #define list_for_each_prev(pos, head) \
253         for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
254                 pos = pos->prev)
255
256 /**
257  * list_for_each_safe   -       iterate over a list safe against removal of list entry
258  * @pos:        the &struct list_head to use as a loop counter.
259  * @n:          another &struct list_head to use as temporary storage
260  * @head:       the head for your list.
261  */
262 #define list_for_each_safe(pos, n, head) \
263         for (pos = (head)->next, n = pos->next; pos != (head); \
264                 pos = n, n = pos->next)
265
266 /**
267  * list_for_each_entry  -       iterate over list of given type
268  * @pos:        the type * to use as a loop counter.
269  * @head:       the head for your list.
270  * @member:     the name of the list_struct within the struct.
271  */
272 #define list_for_each_entry(pos, head, member)                          \
273         for (pos = list_entry((head)->next, typeof(*pos), member);      \
274              prefetch(pos->member.next), &pos->member != (head);        \
275              pos = list_entry(pos->member.next, typeof(*pos), member))
276
277 /**
278  * list_for_each_entry_reverse - iterate backwards over list of given type.
279  * @pos:        the type * to use as a loop counter.
280  * @head:       the head for your list.
281  * @member:     the name of the list_struct within the struct.
282  */
283 #define list_for_each_entry_reverse(pos, head, member)                  \
284         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
285              prefetch(pos->member.prev), &pos->member != (head);        \
286              pos = list_entry(pos->member.prev, typeof(*pos), member))
287
288 /**
289  * list_prepare_entry - prepare a pos entry for use as a start point in
290  *                      list_for_each_entry_continue
291  * @pos:        the type * to use as a start point
292  * @head:       the head of the list
293  * @member:     the name of the list_struct within the struct.
294  */
295 #define list_prepare_entry(pos, head, member) \
296         ((pos) ? : list_entry(head, typeof(*pos), member))
297
298 /**
299  * list_for_each_entry_continue -       iterate over list of given type
300  *                      continuing after existing point
301  * @pos:        the type * to use as a loop counter.
302  * @head:       the head for your list.
303  * @member:     the name of the list_struct within the struct.
304  */
305 #define list_for_each_entry_continue(pos, head, member)                 \
306         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
307              prefetch(pos->member.next), &pos->member != (head);        \
308              pos = list_entry(pos->member.next, typeof(*pos), member))
309
310 /**
311  * list_for_each_entry_from -   iterate over list of given type
312  *                      continuing from existing point
313  * @pos:        the type * to use as a loop counter.
314  * @head:       the head for your list.
315  * @member:     the name of the list_struct within the struct.
316  */
317 #define list_for_each_entry_from(pos, head, member)                     \
318         for (; prefetch(pos->member.next), &pos->member != (head);      \
319              pos = list_entry(pos->member.next, typeof(*pos), member))
320
321 /**
322  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
323  * @pos:        the type * to use as a loop counter.
324  * @n:          another type * to use as temporary storage
325  * @head:       the head for your list.
326  * @member:     the name of the list_struct within the struct.
327  */
328 #define list_for_each_entry_safe(pos, n, head, member)                  \
329         for (pos = list_entry((head)->next, typeof(*pos), member),      \
330                 n = list_entry(pos->member.next, typeof(*pos), member); \
331              &pos->member != (head);                                    \
332              pos = n, n = list_entry(n->member.next, typeof(*n), member))
333
334 /**
335  * list_for_each_entry_safe_continue -  iterate over list of given type
336  *                      continuing after existing point safe against removal of list entry
337  * @pos:        the type * to use as a loop counter.
338  * @n:          another type * to use as temporary storage
339  * @head:       the head for your list.
340  * @member:     the name of the list_struct within the struct.
341  */
342 #define list_for_each_entry_safe_continue(pos, n, head, member)                 \
343         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
344                 n = list_entry(pos->member.next, typeof(*pos), member);         \
345              &pos->member != (head);                                            \
346              pos = n, n = list_entry(n->member.next, typeof(*n), member))
347
348 /**
349  * list_for_each_entry_safe_from - iterate over list of given type
350  *                      from existing point safe against removal of list entry
351  * @pos:        the type * to use as a loop counter.
352  * @n:          another type * to use as temporary storage
353  * @head:       the head for your list.
354  * @member:     the name of the list_struct within the struct.
355  */
356 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
357         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
358              &pos->member != (head);                                            \
359              pos = n, n = list_entry(n->member.next, typeof(*n), member))
360
361 /**
362  * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
363  *                                    removal of list entry
364  * @pos:        the type * to use as a loop counter.
365  * @n:          another type * to use as temporary storage
366  * @head:       the head for your list.
367  * @member:     the name of the list_struct within the struct.
368  */
369 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
370         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
371                 n = list_entry(pos->member.prev, typeof(*pos), member); \
372              &pos->member != (head);                                    \
373              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
374
375 /*
376  * Double linked lists with a single pointer list head.
377  * Mostly useful for hash tables where the two pointer list head is
378  * too wasteful.
379  * You lose the ability to access the tail in O(1).
380  */
381
382 struct hlist_head {
383         struct hlist_node *first;
384 };
385
386 struct hlist_node {
387         struct hlist_node *next, **pprev;
388 };
389
390 #define HLIST_HEAD_INIT { .first = NULL }
391 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
392 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
393 static inline void INIT_HLIST_NODE(struct hlist_node *h)
394 {
395         h->next = NULL;
396         h->pprev = NULL;
397 }
398
399 static inline void hlist_node_init(struct hlist_node *h)
400 {
401         INIT_HLIST_NODE(h);
402 }
403
404 static inline int hlist_unhashed(const struct hlist_node *h)
405 {
406         return !h->pprev;
407 }
408
409 static inline int hlist_empty(const struct hlist_head *h)
410 {
411         return !h->first;
412 }
413
414 static inline void __hlist_del(struct hlist_node *n)
415 {
416         struct hlist_node *next = n->next;
417         struct hlist_node **pprev = n->pprev;
418         *pprev = next;
419         if (next)
420                 next->pprev = pprev;
421 }
422
423 static inline void hlist_del(struct hlist_node *n)
424 {
425         __hlist_del(n);
426         n->next = LIST_POISON1;
427         n->pprev = LIST_POISON2;
428 }
429
430 static inline void hlist_del_init(struct hlist_node *n)
431 {
432         if (!hlist_unhashed(n)) {
433                 __hlist_del(n);
434                 INIT_HLIST_NODE(n);
435         }
436 }
437
438 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
439 {
440         struct hlist_node *first = h->first;
441         n->next = first;
442         if (first)
443                 first->pprev = &n->next;
444         h->first = n;
445         n->pprev = &h->first;
446 }
447
448 /* next must be != NULL */
449 static inline void hlist_add_before(struct hlist_node *n,
450                                         struct hlist_node *next)
451 {
452         n->pprev = next->pprev;
453         n->next = next;
454         next->pprev = &n->next;
455         *(n->pprev) = n;
456 }
457
458 static inline void hlist_add_after(struct hlist_node *n,
459                                         struct hlist_node *next)
460 {
461         next->next = n->next;
462         n->next = next;
463         next->pprev = &n->next;
464
465         if(next->next)
466                 next->next->pprev  = &next->next;
467 }
468
469 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
470
471 #define hlist_for_each(pos, head) \
472         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
473              pos = pos->next)
474
475 #define hlist_for_each_safe(pos, n, head) \
476         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
477              pos = n)
478
479 /**
480  * hlist_for_each_entry - iterate over list of given type
481  * @tpos:       the type * to use as a loop counter.
482  * @pos:        the &struct hlist_node to use as a loop counter.
483  * @head:       the head for your list.
484  * @member:     the name of the hlist_node within the struct.
485  */
486 #define hlist_for_each_entry(tpos, pos, head, member)                    \
487         for (pos = (head)->first;                                        \
488              pos && ({ prefetch(pos->next); 1;}) &&                      \
489                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
490              pos = pos->next)
491
492 /**
493  * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
494  * @tpos:       the type * to use as a loop counter.
495  * @pos:        the &struct hlist_node to use as a loop counter.
496  * @member:     the name of the hlist_node within the struct.
497  */
498 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
499         for (pos = (pos)->next;                                          \
500              pos && ({ prefetch(pos->next); 1;}) &&                      \
501                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
502              pos = pos->next)
503
504 /**
505  * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
506  * @tpos:       the type * to use as a loop counter.
507  * @pos:        the &struct hlist_node to use as a loop counter.
508  * @member:     the name of the hlist_node within the struct.
509  */
510 #define hlist_for_each_entry_from(tpos, pos, member)                     \
511         for (; pos && ({ prefetch(pos->next); 1;}) &&                    \
512                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
513              pos = pos->next)
514
515 /**
516  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
517  * @tpos:       the type * to use as a loop counter.
518  * @pos:        the &struct hlist_node to use as a loop counter.
519  * @n:          another &struct hlist_node to use as temporary storage
520  * @head:       the head for your list.
521  * @member:     the name of the hlist_node within the struct.
522  */
523 #define hlist_for_each_entry_safe(tpos, pos, n, head, member)            \
524         for (pos = (head)->first;                                        \
525              pos && ({ n = pos->next; 1; }) &&                           \
526                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
527              pos = n)
528
529 #else
530 #warning "don't include kernel headers in userspace"
531 #endif /* __KERNEL__ */
532 #endif