-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
782 lines (704 loc) · 23 KB
/
list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/*
* Taken from Linux kernel.
* Some parts:
* Copyright Neil Brown ©2015-2023 <[email protected]>
* May be distributed under terms of GPLv2 - see file:COPYING
*
*/
#ifndef __LIST_H__
#define __LIST_H__
#include "safe.h"
#define ASSERT(x) do { if (!(x)) abort();} while (0)
/*Taken from various places in linux kernel */
#ifndef offsetof
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#endif
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr safe = (ptr); \
(type * safe)( (char *)__mptr - offsetof(type,member) );})
#define container_of_array(ptr, type, member, index) ({ \
const typeof( ((type *)0)->member[index] ) *__mptr safe = (ptr); \
(type * safe)( (char *)__mptr - offsetof(type,member[index]) );})
/*
* These are non-NULL pointers that will result in page faults
* under normal circumstances, used to verify that nobody uses
* non-initialized list entries.
*/
#define LIST_POISON1 ((void *) 0x00100100)
#define LIST_POISON2 ((void *) 0x00200200)
/*
* Simple doubly linked list implementation.
*
* Some of the internal functions ("__xxx") are useful when
* manipulating whole lists rather than single entries, as
* sometimes we already know the next/prev entries and we can
* generate better code by using them directly rather than
* using the generic single-entry routines.
*/
struct list_head {
struct list_head *next safe, *prev safe;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) \
struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { \
(ptr)->next = (ptr); (ptr)->prev = (ptr); \
} while (0)
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_add(struct list_head *new safe,
struct list_head *prev safe,
struct list_head *next safe)
{
next->prev = new;
new->next = next;
new->prev = prev;
prev->next = new;
}
/**
* list_add - add a new entry
* @new: new entry to be added
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void list_add(struct list_head *new safe, struct list_head *head safe)
{
__list_add(new, head, head->next);
}
/**
* list_add_tail - add a new entry
* @new: new entry to be added
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void list_add_tail(struct list_head *new safe, struct list_head *head safe)
{
__list_add(new, head->prev, head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __list_del(struct list_head *prev safe, struct list_head *next safe)
{
next->prev = prev;
prev->next = next;
}
/**
* list_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void list_del(struct list_head *entry safe)
{
__list_del(entry->prev, entry->next);
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
/**
* list_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void list_del_init(struct list_head *entry safe)
{
__list_del(entry->prev, entry->next);
INIT_LIST_HEAD(entry);
}
/**
* list_move - delete from one list and add as another's head
* @list: the entry to move
* @head: the head that will precede our entry
*/
static inline void list_move(struct list_head *list safe, struct list_head *head safe)
{
__list_del(list->prev, list->next);
list_add(list, head);
}
/**
* list_move_tail - delete from one list and add as another's tail
* @list: the entry to move
* @head: the head that will follow our entry
*/
static inline void list_move_tail(struct list_head *list safe,
struct list_head *head safe)
{
__list_del(list->prev, list->next);
list_add_tail(list, head);
}
/**
* list_empty - tests whether a list is empty
* @head: the list to test.
*/
static inline int list_empty(struct list_head *head safe)
{
return head->next == head;
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
/**
* list_first_entry - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_first_entry(ptr, type, member) \
list_entry((ptr)->next, type, member)
/**
* list_last_entry - get the last element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note, that list is expected to be not empty.
*/
#define list_last_entry(ptr, type, member) \
list_entry((ptr)->prev, type, member)
/**
* list_first_entry_or_null - get the first element from a list
* @ptr: the list head to take the element from.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*
* Note that if the list is empty, it returns NULL.
*/
#define list_first_entry_or_null(ptr, type, member) \
(!list_empty(ptr) ? list_first_entry(ptr, type, member) : NULL)
/**
* list_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_next_entry(pos, member) \
list_entry((pos)->member.next, typeof(*(pos)), member)
/**
* list_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define list_prev_entry(pos, member) \
list_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* list_for_each - iterate over a list
* @pos: the &struct list_head to use as a loop cursor.
* @head: the head for your list.
*/
#define list_for_each(pos, head) \
for (pos = (head)->next; pos != (head); pos = pos->next)
/**
* list_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry(pos, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_reverse - iterate backwards over list of given type.
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_reverse(pos, head, member) \
for (pos = list_last_entry(head, typeof(*pos), member); \
&pos->member != (head); \
pos = list_prev_entry(pos, member))
/**
* list_for_each_entry_safe - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @n: &struct list_node temp pointer.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*/
#define list_for_each_entry_safe(pos, n, head, member) \
for (pos = list_first_entry(head, typeof(*pos), member); \
&pos->member != (head) && ({n = pos->member.next; 1;}); \
pos = list_entry(n, typeof(*pos), member))
/**
* list_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define list_for_each_entry_continue(pos, head, member) \
for (pos = list_next_entry(pos, member); \
&pos->member != (head); \
pos = list_next_entry(pos, member))
/**
* list_for_each_entry_from - iterate over list of given type from the current point
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the list_head within the struct.
*
* Iterate over list of given type, continuing from current position.
*/
#define list_for_each_entry_from(pos, head, member) \
for (; pos && &pos->member != (head); \
pos = list_next_entry(pos, member))
/* 'first' has lsb set so the start of the list can be recognised
* with passing the head around.
*/
struct hlist_head {
struct hlist_node *vfirst;
};
#define HLIST_PTR(_h) ((struct hlist_node *)(((unsigned long)_h) & ~1UL))
#define HLIST_HEAD_PTR(_h) ((void*)(((unsigned long)_h) | 1UL))
#define HLIST_IS_HEAD(_h) (((unsigned long)(*(_h))) & 1UL)
struct hlist_node {
struct hlist_node *next;
struct hlist_node **pprev safe;
};
#define HLIST_HEAD_INIT { .vfirst = HLIST_HEAD_PTR(NULL) }
#define HLIST_HEAD(name) struct hlist_head name = { .vfirst = HLIST_HEAD_PTR(NULL) }
#define INIT_HLIST_HEAD(ptr) ((ptr)->vfirst = HLIST_HEAD_PTR(NULL))
static inline void INIT_HLIST_NODE(struct hlist_node *h safe)
{
h->next = NULL;
h->pprev = &h->next;
}
static inline int hlist_unhashed(const struct hlist_node *h safe)
{
return h->pprev == &h->next;
}
static inline int hlist_empty(const struct hlist_head *h safe)
{
#ifndef __CHECKER__
return !HLIST_PTR(h->vfirst);
#else
return (h->vfirst == NULL || h->vfirst == (struct hlist_node*)1);
#endif
}
static inline void __hlist_del(struct hlist_node *n safe)
{
struct hlist_node *next = n->next;
struct hlist_node **pprev = n->pprev;
if (HLIST_IS_HEAD(pprev))
*pprev= HLIST_HEAD_PTR(next);
else
*pprev = next;
if (next)
next->pprev = pprev;
}
static inline void hlist_del(struct hlist_node *n safe)
{
__hlist_del(n);
n->next = LIST_POISON1;
n->pprev = LIST_POISON2;
}
static inline void hlist_del_init(struct hlist_node *n safe)
{
if (!hlist_unhashed(n)) {
__hlist_del(n);
INIT_HLIST_NODE(n);
}
}
static inline void hlist_add_head(struct hlist_node *n safe, struct hlist_head *h safe)
{
struct hlist_node *first = HLIST_PTR(h->vfirst);
n->next = first;
if (first)
first->pprev = &n->next;
h->vfirst = HLIST_HEAD_PTR(n);
n->pprev = &h->vfirst;
}
/* next must be != NULL */
static inline void hlist_add_before(struct hlist_node *n safe,
struct hlist_node *next safe)
{
n->pprev = next->pprev;
n->next = next;
next->pprev = &n->next;
if (HLIST_IS_HEAD((n->pprev)))
*(n->pprev) = HLIST_HEAD_PTR(n);
else
*(n->pprev) = n;
}
static inline void hlist_add_after(struct hlist_node *n safe,
struct hlist_node *next safe)
{
next->next = n->next;
n->next = next;
next->pprev = &n->next;
if(next->next)
next->next->pprev = &next->next;
}
/*
* Move a list from one list head to another. Fixup the pprev
* reference of the first entry if it exists.
*/
static inline void hlist_move_list(struct hlist_head *old safe,
struct hlist_head *new safe)
{
new->vfirst = old->vfirst;
if (!hlist_empty(new))
HLIST_PTR(new->vfirst)->pprev = &new->vfirst;
INIT_HLIST_HEAD(old);
}
#define hlist_entry(ptr, type, member) container_of(HLIST_PTR(ptr),type,member)
#define hlist_next_entry(ptr, member) \
hlist_entry((ptr)->member.next, typeof(*(ptr)), member)
#define hlist_first_entry(head, type, member) \
hlist_entry((head)->vfirst, type, member)
#define hlist_prev(ptr) container_of((ptr)->pprev, struct hlist_node, next)
#define hlist_prev_entry(ptr, member) \
({struct hlist_node *__hln = hlist_prev(&(ptr)->member); \
hlist_entry(__hln, typeof(*(ptr)), member); })
#define hlist_for_each(pos, head) \
for (pos = HLIST_PTR((head)->first); pos ; pos = pos->next)
#define hlist_for_each_safe(pos, n, head) \
for (pos = HLIST_PTR((head)->first); pos && ({ n = pos->next; 1; }); \
pos = n)
/**
* hlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry(pos, head, member) \
for (pos = hlist_empty(head) ? NULL : hlist_first_entry(head, typeof(*pos), member); \
pos ; \
pos = pos->member.next ? hlist_next_entry(pos, member) : NULL)
/**
* hlist_for_each_entry_continue - iterate over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue(pos, member) \
for (pos = (pos)->member.next ? hlist_next_entry(pos, member) : NULL; \
pos ; \
pos = pos->member.next ? hlist_next_entry(pos, member) : NULL)
/**
* hlist_for_each_entry_continue_reverse - iterate backwards over a hlist continuing after current point
* @pos: the type * to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#define hlist_for_each_entry_continue_reverse(pos, member) \
for (pos = HLIST_IS_HEAD(pos->member.pprev) ? NULL : hlist_prev_entry(pos, member); \
pos ; \
pos = HLIST_IS_HEAD(pos->member.pprev) ? NULL : hlist_prev_entry(pos, member ))
/**
* hlist_for_each_entry_from - iterate over a hlist continuing from current point
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @member: the name of the hlist_node within the struct.
*/
#if 0
#define hlist_for_each_entry_from(tpos, pos, member) \
for (; pos && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = pos->next)
#endif
/**
* hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
* @tpos: the type * to use as a loop cursor.
* @pos: the &struct hlist_node to use as a loop cursor.
* @n: another &struct hlist_node to use as temporary storage
* @head: the head for your list.
* @member: the name of the hlist_node within the struct.
*/
#if 0
#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
for (pos = HLIST_PTR((head)->first); \
pos && ({ n = pos->next; 1; }) && \
({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
pos = n)
#endif
/*
* tlists are lists with types.
* The lsbs of each of the two pointers provide 2 bits of type
* information. This is useful when different fields in different
* structures can all be on the same list
*/
struct tlist_head {
struct tlist_head *next safe, *prev safe;
};
#define TLIST_PTR(p) ((struct tlist_head * safe)(((unsigned long)(p)) & ~1UL))
#define __TLIST_TYPE(p,n) ((((unsigned long)(p)) & 1)<<1 | (((unsigned long)(n)) & 1))
#define TLIST_TYPE(e) __TLIST_TYPE((e)->prev, (e)->next)
#define TLIST_PREV(p,t) ((struct tlist_head * safe)((((t)>>1)&1)|(unsigned long)(p)))
#define TLIST_NEXT(n,t) ((struct tlist_head * safe)(((t)&1)|(unsigned long)(n)))
#define TLIST_HEAD_INIT(name, type) { TLIST_NEXT(&(name),(type)), \
TLIST_PREV(&(name),(type)) }
#define INIT_TLIST_HEAD(ptr, type) do { \
(ptr)->next = TLIST_NEXT((ptr),(type)); \
(ptr)->prev = TLIST_PREV((ptr),(type)); \
} while (0)
/**
* tlist_empty - tests whether a tlist is empty
* @head: the list to test.
*/
static inline int tlist_empty(struct tlist_head *head safe)
{
return TLIST_PTR(head->next) == head;
}
/*
* Insert a new entry between two known consecutive entries.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __tlist_add(struct tlist_head *new safe, int type,
struct tlist_head *prev safe,
struct tlist_head *next safe)
{
next->prev = TLIST_PREV(new, __TLIST_TYPE(next->prev, NULL));
new->next = TLIST_NEXT(next, type);
new->prev = TLIST_PREV(prev, type);
prev->next = TLIST_NEXT(new, __TLIST_TYPE(NULL, prev->next));
}
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_head within the struct.
*/
#define tlist_entry(ptr, type, member) \
container_of(TLIST_PTR(ptr), type, member)
/**
* tlist_add - add a new entry
* @new: new entry to be added
* @type: type of location
* @head: list head to add it after
*
* Insert a new entry after the specified head.
* This is good for implementing stacks.
*/
static inline void tlist_add(struct tlist_head *new safe, int type, struct tlist_head *head safe)
{
__tlist_add(new, type, head, TLIST_PTR(head->next));
}
/**
* tlist_add_tail - add a new entry
* @new: new entry to be added
* @type: type of location
* @head: list head to add it before
*
* Insert a new entry before the specified head.
* This is useful for implementing queues.
*/
static inline void tlist_add_tail(struct tlist_head *new safe, int type,
struct tlist_head *head safe)
{
__tlist_add(new, type, TLIST_PTR(head->prev), head);
}
/*
* Delete a list entry by making the prev/next entries
* point to each other.
*
* This is only for internal list manipulation where we know
* the prev/next entries already!
*/
static inline void __tlist_del(struct tlist_head *prev safe, struct tlist_head *next safe)
{
int nt = TLIST_TYPE(next);
int pt = TLIST_TYPE(prev);
next->prev = TLIST_PREV(TLIST_PTR(prev), nt);
prev->next = TLIST_NEXT(TLIST_PTR(next), pt);
}
/**
* tlist_del - deletes entry from list.
* @entry: the element to delete from the list.
* Note: list_empty on entry does not return true after this, the entry is
* in an undefined state.
*/
static inline void tlist_del(struct tlist_head *entry safe)
{
__tlist_del(TLIST_PTR(entry->prev), TLIST_PTR(entry->next));
entry->next = LIST_POISON1;
entry->prev = LIST_POISON2;
}
/**
* tlist_del_init - deletes entry from list and reinitialize it.
* @entry: the element to delete from the list.
*/
static inline void tlist_del_init(struct tlist_head *entry safe)
{
int type = TLIST_TYPE(entry);
__tlist_del(TLIST_PTR(entry->prev), TLIST_PTR(entry->next));
INIT_TLIST_HEAD(entry, type);
}
/**
* tlist_next_entry - get the next element in list
* @pos: the type * to cursor
* @member: the name of the tlist_head within the struct.
*/
#define tlist_next_entry(pos, member) \
tlist_entry((pos)->member.next, typeof(*(pos)), member)
/**
* tlist_prev_entry - get the prev element in list
* @pos: the type * to cursor
* @member: the name of the list_head within the struct.
*/
#define tlist_prev_entry(pos, member) \
tlist_entry((pos)->member.prev, typeof(*(pos)), member)
/**
* tlist_for_each - iterate over a tlist
* @pos: the &struct tlist_head to use as a loop cursor.
* @head: the head for your tlist.
*/
#define tlist_for_each(pos, head) \
for (pos = TLIST_PTR((head)->next); pos != (head); pos = TLIST_PTR(pos->next))
/**
* tlist_for_each_continue - continue iteration over tlist
* @pos: the struct tlist_head * to use as a loop cursor.
* @head_typef: the the type of the head for your list.
*
* Continue to iterate over tlist, continuing after
* the current position.
*/
#define tlist_for_each_continue(pos, head_type) \
for (pos = TLIST_PTR(pos->next); \
TLIST_TYPE(pos) != (head_type); \
pos = TLIST_PTR(pos->next))
/**
* list_for_each_continue_reverse - iterate backwards from the given point
* @pos: the struct tlist_head * to use as a loop cursor.
* @head: the type of the head for your list.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define tlist_for_each_continue_reverse(pos, head_type) \
for (pos = TLIST_PTR(pos->prev); \
TLIST_TYPE(pos) != (head_type); \
pos = TLIST_PTR(pos->prev))
/**
* tlist_for_each_entry - iterate over list of given type
* @pos: the type * to use as a loop cursor.
* @head: the head for your list.
* @member: the name of the tlist_head within the struct.
*
* Iterate over list of given type.
*/
#define tlist_for_each_entry(pos, head, member) \
for (pos = list_entry((head)->next, typeof(*(pos)), member); \
&pos->member != (head); \
pos = tlist_next_entry(pos, member))
/**
* tlist_for_each_entry_continue - continue iteration over list of given type
* @pos: the type * to use as a loop cursor.
* @head_type: type of pointer in the head for your list.
* @member: the name of the tlist_head within the struct.
*
* Continue to iterate over list of given type, continuing after
* the current position.
*/
#define tlist_for_each_entry_continue(pos, head_type, member) \
for (pos = tlist_next_entry(pos, member); \
TLIST_TYPE(&pos->member) != (head_type); \
pos = tlist_next_entry(pos, member))
/**
* list_for_each_entry_continue_reverse - iterate backwards from the given point
* @pos: the type * to use as a loop cursor.
* @head_type: type of pointer in the head for your list.
* @member: the name of the list_head within the struct.
*
* Start to iterate over list of given type backwards, continuing after
* the current position.
*/
#define tlist_for_each_entry_continue_reverse(pos, head_type, member) \
for (pos = tlist_prev_entry(pos, member); \
TLIST_TYPE(&pos->member) != (head_type); \
pos = tlist_prev_entry(pos, member))
/* Natural merge sort of the linked list */
static inline void sort_list(struct list_head *lst safe,
char * (*key)(struct list_head *le, const void *data),
const void *data)
{
struct list_head *de[2];
struct list_head *l;
if (list_empty(lst))
return;
/* Convert to NULL terminated singly-linked list for sorting */
lst->prev->next = safe_cast NULL;
de[0] = lst->next;
de[1] = NULL;
do {
struct list_head ** safe dep[2];
struct list_head *d[2];
int curr = 0;
char *prev = "";
int next = 0;
dep[0] = &de[0];
dep[1] = &de[1];
d[0] = de[0];
d[1] = de[1];
/* d[0] and d[1] are two lists to be merged and split.
* The results will be put in de[0] and de[1].
* dep[0] and dep[1] are end pointers to de[0] and de[1] so far.
*
* Take least of d[0] and d[1].
* If it is larger than prev, add to
* dep[curr], else swap curr then add
*/
while (d[0] || d[1]) {
char *kn = key(d[next], data);
char *kp = key(d[1-next], data);
if (kn == NULL ||
(kp != NULL &&
!((strcmp(prev, kp) <= 0)
^(strcmp(kp, kn) <= 0)
^(strcmp(kn, prev) <= 0)))
) {
char *t = kn;
kn = kp;
kp = t;
next = 1 - next;
}
if (!d[next] || !kn)
break;
if (strcmp(kn, prev) < 0)
curr = 1 - curr;
prev = kn;
*dep[curr] = d[next];
dep[curr] = &d[next]->next;
d[next] = d[next]->next;
}
*dep[0] = NULL;
*dep[1] = NULL;
} while (de[0] && de[1]);
/* Now re-assemble the doublely-linked list */
if (de[0])
lst->next = de[0];
else
lst->next = safe_cast de[1];
l = lst;
while ((void*)l->next) {
l->next->prev = l;
l = l->next;
}
l->next = lst;
lst->prev = l;
}
#endif /* __LIST_H__ */