7 #include <palacios/vmm_string.h>
10 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
13 #define container_of(ptr, type, member) ({ \
14 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
15 (type *)( (char *)__mptr - offsetof(type,member) );})
18 static inline void prefetch(const void *x) {;}
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.
25 #define LIST_POISON1 ((void *) 0x00100100)
26 #define LIST_POISON2 ((void *) 0x00200200)
29 * Simple doubly linked list implementation.
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.
39 struct list_head *next, *prev;
42 #define LIST_HEAD_INIT(name) { &(name), &(name) }
44 #define LIST_HEAD(name) \
45 struct list_head name = LIST_HEAD_INIT(name)
47 static inline void INIT_LIST_HEAD(struct list_head *list)
54 * Insert a new entry between two known consecutive entries.
56 * This is only for internal list manipulation where we know
57 * the prev/next entries already!
59 static inline void __list_add(struct list_head *new,
60 struct list_head *prev,
61 struct list_head *next)
70 * list_add - add a new entry
71 * @new: new entry to be added
72 * @head: list head to add it after
74 * Insert a new entry after the specified head.
75 * This is good for implementing stacks.
77 static inline void list_add(struct list_head *new, struct list_head *head)
79 __list_add(new, head, head->next);
83 * list_add_tail - add a new entry
84 * @new: new entry to be added
85 * @head: list head to add it before
87 * Insert a new entry before the specified head.
88 * This is useful for implementing queues.
90 static inline void list_add_tail(struct list_head *new, struct list_head *head)
92 __list_add(new, head->prev, head);
96 * Delete a list entry by making the prev/next entries
97 * point to each other.
99 * This is only for internal list manipulation where we know
100 * the prev/next entries already!
102 static inline void __list_del(struct list_head * prev, struct list_head * next)
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.
114 static inline void list_del(struct list_head *entry)
116 __list_del(entry->prev, entry->next);
117 entry->next = LIST_POISON1;
118 entry->prev = LIST_POISON2;
122 * list_del_init - deletes entry from list and reinitialize it.
123 * @entry: the element to delete from the list.
125 static inline void list_del_init(struct list_head *entry)
127 __list_del(entry->prev, entry->next);
128 INIT_LIST_HEAD(entry);
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
136 static inline void list_move(struct list_head *list, struct list_head *head)
138 __list_del(list->prev, list->next);
139 list_add(list, head);
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
147 static inline void list_move_tail(struct list_head *list,
148 struct list_head *head)
150 __list_del(list->prev, list->next);
151 list_add_tail(list, head);
155 * list_empty - tests whether a list is empty
156 * @head: the list to test.
158 static inline int list_empty(const struct list_head *head)
160 return head->next == head;
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
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.
173 * @head: the list to test.
175 static inline int list_empty_careful(const struct list_head *head)
177 struct list_head *next = head->next;
178 return (next == head) && (next == head->prev);
181 static inline void __list_splice(struct list_head *list,
182 struct list_head *head)
184 struct list_head *first = list->next;
185 struct list_head *last = list->prev;
186 struct list_head *at = head->next;
196 * list_splice - join two lists
197 * @list: the new list to add.
198 * @head: the place to add it in the first list.
200 static inline void list_splice(struct list_head *list, struct list_head *head)
202 if (!list_empty(list))
203 __list_splice(list, head);
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.
211 * The list at @list is reinitialised
213 static inline void list_splice_init(struct list_head *list,
214 struct list_head *head)
216 if (!list_empty(list)) {
217 __list_splice(list, head);
218 INIT_LIST_HEAD(list);
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.
228 #define list_entry(ptr, type, member) \
229 container_of(ptr, type, member)
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.
236 #define list_for_each(pos, head) \
237 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
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.
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.
250 #define __list_for_each(pos, head) \
251 for (pos = (head)->next; pos != (head); pos = pos->next)
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.
258 #define list_for_each_prev(pos, head) \
259 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
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.
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)
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.
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))
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.
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))
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.
301 #define list_prepare_entry(pos, head, member) \
302 ((pos) ? : list_entry(head, typeof(*pos), member))
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.
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))
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.
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))
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.
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))
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.
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))
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.
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))
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.
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))
382 * Double linked lists with a single pointer list head.
383 * Mostly useful for hash tables where the two pointer list head is
385 * You lose the ability to access the tail in O(1).
389 struct hlist_node *first;
393 struct hlist_node *next, **pprev;
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)
405 static inline int hlist_unhashed(const struct hlist_node *h)
410 static inline int hlist_empty(const struct hlist_head *h)
415 static inline void __hlist_del(struct hlist_node *n)
417 struct hlist_node *next = n->next;
418 struct hlist_node **pprev = n->pprev;
424 static inline void hlist_del(struct hlist_node *n)
427 n->next = LIST_POISON1;
428 n->pprev = LIST_POISON2;
431 static inline void hlist_del_init(struct hlist_node *n)
433 if (!hlist_unhashed(n)) {
439 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
441 struct hlist_node *first = h->first;
444 first->pprev = &n->next;
446 n->pprev = &h->first;
449 /* next must be != NULL */
450 static inline void hlist_add_before(struct hlist_node *n,
451 struct hlist_node *next)
453 n->pprev = next->pprev;
455 next->pprev = &n->next;
459 static inline void hlist_add_after(struct hlist_node *n,
460 struct hlist_node *next)
462 next->next = n->next;
464 next->pprev = &n->next;
467 next->next->pprev = &next->next;
470 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
472 #define hlist_for_each(pos, head) \
473 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
476 #define hlist_for_each_safe(pos, n, head) \
477 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
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.
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;}); \
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.
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;}); \
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.
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;}); \
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.
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;}); \
532 //#endif // ! __V3VEE__