1#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
4#include <linux/types.h>
5#include <linux/stddef.h>
6#include <linux/poison.h>
7#include <linux/prefetch.h>
8#include <asm/system.h>
9
10
11
12
13
14
15
16
17
18
19
20#define LIST_HEAD_INIT(name) { &(name), &(name) }
21
22#define LIST_HEAD(name) \
23 struct list_head name = LIST_HEAD_INIT(name)
24
25static inline void INIT_LIST_HEAD(struct list_head *list)
26{
27 list->next = list;
28 list->prev = list;
29}
30
31
32
33
34
35
36
37#ifndef CONFIG_DEBUG_LIST
38static inline void __list_add(struct list_head *new,
39 struct list_head *prev,
40 struct list_head *next)
41{
42 next->prev = new;
43 new->next = next;
44 new->prev = prev;
45 prev->next = new;
46}
47#else
48extern void __list_add(struct list_head *new,
49 struct list_head *prev,
50 struct list_head *next);
51#endif
52
53
54
55
56
57
58
59
60
61static inline void list_add(struct list_head *new, struct list_head *head)
62{
63 __list_add(new, head, head->next);
64}
65
66
67
68
69
70
71
72
73
74
75static inline void list_add_tail(struct list_head *new, struct list_head *head)
76{
77 __list_add(new, head->prev, head);
78}
79
80
81
82
83
84
85
86
87static inline void __list_del(struct list_head * prev, struct list_head * next)
88{
89 next->prev = prev;
90 prev->next = next;
91}
92
93
94
95
96
97
98
99#ifndef CONFIG_DEBUG_LIST
100static inline void list_del(struct list_head *entry)
101{
102 __list_del(entry->prev, entry->next);
103 entry->next = LIST_POISON1;
104 entry->prev = LIST_POISON2;
105}
106#else
107extern void list_del(struct list_head *entry);
108#endif
109
110
111
112
113
114
115
116
117static inline void list_replace(struct list_head *old,
118 struct list_head *new)
119{
120 new->next = old->next;
121 new->next->prev = new;
122 new->prev = old->prev;
123 new->prev->next = new;
124}
125
126static inline void list_replace_init(struct list_head *old,
127 struct list_head *new)
128{
129 list_replace(old, new);
130 INIT_LIST_HEAD(old);
131}
132
133
134
135
136
137static inline void list_del_init(struct list_head *entry)
138{
139 __list_del(entry->prev, entry->next);
140 INIT_LIST_HEAD(entry);
141}
142
143
144
145
146
147
148static inline void list_move(struct list_head *list, struct list_head *head)
149{
150 __list_del(list->prev, list->next);
151 list_add(list, head);
152}
153
154
155
156
157
158
159static inline void list_move_tail(struct list_head *list,
160 struct list_head *head)
161{
162 __list_del(list->prev, list->next);
163 list_add_tail(list, head);
164}
165
166
167
168
169
170
171static inline int list_is_last(const struct list_head *list,
172 const struct list_head *head)
173{
174 return list->next == head;
175}
176
177
178
179
180
181static inline int list_empty(const struct list_head *head)
182{
183 return head->next == head;
184}
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199static inline int list_empty_careful(const struct list_head *head)
200{
201 struct list_head *next = head->next;
202 return (next == head) && (next == head->prev);
203}
204
205
206
207
208
209static inline void list_rotate_left(struct list_head *head)
210{
211 struct list_head *first;
212
213 if (!list_empty(head)) {
214 first = head->next;
215 list_move_tail(first, head);
216 }
217}
218
219
220
221
222
223static inline int list_is_singular(const struct list_head *head)
224{
225 return !list_empty(head) && (head->next == head->prev);
226}
227
228static inline void __list_cut_position(struct list_head *list,
229 struct list_head *head, struct list_head *entry)
230{
231 struct list_head *new_first = entry->next;
232 list->next = head->next;
233 list->next->prev = list;
234 list->prev = entry;
235 entry->next = list;
236 head->next = new_first;
237 new_first->prev = head;
238}
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254static inline void list_cut_position(struct list_head *list,
255 struct list_head *head, struct list_head *entry)
256{
257 if (list_empty(head))
258 return;
259 if (list_is_singular(head) &&
260 (head->next != entry && head != entry))
261 return;
262 if (entry == head)
263 INIT_LIST_HEAD(list);
264 else
265 __list_cut_position(list, head, entry);
266}
267
268static inline void __list_splice(const struct list_head *list,
269 struct list_head *prev,
270 struct list_head *next)
271{
272 struct list_head *first = list->next;
273 struct list_head *last = list->prev;
274
275 first->prev = prev;
276 prev->next = first;
277
278 last->next = next;
279 next->prev = last;
280}
281
282
283
284
285
286
287static inline void list_splice(const struct list_head *list,
288 struct list_head *head)
289{
290 if (!list_empty(list))
291 __list_splice(list, head, head->next);
292}
293
294
295
296
297
298
299static inline void list_splice_tail(struct list_head *list,
300 struct list_head *head)
301{
302 if (!list_empty(list))
303 __list_splice(list, head->prev, head);
304}
305
306
307
308
309
310
311
312
313static inline void list_splice_init(struct list_head *list,
314 struct list_head *head)
315{
316 if (!list_empty(list)) {
317 __list_splice(list, head, head->next);
318 INIT_LIST_HEAD(list);
319 }
320}
321
322
323
324
325
326
327
328
329
330static inline void list_splice_tail_init(struct list_head *list,
331 struct list_head *head)
332{
333 if (!list_empty(list)) {
334 __list_splice(list, head->prev, head);
335 INIT_LIST_HEAD(list);
336 }
337}
338
339
340
341
342
343
344
345#define list_entry(ptr, type, member) \
346 container_of(ptr, type, member)
347
348
349
350
351
352
353
354
355
356#define list_first_entry(ptr, type, member) \
357 list_entry((ptr)->next, type, member)
358
359
360
361
362
363
364#define list_for_each(pos, head) \
365 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
366 pos = pos->next)
367
368
369
370
371
372
373
374
375
376
377
378#define __list_for_each(pos, head) \
379 for (pos = (head)->next; pos != (head); pos = pos->next)
380
381
382
383
384
385
386#define list_for_each_prev(pos, head) \
387 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
388 pos = pos->prev)
389
390
391
392
393
394
395
396#define list_for_each_safe(pos, n, head) \
397 for (pos = (head)->next, n = pos->next; pos != (head); \
398 pos = n, n = pos->next)
399
400
401
402
403
404
405
406#define list_for_each_prev_safe(pos, n, head) \
407 for (pos = (head)->prev, n = pos->prev; \
408 prefetch(pos->prev), pos != (head); \
409 pos = n, n = pos->prev)
410
411
412
413
414
415
416
417#define list_for_each_entry(pos, head, member) \
418 for (pos = list_entry((head)->next, typeof(*pos), member); \
419 prefetch(pos->member.next), &pos->member != (head); \
420 pos = list_entry(pos->member.next, typeof(*pos), member))
421
422
423
424
425
426
427
428#define list_for_each_entry_reverse(pos, head, member) \
429 for (pos = list_entry((head)->prev, typeof(*pos), member); \
430 prefetch(pos->member.prev), &pos->member != (head); \
431 pos = list_entry(pos->member.prev, typeof(*pos), member))
432
433
434
435
436
437
438
439
440
441#define list_prepare_entry(pos, head, member) \
442 ((pos) ? : list_entry(head, typeof(*pos), member))
443
444
445
446
447
448
449
450
451
452
453#define list_for_each_entry_continue(pos, head, member) \
454 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
455 prefetch(pos->member.next), &pos->member != (head); \
456 pos = list_entry(pos->member.next, typeof(*pos), member))
457
458
459
460
461
462
463
464
465
466
467#define list_for_each_entry_continue_reverse(pos, head, member) \
468 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
469 prefetch(pos->member.prev), &pos->member != (head); \
470 pos = list_entry(pos->member.prev, typeof(*pos), member))
471
472
473
474
475
476
477
478
479
480#define list_for_each_entry_from(pos, head, member) \
481 for (; prefetch(pos->member.next), &pos->member != (head); \
482 pos = list_entry(pos->member.next, typeof(*pos), member))
483
484
485
486
487
488
489
490
491#define list_for_each_entry_safe(pos, n, head, member) \
492 for (pos = list_entry((head)->next, typeof(*pos), member), \
493 n = list_entry(pos->member.next, typeof(*pos), member); \
494 &pos->member != (head); \
495 pos = n, n = list_entry(n->member.next, typeof(*n), member))
496
497
498
499
500
501
502
503
504
505
506
507#define list_for_each_entry_safe_continue(pos, n, head, member) \
508 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
509 n = list_entry(pos->member.next, typeof(*pos), member); \
510 &pos->member != (head); \
511 pos = n, n = list_entry(n->member.next, typeof(*n), member))
512
513
514
515
516
517
518
519
520
521
522
523#define list_for_each_entry_safe_from(pos, n, head, member) \
524 for (n = list_entry(pos->member.next, typeof(*pos), member); \
525 &pos->member != (head); \
526 pos = n, n = list_entry(n->member.next, typeof(*n), member))
527
528
529
530
531
532
533
534
535
536
537
538#define list_for_each_entry_safe_reverse(pos, n, head, member) \
539 for (pos = list_entry((head)->prev, typeof(*pos), member), \
540 n = list_entry(pos->member.prev, typeof(*pos), member); \
541 &pos->member != (head); \
542 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
543
544
545
546
547
548
549
550
551
552
553
554
555
556#define list_safe_reset_next(pos, n, member) \
557 n = list_entry(pos->member.next, typeof(*pos), member)
558
559
560
561
562
563
564
565
566#define HLIST_HEAD_INIT { .first = NULL }
567#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
568#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
569static inline void INIT_HLIST_NODE(struct hlist_node *h)
570{
571 h->next = NULL;
572 h->pprev = NULL;
573}
574
575static inline int hlist_unhashed(const struct hlist_node *h)
576{
577 return !h->pprev;
578}
579
580static inline int hlist_empty(const struct hlist_head *h)
581{
582 return !h->first;
583}
584
585static inline void __hlist_del(struct hlist_node *n)
586{
587 struct hlist_node *next = n->next;
588 struct hlist_node **pprev = n->pprev;
589 *pprev = next;
590 if (next)
591 next->pprev = pprev;
592}
593
594static inline void hlist_del(struct hlist_node *n)
595{
596 __hlist_del(n);
597 n->next = LIST_POISON1;
598 n->pprev = LIST_POISON2;
599}
600
601static inline void hlist_del_init(struct hlist_node *n)
602{
603 if (!hlist_unhashed(n)) {
604 __hlist_del(n);
605 INIT_HLIST_NODE(n);
606 }
607}
608
609static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
610{
611 struct hlist_node *first = h->first;
612 n->next = first;
613 if (first)
614 first->pprev = &n->next;
615 h->first = n;
616 n->pprev = &h->first;
617}
618
619
620static inline void hlist_add_before(struct hlist_node *n,
621 struct hlist_node *next)
622{
623 n->pprev = next->pprev;
624 n->next = next;
625 next->pprev = &n->next;
626 *(n->pprev) = n;
627}
628
629static inline void hlist_add_after(struct hlist_node *n,
630 struct hlist_node *next)
631{
632 next->next = n->next;
633 n->next = next;
634 next->pprev = &n->next;
635
636 if(next->next)
637 next->next->pprev = &next->next;
638}
639
640
641
642
643
644static inline void hlist_move_list(struct hlist_head *old,
645 struct hlist_head *new)
646{
647 new->first = old->first;
648 if (new->first)
649 new->first->pprev = &new->first;
650 old->first = NULL;
651}
652
653#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
654
655#define hlist_for_each(pos, head) \
656 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
657 pos = pos->next)
658
659#define hlist_for_each_safe(pos, n, head) \
660 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
661 pos = n)
662
663
664
665
666
667
668
669
670#define hlist_for_each_entry(tpos, pos, head, member) \
671 for (pos = (head)->first; \
672 pos && ({ prefetch(pos->next); 1;}) && \
673 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
674 pos = pos->next)
675
676
677
678
679
680
681
682#define hlist_for_each_entry_continue(tpos, pos, member) \
683 for (pos = (pos)->next; \
684 pos && ({ prefetch(pos->next); 1;}) && \
685 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
686 pos = pos->next)
687
688
689
690
691
692
693
694#define hlist_for_each_entry_from(tpos, pos, member) \
695 for (; pos && ({ prefetch(pos->next); 1;}) && \
696 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
697 pos = pos->next)
698
699
700
701
702
703
704
705
706
707#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
708 for (pos = (head)->first; \
709 pos && ({ n = pos->next; 1; }) && \
710 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
711 pos = n)
712
713#endif
714