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