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