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.


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