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.


removed derogatory comments about Linux
[palacios.git] / palacios / include / palacios / vmm_list.h
1 #ifndef _VMM_LIST_H
2 #define _VMM_LIST_H
3
4
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) {const void * foo; foo = 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_entry - get the struct for the tail entry
233  * @ptr:        the list_head head pointer.
234  * @type:       the type of the struct this is embedded in.
235  * @member:     the name of the list_struct within the struct.
236  */
237 #define list_tail_entry(head, type, member) ({  \
238       type * tail = NULL;                       \
239       if ((head)->prev != (head)) {                     \
240         tail = list_entry((head)->prev, type, member);  \
241       }                                                 \
242       tail;                                             \
243 })
244
245 /**
246  * list_for_each        -       iterate over a list
247  * @pos:        the &struct list_head to use as a loop counter.
248  * @head:       the head for your list.
249  */
250 #define list_for_each(pos, head) \
251         for (pos = (head)->next; prefetch(pos->next), pos != (head); \
252                 pos = pos->next)
253
254 /**
255  * __list_for_each      -       iterate over a list
256  * @pos:        the &struct list_head to use as a loop counter.
257  * @head:       the head for your list.
258  *
259  * This variant differs from list_for_each() in that it's the
260  * simplest possible list iteration code, no prefetching is done.
261  * Use this for code that knows the list to be very short (empty
262  * or 1 entry) most of the time.
263  */
264 #define __list_for_each(pos, head) \
265         for (pos = (head)->next; pos != (head); pos = pos->next)
266
267 /**
268  * list_for_each_prev   -       iterate over a list backwards
269  * @pos:        the &struct list_head to use as a loop counter.
270  * @head:       the head for your list.
271  */
272 #define list_for_each_prev(pos, head) \
273         for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
274                 pos = pos->prev)
275
276 /**
277  * list_for_each_safe   -       iterate over a list safe against removal of list entry
278  * @pos:        the &struct list_head to use as a loop counter.
279  * @n:          another &struct list_head to use as temporary storage
280  * @head:       the head for your list.
281  */
282 #define list_for_each_safe(pos, n, head) \
283         for (pos = (head)->next, n = pos->next; pos != (head); \
284                 pos = n, n = pos->next)
285
286 /**
287  * list_for_each_entry  -       iterate over list of given type
288  * @pos:        the type * to use as a loop counter.
289  * @head:       the head for your list.
290  * @member:     the name of the list_struct within the struct.
291  */
292 #define list_for_each_entry(pos, head, member)                          \
293         for (pos = list_entry((head)->next, typeof(*pos), member);      \
294              prefetch(pos->member.next), &pos->member != (head);        \
295              pos = list_entry(pos->member.next, typeof(*pos), member))
296
297 /**
298  * list_for_each_entry_reverse - iterate backwards over list of given type.
299  * @pos:        the type * to use as a loop counter.
300  * @head:       the head for your list.
301  * @member:     the name of the list_struct within the struct.
302  */
303 #define list_for_each_entry_reverse(pos, head, member)                  \
304         for (pos = list_entry((head)->prev, typeof(*pos), member);      \
305              prefetch(pos->member.prev), &pos->member != (head);        \
306              pos = list_entry(pos->member.prev, typeof(*pos), member))
307
308 /**
309  * list_prepare_entry - prepare a pos entry for use as a start point in
310  *                      list_for_each_entry_continue
311  * @pos:        the type * to use as a start point
312  * @head:       the head of the list
313  * @member:     the name of the list_struct within the struct.
314  */
315 #define list_prepare_entry(pos, head, member) \
316         ((pos) ? : list_entry(head, typeof(*pos), member))
317
318 /**
319  * list_for_each_entry_continue -       iterate over list of given type
320  *                      continuing after existing point
321  * @pos:        the type * to use as a loop counter.
322  * @head:       the head for your list.
323  * @member:     the name of the list_struct within the struct.
324  */
325 #define list_for_each_entry_continue(pos, head, member)                 \
326         for (pos = list_entry(pos->member.next, typeof(*pos), member);  \
327              prefetch(pos->member.next), &pos->member != (head);        \
328              pos = list_entry(pos->member.next, typeof(*pos), member))
329
330 /**
331  * list_for_each_entry_from -   iterate over list of given type
332  *                      continuing from existing point
333  * @pos:        the type * to use as a loop counter.
334  * @head:       the head for your list.
335  * @member:     the name of the list_struct within the struct.
336  */
337 #define list_for_each_entry_from(pos, head, member)                     \
338         for (; prefetch(pos->member.next), &pos->member != (head);      \
339              pos = list_entry(pos->member.next, typeof(*pos), member))
340
341 /**
342  * list_for_each_entry_safe - iterate over list of given type 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(pos, n, head, member)                  \
349         for (pos = list_entry((head)->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_continue -  iterate over list of given type
356  *                      continuing after 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_continue(pos, n, head, member)                 \
363         for (pos = list_entry(pos->member.next, typeof(*pos), member),          \
364                 n = list_entry(pos->member.next, typeof(*pos), member);         \
365              &pos->member != (head);                                            \
366              pos = n, n = list_entry(n->member.next, typeof(*n), member))
367
368 /**
369  * list_for_each_entry_safe_from - iterate over list of given type
370  *                      from existing point safe against removal of list entry
371  * @pos:        the type * to use as a loop counter.
372  * @n:          another type * to use as temporary storage
373  * @head:       the head for your list.
374  * @member:     the name of the list_struct within the struct.
375  */
376 #define list_for_each_entry_safe_from(pos, n, head, member)                     \
377         for (n = list_entry(pos->member.next, typeof(*pos), member);            \
378              &pos->member != (head);                                            \
379              pos = n, n = list_entry(n->member.next, typeof(*n), member))
380
381 /**
382  * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
383  *                                    removal of list entry
384  * @pos:        the type * to use as a loop counter.
385  * @n:          another type * to use as temporary storage
386  * @head:       the head for your list.
387  * @member:     the name of the list_struct within the struct.
388  */
389 #define list_for_each_entry_safe_reverse(pos, n, head, member)          \
390         for (pos = list_entry((head)->prev, typeof(*pos), member),      \
391                 n = list_entry(pos->member.prev, typeof(*pos), member); \
392              &pos->member != (head);                                    \
393              pos = n, n = list_entry(n->member.prev, typeof(*n), member))
394
395 /*
396  * Double linked lists with a single pointer list head.
397  * Mostly useful for hash tables where the two pointer list head is
398  * too wasteful.
399  * You lose the ability to access the tail in O(1).
400  */
401
402 struct hlist_head {
403         struct hlist_node *first;
404 };
405
406 struct hlist_node {
407         struct hlist_node *next, **pprev;
408 };
409
410 #define HLIST_HEAD_INIT { .first = NULL }
411 #define HLIST_HEAD(name) struct hlist_head name = {  .first = NULL }
412 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
413 static inline void INIT_HLIST_NODE(struct hlist_node *h)
414 {
415         h->next = NULL;
416         h->pprev = NULL;
417 }
418
419 static inline int hlist_unhashed(const struct hlist_node *h)
420 {
421         return !h->pprev;
422 }
423
424 static inline int hlist_empty(const struct hlist_head *h)
425 {
426         return !h->first;
427 }
428
429 static inline void __hlist_del(struct hlist_node *n)
430 {
431         struct hlist_node *next = n->next;
432         struct hlist_node **pprev = n->pprev;
433         *pprev = next;
434         if (next)
435                 next->pprev = pprev;
436 }
437
438 static inline void hlist_del(struct hlist_node *n)
439 {
440         __hlist_del(n);
441         n->next = LIST_POISON1;
442         n->pprev = LIST_POISON2;
443 }
444
445 static inline void hlist_del_init(struct hlist_node *n)
446 {
447         if (!hlist_unhashed(n)) {
448                 __hlist_del(n);
449                 INIT_HLIST_NODE(n);
450         }
451 }
452
453 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
454 {
455         struct hlist_node *first = h->first;
456         n->next = first;
457         if (first)
458                 first->pprev = &n->next;
459         h->first = n;
460         n->pprev = &h->first;
461 }
462
463 /* next must be != NULL */
464 static inline void hlist_add_before(struct hlist_node *n,
465                                         struct hlist_node *next)
466 {
467         n->pprev = next->pprev;
468         n->next = next;
469         next->pprev = &n->next;
470         *(n->pprev) = n;
471 }
472
473 static inline void hlist_add_after(struct hlist_node *n,
474                                         struct hlist_node *next)
475 {
476         next->next = n->next;
477         n->next = next;
478         next->pprev = &n->next;
479
480         if(next->next)
481                 next->next->pprev  = &next->next;
482 }
483
484 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
485
486 #define hlist_for_each(pos, head) \
487         for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
488              pos = pos->next)
489
490 #define hlist_for_each_safe(pos, n, head) \
491         for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
492              pos = n)
493
494 /**
495  * hlist_for_each_entry - iterate over list of given type
496  * @tpos:       the type * to use as a loop counter.
497  * @pos:        the &struct hlist_node to use as a loop counter.
498  * @head:       the head for your list.
499  * @member:     the name of the hlist_node within the struct.
500  */
501 #define hlist_for_each_entry(tpos, pos, head, member)                    \
502         for (pos = (head)->first;                                        \
503              pos && ({ prefetch(pos->next); 1;}) &&                      \
504                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
505              pos = pos->next)
506
507 /**
508  * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
509  * @tpos:       the type * to use as a loop counter.
510  * @pos:        the &struct hlist_node to use as a loop counter.
511  * @member:     the name of the hlist_node within the struct.
512  */
513 #define hlist_for_each_entry_continue(tpos, pos, member)                 \
514         for (pos = (pos)->next;                                          \
515              pos && ({ prefetch(pos->next); 1;}) &&                      \
516                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
517              pos = pos->next)
518
519 /**
520  * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
521  * @tpos:       the type * to use as a loop counter.
522  * @pos:        the &struct hlist_node to use as a loop counter.
523  * @member:     the name of the hlist_node within the struct.
524  */
525 #define hlist_for_each_entry_from(tpos, pos, member)                     \
526         for (; pos && ({ prefetch(pos->next); 1;}) &&                    \
527                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
528              pos = pos->next)
529
530 /**
531  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
532  * @tpos:       the type * to use as a loop counter.
533  * @pos:        the &struct hlist_node to use as a loop counter.
534  * @n:          another &struct hlist_node to use as temporary storage
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_safe(tpos, pos, n, head, member)            \
539         for (pos = (head)->first;                                        \
540              pos && ({ n = pos->next; 1; }) &&                           \
541                 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
542              pos = n)
543
544
545
546 #endif // ! __V3VEE__
547
548 #endif