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