1 /* Stolen From Linux list implementation */
2 /* 2008, Modifications by Jack Lange <jarusl@cs.northwestern.edu> */
11 #include <palacios/vmm_string.h>
14 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
17 #define container_of(ptr, type, member) ({ \
18 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
19 (type *)( (char *)__mptr - offsetof(type,member) );})
22 static inline void prefetch(const void *x) {const void * foo; foo = x;}
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.
29 #define LIST_POISON1 ((void *) 0x00100100)
30 #define LIST_POISON2 ((void *) 0x00200200)
33 * Simple doubly linked list implementation.
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.
43 struct list_head *next, *prev;
46 #define LIST_HEAD_INIT(name) { &(name), &(name) }
48 #define LIST_HEAD(name) \
49 struct list_head name = LIST_HEAD_INIT(name)
51 static inline void INIT_LIST_HEAD(struct list_head *list)
58 * Insert a new entry between two known consecutive entries.
60 * This is only for internal list manipulation where we know
61 * the prev/next entries already!
63 static inline void __list_add(struct list_head *new,
64 struct list_head *prev,
65 struct list_head *next)
74 * list_add - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it after
78 * Insert a new entry after the specified head.
79 * This is good for implementing stacks.
81 static inline void list_add(struct list_head *new, struct list_head *head)
83 __list_add(new, head, head->next);
87 * list_add_tail - add a new entry
88 * @new: new entry to be added
89 * @head: list head to add it before
91 * Insert a new entry before the specified head.
92 * This is useful for implementing queues.
94 static inline void list_add_tail(struct list_head *new, struct list_head *head)
96 __list_add(new, head->prev, head);
100 * Delete a list entry by making the prev/next entries
101 * point to each other.
103 * This is only for internal list manipulation where we know
104 * the prev/next entries already!
106 static inline void __list_del(struct list_head * prev, struct list_head * next)
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.
118 static inline void list_del(struct list_head *entry)
120 __list_del(entry->prev, entry->next);
121 entry->next = LIST_POISON1;
122 entry->prev = LIST_POISON2;
126 * list_del_init - deletes entry from list and reinitialize it.
127 * @entry: the element to delete from the list.
129 static inline void list_del_init(struct list_head *entry)
131 __list_del(entry->prev, entry->next);
132 INIT_LIST_HEAD(entry);
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
140 static inline void list_move(struct list_head *list, struct list_head *head)
142 __list_del(list->prev, list->next);
143 list_add(list, head);
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
151 static inline void list_move_tail(struct list_head *list,
152 struct list_head *head)
154 __list_del(list->prev, list->next);
155 list_add_tail(list, head);
159 * list_empty - tests whether a list is empty
160 * @head: the list to test.
162 static inline int list_empty(const struct list_head *head)
164 return head->next == head;
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
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.
177 * @head: the list to test.
179 static inline int list_empty_careful(const struct list_head *head)
181 struct list_head *next = head->next;
182 return (next == head) && (next == head->prev);
185 static inline void __list_splice(struct list_head *list,
186 struct list_head *head)
188 struct list_head *first = list->next;
189 struct list_head *last = list->prev;
190 struct list_head *at = head->next;
200 * list_splice - join two lists
201 * @list: the new list to add.
202 * @head: the place to add it in the first list.
204 static inline void list_splice(struct list_head *list, struct list_head *head)
206 if (!list_empty(list))
207 __list_splice(list, head);
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.
215 * The list at @list is reinitialised
217 static inline void list_splice_init(struct list_head *list,
218 struct list_head *head)
220 if (!list_empty(list)) {
221 __list_splice(list, head);
222 INIT_LIST_HEAD(list);
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.
232 #define list_entry(ptr, type, member) \
233 container_of(ptr, type, member)
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.
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); \
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.
255 #define list_first_entry(head, type, member) \
257 type * first = NULL; \
258 if ((head)->next != (head)) { \
259 first = list_entry((head)->next, type, member); \
268 * list_for_each - iterate over a list
269 * @pos: the &struct list_head to use as a loop counter.
270 * @head: the head for your list.
272 #define list_for_each(pos, head) \
273 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
277 * __list_for_each - iterate over a list
278 * @pos: the &struct list_head to use as a loop counter.
279 * @head: the head for your list.
281 * This variant differs from list_for_each() in that it's the
282 * simplest possible list iteration code, no prefetching is done.
283 * Use this for code that knows the list to be very short (empty
284 * or 1 entry) most of the time.
286 #define __list_for_each(pos, head) \
287 for (pos = (head)->next; pos != (head); pos = pos->next)
290 * list_for_each_prev - iterate over a list backwards
291 * @pos: the &struct list_head to use as a loop counter.
292 * @head: the head for your list.
294 #define list_for_each_prev(pos, head) \
295 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
299 * list_for_each_safe - iterate over a list safe against removal of list entry
300 * @pos: the &struct list_head to use as a loop counter.
301 * @n: another &struct list_head to use as temporary storage
302 * @head: the head for your list.
304 #define list_for_each_safe(pos, n, head) \
305 for (pos = (head)->next, n = pos->next; pos != (head); \
306 pos = n, n = pos->next)
309 * list_for_each_entry - iterate over list of given type
310 * @pos: the type * to use as a loop counter.
311 * @head: the head for your list.
312 * @member: the name of the list_struct within the struct.
314 #define list_for_each_entry(pos, head, member) \
315 for (pos = list_entry((head)->next, typeof(*pos), member); \
316 prefetch(pos->member.next), &pos->member != (head); \
317 pos = list_entry(pos->member.next, typeof(*pos), member))
320 * list_for_each_entry_reverse - iterate backwards over list of given type.
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.
325 #define list_for_each_entry_reverse(pos, head, member) \
326 for (pos = list_entry((head)->prev, typeof(*pos), member); \
327 prefetch(pos->member.prev), &pos->member != (head); \
328 pos = list_entry(pos->member.prev, typeof(*pos), member))
331 * list_prepare_entry - prepare a pos entry for use as a start point in
332 * list_for_each_entry_continue
333 * @pos: the type * to use as a start point
334 * @head: the head of the list
335 * @member: the name of the list_struct within the struct.
337 #define list_prepare_entry(pos, head, member) \
338 ((pos) ? : list_entry(head, typeof(*pos), member))
341 * list_for_each_entry_continue - iterate over list of given type
342 * continuing after existing point
343 * @pos: the type * to use as a loop counter.
344 * @head: the head for your list.
345 * @member: the name of the list_struct within the struct.
347 #define list_for_each_entry_continue(pos, head, member) \
348 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
349 prefetch(pos->member.next), &pos->member != (head); \
350 pos = list_entry(pos->member.next, typeof(*pos), member))
353 * list_for_each_entry_from - iterate over list of given type
354 * continuing from existing point
355 * @pos: the type * to use as a loop counter.
356 * @head: the head for your list.
357 * @member: the name of the list_struct within the struct.
359 #define list_for_each_entry_from(pos, head, member) \
360 for (; prefetch(pos->member.next), &pos->member != (head); \
361 pos = list_entry(pos->member.next, typeof(*pos), member))
364 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
365 * @pos: the type * to use as a loop counter.
366 * @n: another type * to use as temporary storage
367 * @head: the head for your list.
368 * @member: the name of the list_struct within the struct.
370 #define list_for_each_entry_safe(pos, n, head, member) \
371 for (pos = list_entry((head)->next, typeof(*pos), member), \
372 n = list_entry(pos->member.next, typeof(*pos), member); \
373 &pos->member != (head); \
374 pos = n, n = list_entry(n->member.next, typeof(*n), member))
377 * list_for_each_entry_safe_continue - iterate over list of given type
378 * continuing after existing point safe against removal of list entry
379 * @pos: the type * to use as a loop counter.
380 * @n: another type * to use as temporary storage
381 * @head: the head for your list.
382 * @member: the name of the list_struct within the struct.
384 #define list_for_each_entry_safe_continue(pos, n, head, member) \
385 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
386 n = list_entry(pos->member.next, typeof(*pos), member); \
387 &pos->member != (head); \
388 pos = n, n = list_entry(n->member.next, typeof(*n), member))
391 * list_for_each_entry_safe_from - iterate over list of given type
392 * from existing point safe against removal of list entry
393 * @pos: the type * to use as a loop counter.
394 * @n: another type * to use as temporary storage
395 * @head: the head for your list.
396 * @member: the name of the list_struct within the struct.
398 #define list_for_each_entry_safe_from(pos, n, head, member) \
399 for (n = list_entry(pos->member.next, typeof(*pos), member); \
400 &pos->member != (head); \
401 pos = n, n = list_entry(n->member.next, typeof(*n), member))
404 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
405 * removal of list entry
406 * @pos: the type * to use as a loop counter.
407 * @n: another type * to use as temporary storage
408 * @head: the head for your list.
409 * @member: the name of the list_struct within the struct.
411 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
412 for (pos = list_entry((head)->prev, typeof(*pos), member), \
413 n = list_entry(pos->member.prev, typeof(*pos), member); \
414 &pos->member != (head); \
415 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
418 * Double linked lists with a single pointer list head.
419 * Mostly useful for hash tables where the two pointer list head is
421 * You lose the ability to access the tail in O(1).
425 struct hlist_node *first;
429 struct hlist_node *next, **pprev;
432 #define HLIST_HEAD_INIT { .first = NULL }
433 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
434 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
435 static inline void INIT_HLIST_NODE(struct hlist_node *h)
441 static inline int hlist_unhashed(const struct hlist_node *h)
446 static inline int hlist_empty(const struct hlist_head *h)
451 static inline void __hlist_del(struct hlist_node *n)
453 struct hlist_node *next = n->next;
454 struct hlist_node **pprev = n->pprev;
460 static inline void hlist_del(struct hlist_node *n)
463 n->next = LIST_POISON1;
464 n->pprev = LIST_POISON2;
467 static inline void hlist_del_init(struct hlist_node *n)
469 if (!hlist_unhashed(n)) {
475 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
477 struct hlist_node *first = h->first;
480 first->pprev = &n->next;
482 n->pprev = &h->first;
485 /* next must be != NULL */
486 static inline void hlist_add_before(struct hlist_node *n,
487 struct hlist_node *next)
489 n->pprev = next->pprev;
491 next->pprev = &n->next;
495 static inline void hlist_add_after(struct hlist_node *n,
496 struct hlist_node *next)
498 next->next = n->next;
500 next->pprev = &n->next;
503 next->next->pprev = &next->next;
506 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
508 #define hlist_for_each(pos, head) \
509 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
512 #define hlist_for_each_safe(pos, n, head) \
513 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
517 * hlist_for_each_entry - iterate over list of given type
518 * @tpos: the type * to use as a loop counter.
519 * @pos: the &struct hlist_node to use as a loop counter.
520 * @head: the head for your list.
521 * @member: the name of the hlist_node within the struct.
523 #define hlist_for_each_entry(tpos, pos, head, member) \
524 for (pos = (head)->first; \
525 pos && ({ prefetch(pos->next); 1;}) && \
526 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
530 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
531 * @tpos: the type * to use as a loop counter.
532 * @pos: the &struct hlist_node to use as a loop counter.
533 * @member: the name of the hlist_node within the struct.
535 #define hlist_for_each_entry_continue(tpos, pos, member) \
536 for (pos = (pos)->next; \
537 pos && ({ prefetch(pos->next); 1;}) && \
538 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
542 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
543 * @tpos: the type * to use as a loop counter.
544 * @pos: the &struct hlist_node to use as a loop counter.
545 * @member: the name of the hlist_node within the struct.
547 #define hlist_for_each_entry_from(tpos, pos, member) \
548 for (; pos && ({ prefetch(pos->next); 1;}) && \
549 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
553 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
554 * @tpos: the type * to use as a loop counter.
555 * @pos: the &struct hlist_node to use as a loop counter.
556 * @n: another &struct hlist_node to use as temporary storage
557 * @head: the head for your list.
558 * @member: the name of the hlist_node within the struct.
560 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
561 for (pos = (head)->first; \
562 pos && ({ n = pos->next; 1; }) && \
563 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
568 #endif // ! __V3VEE__