1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
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  */
15 
16 #include "cert_manager_double_list.h"
17 
18 #ifndef NULL
19 #define NULL ((void *)0)
20 #endif
21 
CmInitList(struct DoubleList * listNode)22 void CmInitList(struct DoubleList *listNode)
23 {
24     if (listNode == NULL) {
25         return;
26     }
27 
28     listNode->prev = listNode;
29     listNode->next = listNode;
30 }
31 
CmAddNodeAfterListHead(struct DoubleList * listHead,struct DoubleList * listNode)32 void CmAddNodeAfterListHead(struct DoubleList *listHead, struct DoubleList *listNode)
33 {
34     if ((listHead == NULL) || (listNode == NULL)) {
35         return;
36     }
37 
38     if (listHead->next == NULL) {
39         listHead->next = listHead;
40     }
41 
42     listHead->next->prev = listNode;
43     listNode->next = listHead->next;
44     listNode->prev = listHead;
45     listHead->next = listNode;
46 }
47 
CmAddNodeAtListTail(struct DoubleList * listHead,struct DoubleList * listNode)48 void CmAddNodeAtListTail(struct DoubleList *listHead, struct DoubleList *listNode)
49 {
50     if ((listHead == NULL) || (listNode == NULL)) {
51         return;
52     }
53 
54     if (listHead->prev == NULL) {
55         listHead->prev = listHead;
56     }
57 
58     listHead->prev->next = listNode;
59     listNode->next = listHead;
60     listNode->prev = listHead->prev;
61     listHead->prev = listNode;
62 }
63 
CmRemoveNodeFromList(struct DoubleList * listNode)64 void CmRemoveNodeFromList(struct DoubleList *listNode)
65 {
66     if (listNode == NULL) {
67         return;
68     }
69 
70     if (listNode->next != NULL) {
71         listNode->next->prev = listNode->prev;
72     }
73 
74     if (listNode->prev != NULL) {
75         listNode->prev->next = listNode->next;
76     }
77 
78     listNode->prev = NULL;
79     listNode->next = NULL;
80 }
81 
82