1 /*
2  * Copyright (C) 2016 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 LIBMEMUNREACHABLE_LINKED_LIST_H_
18 #define LIBMEMUNREACHABLE_LINKED_LIST_H_
19 
20 namespace android {
21 
22 template <class T>
23 class LinkedList {
24  public:
LinkedList()25   LinkedList() : next_(this), prev_(this), data_() {}
LinkedList(T data)26   explicit LinkedList(T data) : LinkedList() { data_ = data; }
~LinkedList()27   ~LinkedList() {}
insert(LinkedList<T> & node)28   void insert(LinkedList<T>& node) {
29     assert(node.empty());
30     node.next_ = this->next_;
31     node.next_->prev_ = &node;
32     this->next_ = &node;
33     node.prev_ = this;
34   }
remove()35   void remove() {
36     this->next_->prev_ = this->prev_;
37     this->prev_->next_ = this->next_;
38     this->next_ = this;
39     this->prev_ = this;
40   }
data()41   T data() { return data_; }
empty()42   bool empty() { return next_ == this && prev_ == this; }
next()43   LinkedList<T>* next() { return next_; }
44 
45  private:
46   LinkedList<T>* next_;
47   LinkedList<T>* prev_;
48   T data_;
49 };
50 
51 template <class T>
52 class LinkedListHead {
53  public:
LinkedListHead()54   LinkedListHead() : node_() {}
~LinkedListHead()55   ~LinkedListHead() {}
56 
57  private:
58   LinkedList<T> node_;
59 };
60 
61 }  // namespace android
62 
63 #endif
64