blob: 2741008b8ca21b94cb3dc7e9b6b7941f9a3b060b [file] [log] [blame]
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070027 */
28
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070030
Elliott Hughes5e62b342018-10-25 11:00:00 -070031#include <android-base/macros.h>
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070032
33template<typename T>
34struct LinkedListEntry {
35 LinkedListEntry<T>* next;
36 T* element;
37};
38
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070039// ForwardInputIterator
40template<typename T>
41class LinkedListIterator {
42 public:
43 LinkedListIterator() : entry_(nullptr) {}
44 LinkedListIterator(const LinkedListIterator<T>& that) : entry_(that.entry_) {}
45 explicit LinkedListIterator(LinkedListEntry<T>* entry) : entry_(entry) {}
46
47 LinkedListIterator<T>& operator=(const LinkedListIterator<T>& that) {
48 entry_ = that.entry_;
49 return *this;
50 }
51
52 LinkedListIterator<T>& operator++() {
53 entry_ = entry_->next;
54 return *this;
55 }
56
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070057 T* const operator*() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070058 return entry_->element;
59 }
60
61 bool operator==(const LinkedListIterator<T>& that) const {
62 return entry_ == that.entry_;
63 }
64
65 bool operator!=(const LinkedListIterator<T>& that) const {
66 return entry_ != that.entry_;
67 }
68
69 private:
70 LinkedListEntry<T> *entry_;
71};
72
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070073/*
74 * Represents linked list of objects of type T
75 */
76template<typename T, typename Allocator>
77class LinkedList {
78 public:
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070079 typedef LinkedListIterator<T> iterator;
80 typedef T* value_type;
81
Eric Miaofbee3172021-11-23 17:48:10 +000082 // Allocating the head/tail fields separately from the LinkedList struct saves memory in the
83 // Zygote (e.g. because adding an soinfo to a namespace doesn't dirty the page containing the
84 // soinfo).
85 struct LinkedListHeader {
86 LinkedListEntry<T>* head;
87 LinkedListEntry<T>* tail;
88 };
89
90 // The allocator returns a LinkedListEntry<T>* but we want to treat it as a LinkedListHeader
91 // struct instead.
92 static_assert(sizeof(LinkedListHeader) == sizeof(LinkedListEntry<T>));
93 static_assert(alignof(LinkedListHeader) == alignof(LinkedListEntry<T>));
94
95 constexpr LinkedList() : header_(nullptr) {}
Dmitriy Ivanov14241402014-08-26 14:16:52 -070096 ~LinkedList() {
97 clear();
Eric Miaofbee3172021-11-23 17:48:10 +000098 if (header_ != nullptr) {
99 Allocator::free(reinterpret_cast<LinkedListEntry<T>*>(header_));
100 }
Dmitriy Ivanov14241402014-08-26 14:16:52 -0700101 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700102
Chih-Hung Hsieh9a64b562018-09-25 14:00:44 -0700103 LinkedList(LinkedList&& that) noexcept {
Eric Miaofbee3172021-11-23 17:48:10 +0000104 this->header_ = that.header_;
105 that.header_ = nullptr;
106 }
107
108 bool empty() const {
109 return header_ == nullptr || header_->head == nullptr;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700110 }
111
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700112 void push_front(T* const element) {
Eric Miaofbee3172021-11-23 17:48:10 +0000113 alloc_header();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700114 LinkedListEntry<T>* new_entry = Allocator::alloc();
Eric Miaofbee3172021-11-23 17:48:10 +0000115 new_entry->next = header_->head;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700116 new_entry->element = element;
Eric Miaofbee3172021-11-23 17:48:10 +0000117 header_->head = new_entry;
118 if (header_->tail == nullptr) {
119 header_->tail = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700120 }
121 }
122
123 void push_back(T* const element) {
Eric Miaofbee3172021-11-23 17:48:10 +0000124 alloc_header();
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700125 LinkedListEntry<T>* new_entry = Allocator::alloc();
126 new_entry->next = nullptr;
127 new_entry->element = element;
Eric Miaofbee3172021-11-23 17:48:10 +0000128 if (header_->tail == nullptr) {
129 header_->tail = header_->head = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700130 } else {
Eric Miaofbee3172021-11-23 17:48:10 +0000131 header_->tail->next = new_entry;
132 header_->tail = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700133 }
134 }
135
136 T* pop_front() {
Eric Miaofbee3172021-11-23 17:48:10 +0000137 if (empty()) return nullptr;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700138
Eric Miaofbee3172021-11-23 17:48:10 +0000139 LinkedListEntry<T>* entry = header_->head;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700140 T* element = entry->element;
Eric Miaofbee3172021-11-23 17:48:10 +0000141 header_->head = entry->next;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700142 Allocator::free(entry);
143
Eric Miaofbee3172021-11-23 17:48:10 +0000144 if (header_->head == nullptr) {
145 header_->tail = nullptr;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700146 }
147
148 return element;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700149 }
150
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800151 T* front() const {
Eric Miaofbee3172021-11-23 17:48:10 +0000152 return empty() ? nullptr : header_->head->element;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800153 }
154
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700155 void clear() {
Eric Miaofbee3172021-11-23 17:48:10 +0000156 if (empty()) return;
157
158 while (header_->head != nullptr) {
159 LinkedListEntry<T>* p = header_->head;
160 header_->head = header_->head->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700161 Allocator::free(p);
162 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700163
Eric Miaofbee3172021-11-23 17:48:10 +0000164 header_->tail = nullptr;
Dimitry Ivanovec90e242017-02-10 11:04:20 -0800165 }
166
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700167 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700168 void for_each(F action) const {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700169 visit([&] (T* si) {
170 action(si);
171 return true;
172 });
173 }
174
175 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700176 bool visit(F action) const {
Eric Miaofbee3172021-11-23 17:48:10 +0000177 for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700178 if (!action(e->element)) {
179 return false;
180 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700181 }
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700182 return true;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700183 }
184
185 template<typename F>
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700186 void remove_if(F predicate) {
Eric Miaofbee3172021-11-23 17:48:10 +0000187 if (empty()) return;
188 for (LinkedListEntry<T>* e = header_->head, *p = nullptr; e != nullptr;) {
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700189 if (predicate(e->element)) {
190 LinkedListEntry<T>* next = e->next;
191 if (p == nullptr) {
Eric Miaofbee3172021-11-23 17:48:10 +0000192 header_->head = next;
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700193 } else {
194 p->next = next;
195 }
Dmitriy Ivanov7a9311f2015-11-05 17:41:05 -0800196
Eric Miaofbee3172021-11-23 17:48:10 +0000197 if (header_->tail == e) {
198 header_->tail = p;
Dmitriy Ivanov7a9311f2015-11-05 17:41:05 -0800199 }
200
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700201 Allocator::free(e);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700202
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700203 e = next;
204 } else {
205 p = e;
206 e = e->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700207 }
208 }
209 }
210
Dimitry Ivanovec90e242017-02-10 11:04:20 -0800211 void remove(T* element) {
212 remove_if([&](T* e) {
213 return e == element;
214 });
215 }
216
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700217 template<typename F>
218 T* find_if(F predicate) const {
Eric Miaofbee3172021-11-23 17:48:10 +0000219 for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700220 if (predicate(e->element)) {
221 return e->element;
222 }
223 }
224
225 return nullptr;
226 }
227
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700228 iterator begin() const {
Eric Miaofbee3172021-11-23 17:48:10 +0000229 return iterator(head());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700230 }
231
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700232 iterator end() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700233 return iterator(nullptr);
234 }
235
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700236 iterator find(T* value) const {
Eric Miaofbee3172021-11-23 17:48:10 +0000237 for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700238 if (e->element == value) {
239 return iterator(e);
240 }
241 }
242
243 return end();
244 }
245
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700246 size_t copy_to_array(T* array[], size_t array_length) const {
247 size_t sz = 0;
Eric Miaofbee3172021-11-23 17:48:10 +0000248 for (LinkedListEntry<T>* e = head(); sz < array_length && e != nullptr; e = e->next) {
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700249 array[sz++] = e->element;
250 }
251
252 return sz;
253 }
254
255 bool contains(const T* el) const {
Eric Miaofbee3172021-11-23 17:48:10 +0000256 for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700257 if (e->element == el) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700258 return true;
259 }
260 }
261 return false;
262 }
263
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700264 static LinkedList make_list(T* const element) {
265 LinkedList<T, Allocator> one_element_list;
266 one_element_list.push_back(element);
267 return one_element_list;
268 }
269
Ryan Prichard339ecef2020-01-02 16:36:06 -0800270 size_t size() const {
271 size_t result = 0;
272 for_each([&](T*) { ++result; });
273 return result;
274 }
275
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700276 private:
Eric Miaofbee3172021-11-23 17:48:10 +0000277 void alloc_header() {
278 if (header_ == nullptr) {
279 header_ = reinterpret_cast<LinkedListHeader*>(Allocator::alloc());
280 header_->head = header_->tail = nullptr;
281 }
282 }
283
284 LinkedListEntry<T>* head() const {
285 return header_ != nullptr ? header_->head : nullptr;
286 }
287
288 LinkedListHeader* header_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700289 DISALLOW_COPY_AND_ASSIGN(LinkedList);
290};