0001
0002 #ifndef __TOOLS_LINUX_LIST_H
0003 #define __TOOLS_LINUX_LIST_H
0004
0005 #include <linux/types.h>
0006 #include <linux/poison.h>
0007 #include <linux/kernel.h>
0008 #include <linux/compiler.h>
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #define LIST_HEAD_INIT(name) { &(name), &(name) }
0021
0022 #define LIST_HEAD(name) \
0023 struct list_head name = LIST_HEAD_INIT(name)
0024
0025 static inline void INIT_LIST_HEAD(struct list_head *list)
0026 {
0027 list->next = list;
0028 list->prev = list;
0029 }
0030
0031
0032
0033
0034
0035
0036
0037 #ifndef CONFIG_DEBUG_LIST
0038 static inline void __list_add(struct list_head *new,
0039 struct list_head *prev,
0040 struct list_head *next)
0041 {
0042 next->prev = new;
0043 new->next = next;
0044 new->prev = prev;
0045 prev->next = new;
0046 }
0047 #else
0048 extern void __list_add(struct list_head *new,
0049 struct list_head *prev,
0050 struct list_head *next);
0051 #endif
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061 static inline void list_add(struct list_head *new, struct list_head *head)
0062 {
0063 __list_add(new, head, head->next);
0064 }
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075 static inline void list_add_tail(struct list_head *new, struct list_head *head)
0076 {
0077 __list_add(new, head->prev, head);
0078 }
0079
0080
0081
0082
0083
0084
0085
0086
0087 static inline void __list_del(struct list_head * prev, struct list_head * next)
0088 {
0089 next->prev = prev;
0090 WRITE_ONCE(prev->next, next);
0091 }
0092
0093
0094
0095
0096
0097
0098
0099 #ifndef CONFIG_DEBUG_LIST
0100 static inline void __list_del_entry(struct list_head *entry)
0101 {
0102 __list_del(entry->prev, entry->next);
0103 }
0104
0105 static inline void list_del(struct list_head *entry)
0106 {
0107 __list_del(entry->prev, entry->next);
0108 entry->next = LIST_POISON1;
0109 entry->prev = LIST_POISON2;
0110 }
0111 #else
0112 extern void __list_del_entry(struct list_head *entry);
0113 extern void list_del(struct list_head *entry);
0114 #endif
0115
0116
0117
0118
0119
0120
0121
0122
0123 static inline void list_replace(struct list_head *old,
0124 struct list_head *new)
0125 {
0126 new->next = old->next;
0127 new->next->prev = new;
0128 new->prev = old->prev;
0129 new->prev->next = new;
0130 }
0131
0132 static inline void list_replace_init(struct list_head *old,
0133 struct list_head *new)
0134 {
0135 list_replace(old, new);
0136 INIT_LIST_HEAD(old);
0137 }
0138
0139
0140
0141
0142
0143 static inline void list_del_init(struct list_head *entry)
0144 {
0145 __list_del_entry(entry);
0146 INIT_LIST_HEAD(entry);
0147 }
0148
0149
0150
0151
0152
0153
0154 static inline void list_move(struct list_head *list, struct list_head *head)
0155 {
0156 __list_del_entry(list);
0157 list_add(list, head);
0158 }
0159
0160
0161
0162
0163
0164
0165 static inline void list_move_tail(struct list_head *list,
0166 struct list_head *head)
0167 {
0168 __list_del_entry(list);
0169 list_add_tail(list, head);
0170 }
0171
0172
0173
0174
0175
0176
0177 static inline int list_is_last(const struct list_head *list,
0178 const struct list_head *head)
0179 {
0180 return list->next == head;
0181 }
0182
0183
0184
0185
0186
0187 static inline int list_empty(const struct list_head *head)
0188 {
0189 return head->next == head;
0190 }
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202
0203
0204
0205 static inline int list_empty_careful(const struct list_head *head)
0206 {
0207 struct list_head *next = head->next;
0208 return (next == head) && (next == head->prev);
0209 }
0210
0211
0212
0213
0214
0215 static inline void list_rotate_left(struct list_head *head)
0216 {
0217 struct list_head *first;
0218
0219 if (!list_empty(head)) {
0220 first = head->next;
0221 list_move_tail(first, head);
0222 }
0223 }
0224
0225
0226
0227
0228
0229 static inline int list_is_singular(const struct list_head *head)
0230 {
0231 return !list_empty(head) && (head->next == head->prev);
0232 }
0233
0234 static inline void __list_cut_position(struct list_head *list,
0235 struct list_head *head, struct list_head *entry)
0236 {
0237 struct list_head *new_first = entry->next;
0238 list->next = head->next;
0239 list->next->prev = list;
0240 list->prev = entry;
0241 entry->next = list;
0242 head->next = new_first;
0243 new_first->prev = head;
0244 }
0245
0246
0247
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260 static inline void list_cut_position(struct list_head *list,
0261 struct list_head *head, struct list_head *entry)
0262 {
0263 if (list_empty(head))
0264 return;
0265 if (list_is_singular(head) &&
0266 (head->next != entry && head != entry))
0267 return;
0268 if (entry == head)
0269 INIT_LIST_HEAD(list);
0270 else
0271 __list_cut_position(list, head, entry);
0272 }
0273
0274 static inline void __list_splice(const struct list_head *list,
0275 struct list_head *prev,
0276 struct list_head *next)
0277 {
0278 struct list_head *first = list->next;
0279 struct list_head *last = list->prev;
0280
0281 first->prev = prev;
0282 prev->next = first;
0283
0284 last->next = next;
0285 next->prev = last;
0286 }
0287
0288
0289
0290
0291
0292
0293 static inline void list_splice(const struct list_head *list,
0294 struct list_head *head)
0295 {
0296 if (!list_empty(list))
0297 __list_splice(list, head, head->next);
0298 }
0299
0300
0301
0302
0303
0304
0305 static inline void list_splice_tail(struct list_head *list,
0306 struct list_head *head)
0307 {
0308 if (!list_empty(list))
0309 __list_splice(list, head->prev, head);
0310 }
0311
0312
0313
0314
0315
0316
0317
0318
0319 static inline void list_splice_init(struct list_head *list,
0320 struct list_head *head)
0321 {
0322 if (!list_empty(list)) {
0323 __list_splice(list, head, head->next);
0324 INIT_LIST_HEAD(list);
0325 }
0326 }
0327
0328
0329
0330
0331
0332
0333
0334
0335
0336 static inline void list_splice_tail_init(struct list_head *list,
0337 struct list_head *head)
0338 {
0339 if (!list_empty(list)) {
0340 __list_splice(list, head->prev, head);
0341 INIT_LIST_HEAD(list);
0342 }
0343 }
0344
0345
0346
0347
0348
0349
0350
0351 #define list_entry(ptr, type, member) \
0352 container_of(ptr, type, member)
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362 #define list_first_entry(ptr, type, member) \
0363 list_entry((ptr)->next, type, member)
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373 #define list_last_entry(ptr, type, member) \
0374 list_entry((ptr)->prev, type, member)
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384 #define list_first_entry_or_null(ptr, type, member) \
0385 (!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395 #define list_last_entry_or_null(ptr, type, member) \
0396 (!list_empty(ptr) ? list_last_entry(ptr, type, member) : NULL)
0397
0398
0399
0400
0401
0402
0403 #define list_next_entry(pos, member) \
0404 list_entry((pos)->member.next, typeof(*(pos)), member)
0405
0406
0407
0408
0409
0410
0411 #define list_prev_entry(pos, member) \
0412 list_entry((pos)->member.prev, typeof(*(pos)), member)
0413
0414
0415
0416
0417
0418
0419 #define list_for_each(pos, head) \
0420 for (pos = (head)->next; pos != (head); pos = pos->next)
0421
0422
0423
0424
0425
0426
0427 #define list_for_each_prev(pos, head) \
0428 for (pos = (head)->prev; pos != (head); pos = pos->prev)
0429
0430
0431
0432
0433
0434
0435
0436 #define list_for_each_safe(pos, n, head) \
0437 for (pos = (head)->next, n = pos->next; pos != (head); \
0438 pos = n, n = pos->next)
0439
0440
0441
0442
0443
0444
0445
0446 #define list_for_each_prev_safe(pos, n, head) \
0447 for (pos = (head)->prev, n = pos->prev; \
0448 pos != (head); \
0449 pos = n, n = pos->prev)
0450
0451
0452
0453
0454
0455
0456
0457 #define list_for_each_entry(pos, head, member) \
0458 for (pos = list_first_entry(head, typeof(*pos), member); \
0459 &pos->member != (head); \
0460 pos = list_next_entry(pos, member))
0461
0462
0463
0464
0465
0466
0467
0468 #define list_for_each_entry_reverse(pos, head, member) \
0469 for (pos = list_last_entry(head, typeof(*pos), member); \
0470 &pos->member != (head); \
0471 pos = list_prev_entry(pos, member))
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481 #define list_prepare_entry(pos, head, member) \
0482 ((pos) ? : list_entry(head, typeof(*pos), member))
0483
0484
0485
0486
0487
0488
0489
0490
0491
0492
0493 #define list_for_each_entry_continue(pos, head, member) \
0494 for (pos = list_next_entry(pos, member); \
0495 &pos->member != (head); \
0496 pos = list_next_entry(pos, member))
0497
0498
0499
0500
0501
0502
0503
0504
0505
0506
0507 #define list_for_each_entry_continue_reverse(pos, head, member) \
0508 for (pos = list_prev_entry(pos, member); \
0509 &pos->member != (head); \
0510 pos = list_prev_entry(pos, member))
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520 #define list_for_each_entry_from(pos, head, member) \
0521 for (; &pos->member != (head); \
0522 pos = list_next_entry(pos, member))
0523
0524
0525
0526
0527
0528
0529
0530
0531 #define list_for_each_entry_safe(pos, n, head, member) \
0532 for (pos = list_first_entry(head, typeof(*pos), member), \
0533 n = list_next_entry(pos, member); \
0534 &pos->member != (head); \
0535 pos = n, n = list_next_entry(n, member))
0536
0537
0538
0539
0540
0541
0542
0543
0544
0545
0546
0547 #define list_for_each_entry_safe_continue(pos, n, head, member) \
0548 for (pos = list_next_entry(pos, member), \
0549 n = list_next_entry(pos, member); \
0550 &pos->member != (head); \
0551 pos = n, n = list_next_entry(n, member))
0552
0553
0554
0555
0556
0557
0558
0559
0560
0561
0562
0563 #define list_for_each_entry_safe_from(pos, n, head, member) \
0564 for (n = list_next_entry(pos, member); \
0565 &pos->member != (head); \
0566 pos = n, n = list_next_entry(n, member))
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578 #define list_for_each_entry_safe_reverse(pos, n, head, member) \
0579 for (pos = list_last_entry(head, typeof(*pos), member), \
0580 n = list_prev_entry(pos, member); \
0581 &pos->member != (head); \
0582 pos = n, n = list_prev_entry(n, member))
0583
0584
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
0596 #define list_safe_reset_next(pos, n, member) \
0597 n = list_next_entry(pos, member)
0598
0599
0600
0601
0602
0603
0604
0605
0606 #define HLIST_HEAD_INIT { .first = NULL }
0607 #define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
0608 #define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
0609 static inline void INIT_HLIST_NODE(struct hlist_node *h)
0610 {
0611 h->next = NULL;
0612 h->pprev = NULL;
0613 }
0614
0615 static inline int hlist_unhashed(const struct hlist_node *h)
0616 {
0617 return !h->pprev;
0618 }
0619
0620 static inline int hlist_empty(const struct hlist_head *h)
0621 {
0622 return !h->first;
0623 }
0624
0625 static inline void __hlist_del(struct hlist_node *n)
0626 {
0627 struct hlist_node *next = n->next;
0628 struct hlist_node **pprev = n->pprev;
0629
0630 WRITE_ONCE(*pprev, next);
0631 if (next)
0632 next->pprev = pprev;
0633 }
0634
0635 static inline void hlist_del(struct hlist_node *n)
0636 {
0637 __hlist_del(n);
0638 n->next = LIST_POISON1;
0639 n->pprev = LIST_POISON2;
0640 }
0641
0642 static inline void hlist_del_init(struct hlist_node *n)
0643 {
0644 if (!hlist_unhashed(n)) {
0645 __hlist_del(n);
0646 INIT_HLIST_NODE(n);
0647 }
0648 }
0649
0650 static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
0651 {
0652 struct hlist_node *first = h->first;
0653 n->next = first;
0654 if (first)
0655 first->pprev = &n->next;
0656 h->first = n;
0657 n->pprev = &h->first;
0658 }
0659
0660
0661 static inline void hlist_add_before(struct hlist_node *n,
0662 struct hlist_node *next)
0663 {
0664 n->pprev = next->pprev;
0665 n->next = next;
0666 next->pprev = &n->next;
0667 *(n->pprev) = n;
0668 }
0669
0670 static inline void hlist_add_behind(struct hlist_node *n,
0671 struct hlist_node *prev)
0672 {
0673 n->next = prev->next;
0674 prev->next = n;
0675 n->pprev = &prev->next;
0676
0677 if (n->next)
0678 n->next->pprev = &n->next;
0679 }
0680
0681
0682 static inline void hlist_add_fake(struct hlist_node *n)
0683 {
0684 n->pprev = &n->next;
0685 }
0686
0687 static inline bool hlist_fake(struct hlist_node *h)
0688 {
0689 return h->pprev == &h->next;
0690 }
0691
0692
0693
0694
0695
0696 static inline void hlist_move_list(struct hlist_head *old,
0697 struct hlist_head *new)
0698 {
0699 new->first = old->first;
0700 if (new->first)
0701 new->first->pprev = &new->first;
0702 old->first = NULL;
0703 }
0704
0705 #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
0706
0707 #define hlist_for_each(pos, head) \
0708 for (pos = (head)->first; pos ; pos = pos->next)
0709
0710 #define hlist_for_each_safe(pos, n, head) \
0711 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
0712 pos = n)
0713
0714 #define hlist_entry_safe(ptr, type, member) \
0715 ({ typeof(ptr) ____ptr = (ptr); \
0716 ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
0717 })
0718
0719
0720
0721
0722
0723
0724
0725 #define hlist_for_each_entry(pos, head, member) \
0726 for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member);\
0727 pos; \
0728 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
0729
0730
0731
0732
0733
0734
0735 #define hlist_for_each_entry_continue(pos, member) \
0736 for (pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member);\
0737 pos; \
0738 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
0739
0740
0741
0742
0743
0744
0745 #define hlist_for_each_entry_from(pos, member) \
0746 for (; pos; \
0747 pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
0748
0749
0750
0751
0752
0753
0754
0755
0756 #define hlist_for_each_entry_safe(pos, n, head, member) \
0757 for (pos = hlist_entry_safe((head)->first, typeof(*pos), member);\
0758 pos && ({ n = pos->member.next; 1; }); \
0759 pos = hlist_entry_safe(n, typeof(*pos), member))
0760
0761
0762
0763
0764
0765
0766
0767
0768 static inline void list_del_range(struct list_head *begin,
0769 struct list_head *end)
0770 {
0771 begin->prev->next = end->next;
0772 end->next->prev = begin->prev;
0773 }
0774
0775
0776
0777
0778
0779
0780 #define list_for_each_from(pos, head) \
0781 for (; pos != (head); pos = pos->next)
0782
0783 #endif