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.


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