• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <linux/types.h>
33 
34 #ifndef _TEE_CLIENT_LIST_H_
35 #define _TEE_CLIENT_LIST_H_
36 /*
37  * @ingroup TEEC_List
38  * Linked list type
39  */
40 struct ListNode {
41     struct ListNode *next;
42     struct ListNode *prev;
43 };
44 
45 #define TEEK_INIT_LIST_HEAD(list) do { \
46     (list)->next = (list);        \
47     (list)->prev = (list);        \
48 } while (0)
49 
50 #define LIST_FOR_EACH(node, list)  for ((node) = (list)->next; (node) != (list); (node) = (node)->next)
51 
52 /*
53  * @ingroup TEEC_List
54  * @brief Define a linked list node.
55  * @par Description
56  * Defines a linked list node and initializes it.
57  * @param name [IN] linked list node name
58  */
59 #define LIST_DECLARE(name) \
60     struct ListNode (name) = { \
61         .next = &(name), \
62         .prev = &(name), \
63     }
64 
65 #ifndef NULL
66 #define NULL 0
67 #endif
68 
69 /*
70  * @ingroup TEEC_List
71  * Obtains the prev node of the linked list.
72  */
73 #define LIST_TAIL(list) ((list)->prev)
74 
75 /*
76  * @ingroup TEEC_List
77  * Check whether the linked list is empty.
78  */
79 #define LIST_EMPTY(list) ((list) == (list)->next)
80 
81 /*
82  * @ingroup  TEEC_List
83  * @brief Inserts a new node from the head of a linked list.
84  *
85  * @par Description
86  * Inserts a new node from the head of a linked list
87  *
88  * @param list [IN/OUT]Pointer to the linked list header, the value cannot be empty.
89  * @param entry [IN/OUT]Pointer to the new linked list node, the value cannot be empty.
90  */
ListInsertHead(struct ListNode * list,struct ListNode * entry)91 static inline void ListInsertHead(struct ListNode *list,
92     struct ListNode *entry)
93 {
94     list->next->prev = entry;
95     entry->next = list->next;
96     entry->prev = list;
97     list->next = entry;
98 }
99 
100 /*
101  * @ingroup  TEEC_List
102  * @brief Inserts a new node at the end of the linked list.
103  *
104  * @par Description
105  * Inserts a new node at the end of the linked list.
106  *
107  * @param list [IN/OUT]Pointer to the linked list header, the value cannot be empty.
108  * @param entry [IN/OUT]Pointer to the new linked list node, the value cannot be empty.
109  */
ListInsertTail(struct ListNode * list,struct ListNode * entry)110 static inline void ListInsertTail(struct ListNode *list,
111     struct ListNode *entry)
112 {
113     entry->next = list;
114     entry->prev = list->prev;
115     list->prev->next = entry;
116     list->prev = entry;
117 }
118 
119 /*
120  * @ingroup  TEEC_List
121  * @brief Delete node
122  *
123  * @par Description
124  * Deletes a specified node.
125  *
126  * @attention Release the memory of the node to be deleted.
127  * @param entry [IN]Pointer to the linked list node to be deleted. The value cannot be null.
128  */
ListRemove(struct ListNode * entry)129 static inline void ListRemove(struct ListNode *entry)
130 {
131     entry->prev->next = entry->next;
132     entry->next->prev = entry->prev;
133 }
134 
135 /*
136  * @ingroup  TEEC_List
137  * @brief Delete the head node of the linked list.
138  *
139  * @par Description
140  * Deletes the head node of a specified linked list.
141  *
142  * @attention After return, the memory of the deleted node should be release.
143  * @param list [IN]Pointer to the linked list header, the value cannot be empty.
144  *
145  * @retval #NULL The linked list is empty.
146  * @retval not #NULL Linked list header node
147  */
ListRemoveHead(struct ListNode * list)148 static inline struct ListNode *ListRemoveHead(struct ListNode *list)
149 {
150     struct ListNode *entry = NULL;
151     if (!LIST_EMPTY(list)) {
152         entry = list->next;
153         ListRemove(entry);
154     }
155     return entry;
156 }
157 
158 /*
159  * @ingroup  TEEC_List
160  * @brief Delete the tail node of the linked list.
161  *
162  * @par Description
163  * Delete the tail node of the linked list.
164  *
165  * @attention After return, the memory of the deleted node should be release.
166  * @param list [IN]Pointer to the linked list header, the value cannot be empty.
167  *
168  * @retval NULL The linked list is empty.
169  * @retval not #NULL Linked list header node
170  */
ListRemoveTail(struct ListNode * list)171 static inline struct ListNode *ListRemoveTail(struct ListNode *list)
172 {
173     struct ListNode *entry = NULL;
174     if (!LIST_EMPTY(list)) {
175         entry = list->prev;
176         ListRemove(entry);
177     }
178     return entry;
179 }
180 #endif
181