Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 4 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 14 | * |
Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 27 | */ |
| 28 | |
Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 30 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 31 | #include <android-base/macros.h> |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 32 | |
| 33 | template<typename T> |
| 34 | struct LinkedListEntry { |
| 35 | LinkedListEntry<T>* next; |
| 36 | T* element; |
| 37 | }; |
| 38 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 39 | // ForwardInputIterator |
| 40 | template<typename T> |
| 41 | class 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 Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 57 | T* const operator*() { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 58 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 73 | /* |
| 74 | * Represents linked list of objects of type T |
| 75 | */ |
| 76 | template<typename T, typename Allocator> |
| 77 | class LinkedList { |
| 78 | public: |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 79 | typedef LinkedListIterator<T> iterator; |
| 80 | typedef T* value_type; |
| 81 | |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 82 | // 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 Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 96 | ~LinkedList() { |
| 97 | clear(); |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 98 | if (header_ != nullptr) { |
| 99 | Allocator::free(reinterpret_cast<LinkedListEntry<T>*>(header_)); |
| 100 | } |
Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 101 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 102 | |
Chih-Hung Hsieh | 9a64b56 | 2018-09-25 14:00:44 -0700 | [diff] [blame] | 103 | LinkedList(LinkedList&& that) noexcept { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 104 | this->header_ = that.header_; |
| 105 | that.header_ = nullptr; |
| 106 | } |
| 107 | |
| 108 | bool empty() const { |
| 109 | return header_ == nullptr || header_->head == nullptr; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 112 | void push_front(T* const element) { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 113 | alloc_header(); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 114 | LinkedListEntry<T>* new_entry = Allocator::alloc(); |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 115 | new_entry->next = header_->head; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 116 | new_entry->element = element; |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 117 | header_->head = new_entry; |
| 118 | if (header_->tail == nullptr) { |
| 119 | header_->tail = new_entry; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | void push_back(T* const element) { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 124 | alloc_header(); |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 125 | LinkedListEntry<T>* new_entry = Allocator::alloc(); |
| 126 | new_entry->next = nullptr; |
| 127 | new_entry->element = element; |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 128 | if (header_->tail == nullptr) { |
| 129 | header_->tail = header_->head = new_entry; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 130 | } else { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 131 | header_->tail->next = new_entry; |
| 132 | header_->tail = new_entry; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | T* pop_front() { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 137 | if (empty()) return nullptr; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 138 | |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 139 | LinkedListEntry<T>* entry = header_->head; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 140 | T* element = entry->element; |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 141 | header_->head = entry->next; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 142 | Allocator::free(entry); |
| 143 | |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 144 | if (header_->head == nullptr) { |
| 145 | header_->tail = nullptr; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | return element; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 151 | T* front() const { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 152 | return empty() ? nullptr : header_->head->element; |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 155 | void clear() { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 156 | if (empty()) return; |
| 157 | |
| 158 | while (header_->head != nullptr) { |
| 159 | LinkedListEntry<T>* p = header_->head; |
| 160 | header_->head = header_->head->next; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 161 | Allocator::free(p); |
| 162 | } |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 163 | |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 164 | header_->tail = nullptr; |
Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 167 | template<typename F> |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 168 | void for_each(F action) const { |
Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 169 | visit([&] (T* si) { |
| 170 | action(si); |
| 171 | return true; |
| 172 | }); |
| 173 | } |
| 174 | |
| 175 | template<typename F> |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 176 | bool visit(F action) const { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 177 | for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) { |
Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 178 | if (!action(e->element)) { |
| 179 | return false; |
| 180 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 181 | } |
Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 182 | return true; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | template<typename F> |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 186 | void remove_if(F predicate) { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 187 | if (empty()) return; |
| 188 | for (LinkedListEntry<T>* e = header_->head, *p = nullptr; e != nullptr;) { |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 189 | if (predicate(e->element)) { |
| 190 | LinkedListEntry<T>* next = e->next; |
| 191 | if (p == nullptr) { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 192 | header_->head = next; |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 193 | } else { |
| 194 | p->next = next; |
| 195 | } |
Dmitriy Ivanov | 7a9311f | 2015-11-05 17:41:05 -0800 | [diff] [blame] | 196 | |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 197 | if (header_->tail == e) { |
| 198 | header_->tail = p; |
Dmitriy Ivanov | 7a9311f | 2015-11-05 17:41:05 -0800 | [diff] [blame] | 199 | } |
| 200 | |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 201 | Allocator::free(e); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 202 | |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 203 | e = next; |
| 204 | } else { |
| 205 | p = e; |
| 206 | e = e->next; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 211 | void remove(T* element) { |
| 212 | remove_if([&](T* e) { |
| 213 | return e == element; |
| 214 | }); |
| 215 | } |
| 216 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 217 | template<typename F> |
| 218 | T* find_if(F predicate) const { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 219 | for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) { |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 220 | if (predicate(e->element)) { |
| 221 | return e->element; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return nullptr; |
| 226 | } |
| 227 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 228 | iterator begin() const { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 229 | return iterator(head()); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 232 | iterator end() const { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 233 | return iterator(nullptr); |
| 234 | } |
| 235 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 236 | iterator find(T* value) const { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 237 | for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 238 | if (e->element == value) { |
| 239 | return iterator(e); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | return end(); |
| 244 | } |
| 245 | |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 246 | size_t copy_to_array(T* array[], size_t array_length) const { |
| 247 | size_t sz = 0; |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 248 | for (LinkedListEntry<T>* e = head(); sz < array_length && e != nullptr; e = e->next) { |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 249 | array[sz++] = e->element; |
| 250 | } |
| 251 | |
| 252 | return sz; |
| 253 | } |
| 254 | |
| 255 | bool contains(const T* el) const { |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 256 | for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) { |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 257 | if (e->element == el) { |
Dmitriy Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 258 | return true; |
| 259 | } |
| 260 | } |
| 261 | return false; |
| 262 | } |
| 263 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 264 | 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 Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 270 | size_t size() const { |
| 271 | size_t result = 0; |
| 272 | for_each([&](T*) { ++result; }); |
| 273 | return result; |
| 274 | } |
| 275 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 276 | private: |
Eric Miao | fbee317 | 2021-11-23 17:48:10 +0000 | [diff] [blame] | 277 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 289 | DISALLOW_COPY_AND_ASSIGN(LinkedList); |
| 290 | }; |