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> */
12 #include <palacios/vmm_string.h>
15 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
18 #define container_of(ptr, type, member) ({ \
19 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
20 (type *)( (char *)__mptr - offsetof(type,member) );})
23 static inline void prefetch(const void *x) {;}
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.
30 #define LIST_POISON1 ((void *) 0x00100100)
31 #define LIST_POISON2 ((void *) 0x00200200)
34 * Simple doubly linked list implementation.
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.
44 struct list_head *next, *prev;
47 #define LIST_HEAD_INIT(name) { &(name), &(name) }
49 #define LIST_HEAD(name) \
50 struct list_head name = LIST_HEAD_INIT(name)
52 static inline void INIT_LIST_HEAD(struct list_head *list)
59 * Insert a new entry between two known consecutive entries.
61 * This is only for internal list manipulation where we know
62 * the prev/next entries already!
64 static inline void __list_add(struct list_head *new,
65 struct list_head *prev,
66 struct list_head *next)
75 * list_add - add a new entry
76 * @new: new entry to be added
77 * @head: list head to add it after
79 * Insert a new entry after the specified head.
80 * This is good for implementing stacks.
82 static inline void list_add(struct list_head *new, struct list_head *head)
84 __list_add(new, head, head->next);
88 * list_add_tail - add a new entry
89 * @new: new entry to be added
90 * @head: list head to add it before
92 * Insert a new entry before the specified head.
93 * This is useful for implementing queues.
95 static inline void list_add_tail(struct list_head *new, struct list_head *head)
97 __list_add(new, head->prev, head);
101 * Delete a list entry by making the prev/next entries
102 * point to each other.
104 * This is only for internal list manipulation where we know
105 * the prev/next entries already!
107 static inline void __list_del(struct list_head * prev, struct list_head * next)
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.
119 static inline void list_del(struct list_head *entry)
121 __list_del(entry->prev, entry->next);
122 entry->next = LIST_POISON1;
123 entry->prev = LIST_POISON2;
127 * list_del_init - deletes entry from list and reinitialize it.
128 * @entry: the element to delete from the list.
130 static inline void list_del_init(struct list_head *entry)
132 __list_del(entry->prev, entry->next);
133 INIT_LIST_HEAD(entry);
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
141 static inline void list_move(struct list_head *list, struct list_head *head)
143 __list_del(list->prev, list->next);
144 list_add(list, head);
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
152 static inline void list_move_tail(struct list_head *list,
153 struct list_head *head)
155 __list_del(list->prev, list->next);
156 list_add_tail(list, head);
160 * list_empty - tests whether a list is empty
161 * @head: the list to test.
163 static inline int list_empty(const struct list_head *head)
165 return head->next == head;
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
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.
178 * @head: the list to test.
180 static inline int list_empty_careful(const struct list_head *head)
182 struct list_head *next = head->next;
183 return (next == head) && (next == head->prev);
186 static inline void __list_splice(struct list_head *list,
187 struct list_head *head)
189 struct list_head *first = list->next;
190 struct list_head *last = list->prev;
191 struct list_head *at = head->next;
201 * list_splice - join two lists
202 * @list: the new list to add.
203 * @head: the place to add it in the first list.
205 static inline void list_splice(struct list_head *list, struct list_head *head)
207 if (!list_empty(list))
208 __list_splice(list, head);
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.
216 * The list at @list is reinitialised
218 static inline void list_splice_init(struct list_head *list,
219 struct list_head *head)
221 if (!list_empty(list)) {
222 __list_splice(list, head);
223 INIT_LIST_HEAD(list);
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.
233 #define list_entry(ptr, type, member) \
234 container_of(ptr, type, member)
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.
241 #define list_for_each(pos, head) \
242 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
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.
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.
255 #define __list_for_each(pos, head) \
256 for (pos = (head)->next; pos != (head); pos = pos->next)
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.
263 #define list_for_each_prev(pos, head) \
264 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
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.
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)
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.
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))
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.
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))
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.
306 #define list_prepare_entry(pos, head, member) \
307 ((pos) ? : list_entry(head, typeof(*pos), member))
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.
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))
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.
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))
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.
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))
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.
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))
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.
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))
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.
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))
387 * Double linked lists with a single pointer list head.
388 * Mostly useful for hash tables where the two pointer list head is
390 * You lose the ability to access the tail in O(1).
394 struct hlist_node *first;
398 struct hlist_node *next, **pprev;
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)
410 static inline int hlist_unhashed(const struct hlist_node *h)
415 static inline int hlist_empty(const struct hlist_head *h)
420 static inline void __hlist_del(struct hlist_node *n)
422 struct hlist_node *next = n->next;
423 struct hlist_node **pprev = n->pprev;
429 static inline void hlist_del(struct hlist_node *n)
432 n->next = LIST_POISON1;
433 n->pprev = LIST_POISON2;
436 static inline void hlist_del_init(struct hlist_node *n)
438 if (!hlist_unhashed(n)) {
444 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
446 struct hlist_node *first = h->first;
449 first->pprev = &n->next;
451 n->pprev = &h->first;
454 /* next must be != NULL */
455 static inline void hlist_add_before(struct hlist_node *n,
456 struct hlist_node *next)
458 n->pprev = next->pprev;
460 next->pprev = &n->next;
464 static inline void hlist_add_after(struct hlist_node *n,
465 struct hlist_node *next)
467 next->next = n->next;
469 next->pprev = &n->next;
472 next->next->pprev = &next->next;
475 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
477 #define hlist_for_each(pos, head) \
478 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
481 #define hlist_for_each_safe(pos, n, head) \
482 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
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.
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;}); \
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.
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;}); \
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.
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;}); \
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.
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;}); \
537 //#endif // ! __V3VEE__