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 #ifndef CHRE_UTIL_INTRUSIVE_LIST_BASE_H_ 18 #define CHRE_UTIL_INTRUSIVE_LIST_BASE_H_ 19 20 #include <cstddef> 21 22 #include "chre/util/non_copyable.h" 23 24 namespace chre { 25 namespace intrusive_list_internal { 26 27 struct Node : public NonCopyable { 28 Node *next = nullptr; 29 Node *prev = nullptr; 30 31 bool operator==(Node const &other) const { 32 return &other == this; 33 } 34 35 bool operator!=(Node const &other) const { 36 return &other != this; 37 } 38 }; 39 40 class IntrusiveListBase : public NonCopyable { 41 protected: 42 /** 43 * The sentinel node for easier access to the first (mSentinelNode.next) 44 * and last (mSentinelNode.prev) element of the linked list. 45 */ 46 Node mSentinelNode; 47 48 /** 49 * Number of elements currently stored in the linked list. 50 */ 51 size_t mSize = 0; 52 IntrusiveListBase()53 IntrusiveListBase() { 54 mSentinelNode.next = &mSentinelNode; 55 mSentinelNode.prev = &mSentinelNode; 56 } 57 58 /** 59 * Link a new node to the front of the linked list. 60 * 61 * @param newNode: The node to push onto the linked list. 62 */ 63 void doLinkFront(Node *newNode); 64 65 /** 66 * Link a new node to the end of the linked list. 67 * 68 * @param newNode: The node to push onto the linked list. 69 */ 70 void doLinkBack(Node *newNode); 71 72 /** 73 * Unlink a node from the linked list. 74 * 75 * @param node: The node to remove from the linked list. 76 */ 77 void doUnlinkNode(Node *node); 78 79 /** 80 * Link a node after a given node. 81 * 82 * @param frontNode: The node that will lead the new node. 83 * @param newNode: The new node to link. 84 */ 85 void doLinkAfter(Node *frontNode, Node *newNode); 86 87 /** 88 * Unlinks all node in this list. 89 */ 90 void doUnlinkAll(); 91 }; 92 93 } // namespace intrusive_list_internal 94 } // namespace chre 95 96 #endif // CHRE_UTIL_INTRUSIVE_LIST_BASE_H_ 97