Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef TRACE_LIST_DEF_H
12 : #define TRACE_LIST_DEF_H
13 :
14 : #include <stddef.h>
15 : #include <stdbool.h>
16 :
17 : #ifdef __cplusplus
18 : extern "C" {
19 : #endif
20 :
21 : struct ListHead {
22 : struct ListHead* next;
23 : struct ListHead* prev;
24 : };
25 :
26 : #define INIT_LIST_HEAD(ptr) \
27 : do { \
28 : (ptr)->next = (ptr); \
29 : (ptr)->prev = (ptr); \
30 : } while (0)
31 :
32 : /**
33 : * @brief Insert a new entry between two known consecutive entries.
34 : * @param [in] item: new entry to be added
35 : * @param [in] prev: previous entry to add it after
36 : * @param [in] next: next entry to add it before
37 : * @return NA
38 : */
39 4749 : static inline void ListAdd(struct ListHead* item, struct ListHead* prev, struct ListHead* next)
40 : {
41 4749 : next->prev = item;
42 4749 : item->next = next;
43 4749 : item->prev = prev;
44 4749 : prev->next = item;
45 4749 : }
46 :
47 : /**
48 : * @brief Insert a new entry after the specified head.
49 : * @param [in] item: new entry to be added
50 : * @param [in] head: list head to add it after
51 : * @return NA
52 : */
53 : static inline void ListAddAfterEntry(struct ListHead* item, struct ListHead* head) { ListAdd(item, head, head->next); }
54 :
55 : /**
56 : * @brief Insert a new entry before the specified head.
57 : * @param [in] item: new entry to be added
58 : * @param [in] head: list head to add it before
59 : * @return NA
60 : */
61 4749 : static inline void ListAddBeforeEntry(struct ListHead* item, struct ListHead* head) { ListAdd(item, head->prev, head); }
62 :
63 : /*
64 : * @brief Delete an existing entry between two known consecutive entries.
65 : * @param [in] prev: previous entry to delete it after
66 : * @param [in] next: next entry to delete it before
67 : * @return NA
68 : */
69 4731 : static inline void ListDel(struct ListHead* prev, struct ListHead* next)
70 : {
71 4731 : next->prev = prev;
72 4731 : prev->next = next;
73 4731 : }
74 :
75 : /**
76 : * @brief Delete an existing entry specified by entry.
77 : * @param [in] entry: entry to be deleted
78 : * @return NA
79 : */
80 4731 : static inline void ListDelEntry(struct ListHead* entry)
81 : {
82 4731 : ListDel(entry->prev, entry->next);
83 4731 : entry->next = entry;
84 4731 : entry->prev = entry;
85 4731 : }
86 :
87 : /**
88 : * @brief Check the head list empty or not specified by head.
89 : * @param [in] head: list head to be checked
90 : * @return true or false
91 : */
92 12487 : static inline bool ListEmpty(const struct ListHead* head) { return (const struct ListHead*)head->next == head; }
93 :
94 : #define LIST_ENTRY(ptr, type, member) ((type*)((char*)(ptr)-offsetof(type, member)))
95 :
96 : #define LIST_FIRST_ENTRY(ptr, type, member) LIST_ENTRY((ptr)->next, type, member)
97 :
98 : #define LIST_FOR_EACH(pos, head) for ((pos) = (head)->next; ((pos) != NULL) && ((head) != (pos)); (pos) = (pos)->next)
99 :
100 : #define LIST_FOR_EACH_ENTRY(pos, head, type, member) \
101 : for ((pos) = LIST_ENTRY((head)->next, type, member); ((pos) != NULL) && (&(pos)->member != (head)); \
102 : (pos) = LIST_ENTRY((pos)->member.next, type, member))
103 :
104 : #ifdef __cplusplus
105 : }
106 : #endif
107 :
108 : #endif
|