blob: 519be3668be8e60acc7a0c0af019a72f92448e7a [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001#ifndef _LINUX_LIST_H
2#define _LINUX_LIST_H
3
4#if 0
5#include <mtd_linux/stddef.h>
6#include <mtd_linux/poison.h>
7#endif
8#include "poison.h"
9#include "Types.h"
10
11#ifndef ARCH_HAS_PREFETCH
12#define ARCH_HAS_PREFETCH
13static inline void prefetch(const void *x) {;}
14#endif
15
16/*
17 * Simple doubly linked list implementation.
18 *
19 * Some of the internal functions ("__xxx") are useful when
20 * manipulating whole lists rather than single entries, as
21 * sometimes we already know the next/prev entries and we can
22 * generate better code by using them directly rather than
23 * using the generic single-entry routines.
24 */
25#if 0
26struct list_head {
27 struct list_head *next, *prev;
28};
29#endif
30#ifndef _OS_LINUX
31struct work_struct { } ;
32struct timer_list { } ;
33
34#define LIST_HEAD_INIT(name) { &(name), &(name) }
35
36#define LIST_HEAD(name) \
37 struct list_head name = LIST_HEAD_INIT(name)
38
39#endif
40
41#if 0
42static inline void INIT_LIST_HEAD(struct list_head *list)
43{
44 list->next = list;
45 list->prev = list;
46}
47#endif
48
49/*
50 * Insert a new entry between two known consecutive entries.
51 *
52 * This is only for internal list manipulation where we know
53 * the prev/next entries already!
54 */
55 #if 0
56static inline void __list_add(struct list_head *new,
57 struct list_head *prev,
58 struct list_head *next)
59{
60 next->prev = new;
61 new->next = next;
62 new->prev = prev;
63 prev->next = new;
64}
65#endif
66
67/**
68 * list_add - add a new entry
69 * @new: new entry to be added
70 * @head: list head to add it after
71 *
72 * Insert a new entry after the specified head.
73 * This is good for implementing stacks.
74 */
75 #if 0
76static inline void list_add(struct list_head *new, struct list_head *head)
77{
78 __list_add(new, head, head->next);
79}
80#endif
81
82/**
83 * list_add_tail - add a new entry
84 * @new: new entry to be added
85 * @head: list head to add it before
86 *
87 * Insert a new entry before the specified head.
88 * This is useful for implementing queues.
89 */
90 #if 0
91static inline void list_add_tail(struct list_head *new, struct list_head *head)
92{
93 __list_add(new, head->prev, head);
94}
95#endif
96
97/*
98 * Delete a list entry by making the prev/next entries
99 * point to each other.
100 *
101 * This is only for internal list manipulation where we know
102 * the prev/next entries already!
103 */
104
105#if 0
106static inline void __list_del(struct list_head *prev, struct list_head *next)
107{
108 next->prev = prev;
109 prev->next = next;
110}
111#endif
112/**
113 * list_del - deletes entry from list.
114 * @entry: the element to delete from the list.
115 * Note: list_empty() on entry does not return true after this, the entry is
116 * in an undefined state.
117 */
118 #if 0
119static inline void list_del(struct list_head *entry)
120{
121 __list_del(entry->prev, entry->next);
122 entry->next = LIST_POISON1;
123 entry->prev = LIST_POISON2;
124}
125#endif
126/**
127 * list_replace - replace old entry by new one
128 * @old : the element to be replaced
129 * @new : the new element to insert
130 *
131 * If @old was empty, it will be overwritten.
132 */
133 #if 0
134static inline void list_replace(struct list_head *old,
135 struct list_head *new)
136{
137 new->next = old->next;
138 new->next->prev = new;
139 new->prev = old->prev;
140 new->prev->next = new;
141}
142#endif
143
144#if 0
145static inline void list_replace_init(struct list_head *old,
146 struct list_head *new)
147{
148 list_replace(old, new);
149 INIT_LIST_HEAD(old);
150}
151#endif
152
153/**
154 * list_del_init - deletes entry from list and reinitialize it.
155 * @entry: the element to delete from the list.
156 */
157 #if 0
158static inline void list_del_init(struct list_head *entry)
159{
160 __list_del(entry->prev, entry->next);
161 INIT_LIST_HEAD(entry);
162}
163#endif
164
165/**
166 * list_move - delete from one list and add as another's head
167 * @list: the entry to move
168 * @head: the head that will precede our entry
169 */
170 #if 0
171static inline void list_move(struct list_head *list, struct list_head *head)
172{
173 __list_del(list->prev, list->next);
174 list_add(list, head);
175}
176#endif
177/**
178 * list_move_tail - delete from one list and add as another's tail
179 * @list: the entry to move
180 * @head: the head that will follow our entry
181 */
182 #if 0
183static inline void list_move_tail(struct list_head *list,
184 struct list_head *head)
185{
186 __list_del(list->prev, list->next);
187 list_add_tail(list, head);
188}
189#endif
190/**
191 * list_is_last - tests whether @list is the last entry in list @head
192 * @list: the entry to test
193 * @head: the head of the list
194 */
195 #if 0
196static inline int list_is_last(const struct list_head *list,
197 const struct list_head *head)
198{
199 return list->next == head;
200}
201#endif
202/**
203 * list_empty - tests whether a list is empty
204 * @head: the list to test.
205 */
206 #if 0
207static inline int list_empty(const struct list_head *head)
208{
209 return head->next == head;
210}
211#endif
212/**
213 * list_empty_careful - tests whether a list is empty and not being modified
214 * @head: the list to test
215 *
216 * Description:
217 * tests whether a list is empty _and_ checks that no other CPU might be
218 * in the process of modifying either member (next or prev)
219 *
220 * NOTE: using list_empty_careful() without synchronization
221 * can only be safe if the only activity that can happen
222 * to the list entry is list_del_init(). Eg. it cannot be used
223 * if another CPU could re-list_add() it.
224 */
225 #if 0
226static inline int list_empty_careful(const struct list_head *head)
227{
228 struct list_head *next = head->next;
229 return (next == head) && (next == head->prev);
230}
231#endif
232/**
233 * list_is_singular - tests whether a list has just one entry.
234 * @head: the list to test.
235 */
236 #if 0
237static inline int list_is_singular(const struct list_head *head)
238{
239 return !list_empty(head) && (head->next == head->prev);
240}
241
242static inline void __list_cut_position(struct list_head *list,
243 struct list_head *head, struct list_head *entry)
244{
245 struct list_head *new_first = entry->next;
246 list->next = head->next;
247 list->next->prev = list;
248 list->prev = entry;
249 entry->next = list;
250 head->next = new_first;
251 new_first->prev = head;
252}
253
254/**
255 * list_cut_position - cut a list into two
256 * @list: a new list to add all removed entries
257 * @head: a list with entries
258 * @entry: an entry within head, could be the head itself
259 * and if so we won't cut the list
260 *
261 * This helper moves the initial part of @head, up to and
262 * including @entry, from @head to @list. You should
263 * pass on @entry an element you know is on @head. @list
264 * should be an empty list or a list you do not care about
265 * losing its data.
266 *
267 */
268static inline void list_cut_position(struct list_head *list,
269 struct list_head *head, struct list_head *entry)
270{
271 if (list_empty(head))
272 return;
273 if (list_is_singular(head) &&
274 (head->next != entry && head != entry))
275 return;
276 if (entry == head)
277 INIT_LIST_HEAD(list);
278 else
279 __list_cut_position(list, head, entry);
280}
281
282static inline void __list_splice(const struct list_head *list,
283 struct list_head *prev,
284 struct list_head *next)
285{
286 struct list_head *first = list->next;
287 struct list_head *last = list->prev;
288
289 first->prev = prev;
290 prev->next = first;
291
292 last->next = next;
293 next->prev = last;
294}
295
296/**
297 * list_splice - join two lists, this is designed for stacks
298 * @list: the new list to add.
299 * @head: the place to add it in the first list.
300 */
301static inline void list_splice(const struct list_head *list,
302 struct list_head *head)
303{
304 if (!list_empty(list))
305 __list_splice(list, head, head->next);
306}
307
308/**
309 * list_splice_tail - join two lists, each list being a queue
310 * @list: the new list to add.
311 * @head: the place to add it in the first list.
312 */
313static inline void list_splice_tail(struct list_head *list,
314 struct list_head *head)
315{
316 if (!list_empty(list))
317 __list_splice(list, head->prev, head);
318}
319
320/**
321 * list_splice_init - join two lists and reinitialise the emptied list.
322 * @list: the new list to add.
323 * @head: the place to add it in the first list.
324 *
325 * The list at @list is reinitialised
326 */
327static inline void list_splice_init(struct list_head *list,
328 struct list_head *head)
329{
330 if (!list_empty(list)) {
331 __list_splice(list, head, head->next);
332 INIT_LIST_HEAD(list);
333 }
334}
335
336/**
337 * list_splice_tail_init - join two lists and reinitialise the emptied list
338 * @list: the new list to add.
339 * @head: the place to add it in the first list.
340 *
341 * Each of the lists is a queue.
342 * The list at @list is reinitialised
343 */
344static inline void list_splice_tail_init(struct list_head *list,
345 struct list_head *head)
346{
347 if (!list_empty(list)) {
348 __list_splice(list, head->prev, head);
349 INIT_LIST_HEAD(list);
350 }
351}
352
353/**
354 * list_entry - get the struct for this entry
355 * @ptr: the &struct list_head pointer.
356 * @type: the type of the struct this is embedded in.
357 * @member: the name of the list_struct within the struct.
358 */
359#define list_entry(ptr, type, member) \
360 container_of(ptr, type, member)
361
362/**
363 * list_first_entry - get the first element from a list
364 * @ptr: the list head to take the element from.
365 * @type: the type of the struct this is embedded in.
366 * @member: the name of the list_struct within the struct.
367 *
368 * Note, that list is expected to be not empty.
369 */
370#define list_first_entry(ptr, type, member) \
371 list_entry((ptr)->next, type, member)
372
373/**
374 * list_for_each - iterate over a list
375 * @pos: the &struct list_head to use as a loop cursor.
376 * @head: the head for your list.
377 */
378#define list_for_each(pos, head) \
379 for (pos = (head)->next; prefetch(pos->next), pos != (head); \
380 pos = pos->next)
381
382/**
383 * __list_for_each - iterate over a list
384 * @pos: the &struct list_head to use as a loop cursor.
385 * @head: the head for your list.
386 *
387 * This variant differs from list_for_each() in that it's the
388 * simplest possible list iteration code, no prefetching is done.
389 * Use this for code that knows the list to be very short (empty
390 * or 1 entry) most of the time.
391 */
392#define __list_for_each(pos, head) \
393 for (pos = (head)->next; pos != (head); pos = pos->next)
394
395/**
396 * list_for_each_prev - iterate over a list backwards
397 * @pos: the &struct list_head to use as a loop cursor.
398 * @head: the head for your list.
399 */
400#define list_for_each_prev(pos, head) \
401 for (pos = (head)->prev; prefetch(pos->prev), pos != (head); \
402 pos = pos->prev)
403
404/**
405 * list_for_each_safe - iterate over a list safe against removal of list entry
406 * @pos: the &struct list_head to use as a loop cursor.
407 * @n: another &struct list_head to use as temporary storage
408 * @head: the head for your list.
409 */
410#define list_for_each_safe(pos, n, head) \
411 for (pos = (head)->next, n = pos->next; pos != (head); \
412 pos = n, n = pos->next)
413
414/**
415 * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
416 * @pos: the &struct list_head to use as a loop cursor.
417 * @n: another &struct list_head to use as temporary storage
418 * @head: the head for your list.
419 */
420#define list_for_each_prev_safe(pos, n, head) \
421 for (pos = (head)->prev, n = pos->prev; \
422 prefetch(pos->prev), pos != (head); \
423 pos = n, n = pos->prev)
424
425/**
426 * list_for_each_entry - iterate over list of given type
427 * @pos: the type * to use as a loop cursor.
428 * @head: the head for your list.
429 * @member: the name of the list_struct within the struct.
430 */
431#define list_for_each_entry(pos, head, member) \
432 for (pos = list_entry((head)->next, typeof(*pos), member); \
433 prefetch(pos->member.next), &pos->member != (head); \
434 pos = list_entry(pos->member.next, typeof(*pos), member))
435
436/**
437 * list_for_each_entry_reverse - iterate backwards over list of given type.
438 * @pos: the type * to use as a loop cursor.
439 * @head: the head for your list.
440 * @member: the name of the list_struct within the struct.
441 */
442#define list_for_each_entry_reverse(pos, head, member) \
443 for (pos = list_entry((head)->prev, typeof(*pos), member); \
444 prefetch(pos->member.prev), &pos->member != (head); \
445 pos = list_entry(pos->member.prev, typeof(*pos), member))
446
447/**
448 * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
449 * @pos: the type * to use as a start point
450 * @head: the head of the list
451 * @member: the name of the list_struct within the struct.
452 *
453 * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
454 */
455#define list_prepare_entry(pos, head, member) \
456 ((pos) ? : list_entry(head, typeof(*pos), member))
457
458/**
459 * list_for_each_entry_continue - continue iteration over list of given type
460 * @pos: the type * to use as a loop cursor.
461 * @head: the head for your list.
462 * @member: the name of the list_struct within the struct.
463 *
464 * Continue to iterate over list of given type, continuing after
465 * the current position.
466 */
467#define list_for_each_entry_continue(pos, head, member) \
468 for (pos = list_entry(pos->member.next, typeof(*pos), member); \
469 prefetch(pos->member.next), &pos->member != (head); \
470 pos = list_entry(pos->member.next, typeof(*pos), member))
471
472/**
473 * list_for_each_entry_continue_reverse - iterate backwards from the given point
474 * @pos: the type * to use as a loop cursor.
475 * @head: the head for your list.
476 * @member: the name of the list_struct within the struct.
477 *
478 * Start to iterate over list of given type backwards, continuing after
479 * the current position.
480 */
481#define list_for_each_entry_continue_reverse(pos, head, member) \
482 for (pos = list_entry(pos->member.prev, typeof(*pos), member); \
483 prefetch(pos->member.prev), &pos->member != (head); \
484 pos = list_entry(pos->member.prev, typeof(*pos), member))
485
486/**
487 * list_for_each_entry_from - iterate over list of given type from the current point
488 * @pos: the type * to use as a loop cursor.
489 * @head: the head for your list.
490 * @member: the name of the list_struct within the struct.
491 *
492 * Iterate over list of given type, continuing from current position.
493 */
494#define list_for_each_entry_from(pos, head, member) \
495 for (; prefetch(pos->member.next), &pos->member != (head); \
496 pos = list_entry(pos->member.next, typeof(*pos), member))
497
498/**
499 * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
500 * @pos: the type * to use as a loop cursor.
501 * @n: another type * to use as temporary storage
502 * @head: the head for your list.
503 * @member: the name of the list_struct within the struct.
504 */
505#define list_for_each_entry_safe(pos, n, head, member) \
506 for (pos = list_entry((head)->next, typeof(*pos), member), \
507 n = list_entry(pos->member.next, typeof(*pos), member); \
508 &pos->member != (head); \
509 pos = n, n = list_entry(n->member.next, typeof(*n), member))
510
511/**
512 * list_for_each_entry_safe_continue
513 * @pos: the type * to use as a loop cursor.
514 * @n: another type * to use as temporary storage
515 * @head: the head for your list.
516 * @member: the name of the list_struct within the struct.
517 *
518 * Iterate over list of given type, continuing after current point,
519 * safe against removal of list entry.
520 */
521#define list_for_each_entry_safe_continue(pos, n, head, member) \
522 for (pos = list_entry(pos->member.next, typeof(*pos), member), \
523 n = list_entry(pos->member.next, typeof(*pos), member); \
524 &pos->member != (head); \
525 pos = n, n = list_entry(n->member.next, typeof(*n), member))
526
527/**
528 * list_for_each_entry_safe_from
529 * @pos: the type * to use as a loop cursor.
530 * @n: another type * to use as temporary storage
531 * @head: the head for your list.
532 * @member: the name of the list_struct within the struct.
533 *
534 * Iterate over list of given type from current point, safe against
535 * removal of list entry.
536 */
537#define list_for_each_entry_safe_from(pos, n, head, member) \
538 for (n = list_entry(pos->member.next, typeof(*pos), member); \
539 &pos->member != (head); \
540 pos = n, n = list_entry(n->member.next, typeof(*n), member))
541
542/**
543 * list_for_each_entry_safe_reverse
544 * @pos: the type * to use as a loop cursor.
545 * @n: another type * to use as temporary storage
546 * @head: the head for your list.
547 * @member: the name of the list_struct within the struct.
548 *
549 * Iterate backwards over list of given type, safe against removal
550 * of list entry.
551 */
552#define list_for_each_entry_safe_reverse(pos, n, head, member) \
553 for (pos = list_entry((head)->prev, typeof(*pos), member), \
554 n = list_entry(pos->member.prev, typeof(*pos), member); \
555 &pos->member != (head); \
556 pos = n, n = list_entry(n->member.prev, typeof(*n), member))
557
558/*
559 * Double linked lists with a single pointer list head.
560 * Mostly useful for hash tables where the two pointer list head is
561 * too wasteful.
562 * You lose the ability to access the tail in O(1).
563 */
564
565struct hlist_head {
566 struct hlist_node *first;
567};
568
569struct hlist_node {
570 struct hlist_node *next, **pprev;
571};
572
573#define HLIST_HEAD_INIT { .first = NULL }
574#define HLIST_HEAD(name) struct hlist_head name = { .first = NULL }
575#define INIT_HLIST_HEAD(ptr) ((ptr)->first = NULL)
576static inline void INIT_HLIST_NODE(struct hlist_node *h)
577{
578 h->next = NULL;
579 h->pprev = NULL;
580}
581
582static inline int hlist_unhashed(const struct hlist_node *h)
583{
584 return !h->pprev;
585}
586
587static inline int hlist_empty(const struct hlist_head *h)
588{
589 return !h->first;
590}
591
592static inline void __hlist_del(struct hlist_node *n)
593{
594 struct hlist_node *next = n->next;
595 struct hlist_node **pprev = n->pprev;
596 *pprev = next;
597 if (next)
598 next->pprev = pprev;
599}
600
601static inline void hlist_del(struct hlist_node *n)
602{
603 __hlist_del(n);
604 n->next = LIST_POISON1;
605 n->pprev = LIST_POISON2;
606}
607
608static inline void hlist_del_init(struct hlist_node *n)
609{
610 if (!hlist_unhashed(n)) {
611 __hlist_del(n);
612 INIT_HLIST_NODE(n);
613 }
614}
615
616static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
617{
618 struct hlist_node *first = h->first;
619 n->next = first;
620 if (first)
621 first->pprev = &n->next;
622 h->first = n;
623 n->pprev = &h->first;
624}
625
626/* next must be != NULL */
627static inline void hlist_add_before(struct hlist_node *n,
628 struct hlist_node *next)
629{
630 n->pprev = next->pprev;
631 n->next = next;
632 next->pprev = &n->next;
633 *(n->pprev) = n;
634}
635
636static inline void hlist_add_after(struct hlist_node *n,
637 struct hlist_node *next)
638{
639 next->next = n->next;
640 n->next = next;
641 next->pprev = &n->next;
642
643 if(next->next)
644 next->next->pprev = &next->next;
645}
646
647#define hlist_entry(ptr, type, member) container_of(ptr,type,member)
648
649#define hlist_for_each(pos, head) \
650 for (pos = (head)->first; pos && ({ prefetch(pos->next); 1; }); \
651 pos = pos->next)
652
653#define hlist_for_each_safe(pos, n, head) \
654 for (pos = (head)->first; pos && ({ n = pos->next; 1; }); \
655 pos = n)
656
657/**
658 * hlist_for_each_entry - iterate over list of given type
659 * @tpos: the type * to use as a loop cursor.
660 * @pos: the &struct hlist_node to use as a loop cursor.
661 * @head: the head for your list.
662 * @member: the name of the hlist_node within the struct.
663 */
664#define hlist_for_each_entry(tpos, pos, head, member) \
665 for (pos = (head)->first; \
666 pos && ({ prefetch(pos->next); 1;}) && \
667 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
668 pos = pos->next)
669
670/**
671 * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
672 * @tpos: the type * to use as a loop cursor.
673 * @pos: the &struct hlist_node to use as a loop cursor.
674 * @member: the name of the hlist_node within the struct.
675 */
676#define hlist_for_each_entry_continue(tpos, pos, member) \
677 for (pos = (pos)->next; \
678 pos && ({ prefetch(pos->next); 1;}) && \
679 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
680 pos = pos->next)
681
682/**
683 * hlist_for_each_entry_from - iterate over a hlist continuing from current point
684 * @tpos: the type * to use as a loop cursor.
685 * @pos: the &struct hlist_node to use as a loop cursor.
686 * @member: the name of the hlist_node within the struct.
687 */
688#define hlist_for_each_entry_from(tpos, pos, member) \
689 for (; pos && ({ prefetch(pos->next); 1;}) && \
690 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
691 pos = pos->next)
692
693/**
694 * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
695 * @tpos: the type * to use as a loop cursor.
696 * @pos: the &struct hlist_node to use as a loop cursor.
697 * @n: another &struct hlist_node to use as temporary storage
698 * @head: the head for your list.
699 * @member: the name of the hlist_node within the struct.
700 */
701#define hlist_for_each_entry_safe(tpos, pos, n, head, member) \
702 for (pos = (head)->first; \
703 pos && ({ n = pos->next; 1; }) && \
704 ({ tpos = hlist_entry(pos, typeof(*tpos), member); 1;}); \
705 pos = n)
706#endif
707#endif