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.


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