WS63 SDK 文档 7021f4f@fbb_ws63
ws63 和 ws63e 解决方案的 SDK 文档
载入中...
搜索中...
未找到
osal_list.h
浏览该文件的文档.
1/*
2 * Copyright (c) HiSilicon (Shanghai) Technologies Co., Ltd. 2021-2022. All rights reserved.
3 * Description: osal list
4 *
5 * Create: 2021-12-16
6 */
7
8#ifndef _OSAL_LIST_H
9#define _OSAL_LIST_H
10
11#ifdef __cplusplus
12#if __cplusplus
13extern "C" {
14#endif
15#endif
16
17#ifndef NULL
18#define NULL ((void*)0)
19#endif
20
21#ifndef INLINE__
22#ifdef INLINE_TO_FORCEINLINE
23#define INLINE__ __inline__ __attribute__((always_inline))
24#else
25#define INLINE__ __inline
26#endif
27#endif
28
29/*
30 * Simple doubly linked list implementation.
31 *
32 * Some of the internal functions ("__xxx") are useful when
33 * manipulating whole lists rather than single entries, as
34 * sometimes we already know the next/prev entries and we can
35 * generate better code by using them directly rather than
36 * using the generic single-entry routines.
37 */
38
41};
42#define OSAL_LIST_HEAD_INIT(name) \
43 { \
44 &(name), &(name) \
45 }
46
47#define OSAL_LIST_HEAD(name) \
48 struct osal_list_head name = OSAL_LIST_HEAD_INIT(name)
49
50static INLINE__ void OSAL_INIT_LIST_HEAD(struct osal_list_head *list)
51{
52 if (list == NULL) {
53 return;
54 }
55 list->next = list;
56 list->prev = list;
57}
58
59/*
60 * Insert a new entry between two known consecutive entries.
61 *
62 * This is only for internal list manipulation where we know
63 * the prev/next entries already!
64 */
65static INLINE__ void osal___list_add(struct osal_list_head *_new,
66 struct osal_list_head *prev,
67 struct osal_list_head *next)
68{
69 if (_new == NULL || prev == NULL || next == NULL) {
70 return;
71 }
72 next->prev = _new;
73 _new->next = next;
74 _new->prev = prev;
75 prev->next = _new;
76}
77
86static INLINE__ void osal_list_add(struct osal_list_head *cur, struct osal_list_head *head)
87{
88 osal___list_add(cur, head, head->next);
89}
90
99static INLINE__ void osal_list_add_tail(struct osal_list_head *cur, struct osal_list_head *head)
100{
101 osal___list_add(cur, head->prev, head);
102}
103
104/*
105 * Delete a list entry by making the prev/next entries
106 * point to each other.
107 *
108 * This is only for internal list manipulation where we know
109 * the prev/next entries already!
110 */
111static INLINE__ void osal___list_del(struct osal_list_head *prev, struct osal_list_head *next)
112{
113 if (prev == NULL || next == NULL) {
114 return;
115 }
116
117 next->prev = prev;
118 prev->next = next;
119}
120
127static INLINE__ void osal___list_del_entry(struct osal_list_head *entry)
128{
129 if (entry == NULL) {
130 return;
131 }
132 osal___list_del(entry->prev, entry->next);
133}
134
135#define OSAL_LIST_POISON1 0x00100100
136#define OSAL_LIST_POISON2 0x00200200
137
138static INLINE__ void osal_list_del(struct osal_list_head *entry)
139{
140 if (entry == NULL) {
141 return;
142 }
143 osal___list_del(entry->prev, entry->next);
144 entry->next = (struct osal_list_head *) OSAL_LIST_POISON1;
145 entry->prev = (struct osal_list_head *) OSAL_LIST_POISON2;
146}
147
155static INLINE__ void osal_list_replace(struct osal_list_head *old,
156 struct osal_list_head *_new)
157{
158 _new->next = old->next;
159 _new->next->prev = _new;
160 _new->prev = old->prev;
161 _new->prev->next = _new;
162}
163
164static INLINE__ void osal_list_replace_init(struct osal_list_head *old,
165 struct osal_list_head *_new)
166{
167 osal_list_replace(old, _new);
168 OSAL_INIT_LIST_HEAD(old);
169}
170
175static INLINE__ void osal_list_del_init(struct osal_list_head *entry)
176{
177 osal___list_del_entry(entry);
178 OSAL_INIT_LIST_HEAD(entry);
179}
180
186static INLINE__ void osal_list_move(struct osal_list_head *list, struct osal_list_head *head)
187{
188 osal___list_del_entry(list);
189 osal_list_add(list, head);
190}
191
197static INLINE__ void osal_list_move_tail(struct osal_list_head *list,
198 struct osal_list_head *head)
199{
200 osal___list_del_entry(list);
201 osal_list_add_tail(list, head);
202}
203
209static INLINE__ int osal_list_is_last(const struct osal_list_head *list,
210 const struct osal_list_head *head)
211{
212 if (list == NULL || head == NULL) {
213 return -1;
214 }
215
216 return list->next == head;
217}
218
223static INLINE__ int osal_list_empty(const struct osal_list_head *head)
224{
225 if (head == NULL) {
226 return -1;
227 }
228
229 return head->next == head;
230}
231
244static INLINE__ int osal_list_empty_careful(const struct osal_list_head *head)
245{
246 struct osal_list_head *next = NULL;
247
248 if (head == NULL) {
249 return -1;
250 }
251
252 next = head->next;
253
254 return (next == head) && (next == head->prev);
255}
256
261static INLINE__ void osal_list_rotate_left(struct osal_list_head *head)
262{
263 struct osal_list_head *first = NULL;
264
265 if (osal_list_empty(head) == 0) {
266 first = head->next;
267 osal_list_move_tail(first, head);
268 }
269}
270
275static INLINE__ int osal_list_is_singular(const struct osal_list_head *head)
276{
277 return (osal_list_empty(head) == 0) && (head->next == head->prev);
278}
279
280static INLINE__ void osal___list_cut_position(struct osal_list_head *list,
281 struct osal_list_head *head, struct osal_list_head *entry)
282{
283 struct osal_list_head *new_first = entry->next;
284 list->next = head->next;
285 list->next->prev = list;
286 list->prev = entry;
287 entry->next = list;
288 head->next = new_first;
289 new_first->prev = head;
290}
291
306static INLINE__ void osal_list_cut_position(struct osal_list_head *list,
307 struct osal_list_head *head, struct osal_list_head *entry)
308{
309 if (osal_list_empty(head) != 0) {
310 return;
311 }
312 if (osal_list_is_singular(head) != 0 &&
313 ((head->next != entry) && (head != entry))) {
314 return;
315 }
316 (entry == head) ? OSAL_INIT_LIST_HEAD(list) : osal___list_cut_position(list, head, entry);
317}
318
319static INLINE__ void osal___list_splice(const struct osal_list_head *list,
320 struct osal_list_head *prev,
321 struct osal_list_head *next)
322{
323 struct osal_list_head *first = list->next;
324 struct osal_list_head *last = list->prev;
325
326 first->prev = prev;
327 prev->next = first;
328
329 last->next = next;
330 next->prev = last;
331}
332
338static INLINE__ void osal_list_splice(const struct osal_list_head *list,
339 struct osal_list_head *head)
340{
341 if (osal_list_empty(list) == 0) {
342 osal___list_splice(list, head, head->next);
343 }
344}
345
351static INLINE__ void osal_list_splice_tail(struct osal_list_head *list,
352 struct osal_list_head *head)
353{
354 if (osal_list_empty(list) == 0) {
355 osal___list_splice(list, head->prev, head);
356 }
357}
358
366static INLINE__ void osal_list_splice_init(struct osal_list_head *list,
367 struct osal_list_head *head)
368{
369 if (osal_list_empty(list) == 0) {
370 osal___list_splice(list, head, head->next);
371 OSAL_INIT_LIST_HEAD(list);
372 }
373}
374
383static INLINE__ void osal_list_splice_tail_init(struct osal_list_head *list,
384 struct osal_list_head *head)
385{
386 if (osal_list_empty(list) == 0) {
387 osal___list_splice(list, head->prev, head);
388 OSAL_INIT_LIST_HEAD(list);
389 }
390}
391
392#undef osal_offsetof
393#ifdef __compiler_offsetof
394#define osal_offsetof(TYPE, MEMBER) __compiler_offsetof(TYPE, MEMBER)
395#else
396#define osal_offsetof(TYPE, MEMBER) ((int)(unsigned long)&((TYPE *)0)->MEMBER)
397#endif
398
399#define osal_container_of(ptr, type, member) ({ \
400 const __typeof__( ((type *)0)->member ) *__mptr = (ptr); \
401 (type *)((char *)__mptr - osal_offsetof(type, member)); })
402
409#define osal_list_entry(ptr, type, member) \
410 osal_container_of(ptr, type, member)
411
420#define osal_list_first_entry(ptr, type, member) \
421 osal_list_entry((ptr)->next, type, member)
422
428#define osal_list_for_each(pos, head) \
429 for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)
430
439#define osal___list_for_each(pos, head) \
440 for ((pos) = (head)->next; (pos) != (head); (pos) = (pos)->next)
441
447#define osal_list_for_each_prev(pos, head) \
448 for ((pos) = (head)->prev; (pos) != (head); (pos) = (pos)->prev)
449
456#define osal_list_for_each_safe(pos, n, head) \
457 for ((pos) = (head)->next, (n) = (pos)->next; (pos) != (head); \
458 (pos) = (n), (n) = (pos)->next)
459
466#define osal_list_for_each_prev_safe(pos, n, head) \
467 for ((pos) = (head)->prev, (n) = (pos)->prev; \
468 (pos) != (head); \
469 (pos) = (n), (n) = (pos)->prev)
470
477#define osal_list_for_each_entry(pos, head, member) \
478 for ((pos) = osal_list_entry((head)->next, __typeof__(*pos), member); \
479 &pos->member != (head); \
480 (pos) = osal_list_entry((pos)->member.next, __typeof__(*pos), member))
481
488#define osal_list_for_each_entry_reverse(pos, head, member) \
489 for ((pos) = osal_list_entry((head)->prev, __typeof__(*pos), member); \
490 &pos->member != (head); \
491 (pos) = osal_list_entry((pos)->member.prev, __typeof__(*pos), member))
492
501#define osal_list_prepare_entry(pos, head, member) \
502 ((pos) ? : osal_list_entry(head, __typeof__(*pos), member))
503
513#define osal_list_for_each_entry_continue(pos, head, member) \
514 for ((pos) = osal_list_entry((pos)->member.next, __typeof__(*pos), member); \
515 &pos->member != (head); \
516 (pos) = osal_list_entry((pos)->member.next, __typeof__(*pos), member))
517
527#define osal_list_for_each_entry_continue_reverse(pos, head, member) \
528 for ((pos) = osal_list_entry((pos)->member.prev, __typeof__(*pos), member); \
529 &pos->member != (head); \
530 (pos) = osal_list_entry((pos)->member.prev, __typeof__(*pos), member))
531
540#define osal_list_for_each_entry_from(pos, head, member) \
541 for (; &pos->member != (head); \
542 (pos) = osal_list_entry((pos)->member.next, __typeof__(*pos), member))
543
551#define osal_list_for_each_entry_safe(pos, n, head, member) \
552 for ((pos) = osal_list_entry((head)->next, __typeof__(*pos), member), \
553 (n) = osal_list_entry((pos)->member.next, __typeof__(*pos), member); \
554 &pos->member != (head); \
555 (pos) = (n), (n) = osal_list_entry((n)->member.next, __typeof__(*n), member))
556
567#define osal_list_for_each_entry_safe_continue(pos, n, head, member) \
568 for ((pos) = osal_list_entry((pos)->member.next, __typeof__(*pos), member), \
569 (n) = osal_list_entry((pos)->member.next, __typeof__(*pos), member); \
570 &pos->member != (head); \
571 (pos) = (n), (n) = osal_list_entry((n)->member.next, __typeof__(*n), member))
572
583#define osal_list_for_each_entry_safe_from(pos, n, head, member) \
584 for ((n) = osal_list_entry((pos)->member.next, __typeof__(*pos), member); \
585 &pos->member != (head); \
586 (pos) = (n), (n) = osal_list_entry((n)->member.next, __typeof__(*n), member))
587
588
599#define osal_list_for_each_entry_safe_reverse(pos, n, head, member) \
600 for ((pos) = osal_list_entry((head)->prev, __typeof__(*pos), member), \
601 (n) = osal_list_entry((pos)->member.prev, __typeof__(*pos), member); \
602 &pos->member != (head); \
603 (pos) = (n), (n) = osal_list_entry((n)->member.prev, __typeof__(*n), member))
604
617#define osal_list_safe_reset_next(pos, n, member) \
618 (n) = osal_list_entry((pos)->member.next, __typeof__(*pos), member)
619
620/*
621 * Double linked lists with a single pointer list head.
622 * Mostly useful for hash tables where the two pointer list head is
623 * too wasteful.
624 * You lose the ability to access the tail in O(1).
625 */
632
633#define OSAL_HLIST_HEAD_INIT \
634 { \
635 .first = NULL \
636 }
637#define OSAL_HLIST_HEAD(name) struct osal_hlist_head name = { .first = NULL }
638#define INIT_OSAL_HLIST_HEAD(ptr) ((ptr)->first = NULL)
639static INLINE__ void INIT_OSAL_HLIST_NODE(struct osal_hlist_node *h)
640{
641 h->next = NULL;
642 h->pprev = NULL;
643}
644
645static INLINE__ int osal_hlist_unhashed(const struct osal_hlist_node *h)
646{
647 return !h->pprev;
648}
649
650static INLINE__ int osal_hlist_empty(const struct osal_hlist_head *h)
651{
652 return !h->first;
653}
654
655static INLINE__ void osal___hlist_del(struct osal_hlist_node *n)
656{
657 struct osal_hlist_node *next = n->next;
658 struct osal_hlist_node **pprev = n->pprev;
659 *pprev = next;
660 if (next != NULL) {
661 next->pprev = pprev;
662 }
663}
664
665static INLINE__ void osal_hlist_del(struct osal_hlist_node *n)
666{
667 osal___hlist_del(n);
668 n->next = (struct osal_hlist_node *) OSAL_LIST_POISON1;
669 n->pprev = (struct osal_hlist_node **) OSAL_LIST_POISON2;
670}
671
672static INLINE__ void osal_hlist_del_init(struct osal_hlist_node *n)
673{
674 if (osal_hlist_unhashed(n) == 0) {
675 osal___hlist_del(n);
676 INIT_OSAL_HLIST_NODE(n);
677 }
678}
679
680static INLINE__ void osal_hlist_add_head(struct osal_hlist_node *n, struct osal_hlist_head *h)
681{
682 struct osal_hlist_node *first = h->first;
683 n->next = first;
684 if (first != NULL) {
685 first->pprev = &n->next;
686 }
687 h->first = n;
688 n->pprev = &h->first;
689}
690
691/* next must be != NULL */
692static INLINE__ void osal_hlist_add_before(struct osal_hlist_node *n,
693 struct osal_hlist_node *next)
694{
695 n->pprev = next->pprev;
696 n->next = next;
697 next->pprev = &n->next;
698 *(n->pprev) = n;
699}
700
701static INLINE__ void osal_hlist_add_after(struct osal_hlist_node *n,
702 struct osal_hlist_node *next)
703{
704 next->next = n->next;
705 n->next = next;
706 next->pprev = &n->next;
707
708 if (next->next) {
709 next->next->pprev = &next->next;
710 }
711}
712
713/* after that we'll appear to be on some hlist and hlist_del will work */
714static INLINE__ void osal_hlist_add_fake(struct osal_hlist_node *n)
715{
716 n->pprev = &n->next;
717}
718
719/*
720 * Move a list from one list head to another. Fixup the pprev
721 * reference of the first entry if it exists.
722 */
723static INLINE__ void osal_hlist_move_list(struct osal_hlist_head *old,
724 struct osal_hlist_head *cur)
725{
726 cur->first = old->first;
727 if (cur->first) {
728 cur->first->pprev = &cur->first;
729 }
730 old->first = NULL;
731}
732
733#define osal_hlist_entry(ptr, type, member) osal_container_of(ptr, type, member)
734
735#define osal_hlist_for_each(pos, head) \
736 for ((pos) = (head)->first; (pos); (pos) = (pos)->next)
737
738#define osal_hlist_for_each_safe(pos, n, head) \
739 for ((pos) = (head)->first; (pos) && ({ (n) = (pos)->next; 1; }); \
740 (pos) = (n))
741
749#define osal_hlist_for_each_entry(tpos, pos, head, member) \
750 for ((pos) = (head)->first; \
751 (pos) && \
752 ({ (tpos) = osal_hlist_entry((pos), __typeof__(*tpos), member); 1; }); \
753 (pos) = (pos)->next)
754
761#define osal_hlist_for_each_entry_continue(tpos, pos, member) \
762 for ((pos) = (pos)->next; \
763 (pos) && \
764 ({ (tpos) = osal_hlist_entry((pos), __typeof__(*tpos), member); 1; }); \
765 (pos) = (pos)->next)
766
773#define osal_hlist_for_each_entry_from(tpos, pos, member) \
774 for (; (pos) && \
775 ({ (tpos) = osal_hlist_entry((pos), __typeof__(*tpos), member); 1; }); \
776 (pos) = (pos)->next)
777
786#define osal_hlist_for_each_entry_safe(tpos, pos, n, head, member) \
787 for ((pos) = (head)->first; \
788 (pos) && ({ (n) = (pos)->next; 1; }) && \
789 ({ (tpos) = osal_hlist_entry((pos), __typeof__(*tpos), member); 1; }); \
790 (pos) = (n))
791
792#define get_first_item(attached, type, member) \
793 ((type *)((char *)((attached)->next) - (unsigned long)(&((type *)0)->member)))
794
795#ifdef __cplusplus
796#if __cplusplus
797}
798#endif // __cplusplus end
799#endif // __cplusplus end
800
801#endif // _OSAL_LIST_H end
802
#define NULL
Definition osal_list.h:18
#define OSAL_LIST_POISON1
Definition osal_list.h:135
#define INLINE__
Definition osal_list.h:25
#define OSAL_LIST_POISON2
Definition osal_list.h:136
Definition osal_list.h:629
struct osal_hlist_node * first
Definition osal_list.h:630
Definition osal_list.h:626
struct osal_hlist_node * next
Definition osal_list.h:627
struct osal_hlist_node ** pprev
Definition osal_list.h:627
Definition osal_list.h:39
struct osal_list_head * next
Definition osal_list.h:40
struct osal_list_head * prev
Definition osal_list.h:40