1 #ifndef _LINUX_LIST_H
2 #define _LINUX_LIST_H
3
4 #include <stddef.h>
5
prefetch(const void * x)6 static inline void prefetch(const void *x) {;}
7
8 #ifndef offsetof
9 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
10 #endif
11
12 #ifdef QEMU_OLD
13 /**
14 * container_of - cast a member of a structure out to the containing structure
15 *
16 * @ptr: the pointer to the member.
17 * @type: the type of the container struct this is embedded in.
18 * @member: the name of the member within the struct.
19 *
20 */
21 #define container_of(ptr, type, member) ({ \
22 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
23 (type *)( (char *)__mptr - offsetof(type,member) );})
24 #endif
25
26 /*
27 * These are non-NULL pointers that will result in page faults
28 * under normal circumstances, used to verify that nobody uses
29 * non-initialized list entries.
30 */
31 #define LIST_POISON1 ((void *) 0x00100100)
32 #define LIST_POISON2 ((void *) 0x00200200)
33
34 /*
35 * Simple doubly linked list implementation.
36 *
37 * Some of the internal functions ("__xxx") are useful when
38 * manipulating whole lists rather than single entries, as
39 * sometimes we already know the next/prev entries and we can
40 * generate better code by using them directly rather than
41 * using the generic single-entry routines.
42 */
43
44 struct list_head {
45 struct list_head *next, *prev;
46 };
47
48 #define LIST_HEAD_INIT(name) { &(name), &(name) }
49
50 #define LIST_HEAD(name) \
51 struct list_head name = LIST_HEAD_INIT(name)
52
53 #define INIT_LIST_HEAD(ptr) do { \
54 (ptr)->next = (ptr); (ptr)->prev = (ptr); \
55 } while (0)
56
57 /*
58 * Insert a new entry between two known consecutive entries.
59 *
60 * This is only for internal list manipulation where we know
61 * the prev/next entries already!
62 */
__list_add(struct list_head * new,struct list_head * prev,struct list_head * next)63 static inline void __list_add(struct list_head *new,
64 struct list_head *prev,
65 struct list_head *next)
66 {
67 next->prev = new;
68 new->next = next;
69 new->prev = prev;
70 prev->next = new;
71 }
72
73 /**
74 * list_add - add a new entry
75 * @new: new entry to be added
76 * @head: list head to add it after
77 *
78 * Insert a new entry after the specified head.
79 * This is good for implementing stacks.
80 */
list_add(struct list_head * new,struct list_head * head)81 static inline void list_add(struct list_head *new, struct list_head *head)
82 {
83 __list_add(new, head, head->next);
84 }
85
86 /**
87 * list_add_tail - add a new entry
88 * @new: new entry to be added
89 * @head: list head to add it before
90 *
91 * Insert a new entry before the specified head.
92 * This is useful for implementing queues.
93 */
list_add_tail(struct list_head * new,struct list_head * head)94 static inline void list_add_tail(struct list_head *new, struct list_head *head)
95 {
96 __list_add(new, head->prev, head);
97 }
98
99 /*
100 * Delete a list entry by making the prev/next entries
101 * point to each other.
102 *
103 * This is only for internal list manipulation where we know
104 * the prev/next entries already!
105 */
__list_del(struct list_head * prev,struct list_head * next)106 static inline void __list_del(struct list_head * prev, struct list_head * next)
107 {
108 next->prev = prev;
109 prev->next = next;
110 }
111
112 /**
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.
117 */
list_del(struct list_head * entry)118 static inline void list_del(struct list_head *entry)
119 {
120 __list_del(entry->prev, entry->next);
121 entry->next = LIST_POISON1;
122 entry->prev = LIST_POISON2;
123 }
124
125 /**
126 * list_del_init - deletes entry from list and reinitialize it.
127 * @entry: the element to delete from the list.
128 */
list_del_init(struct list_head * entry)129 static inline void list_del_init(struct list_head *entry)
130 {
131 __list_del(entry->prev, entry->next);
132 INIT_LIST_HEAD(entry);
133 }
134
135 /**
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
139 */
list_move(struct list_head * list,struct list_head * head)140 static inline void list_move(struct list_head *list, struct list_head *head)
141 {
142 __list_del(list->prev, list->next);
143 list_add(list, head);
144 }
145
146 /**
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
150 */
list_move_tail(struct list_head * list,struct list_head * head)151 static inline void list_move_tail(struct list_head *list,
152 struct list_head *head)
153 {
154 __list_del(list->prev, list->next);
155 list_add_tail(list, head);
156 }
157
158 /**
159 * list_empty - tests whether a list is empty
160 * @head: the list to test.
161 */
list_empty(const struct list_head * head)162 static inline int list_empty(const struct list_head *head)
163 {
164 return head->next == head;
165 }
166
167 /**
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
171 *
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.
176 *
177 * @head: the list to test.
178 */
list_empty_careful(const struct list_head * head)179 static inline int list_empty_careful(const struct list_head *head)
180 {
181 struct list_head *next = head->next;
182 return (next == head) && (next == head->prev);
183 }
184
__list_splice(struct list_head * list,struct list_head * head)185 static inline void __list_splice(struct list_head *list,
186 struct list_head *head)
187 {
188 struct list_head *first = list->next;
189 struct list_head *last = list->prev;
190 struct list_head *at = head->next;
191
192 first->prev = head;
193 head->next = first;
194
195 last->next = at;
196 at->prev = last;
197 }
198
199 /**
200 * list_splice - join two lists
201 * @list: the new list to add.
202 * @head: the place to add it in the first list.
203 */
list_splice(struct list_head * list,struct list_head * head)204 static inline void list_splice(struct list_head *list, struct list_head *head)
205 {
206 if (!list_empty(list))
207 __list_splice(list, head);
208 }
209
210 /**
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.
214 *
215 * The list at @list is reinitialised
216 */
list_splice_init(struct list_head * list,struct list_head * head)217 static inline void list_splice_init(struct list_head *list,
218 struct list_head *head)
219 {
220 if (!list_empty(list)) {
221 __list_splice(list, head);
222 INIT_LIST_HEAD(list);
223 }
224 }
225
226 /**
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.
231 */
232 #define list_entry(ptr, type, member) \
233 container_of(ptr, type, member)
234
235 /**
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.
239 */
240 #define list_for_each(pos, head) \
241 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
242 pos = pos->next)
243
244 /**
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.
248 *
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.
253 */
254 #define __list_for_each(pos, head) \
255 for (pos = (head)->next; pos != (head); pos = pos->next)
256
257 /**
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.
261 */
262 #define list_for_each_prev(pos, head) \
263 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
264 pos = pos->prev)
265
266 /**
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.
271 */
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)
275
276 /**
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.
281 */
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))
286
287 /**
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.
292 */
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))
297
298 /**
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.
304 */
305 #define list_prepare_entry(pos, head, member) \
306 ((pos) ? : list_entry(head, typeof(*pos), member))
307
308 /**
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.
314 */
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))
319
320 /**
321 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
322 * @pos: the type * to use as a loop counter.
323 * @n: another type * to use as temporary storage
324 * @head: the head for your list.
325 * @member: the name of the list_struct within the struct.
326 */
327 #define list_for_each_entry_safe(pos, n, head, member) \
328 for (pos = list_entry((head)->next, typeof(*pos), member), \
329 n = list_entry(pos->member.next, typeof(*pos), member); \
330 &pos->member != (head); \
331 pos = n, n = list_entry(n->member.next, typeof(*n), member))
332
333 /**
334 * list_for_each_entry_safe_reverse - iterate backwards over list of given type safe against removal of list entry.
335 * @pos: the type * to use as a loop counter.
336 * @n: another type * to use as temporary storage
337 * @head: the head for your list.
338 * @member: the name of the list_struct within the struct.
339 */
340 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
341 for (pos = list_entry((head)->prev, typeof(*pos), member), \
342 n = list_entry(pos->member.prev, typeof(*pos), member); \
343 prefetch(pos->member.prev), &pos->member != (head); \
344 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
345
346 /**
347 * list_for_each_rcu - iterate over an rcu-protected list
348 * @pos: the &struct list_head to use as a loop counter.
349 * @head: the head for your list.
350 *
351 * This list-traversal primitive may safely run concurrently with
352 * the _rcu list-mutation primitives such as list_add_rcu()
353 * as long as the traversal is guarded by rcu_read_lock().
354 */
355 #define list_for_each_rcu(pos, head) \
356 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
357 pos = rcu_dereference(pos->next))
358
359 #define __list_for_each_rcu(pos, head) \
360 for (pos = (head)->next; pos != (head); \
361 pos = rcu_dereference(pos->next))
362
363 /**
364 * list_for_each_safe_rcu - iterate over an rcu-protected list safe
365 * against removal of list entry
366 * @pos: the &struct list_head to use as a loop counter.
367 * @n: another &struct list_head to use as temporary storage
368 * @head: the head for your list.
369 *
370 * This list-traversal primitive may safely run concurrently with
371 * the _rcu list-mutation primitives such as list_add_rcu()
372 * as long as the traversal is guarded by rcu_read_lock().
373 */
374 #define list_for_each_safe_rcu(pos, n, head) \
375 for (pos = (head)->next, n = pos->next; pos != (head); \
376 pos = rcu_dereference(n), n = pos->next)
377
378 /**
379 * list_for_each_entry_rcu - iterate over rcu list of given type
380 * @pos: the type * to use as a loop counter.
381 * @head: the head for your list.
382 * @member: the name of the list_struct within the struct.
383 *
384 * This list-traversal primitive may safely run concurrently with
385 * the _rcu list-mutation primitives such as list_add_rcu()
386 * as long as the traversal is guarded by rcu_read_lock().
387 */
388 #define list_for_each_entry_rcu(pos, head, member) \
389 for (pos = list_entry((head)->next, typeof(*pos), member); \
390 prefetch(pos->member.next), &pos->member != (head); \
391 pos = rcu_dereference(list_entry(pos->member.next, \
392 typeof(*pos), member)))
393
394
395 /**
396 * list_for_each_continue_rcu - iterate over an rcu-protected list
397 * continuing after existing point.
398 * @pos: the &struct list_head to use as a loop counter.
399 * @head: the head for your list.
400 *
401 * This list-traversal primitive may safely run concurrently with
402 * the _rcu list-mutation primitives such as list_add_rcu()
403 * as long as the traversal is guarded by rcu_read_lock().
404 */
405 #define list_for_each_continue_rcu(pos, head) \
406 for ((pos) = (pos)->next; prefetch((pos)->next), (pos) != (head); \
407 (pos) = rcu_dereference((pos)->next))
408
409 /*
410 * Double linked lists with a single pointer list head.
411 * Mostly useful for hash tables where the two pointer list head is
412 * too wasteful.
413 * You lose the ability to access the tail in O(1).
414 */
415
416 struct hlist_head {
417 struct hlist_node *first;
418 };
419
420 struct hlist_node {
421 struct hlist_node *next, **pprev;
422 };
423
424 #define HLIST_HEAD_INIT { .first = NULL }
425 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
426 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
427 #define INIT_HLIST_NODE(ptr) ((ptr)->next = NULL, (ptr)->pprev = NULL)
428
hlist_unhashed(const struct hlist_node * h)429 static inline int hlist_unhashed(const struct hlist_node *h)
430 {
431 return !h->pprev;
432 }
433
hlist_empty(const struct hlist_head * h)434 static inline int hlist_empty(const struct hlist_head *h)
435 {
436 return !h->first;
437 }
438
__hlist_del(struct hlist_node * n)439 static inline void __hlist_del(struct hlist_node *n)
440 {
441 struct hlist_node *next = n->next;
442 struct hlist_node **pprev = n->pprev;
443 *pprev = next;
444 if (next)
445 next->pprev = pprev;
446 }
447
hlist_del(struct hlist_node * n)448 static inline void hlist_del(struct hlist_node *n)
449 {
450 __hlist_del(n);
451 n->next = LIST_POISON1;
452 n->pprev = LIST_POISON2;
453 }
454
455 /**
456 * hlist_del_rcu - deletes entry from hash list without re-initialization
457 * @n: the element to delete from the hash list.
458 *
459 * Note: list_unhashed() on entry does not return true after this,
460 * the entry is in an undefined state. It is useful for RCU based
461 * lockfree traversal.
462 *
463 * In particular, it means that we can not poison the forward
464 * pointers that may still be used for walking the hash list.
465 *
466 * The caller must take whatever precautions are necessary
467 * (such as holding appropriate locks) to avoid racing
468 * with another list-mutation primitive, such as hlist_add_head_rcu()
469 * or hlist_del_rcu(), running on this same list.
470 * However, it is perfectly legal to run concurrently with
471 * the _rcu list-traversal primitives, such as
472 * hlist_for_each_entry().
473 */
hlist_del_rcu(struct hlist_node * n)474 static inline void hlist_del_rcu(struct hlist_node *n)
475 {
476 __hlist_del(n);
477 n->pprev = LIST_POISON2;
478 }
479
hlist_del_init(struct hlist_node * n)480 static inline void hlist_del_init(struct hlist_node *n)
481 {
482 if (n->pprev) {
483 __hlist_del(n);
484 INIT_HLIST_NODE(n);
485 }
486 }
487
hlist_add_head(struct hlist_node * n,struct hlist_head * h)488 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
489 {
490 struct hlist_node *first = h->first;
491 n->next = first;
492 if (first)
493 first->pprev = &n->next;
494 h->first = n;
495 n->pprev = &h->first;
496 }
497
498 /* next must be != NULL */
hlist_add_before(struct hlist_node * n,struct hlist_node * next)499 static inline void hlist_add_before(struct hlist_node *n,
500 struct hlist_node *next)
501 {
502 n->pprev = next->pprev;
503 n->next = next;
504 next->pprev = &n->next;
505 *(n->pprev) = n;
506 }
507
hlist_add_after(struct hlist_node * n,struct hlist_node * next)508 static inline void hlist_add_after(struct hlist_node *n,
509 struct hlist_node *next)
510 {
511 next->next = n->next;
512 n->next = next;
513 next->pprev = &n->next;
514
515 if(next->next)
516 next->next->pprev = &next->next;
517 }
518
519 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
520
521 #define hlist_for_each(pos, head) \
522 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
523 pos = pos->next)
524
525 #define hlist_for_each_safe(pos, n, head) \
526 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
527 pos = n)
528
529 #define hlist_for_each_rcu(pos, head) \
530 for ((pos) = (head)->first; pos && ({ prefetch((pos)->next); 1; }); \
531 (pos) = rcu_dereference((pos)->next))
532
533 /**
534 * hlist_for_each_entry - iterate over list of given type
535 * @tpos: the type * to use as a loop counter.
536 * @pos: the &struct hlist_node to use as a loop counter.
537 * @head: the head for your list.
538 * @member: the name of the hlist_node within the struct.
539 */
540 #define hlist_for_each_entry(tpos, pos, head, member) \
541 for (pos = (head)->first; \
542 pos && ({ prefetch(pos->next); 1;}) && \
543 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
544 pos = pos->next)
545
546 /**
547 * hlist_for_each_entry_continue - iterate over a hlist continuing after existing point
548 * @tpos: the type * to use as a loop counter.
549 * @pos: the &struct hlist_node to use as a loop counter.
550 * @member: the name of the hlist_node within the struct.
551 */
552 #define hlist_for_each_entry_continue(tpos, pos, member) \
553 for (pos = (pos)->next; \
554 pos && ({ prefetch(pos->next); 1;}) && \
555 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
556 pos = pos->next)
557
558 /**
559 * hlist_for_each_entry_from - iterate over a hlist continuing from existing point
560 * @tpos: the type * to use as a loop counter.
561 * @pos: the &struct hlist_node to use as a loop counter.
562 * @member: the name of the hlist_node within the struct.
563 */
564 #define hlist_for_each_entry_from(tpos, pos, member) \
565 for (; pos && ({ prefetch(pos->next); 1;}) && \
566 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
567 pos = pos->next)
568
569 /**
570 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
571 * @tpos: the type * to use as a loop counter.
572 * @pos: the &struct hlist_node to use as a loop counter.
573 * @n: another &struct hlist_node to use as temporary storage
574 * @head: the head for your list.
575 * @member: the name of the hlist_node within the struct.
576 */
577 #define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
578 for (pos = (head)->first; \
579 pos && ({ n = pos->next; 1; }) && \
580 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
581 pos = n)
582
583 /**
584 * hlist_for_each_entry_rcu - iterate over rcu list of given type
585 * @pos: the type * to use as a loop counter.
586 * @pos: the &struct hlist_node to use as a loop counter.
587 * @head: the head for your list.
588 * @member: the name of the hlist_node within the struct.
589 *
590 * This list-traversal primitive may safely run concurrently with
591 * the _rcu list-mutation primitives such as hlist_add_head_rcu()
592 * as long as the traversal is guarded by rcu_read_lock().
593 */
594 #define hlist_for_each_entry_rcu(tpos, pos, head, member) \
595 for (pos = (head)->first; \
596 pos && ({ prefetch(pos->next); 1;}) && \
597 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
598 pos = rcu_dereference(pos->next))
599
600 #endif /* _LINUX_LIST_H */
601