1 /* Taken From Linux list implementation
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 /* 2008, Modifications by Jack Lange <jarusl@cs.northwestern.edu> */
28 #include <palacios/vmm_string.h>
31 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
34 #define container_of(ptr, type, member) ({ \
35 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
36 (type *)( (char *)__mptr - offsetof(type,member) );})
39 static inline void prefetch(const void *x) {const void * foo; foo = x;}
42 * These are non-NULL pointers that will result in page faults
43 * under normal circumstances, used to verify that nobody uses
44 * non-initialized list entries.
46 #define LIST_POISON1 ((void *) 0x00100100)
47 #define LIST_POISON2 ((void *) 0x00200200)
50 * Simple doubly linked list implementation.
52 * Some of the internal functions ("__xxx") are useful when
53 * manipulating whole lists rather than single entries, as
54 * sometimes we already know the next/prev entries and we can
55 * generate better code by using them directly rather than
56 * using the generic single-entry routines.
60 struct list_head *next, *prev;
63 #define LIST_HEAD_INIT(name) { &(name), &(name) }
65 #define LIST_HEAD(name) \
66 struct list_head name = LIST_HEAD_INIT(name)
68 static inline void INIT_LIST_HEAD(struct list_head *list)
75 * Insert a new entry between two known consecutive entries.
77 * This is only for internal list manipulation where we know
78 * the prev/next entries already!
80 static inline void __list_add(struct list_head *new,
81 struct list_head *prev,
82 struct list_head *next)
91 * list_add - add a new entry
92 * @new: new entry to be added
93 * @head: list head to add it after
95 * Insert a new entry after the specified head.
96 * This is good for implementing stacks.
98 static inline void list_add(struct list_head *new, struct list_head *head)
100 __list_add(new, head, head->next);
104 * list_add_tail - add a new entry
105 * @new: new entry to be added
106 * @head: list head to add it before
108 * Insert a new entry before the specified head.
109 * This is useful for implementing queues.
111 static inline void list_add_tail(struct list_head *new, struct list_head *head)
113 __list_add(new, head->prev, head);
117 * Delete a list entry by making the prev/next entries
118 * point to each other.
120 * This is only for internal list manipulation where we know
121 * the prev/next entries already!
123 static inline void __list_del(struct list_head * prev, struct list_head * next)
130 * list_del - deletes entry from list.
131 * @entry: the element to delete from the list.
132 * Note: list_empty on entry does not return true after this, the entry is
133 * in an undefined state.
135 static inline void list_del(struct list_head *entry)
137 __list_del(entry->prev, entry->next);
138 entry->next = LIST_POISON1;
139 entry->prev = LIST_POISON2;
143 * list_del_init - deletes entry from list and reinitialize it.
144 * @entry: the element to delete from the list.
146 static inline void list_del_init(struct list_head *entry)
148 __list_del(entry->prev, entry->next);
149 INIT_LIST_HEAD(entry);
153 * list_move - delete from one list and add as another's head
154 * @list: the entry to move
155 * @head: the head that will precede our entry
157 static inline void list_move(struct list_head *list, struct list_head *head)
159 __list_del(list->prev, list->next);
160 list_add(list, head);
164 * list_move_tail - delete from one list and add as another's tail
165 * @list: the entry to move
166 * @head: the head that will follow our entry
168 static inline void list_move_tail(struct list_head *list,
169 struct list_head *head)
171 __list_del(list->prev, list->next);
172 list_add_tail(list, head);
176 * list_empty - tests whether a list is empty
177 * @head: the list to test.
179 static inline int list_empty(const struct list_head *head)
181 return head->next == head;
185 * list_empty_careful - tests whether a list is
186 * empty _and_ checks that no other CPU might be
187 * in the process of still modifying either member
189 * NOTE: using list_empty_careful() without synchronization
190 * can only be safe if the only activity that can happen
191 * to the list entry is list_del_init(). Eg. it cannot be used
192 * if another CPU could re-list_add() it.
194 * @head: the list to test.
196 static inline int list_empty_careful(const struct list_head *head)
198 struct list_head *next = head->next;
199 return (next == head) && (next == head->prev);
202 static inline void __list_splice(struct list_head *list,
203 struct list_head *head)
205 struct list_head *first = list->next;
206 struct list_head *last = list->prev;
207 struct list_head *at = head->next;
217 * list_splice - join two lists
218 * @list: the new list to add.
219 * @head: the place to add it in the first list.
221 static inline void list_splice(struct list_head *list, struct list_head *head)
223 if (!list_empty(list))
224 __list_splice(list, head);
228 * list_splice_init - join two lists and reinitialise the emptied list.
229 * @list: the new list to add.
230 * @head: the place to add it in the first list.
232 * The list at @list is reinitialised
234 static inline void list_splice_init(struct list_head *list,
235 struct list_head *head)
237 if (!list_empty(list)) {
238 __list_splice(list, head);
239 INIT_LIST_HEAD(list);
244 * list_entry - get the struct for this entry
245 * @ptr: the &struct list_head pointer.
246 * @type: the type of the struct this is embedded in.
247 * @member: the name of the list_struct within the struct.
249 #define list_entry(ptr, type, member) \
250 container_of(ptr, type, member)
253 * list_entry - get the struct for the tail entry
254 * @ptr: the list_head head pointer.
255 * @type: the type of the struct this is embedded in.
256 * @member: the name of the list_struct within the struct.
258 #define list_tail_entry(head, type, member) ({ \
259 type * tail = NULL; \
260 if ((head)->prev != (head)) { \
261 tail = list_entry((head)->prev, type, member); \
267 * list_first_entry - get the struct for the first entry
268 * @ptr: the list_head head pointer.
269 * @type: the type of the struct this is embedded in.
270 * @member: the name of the list_struct within the struct.
272 #define list_first_entry(head, type, member) \
274 type * first = NULL; \
275 if ((head)->next != (head)) { \
276 first = list_entry((head)->next, type, member); \
284 * list_next_entry -- get the struct for the next entry in a list
285 * @ptr: current list entry
286 * @type: type of the struct this is embedded in
287 * @member: name of the list struct within the struct
289 #define list_next_entry(node, type, member) \
291 type * next = NULL; \
292 if ((node)->next != (node)) { \
293 next = list_entry((node)->next, type, member); \
300 * list_for_each - iterate over a list
301 * @pos: the &struct list_head to use as a loop counter.
302 * @head: the head for your list.
304 #define list_for_each(pos, head) \
305 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
309 * __list_for_each - iterate over a list
310 * @pos: the &struct list_head to use as a loop counter.
311 * @head: the head for your list.
313 * This variant differs from list_for_each() in that it's the
314 * simplest possible list iteration code, no prefetching is done.
315 * Use this for code that knows the list to be very short (empty
316 * or 1 entry) most of the time.
318 #define __list_for_each(pos, head) \
319 for (pos = (head)->next; pos != (head); pos = pos->next)
322 * list_for_each_prev - iterate over a list backwards
323 * @pos: the &struct list_head to use as a loop counter.
324 * @head: the head for your list.
326 #define list_for_each_prev(pos, head) \
327 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
331 * list_for_each_safe - iterate over a list safe against removal of list entry
332 * @pos: the &struct list_head to use as a loop counter.
333 * @n: another &struct list_head to use as temporary storage
334 * @head: the head for your list.
336 #define list_for_each_safe(pos, n, head) \
337 for (pos = (head)->next, n = pos->next; pos != (head); \
338 pos = n, n = pos->next)
341 * list_for_each_entry - iterate over list of given type
342 * @pos: the type * to use as a loop counter.
343 * @head: the head for your list.
344 * @member: the name of the list_struct within the struct.
346 #define list_for_each_entry(pos, head, member) \
347 for (pos = list_entry((head)->next, typeof(*pos), member); \
348 prefetch(pos->member.next), &pos->member != (head); \
349 pos = list_entry(pos->member.next, typeof(*pos), member))
352 * list_for_each_entry_reverse - iterate backwards over list of given type.
353 * @pos: the type * to use as a loop counter.
354 * @head: the head for your list.
355 * @member: the name of the list_struct within the struct.
357 #define list_for_each_entry_reverse(pos, head, member) \
358 for (pos = list_entry((head)->prev, typeof(*pos), member); \
359 prefetch(pos->member.prev), &pos->member != (head); \
360 pos = list_entry(pos->member.prev, typeof(*pos), member))
363 * list_prepare_entry - prepare a pos entry for use as a start point in
364 * list_for_each_entry_continue
365 * @pos: the type * to use as a start point
366 * @head: the head of the list
367 * @member: the name of the list_struct within the struct.
369 #define list_prepare_entry(pos, head, member) \
370 ((pos) ? : list_entry(head, typeof(*pos), member))
373 * list_for_each_entry_continue - iterate over list of given type
374 * continuing after existing point
375 * @pos: the type * to use as a loop counter.
376 * @head: the head for your list.
377 * @member: the name of the list_struct within the struct.
379 #define list_for_each_entry_continue(pos, head, member) \
380 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
381 prefetch(pos->member.next), &pos->member != (head); \
382 pos = list_entry(pos->member.next, typeof(*pos), member))
385 * list_for_each_entry_from - iterate over list of given type
386 * continuing from existing point
387 * @pos: the type * to use as a loop counter.
388 * @head: the head for your list.
389 * @member: the name of the list_struct within the struct.
391 #define list_for_each_entry_from(pos, head, member) \
392 for (; prefetch(pos->member.next), &pos->member != (head); \
393 pos = list_entry(pos->member.next, typeof(*pos), member))
396 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
397 * @pos: the type * to use as a loop counter.
398 * @n: another type * to use as temporary storage
399 * @head: the head for your list.
400 * @member: the name of the list_struct within the struct.
402 #define list_for_each_entry_safe(pos, n, head, member) \
403 for (pos = list_entry((head)->next, typeof(*pos), member), \
404 n = list_entry(pos->member.next, typeof(*pos), member); \
405 &pos->member != (head); \
406 pos = n, n = list_entry(n->member.next, typeof(*n), member))
409 * list_for_each_entry_safe_continue - iterate over list of given type
410 * continuing after existing point safe against removal of list entry
411 * @pos: the type * to use as a loop counter.
412 * @n: another type * to use as temporary storage
413 * @head: the head for your list.
414 * @member: the name of the list_struct within the struct.
416 #define list_for_each_entry_safe_continue(pos, n, head, member) \
417 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
418 n = list_entry(pos->member.next, typeof(*pos), member); \
419 &pos->member != (head); \
420 pos = n, n = list_entry(n->member.next, typeof(*n), member))
423 * list_for_each_entry_safe_from - iterate over list of given type
424 * from existing point safe against removal of list entry
425 * @pos: the type * to use as a loop counter.
426 * @n: another type * to use as temporary storage
427 * @head: the head for your list.
428 * @member: the name of the list_struct within the struct.
430 #define list_for_each_entry_safe_from(pos, n, head, member) \
431 for (n = list_entry(pos->member.next, typeof(*pos), member); \
432 &pos->member != (head); \
433 pos = n, n = list_entry(n->member.next, typeof(*n), member))
436 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
437 * removal of list entry
438 * @pos: the type * to use as a loop counter.
439 * @n: another type * to use as temporary storage
440 * @head: the head for your list.
441 * @member: the name of the list_struct within the struct.
443 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
444 for (pos = list_entry((head)->prev, typeof(*pos), member), \
445 n = list_entry(pos->member.prev, typeof(*pos), member); \
446 &pos->member != (head); \
447 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
450 * Double linked lists with a single pointer list head.
451 * Mostly useful for hash tables where the two pointer list head is
453 * You lose the ability to access the tail in O(1).
457 struct hlist_node *first;
461 struct hlist_node *next, **pprev;
464 #define HLIST_HEAD_INIT { .first = NULL }
465 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
466 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
467 static inline void INIT_HLIST_NODE(struct hlist_node *h)
473 static inline int hlist_unhashed(const struct hlist_node *h)
478 static inline int hlist_empty(const struct hlist_head *h)
483 static inline void __hlist_del(struct hlist_node *n)
485 struct hlist_node *next = n->next;
486 struct hlist_node **pprev = n->pprev;
492 static inline void hlist_del(struct hlist_node *n)
495 n->next = LIST_POISON1;
496 n->pprev = LIST_POISON2;
499 static inline void hlist_del_init(struct hlist_node *n)
501 if (!hlist_unhashed(n)) {
507 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
509 struct hlist_node *first = h->first;
512 first->pprev = &n->next;
514 n->pprev = &h->first;
517 /* next must be != NULL */
518 static inline void hlist_add_before(struct hlist_node *n,
519 struct hlist_node *next)
521 n->pprev = next->pprev;
523 next->pprev = &n->next;
527 static inline void hlist_add_after(struct hlist_node *n,
528 struct hlist_node *next)
530 next->next = n->next;
532 next->pprev = &n->next;
535 next->next->pprev = &next->next;
538 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
540 #define hlist_for_each(pos, head) \
541 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
544 #define hlist_for_each_safe(pos, n, head) \
545 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
549 * hlist_for_each_entry - iterate over list of given type
550 * @tpos: the type * to use as a loop counter.
551 * @pos: the &struct hlist_node to use as a loop counter.
552 * @head: the head for your list.
553 * @member: the name of the hlist_node within the struct.
555 #define hlist_for_each_entry(tpos, pos, head, member) \
556 for (pos = (head)->first; \
557 pos && ({ prefetch(pos->next); 1;}) && \
558 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
562 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
563 * @tpos: the type * to use as a loop counter.
564 * @pos: the &struct hlist_node to use as a loop counter.
565 * @member: the name of the hlist_node within the struct.
567 #define hlist_for_each_entry_continue(tpos, pos, member) \
568 for (pos = (pos)->next; \
569 pos && ({ prefetch(pos->next); 1;}) && \
570 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
574 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
575 * @tpos: the type * to use as a loop counter.
576 * @pos: the &struct hlist_node to use as a loop counter.
577 * @member: the name of the hlist_node within the struct.
579 #define hlist_for_each_entry_from(tpos, pos, member) \
580 for (; pos && ({ prefetch(pos->next); 1;}) && \
581 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
585 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
586 * @tpos: the type * to use as a loop counter.
587 * @pos: the &struct hlist_node to use as a loop counter.
588 * @n: another &struct hlist_node to use as temporary storage
589 * @head: the head for your list.
590 * @member: the name of the hlist_node within the struct.
592 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
593 for (pos = (head)->first; \
594 pos && ({ n = pos->next; 1; }) && \
595 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
600 #endif // ! __V3VEE__