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) {;}
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_for_each - iterate over a list
237 * @pos: the &struct list_head to use as a loop counter.
238 * @head: the head for your list.
240 #define list_for_each(pos, head) \
241 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
245 * __list_for_each - iterate over a list
246 * @pos: the &struct list_head to use as a loop counter.
247 * @head: the head for your list.
249 * This variant differs from list_for_each() in that it's the
250 * simplest possible list iteration code, no prefetching is done.
251 * Use this for code that knows the list to be very short (empty
252 * or 1 entry) most of the time.
254 #define __list_for_each(pos, head) \
255 for (pos = (head)->next; pos != (head); pos = pos->next)
258 * list_for_each_prev - iterate over a list backwards
259 * @pos: the &struct list_head to use as a loop counter.
260 * @head: the head for your list.
262 #define list_for_each_prev(pos, head) \
263 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
267 * list_for_each_safe - iterate over a list safe against removal of list entry
268 * @pos: the &struct list_head to use as a loop counter.
269 * @n: another &struct list_head to use as temporary storage
270 * @head: the head for your list.
272 #define list_for_each_safe(pos, n, head) \
273 for (pos = (head)->next, n = pos->next; pos != (head); \
274 pos = n, n = pos->next)
277 * list_for_each_entry - iterate over list of given type
278 * @pos: the type * to use as a loop counter.
279 * @head: the head for your list.
280 * @member: the name of the list_struct within the struct.
282 #define list_for_each_entry(pos, head, member) \
283 for (pos = list_entry((head)->next, typeof(*pos), member); \
284 prefetch(pos->member.next), &pos->member != (head); \
285 pos = list_entry(pos->member.next, typeof(*pos), member))
288 * list_for_each_entry_reverse - iterate backwards over list of given type.
289 * @pos: the type * to use as a loop counter.
290 * @head: the head for your list.
291 * @member: the name of the list_struct within the struct.
293 #define list_for_each_entry_reverse(pos, head, member) \
294 for (pos = list_entry((head)->prev, typeof(*pos), member); \
295 prefetch(pos->member.prev), &pos->member != (head); \
296 pos = list_entry(pos->member.prev, typeof(*pos), member))
299 * list_prepare_entry - prepare a pos entry for use as a start point in
300 * list_for_each_entry_continue
301 * @pos: the type * to use as a start point
302 * @head: the head of the list
303 * @member: the name of the list_struct within the struct.
305 #define list_prepare_entry(pos, head, member) \
306 ((pos) ? : list_entry(head, typeof(*pos), member))
309 * list_for_each_entry_continue - iterate over list of given type
310 * continuing after existing point
311 * @pos: the type * to use as a loop counter.
312 * @head: the head for your list.
313 * @member: the name of the list_struct within the struct.
315 #define list_for_each_entry_continue(pos, head, member) \
316 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
317 prefetch(pos->member.next), &pos->member != (head); \
318 pos = list_entry(pos->member.next, typeof(*pos), member))
321 * list_for_each_entry_from - iterate over list of given type
322 * continuing from existing point
323 * @pos: the type * to use as a loop counter.
324 * @head: the head for your list.
325 * @member: the name of the list_struct within the struct.
327 #define list_for_each_entry_from(pos, head, member) \
328 for (; prefetch(pos->member.next), &pos->member != (head); \
329 pos = list_entry(pos->member.next, typeof(*pos), member))
332 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
333 * @pos: the type * to use as a loop counter.
334 * @n: another type * to use as temporary storage
335 * @head: the head for your list.
336 * @member: the name of the list_struct within the struct.
338 #define list_for_each_entry_safe(pos, n, head, member) \
339 for (pos = list_entry((head)->next, typeof(*pos), member), \
340 n = list_entry(pos->member.next, typeof(*pos), member); \
341 &pos->member != (head); \
342 pos = n, n = list_entry(n->member.next, typeof(*n), member))
345 * list_for_each_entry_safe_continue - iterate over list of given type
346 * continuing after existing point safe against removal of list entry
347 * @pos: the type * to use as a loop counter.
348 * @n: another type * to use as temporary storage
349 * @head: the head for your list.
350 * @member: the name of the list_struct within the struct.
352 #define list_for_each_entry_safe_continue(pos, n, head, member) \
353 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
354 n = list_entry(pos->member.next, typeof(*pos), member); \
355 &pos->member != (head); \
356 pos = n, n = list_entry(n->member.next, typeof(*n), member))
359 * list_for_each_entry_safe_from - iterate over list of given type
360 * from existing point safe against removal of list entry
361 * @pos: the type * to use as a loop counter.
362 * @n: another type * to use as temporary storage
363 * @head: the head for your list.
364 * @member: the name of the list_struct within the struct.
366 #define list_for_each_entry_safe_from(pos, n, head, member) \
367 for (n = list_entry(pos->member.next, typeof(*pos), member); \
368 &pos->member != (head); \
369 pos = n, n = list_entry(n->member.next, typeof(*n), member))
372 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against
373 * removal of list entry
374 * @pos: the type * to use as a loop counter.
375 * @n: another type * to use as temporary storage
376 * @head: the head for your list.
377 * @member: the name of the list_struct within the struct.
379 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
380 for (pos = list_entry((head)->prev, typeof(*pos), member), \
381 n = list_entry(pos->member.prev, typeof(*pos), member); \
382 &pos->member != (head); \
383 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
386 * Double linked lists with a single pointer list head.
387 * Mostly useful for hash tables where the two pointer list head is
389 * You lose the ability to access the tail in O(1).
393 struct hlist_node *first;
397 struct hlist_node *next, **pprev;
400 #define HLIST_HEAD_INIT { .first = NULL }
401 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
402 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
403 static inline void INIT_HLIST_NODE(struct hlist_node *h)
409 static inline int hlist_unhashed(const struct hlist_node *h)
414 static inline int hlist_empty(const struct hlist_head *h)
419 static inline void __hlist_del(struct hlist_node *n)
421 struct hlist_node *next = n->next;
422 struct hlist_node **pprev = n->pprev;
428 static inline void hlist_del(struct hlist_node *n)
431 n->next = LIST_POISON1;
432 n->pprev = LIST_POISON2;
435 static inline void hlist_del_init(struct hlist_node *n)
437 if (!hlist_unhashed(n)) {
443 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
445 struct hlist_node *first = h->first;
448 first->pprev = &n->next;
450 n->pprev = &h->first;
453 /* next must be != NULL */
454 static inline void hlist_add_before(struct hlist_node *n,
455 struct hlist_node *next)
457 n->pprev = next->pprev;
459 next->pprev = &n->next;
463 static inline void hlist_add_after(struct hlist_node *n,
464 struct hlist_node *next)
466 next->next = n->next;
468 next->pprev = &n->next;
471 next->next->pprev = &next->next;
474 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
476 #define hlist_for_each(pos, head) \
477 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
480 #define hlist_for_each_safe(pos, n, head) \
481 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
485 * hlist_for_each_entry - iterate over list of given type
486 * @tpos: the type * to use as a loop counter.
487 * @pos: the &struct hlist_node to use as a loop counter.
488 * @head: the head for your list.
489 * @member: the name of the hlist_node within the struct.
491 #define hlist_for_each_entry(tpos, pos, head, member) \
492 for (pos = (head)->first; \
493 pos && ({ prefetch(pos->next); 1;}) && \
494 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
498 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
499 * @tpos: the type * to use as a loop counter.
500 * @pos: the &struct hlist_node to use as a loop counter.
501 * @member: the name of the hlist_node within the struct.
503 #define hlist_for_each_entry_continue(tpos, pos, member) \
504 for (pos = (pos)->next; \
505 pos && ({ prefetch(pos->next); 1;}) && \
506 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
510 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
511 * @tpos: the type * to use as a loop counter.
512 * @pos: the &struct hlist_node to use as a loop counter.
513 * @member: the name of the hlist_node within the struct.
515 #define hlist_for_each_entry_from(tpos, pos, member) \
516 for (; pos && ({ prefetch(pos->next); 1;}) && \
517 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
521 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
522 * @tpos: the type * to use as a loop counter.
523 * @pos: the &struct hlist_node to use as a loop counter.
524 * @n: another &struct hlist_node to use as temporary storage
525 * @head: the head for your list.
526 * @member: the name of the hlist_node within the struct.
528 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
529 for (pos = (head)->first; \
530 pos && ({ n = pos->next; 1; }) && \
531 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
536 #endif // ! __V3VEE__