1526fd984Sopenharmony_ci/*
2526fd984Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3526fd984Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4526fd984Sopenharmony_ci * you may not use this file except in compliance with the License.
5526fd984Sopenharmony_ci * You may obtain a copy of the License at
6526fd984Sopenharmony_ci *
7526fd984Sopenharmony_ci *    http://www.apache.org/licenses/LICENSE-2.0
8526fd984Sopenharmony_ci *
9526fd984Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10526fd984Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11526fd984Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12526fd984Sopenharmony_ci * See the License for the specific language governing permissions and
13526fd984Sopenharmony_ci * limitations under the License.
14526fd984Sopenharmony_ci */
15526fd984Sopenharmony_ci
16526fd984Sopenharmony_ci#include "hks_double_list.h"
17526fd984Sopenharmony_ci
18526fd984Sopenharmony_ci#ifndef NULL
19526fd984Sopenharmony_ci#define NULL ((void *)0)
20526fd984Sopenharmony_ci#endif
21526fd984Sopenharmony_ci
22526fd984Sopenharmony_civoid InitializeDoubleList(struct DoubleList *node)
23526fd984Sopenharmony_ci{
24526fd984Sopenharmony_ci    if (node == NULL) {
25526fd984Sopenharmony_ci        return;
26526fd984Sopenharmony_ci    }
27526fd984Sopenharmony_ci
28526fd984Sopenharmony_ci    node->prev = node;
29526fd984Sopenharmony_ci    node->next = node;
30526fd984Sopenharmony_ci}
31526fd984Sopenharmony_ci
32526fd984Sopenharmony_civoid AddNodeAfterDoubleListHead(struct DoubleList *head, struct DoubleList *node)
33526fd984Sopenharmony_ci{
34526fd984Sopenharmony_ci    if ((head == NULL) || (node == NULL)) {
35526fd984Sopenharmony_ci        return;
36526fd984Sopenharmony_ci    }
37526fd984Sopenharmony_ci
38526fd984Sopenharmony_ci    if (head->next == NULL) {
39526fd984Sopenharmony_ci        head->next = head;
40526fd984Sopenharmony_ci    }
41526fd984Sopenharmony_ci
42526fd984Sopenharmony_ci    head->next->prev = node;
43526fd984Sopenharmony_ci    node->next = head->next;
44526fd984Sopenharmony_ci    node->prev = head;
45526fd984Sopenharmony_ci    head->next = node;
46526fd984Sopenharmony_ci}
47526fd984Sopenharmony_ci
48526fd984Sopenharmony_civoid AddNodeAtDoubleListTail(struct DoubleList *head, struct DoubleList *node)
49526fd984Sopenharmony_ci{
50526fd984Sopenharmony_ci    if ((head == NULL) || (node == NULL)) {
51526fd984Sopenharmony_ci        return;
52526fd984Sopenharmony_ci    }
53526fd984Sopenharmony_ci
54526fd984Sopenharmony_ci    if (head->prev == NULL) {
55526fd984Sopenharmony_ci        head->prev = head;
56526fd984Sopenharmony_ci    }
57526fd984Sopenharmony_ci
58526fd984Sopenharmony_ci    head->prev->next = node;
59526fd984Sopenharmony_ci    node->next = head;
60526fd984Sopenharmony_ci    node->prev = head->prev;
61526fd984Sopenharmony_ci    head->prev = node;
62526fd984Sopenharmony_ci}
63526fd984Sopenharmony_ci
64526fd984Sopenharmony_civoid RemoveDoubleListNode(struct DoubleList *node)
65526fd984Sopenharmony_ci{
66526fd984Sopenharmony_ci    if (node == NULL) {
67526fd984Sopenharmony_ci        return;
68526fd984Sopenharmony_ci    }
69526fd984Sopenharmony_ci
70526fd984Sopenharmony_ci    if (node->next != NULL) {
71526fd984Sopenharmony_ci        node->next->prev = node->prev;
72526fd984Sopenharmony_ci    }
73526fd984Sopenharmony_ci
74526fd984Sopenharmony_ci    if (node->prev != NULL) {
75526fd984Sopenharmony_ci        node->prev->next = node->next;
76526fd984Sopenharmony_ci    }
77526fd984Sopenharmony_ci
78526fd984Sopenharmony_ci    node->prev = NULL;
79526fd984Sopenharmony_ci    node->next = NULL;
80526fd984Sopenharmony_ci}
81526fd984Sopenharmony_ci
82