1 /*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16 #include "hitls_build.h"
17 #ifdef HITLS_BSL_LIST
18
19 #include "securec.h"
20 #include "bsl_sal.h"
21 #include "bsl_list.h"
22
BSL_LIST_FirstNode(const BslList * list)23 BslListNode *BSL_LIST_FirstNode(const BslList *list)
24 {
25 if (list == NULL) {
26 return NULL;
27 }
28
29 return list->first;
30 }
31
BSL_LIST_GetData(const BslListNode * pstNode)32 void *BSL_LIST_GetData(const BslListNode *pstNode)
33 {
34 if (pstNode == NULL) {
35 return NULL;
36 }
37
38 return pstNode->data;
39 }
40
BSL_LIST_GetNextNode(const BslList * pstList,const BslListNode * pstListNode)41 BslListNode *BSL_LIST_GetNextNode(const BslList *pstList, const BslListNode *pstListNode)
42 {
43 if (pstList == NULL) {
44 return NULL;
45 }
46
47 if (pstListNode != NULL) {
48 return pstListNode->next;
49 }
50
51 return pstList->first;
52 }
53
BSL_LIST_GetIndexNodeEx(uint32_t ulIndex,const BslListNode * pstListNode,const BslList * pstList)54 void *BSL_LIST_GetIndexNodeEx(uint32_t ulIndex, const BslListNode *pstListNode, const BslList *pstList)
55 {
56 const BslListNode *pstTmpListNode = NULL;
57 (void)pstListNode;
58 if (pstList == NULL) {
59 return NULL;
60 }
61
62 if (ulIndex >= (uint32_t)pstList->count) {
63 return NULL;
64 }
65
66 if (pstList->first == NULL) {
67 return NULL;
68 }
69
70 pstTmpListNode = pstList->first;
71 for (uint32_t ulIter = 0; ulIter < ulIndex; ulIter++) {
72 pstTmpListNode = pstTmpListNode->next;
73 if (pstTmpListNode == NULL) {
74 return NULL;
75 }
76 }
77
78 return pstTmpListNode->data;
79 }
80
BSL_LIST_GetPrevNode(const BslListNode * pstListNode)81 BslListNode *BSL_LIST_GetPrevNode(const BslListNode *pstListNode)
82 {
83 if (pstListNode == NULL) {
84 return NULL;
85 }
86
87 return pstListNode->prev;
88 }
89
BSL_LIST_DeleteNode(BslList * pstList,const BslListNode * pstListNode,BSL_LIST_PFUNC_FREE pfFreeFunc)90 void BSL_LIST_DeleteNode(BslList *pstList, const BslListNode *pstListNode, BSL_LIST_PFUNC_FREE pfFreeFunc)
91 {
92 BslListNode *pstCurrentNode = NULL;
93
94 if (pstList == NULL) {
95 return;
96 }
97
98 pstCurrentNode = pstList->first;
99
100 while (pstCurrentNode != NULL) {
101 if (pstCurrentNode == pstListNode) {
102 // found matching node, delete this node and adjust the list
103 if ((pstCurrentNode->next) != NULL) {
104 pstCurrentNode->next->prev = pstCurrentNode->prev;
105 } else {
106 pstList->last = pstCurrentNode->prev;
107 }
108
109 if ((pstCurrentNode->prev) != NULL) {
110 pstCurrentNode->prev->next = pstCurrentNode->next;
111 } else {
112 pstList->first = pstCurrentNode->next;
113 }
114 if (pstCurrentNode == pstList->curr) {
115 pstList->curr = pstList->curr->next;
116 }
117 pstList->count--;
118
119 if (pfFreeFunc == NULL) {
120 BSL_SAL_FREE(pstCurrentNode->data);
121 } else {
122 pfFreeFunc(pstCurrentNode->data);
123 }
124
125 BSL_SAL_FREE(pstCurrentNode);
126 return;
127 }
128
129 pstCurrentNode = pstCurrentNode->next;
130 }
131
132 return;
133 }
134
BSL_LIST_DetachNode(BslList * pstList,BslListNode ** pstListNode)135 void BSL_LIST_DetachNode(BslList *pstList, BslListNode **pstListNode)
136 {
137 if (pstList == NULL || pstListNode == NULL) {
138 return;
139 }
140
141 BslListNode *pstCurrentNode = pstList->first;
142 while (pstCurrentNode != NULL) {
143 if (pstCurrentNode != *pstListNode) {
144 pstCurrentNode = pstCurrentNode->next;
145 continue;
146 }
147 // found matching node, delete this node and adjust the list
148 if ((pstCurrentNode->next) != NULL) {
149 pstCurrentNode->next->prev = pstCurrentNode->prev;
150 if (*pstListNode == pstList->curr) {
151 pstList->curr = pstCurrentNode->next;
152 }
153 *pstListNode = pstCurrentNode->next; // update the current node and point it to the next node
154 } else {
155 pstList->last = pstCurrentNode->prev;
156 if (*pstListNode == pstList->curr) {
157 pstList->curr = pstCurrentNode->prev;
158 }
159 *pstListNode = pstList->last;
160 }
161
162 if ((pstCurrentNode->prev) != NULL) {
163 pstCurrentNode->prev->next = pstCurrentNode->next;
164 } else {
165 pstList->first = pstCurrentNode->next;
166 }
167
168 pstList->count--;
169
170 BSL_SAL_FREE(pstCurrentNode);
171 return;
172 }
173 return;
174 }
175 #endif /* HITLS_BSL_LIST */
176