1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "chre/util/intrusive_list_base.h"
18 
19 #include "chre/util/container_support.h"
20 
21 namespace chre {
22 namespace intrusive_list_internal {
23 
doLinkFront(Node * newNode)24 void IntrusiveListBase::doLinkFront(Node *newNode) {
25   Node *nextNode = mSentinelNode.next;
26   nextNode->prev = newNode;
27   newNode->next = nextNode;
28   newNode->prev = &mSentinelNode;
29   mSentinelNode.next = newNode;
30   mSize++;
31 }
32 
doLinkBack(Node * newNode)33 void IntrusiveListBase::doLinkBack(Node *newNode) {
34   Node *prevNode = mSentinelNode.prev;
35   prevNode->next = newNode;
36   newNode->prev = prevNode;
37   newNode->next = &mSentinelNode;
38   mSentinelNode.prev = newNode;
39   mSize++;
40 }
41 
doUnlinkNode(Node * node)42 void IntrusiveListBase::doUnlinkNode(Node *node) {
43   node->prev->next = node->next;
44   node->next->prev = node->prev;
45   node->next = nullptr;
46   node->prev = nullptr;
47   mSize--;
48 }
49 
doLinkAfter(Node * frontNode,Node * newNode)50 void IntrusiveListBase::doLinkAfter(Node *frontNode, Node *newNode) {
51   Node *backNode = frontNode->next;
52   frontNode->next = newNode;
53   newNode->prev = frontNode;
54   newNode->next = backNode;
55   backNode->prev = newNode;
56   mSize++;
57 }
58 
doUnlinkAll()59 void IntrusiveListBase::doUnlinkAll() {
60   Node *currentNodePtr, *nextNodePtr;
61   currentNodePtr = mSentinelNode.next;
62 
63   while (currentNodePtr != &mSentinelNode) {
64     nextNodePtr = currentNodePtr->next;
65     currentNodePtr->next = nullptr;
66     currentNodePtr->prev = nullptr;
67     currentNodePtr = nextNodePtr;
68   }
69 }
70 
71 }  // namespace intrusive_list_internal
72 }  // namespace chre
73