• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef _OSAL_LIST_H
17 #define _OSAL_LIST_H
18 
19 #define OSAL_NULL   0
20 
21 /*
22  * Simple doubly linked list implementation.
23  *
24  * Some of the internal functions ("__xxx") are useful when
25  * manipulating whole lists rather than single entries, as
26  * sometimes we already know the next/prev entries and we can
27  * generate better code by using them directly rather than
28  * using the generic single-entry routines.
29  */
30 struct osal_list_head {
31     struct osal_list_head *next, *prev;
32 };
33 #define OSAL_LIST_HEAD_INIT(name) \
34     {                             \
35         &(name), &(name)          \
36     }
37 
38 #define OSAL_LIST_HEAD(name) \
39     struct osal_list_head name = OSAL_LIST_HEAD_INIT(name)
40 
OSAL_INIT_LIST_HEAD(struct osal_list_head * list)41 static inline void OSAL_INIT_LIST_HEAD(struct osal_list_head *list)
42 {
43     list->next = list;
44     list->prev = list;
45 }
46 
47 /*
48  * Insert a new entry between two known consecutive entries.
49  *
50  * This is only for internal list manipulation where we know
51  * the prev/next entries already!
52  */
osal___list_add(struct osal_list_head * new,struct osal_list_head * prev,struct osal_list_head * next)53 static inline void osal___list_add(struct osal_list_head *new,
54                                    struct osal_list_head *prev,
55                                    struct osal_list_head *next)
56 {
57     next->prev = new;
58     new->next = next;
59     new->prev = prev;
60     prev->next = new;
61 }
62 
63 /*
64  * list_add - add a new entry
65  * @new: new entry to be added
66  * @head: list head to add it after
67  *
68  * Insert a new entry after the specified head.
69  * This is good for implementing stacks.
70  */
osal_list_add(struct osal_list_head * new,struct osal_list_head * head)71 static inline void osal_list_add(struct osal_list_head *new, struct osal_list_head *head)
72 {
73     osal___list_add(new, head, head->next);
74 }
75 
76 /*
77  * list_add_tail - add a new entry
78  * @new: new entry to be added
79  * @head: list head to add it before
80  *
81  * Insert a new entry before the specified head.
82  * This is useful for implementing queues.
83  */
osal_list_add_tail(struct osal_list_head * new,struct osal_list_head * head)84 static inline void osal_list_add_tail(struct osal_list_head *new, struct osal_list_head *head)
85 {
86     osal___list_add(new, head->prev, head);
87 }
88 
89 /*
90  * Delete a list entry by making the prev/next entries
91  * point to each other.
92  *
93  * This is only for internal list manipulation where we know
94  * the prev/next entries already!
95  */
osal___list_del(struct osal_list_head * prev,struct osal_list_head * next)96 static inline void osal___list_del(struct osal_list_head *prev, struct osal_list_head *next)
97 {
98     next->prev = prev;
99     prev->next = next;
100 }
101 
102 /*
103  * list_del - deletes entry from list.
104  * @entry: the element to delete from the list.
105  * Note: list_empty() on entry does not return true after this, the entry is
106  * in an undefined state.
107  */
osal___list_del_entry(struct osal_list_head * entry)108 static inline void osal___list_del_entry(struct osal_list_head *entry)
109 {
110     osal___list_del(entry->prev, entry->next);
111 }
112 
113 #define OSAL_LIST_POISON1    ((void *)0x00100100)
114 #define OSAL_LIST_POISON2    ((void *)0x00200200)
115 
osal_list_del(struct osal_list_head * entry)116 static inline void osal_list_del(struct osal_list_head *entry)
117 {
118     osal___list_del(entry->prev, entry->next);
119     entry->next = OSAL_LIST_POISON1;
120     entry->prev = OSAL_LIST_POISON2;
121 }
122 
123 /*
124  * list_replace - replace old entry by new one
125  * @old : the element to be replaced
126  * @new : the new element to insert
127  *
128  * If @old was empty, it will be overwritten.
129  */
osal_list_replace(struct osal_list_head * old,struct osal_list_head * new)130 static inline void osal_list_replace(struct osal_list_head *old,
131                                      struct osal_list_head *new)
132 {
133     new->next = old->next;
134     new->next->prev = new;
135     new->prev = old->prev;
136     new->prev->next = new;
137 }
138 
osal_list_replace_init(struct osal_list_head * old,struct osal_list_head * new)139 static inline void osal_list_replace_init(struct osal_list_head *old,
140                                           struct osal_list_head *new)
141 {
142     osal_list_replace(old, new);
143     OSAL_INIT_LIST_HEAD(old);
144 }
145 
146 /*
147  * list_del_init - deletes entry from list and reinitialize it.
148  * @entry: the element to delete from the list.
149  */
osal_list_del_init(struct osal_list_head * entry)150 static inline void osal_list_del_init(struct osal_list_head *entry)
151 {
152     osal___list_del_entry(entry);
153     OSAL_INIT_LIST_HEAD(entry);
154 }
155 
156 /*
157  * list_move - delete from one list and add as another head
158  * @list: the entry to move
159  * @head: the head that will precede our entry
160  */
osal_list_move(struct osal_list_head * list,struct osal_list_head * head)161 static inline void osal_list_move(struct osal_list_head *list, struct osal_list_head *head)
162 {
163     osal___list_del_entry(list);
164     osal_list_add(list, head);
165 }
166 
167 /*
168  * list_move_tail - delete from one list and add as another tail
169  * @list: the entry to move
170  * @head: the head that will follow our entry
171  */
osal_list_move_tail(struct osal_list_head * list,struct osal_list_head * head)172 static inline void osal_list_move_tail(struct osal_list_head *list,
173                                        struct osal_list_head *head)
174 {
175     osal___list_del_entry(list);
176     osal_list_add_tail(list, head);
177 }
178 
179 /*
180  * list_is_last - tests whether @list is the last entry in list @head
181  * @list: the entry to test
182  * @head: the head of the list
183  */
osal_list_is_last(const struct osal_list_head * list,const struct osal_list_head * head)184 static inline int osal_list_is_last(const struct osal_list_head *list,
185                                     const struct osal_list_head *head)
186 {
187     return list->next == head;
188 }
189 
190 /*
191  * list_empty - tests whether a list is empty
192  * @head: the list to test.
193  */
osal_list_empty(const struct osal_list_head * head)194 static inline int osal_list_empty(const struct osal_list_head *head)
195 {
196     return head->next == head;
197 }
198 
199 /*
200  * list_empty_careful - tests whether a list is empty and not being modified
201  * @head: the list to test
202  *
203  * Description:
204  * tests whether a list is empty _and_ checks that no other CPU might be
205  * in the process of modifying either member (next or prev)
206  *
207  * NOTE: using list_empty_careful() without synchronization
208  * can only be safe if the only activity that can happen
209  * to the list entry is list_del_init(). Eg. it cannot be used
210  * if another CPU could re-list_add() it.
211  */
osal_list_empty_careful(const struct osal_list_head * head)212 static inline int osal_list_empty_careful(const struct osal_list_head *head)
213 {
214     struct osal_list_head *next = head->next;
215     return (next == head) && (next == head->prev);
216 }
217 
218 /*
219  * list_rotate_left - rotate the list to the left
220  * @head: the head of the list
221  */
osal_list_rotate_left(struct osal_list_head * head)222 static inline void osal_list_rotate_left(struct osal_list_head *head)
223 {
224     struct osal_list_head *first = OSAL_NULL;
225 
226     if (!osal_list_empty(head)) {
227         first = head->next;
228         osal_list_move_tail(first, head);
229     }
230 }
231 
232 /*
233  * list_is_singular - tests whether a list has just one entry.
234  * @head: the list to test.
235  */
osal_list_is_singular(const struct osal_list_head * head)236 static inline int osal_list_is_singular(const struct osal_list_head *head)
237 {
238     return !osal_list_empty(head) && (head->next == head->prev);
239 }
240 
osal___list_cut_position(struct osal_list_head * list,struct osal_list_head * head,struct osal_list_head * entry)241 static inline void osal___list_cut_position(struct osal_list_head *list,
242                                             struct osal_list_head *head, struct osal_list_head *entry)
243 {
244     struct osal_list_head *new_first = entry->next;
245     list->next = head->next;
246     list->next->prev = list;
247     list->prev = entry;
248     entry->next = list;
249     head->next = new_first;
250     new_first->prev = head;
251 }
252 
253 /*
254  * list_cut_position - cut a list into two
255  * @list: a new list to add all removed entries
256  * @head: a list with entries
257  * @entry: an entry within head, could be the head itself
258  *    and if so we won't cut the list
259  *
260  * This helper moves the initial part of @head, up to and
261  * including @entry, from @head to @list. You should
262  * pass on @entry an element you know is on @head. @list
263  * should be an empty list or a list you do not care about
264  * losing its data.
265  *
266  */
osal_list_cut_position(struct osal_list_head * list,struct osal_list_head * head,struct osal_list_head * entry)267 static inline void osal_list_cut_position(struct osal_list_head *list,
268                                           struct osal_list_head *head, struct osal_list_head *entry)
269 {
270     if (osal_list_empty(head)) {
271         return;
272     }
273     if (osal_list_is_singular(head) &&
274         ((head->next != entry) && (head != entry))) {
275         return;
276     }
277     if (entry == head) {
278         OSAL_INIT_LIST_HEAD(list);
279     } else {
280         osal___list_cut_position(list, head, entry);
281     }
282 }
283 
osal___list_splice(const struct osal_list_head * list,struct osal_list_head * prev,struct osal_list_head * next)284 static inline void osal___list_splice(const struct osal_list_head *list,
285                                       struct osal_list_head *prev,
286                                       struct osal_list_head *next)
287 {
288     struct osal_list_head *first = list->next;
289     struct osal_list_head *last = list->prev;
290 
291     first->prev = prev;
292     prev->next = first;
293 
294     last->next = next;
295     next->prev = last;
296 }
297 
298 /*
299  * list_splice - join two lists, this is designed for stacks
300  * @list: the new list to add.
301  * @head: the place to add it in the first list.
302  */
osal_list_splice(const struct osal_list_head * list,struct osal_list_head * head)303 static inline void osal_list_splice(const struct osal_list_head *list,
304                                     struct osal_list_head *head)
305 {
306     if (!osal_list_empty(list)) {
307         osal___list_splice(list, head, head->next);
308     }
309 }
310 
311 /*
312  * list_splice_tail - join two lists, each list being a queue
313  * @list: the new list to add.
314  * @head: the place to add it in the first list.
315  */
osal_list_splice_tail(struct osal_list_head * list,struct osal_list_head * head)316 static inline void osal_list_splice_tail(struct osal_list_head *list,
317                                          struct osal_list_head *head)
318 {
319     if (!osal_list_empty(list)) {
320         osal___list_splice(list, head->prev, head);
321     }
322 }
323 
324 /*
325  * list_splice_init - join two lists and reinitialise the emptied list.
326  * @list: the new list to add.
327  * @head: the place to add it in the first list.
328  *
329  * The list at @list is reinitialised
330  */
osal_list_splice_init(struct osal_list_head * list,struct osal_list_head * head)331 static inline void osal_list_splice_init(struct osal_list_head *list,
332                                          struct osal_list_head *head)
333 {
334     if (!osal_list_empty(list)) {
335         osal___list_splice(list, head, head->next);
336         OSAL_INIT_LIST_HEAD(list);
337     }
338 }
339 
340 /*
341  * list_splice_tail_init - join two lists and reinitialise the emptied list
342  * @list: the new list to add.
343  * @head: the place to add it in the first list.
344  *
345  * Each of the lists is a queue.
346  * The list at @list is reinitialised
347  */
osal_list_splice_tail_init(struct osal_list_head * list,struct osal_list_head * head)348 static inline void osal_list_splice_tail_init(struct osal_list_head *list,
349                                               struct osal_list_head *head)
350 {
351     if (!osal_list_empty(list)) {
352         osal___list_splice(list, head->prev, head);
353         OSAL_INIT_LIST_HEAD(list);
354     }
355 }
356 
357 #undef osal_offsetof
358 #ifdef __compiler_offsetof
359 #define osal_offsetof(TYPE, MEMBER)  (__compiler_offsetof(TYPE, MEMBER))
360 #else
361 #define osal_offsetof(TYPE, MEMBER) ((int)(unsigned long)&((TYPE *)0)->MEMBER)
362 #endif
363 
364 #define osal_container_of(ptr, type, member) ({                \
365     __typeof__(((type *)0)->member) *__mptr = (ptr);    \
366     (type *)((char *)__mptr - (osal_offsetof(type, member))); })
367 
368 /*
369  * list_entry - get the struct for this entry
370  * @ptr:    the &struct list_head pointer.
371  * @type:    the type of the struct this is embedded in.
372  * @member:    the name of the list_struct within the struct.
373  */
374 #define osal_list_entry(ptr, type, member) \
375     osal_container_of(ptr, type, member)
376 
377 /*
378  * list_first_entry - get the first element from a list
379  * @ptr:    the list head to take the element from.
380  * @type:    the type of the struct this is embedded in.
381  * @member:    the name of the list_struct within the struct.
382  *
383  * Note, that list is expected to be not empty.
384  */
385 #define osal_list_first_entry(ptr, type, member) \
386     osal_list_entry((ptr)->next, type, member)
387 
388 /**
389  * list_for_each    -    iterate over a list
390  * @pos:    the &struct list_head to use as a loop cursor.
391  * @head:    the head for your list.
392  */
393 #define osal_list_for_each(pos, head) \
394     for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)
395 
396 /*
397  * __list_for_each    -    iterate over a list
398  * @pos:    the &struct list_head to use as a loop cursor.
399  * @head:    the head for your list.
400  *
401  * This variant doesn't differ from list_for_each() any more.
402  * We don't do prefetching in either case.
403  */
404 #define osal___list_for_each(pos, head) \
405     for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)
406 
407 /*
408  * list_for_each_prev    -    iterate over a list backwards
409  * @pos:    the &struct list_head to use as a loop cursor.
410  * @head:    the head for your list.
411  */
412 #define osal_list_for_each_prev(pos, head) \
413     for ((pos) = (head)->prev; (pos) != (head); (pos) = (pos)->prev)
414 
415 /*
416  * list_for_each_safe - iterate over a list safe against removal of list entry
417  * @pos:    the &struct list_head to use as a loop cursor.
418  * @n:        another &struct list_head to use as temporary storage
419  * @head:    the head for your list.
420  */
421 #define osal_list_for_each_safe(pos, n, head)              \
422     for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); \
423          (pos) = (n), (n) = (pos)->next)
424 
425 /*
426  * list_for_each_prev_safe - iterate over a list backwards safe against removal of list entry
427  * @pos:    the &struct list_head to use as a loop cursor.
428  * @n:        another &struct list_head to use as temporary storage
429  * @head:    the head for your list.
430  */
431 #define osal_list_for_each_prev_safe(pos, n, head) \
432     for ((pos) = (head)->prev, (n) = (pos)->prev;        \
433          (pos) != (head);                            \
434          (pos) = (n), (n) = (pos)->prev)
435 
436 /*
437  * list_for_each_entry    -    iterate 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 osal_list_for_each_entry(pos, head, member)                     \
443     for ((pos) = osal_list_entry((head)->next, __typeof__(*(pos)), member); \
444          &(pos)->member != (head);                                        \
445          (pos) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member))
446 
447 /*
448  * list_for_each_entry_reverse - iterate backwards over list of given type.
449  * @pos:    the type * to use as a loop cursor.
450  * @head:    the head for your list.
451  * @member:    the name of the list_struct within the struct.
452  */
453 #define osal_list_for_each_entry_reverse(pos, head, member)             \
454     for ((pos) = osal_list_entry((head)->prev, __typeof__(*(pos)), member); \
455          &(pos)->member != (head);                                        \
456          (pos) = osal_list_entry((pos)->member.prev, __typeof__(*(pos)), member))
457 
458 /*
459  * list_prepare_entry - prepare a pos entry for use in list_for_each_entry_continue()
460  * @pos:    the type * to use as a start point
461  * @head:    the head of the list
462  * @member:    the name of the list_struct within the struct.
463  *
464  * Prepares a pos entry for use as a start point in list_for_each_entry_continue().
465  */
466 #define osal_list_prepare_entry(pos, head, member) \
467     ((pos) ? : osal_list_entry(head, __typeof__(*(pos)), member))
468 
469 /**
470  * list_for_each_entry_continue - continue iteration over list of given type
471  * @pos:    the type * to use as a loop cursor.
472  * @head:    the head for your list.
473  * @member:    the name of the list_struct within the struct.
474  *
475  * Continue to iterate over list of given type, continuing after
476  * the current position.
477  */
478 #define osal_list_for_each_entry_continue(pos, head, member)                \
479     for ((pos) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member); \
480          &(pos)->member != (head);                                            \
481          (pos) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member))
482 
483 /*
484  * list_for_each_entry_continue_reverse - iterate backwards from the given point
485  * @pos:    the type * to use as a loop cursor.
486  * @head:    the head for your list.
487  * @member:    the name of the list_struct within the struct.
488  *
489  * Start to iterate over list of given type backwards, continuing after
490  * the current position.
491  */
492 #define osal_list_for_each_entry_continue_reverse(pos, head, member)        \
493     for ((pos) = osal_list_entry((pos)->member.prev, __typeof__(*(pos)), member); \
494          &(pos)->member != (head);                                            \
495          (pos) = osal_list_entry((pos)->member.prev, __typeof__(*(pos)), member))
496 
497 /*
498  * list_for_each_entry_from - iterate over list of given type from the current point
499  * @pos:    the type * to use as a loop cursor.
500  * @head:    the head for your list.
501  * @member:    the name of the list_struct within the struct.
502  *
503  * Iterate over list of given type, continuing from current position.
504  */
505 #define osal_list_for_each_entry_from(pos, head, member) \
506     for (; &(pos)->member != (head);                       \
507          (pos) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member))
508 
509 /*
510  * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
511  * @pos:    the type * to use as a loop cursor.
512  * @n:        another type * to use as temporary storage
513  * @head:    the head for your list.
514  * @member:    the name of the list_struct within the struct.
515  */
516 #define osal_list_for_each_entry_safe(pos, n, head, member)              \
517     for ((pos) = osal_list_entry((head)->next, __typeof__(*(pos)), member),  \
518         (n) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member); \
519          &(pos)->member != (head);                                         \
520          (pos) = (n), (n) = osal_list_entry((n)->member.next, __typeof__(*(n)), member))
521 
522 /*
523  * list_for_each_entry_safe_continue - continue list iteration safe against removal
524  * @pos:    the type * to use as a loop cursor.
525  * @n:        another type * to use as temporary storage
526  * @head:    the head for your list.
527  * @member:    the name of the list_struct within the struct.
528  *
529  * Iterate over list of given type, continuing after current point,
530  * safe against removal of list entry.
531  */
532 #define osal_list_for_each_entry_safe_continue(pos, n, head, member)        \
533     for ((pos) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member), \
534         (n) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member);    \
535          &(pos)->member != (head);                                            \
536          (pos) = (n), (n) = osal_list_entry((n)->member.next, __typeof__(*(n)), member))
537 
538 /*
539  * list_for_each_entry_safe_from - iterate over list from current point safe against removal
540  * @pos:    the type * to use as a loop cursor.
541  * @n:        another type * to use as temporary storage
542  * @head:    the head for your list.
543  * @member:    the name of the list_struct within the struct.
544  *
545  * Iterate over list of given type from current point, safe against
546  * removal of list entry.
547  */
548 #define osal_list_for_each_entry_safe_from(pos, n, head, member)          \
549     for ((n) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member); \
550          &(pos)->member != (head);                                          \
551          (pos) = (n), (n) = osal_list_entry((n)->member.next, __typeof__(*(n)), member))
552 
553 /*
554  * list_for_each_entry_safe_reverse - iterate backwards over list safe against removal
555  * @pos:    the type * to use as a loop cursor.
556  * @n:        another type * to use as temporary storage
557  * @head:    the head for your list.
558  * @member:    the name of the list_struct within the struct.
559  *
560  * Iterate backwards over list of given type, safe against removal
561  * of list entry.
562  */
563 #define osal_list_for_each_entry_safe_reverse(pos, n, head, member)      \
564     for ((pos) = osal_list_entry((head)->prev, __typeof__(*(pos)), member),  \
565         (n) = osal_list_entry((pos)->member.prev, __typeof__(*(pos)), member); \
566          &(pos)->member != (head);                                         \
567          (pos) = (n), (n) = osal_list_entry((n)->member.prev, __typeof__(*(n)), member))
568 
569 /*
570  * list_safe_reset_next - reset a stale list_for_each_entry_safe loop
571  * @pos:    the loop cursor used in the list_for_each_entry_safe loop
572  * @n:        temporary storage used in list_for_each_entry_safe
573  * @member:    the name of the list_struct within the struct.
574  *
575  * list_safe_reset_next is not safe to use in general if the list may be
576  * modified concurrently (eg. the lock is dropped in the loop body). An
577  * exception to this is if the cursor element (pos) is pinned in the list,
578  * and list_safe_reset_next is called after re-taking the lock and before
579  * completing the current iteration of the loop body.
580  */
581 #define osal_list_safe_reset_next(pos, n, member) \
582     (n) = osal_list_entry((pos)->member.next, __typeof__(*(pos)), member)
583 
584 /*
585  * Double linked lists with a single pointer list head.
586  * Mostly useful for hash tables where the two pointer list head is
587  * too wasteful.
588  * You lose the ability to access the tail in O(1).
589  */
590 struct osal_hlist_node {
591     struct osal_hlist_node *next, **pprev;
592 };
593 struct osal_hlist_head {
594     struct osal_hlist_node *first;
595 };
596 
597 #define OSAL_HLIST_HEAD_INIT \
598     {                        \
599         .first = OSAL_NULL   \
600     }
601 #define OSAL_HLIST_HEAD(name) struct osal_hlist_head name = { .first = OSAL_NULL }
602 #define INIT_OSAL_HLIST_HEAD(ptr) ((ptr)->first = OSAL_NULL)
INIT_OSAL_HLIST_NODE(struct osal_hlist_node * h)603 static inline void INIT_OSAL_HLIST_NODE(struct osal_hlist_node *h)
604 {
605     h->next = OSAL_NULL;
606     h->pprev = OSAL_NULL;
607 }
608 
osal_hlist_unhashed(const struct osal_hlist_node * h)609 static inline int osal_hlist_unhashed(const struct osal_hlist_node *h)
610 {
611     return !h->pprev;
612 }
613 
osal_hlist_empty(const struct osal_hlist_head * h)614 static inline int osal_hlist_empty(const struct osal_hlist_head *h)
615 {
616     return !h->first;
617 }
618 
osal___hlist_del(struct osal_hlist_node * n)619 static inline void osal___hlist_del(struct osal_hlist_node *n)
620 {
621     struct osal_hlist_node *next = n->next;
622     struct osal_hlist_node **pprev = n->pprev;
623     *pprev = next;
624     if (next != OSAL_NULL) {
625         next->pprev = pprev;
626     }
627 }
628 
osal_hlist_del(struct osal_hlist_node * n)629 static inline void osal_hlist_del(struct osal_hlist_node *n)
630 {
631     osal___hlist_del(n);
632     n->next = OSAL_LIST_POISON1;
633     n->pprev = OSAL_LIST_POISON2;
634 }
635 
osal_hlist_del_init(struct osal_hlist_node * n)636 static inline void osal_hlist_del_init(struct osal_hlist_node *n)
637 {
638     if (!osal_hlist_unhashed(n)) {
639         osal___hlist_del(n);
640         INIT_OSAL_HLIST_NODE(n);
641     }
642 }
643 
osal_hlist_add_head(struct osal_hlist_node * n,struct osal_hlist_head * h)644 static inline void osal_hlist_add_head(struct osal_hlist_node *n, struct osal_hlist_head *h)
645 {
646     struct osal_hlist_node *first = h->first;
647     n->next = first;
648     if (first != OSAL_NULL) {
649         first->pprev = &n->next;
650     }
651     h->first = n;
652     n->pprev = &h->first;
653 }
654 
655 /* next must be != NULL */
osal_hlist_add_before(struct osal_hlist_node * n,struct osal_hlist_node * next)656 static inline void osal_hlist_add_before(struct osal_hlist_node *n,
657                                          struct osal_hlist_node *next)
658 {
659     n->pprev = next->pprev;
660     n->next = next;
661     next->pprev = &n->next;
662     *(n->pprev) = n;
663 }
664 
osal_hlist_add_after(struct osal_hlist_node * n,struct osal_hlist_node * next)665 static inline void osal_hlist_add_after(struct osal_hlist_node *n,
666                                         struct osal_hlist_node *next)
667 {
668     next->next = n->next;
669     n->next = next;
670     next->pprev = &n->next;
671 
672     if (next->next != OSAL_NULL) {
673         next->next->pprev = &next->next;
674     }
675 }
676 
677 /* after that we'll appear to be on some hlist and hlist_del will work */
osal_hlist_add_fake(struct osal_hlist_node * n)678 static inline void osal_hlist_add_fake(struct osal_hlist_node *n)
679 {
680     n->pprev = &n->next;
681 }
682 
683 /*
684  * Move a list from one list head to another. Fixup the pprev
685  * reference of the first entry if it exists.
686  */
osal_hlist_move_list(struct osal_hlist_head * old,struct osal_hlist_head * new)687 static inline void osal_hlist_move_list(struct osal_hlist_head *old,
688                                         struct osal_hlist_head *new)
689 {
690     new->first = old->first;
691     if (new->first != OSAL_NULL) {
692         new->first->pprev = &new->first;
693     }
694     old->first = OSAL_NULL;
695 }
696 
697 #define osal_hlist_entry(ptr, type, member) osal_container_of(ptr, type, member)
698 
699 #define osal_hlist_for_each(pos, head) \
700     for ((pos) = (head)->first; (pos); (pos) = (pos)->next)
701 
702 #define osal_hlist_for_each_safe(pos, n, head) \
703     for ((pos) = (head)->first; (pos) && ({ n = (pos)->next; 1; });    \
704          (pos) = (n))
705 
706 /*
707  * hlist_for_each_entry    - iterate over list of given type
708  * @tpos:    the type * to use as a loop cursor.
709  * @pos:    the &struct hlist_node to use as a loop cursor.
710  * @head:    the head for your list.
711  * @member:    the name of the hlist_node within the struct.
712  */
713 #define osal_hlist_for_each_entry(tpos, pos, head, member) \
714     for ((pos) = (head)->first;                              \
715          (pos) &&                                            \
716          ({ (tpos) = osal_hlist_entry((pos), __typeof__(*(tpos)), member); 1; });  \
717          (pos) = (pos)->next)
718 
719 /*
720  * hlist_for_each_entry_continue - iterate over a hlist continuing after current point
721  * @tpos:    the type * to use as a loop cursor.
722  * @pos:    the &struct hlist_node to use as a loop cursor.
723  * @member:    the name of the hlist_node within the struct.
724  */
725 #define osal_hlist_for_each_entry_continue(tpos, pos, member) \
726     for ((pos) = (pos)->next;                                   \
727          (pos) &&                                               \
728          ({ (tpos) = osal_hlist_entry((pos), __typeof__(*(tpos)), member); 1; });  \
729          (pos) = (pos)->next)
730 
731 /*
732  * hlist_for_each_entry_from - iterate over a hlist continuing from current point
733  * @tpos:    the type * to use as a loop cursor.
734  * @pos:    the &struct hlist_node to use as a loop cursor.
735  * @member:    the name of the hlist_node within the struct.
736  */
737 #define osal_hlist_for_each_entry_from(tpos, pos, member) \
738     for (; (pos) &&                                         \
739            ({ (tpos) = osal_hlist_entry((pos), __typeof__(*(tpos)), member); 1; });  \
740          (pos) = (pos)->next)
741 
742 /*
743  * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
744  * @tpos:    the type * to use as a loop cursor.
745  * @pos:    the &struct hlist_node to use as a loop cursor.
746  * @n:        another &struct hlist_node to use as temporary storage
747  * @head:    the head for your list.
748  * @member:    the name of the hlist_node within the struct.
749  */
750 #define osal_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
751     for ((pos) = (head)->first;                                      \
752          (pos) && ({ n = (pos)->next; 1; }) &&                                           \
753          ({ (tpos) = osal_hlist_entry((pos), __typeof__(*(tpos)), member); 1; });          \
754          (pos) = (n))
755 
756 #endif
757