Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_BASE_ARENA_CONTAINERS_H_ |
| 18 | #define ART_RUNTIME_BASE_ARENA_CONTAINERS_H_ |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 19 | |
| 20 | #include <deque> |
| 21 | #include <queue> |
| 22 | #include <set> |
Matthew Gharrity | 33ee120 | 2016-07-29 09:13:33 -0700 | [diff] [blame] | 23 | #include <stack> |
Vladimir Marko | 9c57113 | 2017-02-22 11:59:57 +0000 | [diff] [blame] | 24 | #include <unordered_map> |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 25 | #include <utility> |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 26 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 27 | #include "arena_allocator.h" |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 28 | #include "base/dchecked_vector.h" |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 29 | #include "hash_map.h" |
| 30 | #include "hash_set.h" |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 31 | #include "safe_map.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | // Adapter for use of ArenaAllocator in STL containers. |
| 36 | // Use ArenaAllocator::Adapter() to create an adapter to pass to container constructors. |
| 37 | // For example, |
| 38 | // struct Foo { |
| 39 | // explicit Foo(ArenaAllocator* allocator) |
| 40 | // : foo_vector(allocator->Adapter(kArenaAllocMisc)), |
| 41 | // foo_map(std::less<int>(), allocator->Adapter()) { |
| 42 | // } |
| 43 | // ArenaVector<int> foo_vector; |
| 44 | // ArenaSafeMap<int, int> foo_map; |
| 45 | // }; |
| 46 | template <typename T> |
| 47 | class ArenaAllocatorAdapter; |
| 48 | |
| 49 | template <typename T> |
| 50 | using ArenaDeque = std::deque<T, ArenaAllocatorAdapter<T>>; |
| 51 | |
| 52 | template <typename T> |
| 53 | using ArenaQueue = std::queue<T, ArenaDeque<T>>; |
| 54 | |
| 55 | template <typename T> |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 56 | using ArenaVector = dchecked_vector<T, ArenaAllocatorAdapter<T>>; |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 57 | |
| 58 | template <typename T, typename Comparator = std::less<T>> |
Matthew Gharrity | 33ee120 | 2016-07-29 09:13:33 -0700 | [diff] [blame] | 59 | using ArenaPriorityQueue = std::priority_queue<T, ArenaVector<T>, Comparator>; |
| 60 | |
| 61 | template <typename T> |
| 62 | using ArenaStdStack = std::stack<T, ArenaDeque<T>>; |
| 63 | |
| 64 | template <typename T, typename Comparator = std::less<T>> |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 65 | using ArenaSet = std::set<T, Comparator, ArenaAllocatorAdapter<T>>; |
| 66 | |
| 67 | template <typename K, typename V, typename Comparator = std::less<K>> |
| 68 | using ArenaSafeMap = |
| 69 | SafeMap<K, V, Comparator, ArenaAllocatorAdapter<std::pair<const K, V>>>; |
| 70 | |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 71 | template <typename T, |
| 72 | typename EmptyFn = DefaultEmptyFn<T>, |
| 73 | typename HashFn = std::hash<T>, |
| 74 | typename Pred = std::equal_to<T>> |
| 75 | using ArenaHashSet = HashSet<T, EmptyFn, HashFn, Pred, ArenaAllocatorAdapter<T>>; |
| 76 | |
| 77 | template <typename Key, |
| 78 | typename Value, |
| 79 | typename EmptyFn = DefaultEmptyFn<std::pair<Key, Value>>, |
| 80 | typename HashFn = std::hash<Key>, |
| 81 | typename Pred = std::equal_to<Key>> |
| 82 | using ArenaHashMap = HashMap<Key, |
| 83 | Value, |
| 84 | EmptyFn, |
| 85 | HashFn, |
| 86 | Pred, |
| 87 | ArenaAllocatorAdapter<std::pair<Key, Value>>>; |
| 88 | |
Vladimir Marko | 9c57113 | 2017-02-22 11:59:57 +0000 | [diff] [blame] | 89 | template <typename Key, |
| 90 | typename Value, |
| 91 | typename Hash = std::hash<Key>, |
| 92 | typename Pred = std::equal_to<Value>> |
| 93 | using ArenaUnorderedMap = std::unordered_map<Key, |
| 94 | Value, |
| 95 | Hash, |
| 96 | Pred, |
| 97 | ArenaAllocatorAdapter<std::pair<const Key, Value>>>; |
| 98 | |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 99 | // Implementation details below. |
| 100 | |
| 101 | template <bool kCount> |
| 102 | class ArenaAllocatorAdapterKindImpl; |
| 103 | |
| 104 | template <> |
| 105 | class ArenaAllocatorAdapterKindImpl<false> { |
| 106 | public: |
| 107 | // Not tracking allocations, ignore the supplied kind and arbitrarily provide kArenaAllocSTL. |
Andreas Gampe | c801f0d | 2015-02-24 20:55:16 -0800 | [diff] [blame] | 108 | explicit ArenaAllocatorAdapterKindImpl(ArenaAllocKind kind ATTRIBUTE_UNUSED) {} |
Andreas Gampe | 758a801 | 2015-04-03 21:28:42 -0700 | [diff] [blame] | 109 | ArenaAllocatorAdapterKindImpl(const ArenaAllocatorAdapterKindImpl&) = default; |
Andreas Gampe | c801f0d | 2015-02-24 20:55:16 -0800 | [diff] [blame] | 110 | ArenaAllocatorAdapterKindImpl& operator=(const ArenaAllocatorAdapterKindImpl&) = default; |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 111 | ArenaAllocKind Kind() { return kArenaAllocSTL; } |
| 112 | }; |
| 113 | |
| 114 | template <bool kCount> |
| 115 | class ArenaAllocatorAdapterKindImpl { |
| 116 | public: |
| 117 | explicit ArenaAllocatorAdapterKindImpl(ArenaAllocKind kind) : kind_(kind) { } |
Vladimir Marko | f9f6441 | 2015-09-02 14:05:49 +0100 | [diff] [blame] | 118 | ArenaAllocatorAdapterKindImpl(const ArenaAllocatorAdapterKindImpl&) = default; |
Andreas Gampe | c801f0d | 2015-02-24 20:55:16 -0800 | [diff] [blame] | 119 | ArenaAllocatorAdapterKindImpl& operator=(const ArenaAllocatorAdapterKindImpl&) = default; |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 120 | ArenaAllocKind Kind() { return kind_; } |
| 121 | |
| 122 | private: |
| 123 | ArenaAllocKind kind_; |
| 124 | }; |
| 125 | |
| 126 | typedef ArenaAllocatorAdapterKindImpl<kArenaAllocatorCountAllocations> ArenaAllocatorAdapterKind; |
| 127 | |
| 128 | template <> |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 129 | class ArenaAllocatorAdapter<void> : private ArenaAllocatorAdapterKind { |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 130 | public: |
| 131 | typedef void value_type; |
| 132 | typedef void* pointer; |
| 133 | typedef const void* const_pointer; |
| 134 | |
| 135 | template <typename U> |
| 136 | struct rebind { |
| 137 | typedef ArenaAllocatorAdapter<U> other; |
| 138 | }; |
| 139 | |
| 140 | explicit ArenaAllocatorAdapter(ArenaAllocator* arena_allocator, |
| 141 | ArenaAllocKind kind = kArenaAllocSTL) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 142 | : ArenaAllocatorAdapterKind(kind), |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 143 | arena_allocator_(arena_allocator) { |
| 144 | } |
| 145 | template <typename U> |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 146 | ArenaAllocatorAdapter(const ArenaAllocatorAdapter<U>& other) // NOLINT, implicit |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 147 | : ArenaAllocatorAdapterKind(other), |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 148 | arena_allocator_(other.arena_allocator_) { |
| 149 | } |
Andreas Gampe | c801f0d | 2015-02-24 20:55:16 -0800 | [diff] [blame] | 150 | ArenaAllocatorAdapter(const ArenaAllocatorAdapter&) = default; |
| 151 | ArenaAllocatorAdapter& operator=(const ArenaAllocatorAdapter&) = default; |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 152 | ~ArenaAllocatorAdapter() = default; |
| 153 | |
| 154 | private: |
| 155 | ArenaAllocator* arena_allocator_; |
| 156 | |
| 157 | template <typename U> |
| 158 | friend class ArenaAllocatorAdapter; |
| 159 | }; |
| 160 | |
| 161 | template <typename T> |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 162 | class ArenaAllocatorAdapter : private ArenaAllocatorAdapterKind { |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 163 | public: |
| 164 | typedef T value_type; |
| 165 | typedef T* pointer; |
| 166 | typedef T& reference; |
| 167 | typedef const T* const_pointer; |
| 168 | typedef const T& const_reference; |
| 169 | typedef size_t size_type; |
| 170 | typedef ptrdiff_t difference_type; |
| 171 | |
| 172 | template <typename U> |
| 173 | struct rebind { |
| 174 | typedef ArenaAllocatorAdapter<U> other; |
| 175 | }; |
| 176 | |
Roland Levillain | 3887c46 | 2015-08-12 18:15:42 +0100 | [diff] [blame] | 177 | ArenaAllocatorAdapter(ArenaAllocator* arena_allocator, ArenaAllocKind kind) |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 178 | : ArenaAllocatorAdapterKind(kind), |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 179 | arena_allocator_(arena_allocator) { |
| 180 | } |
| 181 | template <typename U> |
Chih-Hung Hsieh | a593118 | 2016-09-01 15:08:13 -0700 | [diff] [blame] | 182 | ArenaAllocatorAdapter(const ArenaAllocatorAdapter<U>& other) // NOLINT, implicit |
David Brazdil | 8d5b8b2 | 2015-03-24 10:51:52 +0000 | [diff] [blame] | 183 | : ArenaAllocatorAdapterKind(other), |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 184 | arena_allocator_(other.arena_allocator_) { |
| 185 | } |
Andreas Gampe | c801f0d | 2015-02-24 20:55:16 -0800 | [diff] [blame] | 186 | ArenaAllocatorAdapter(const ArenaAllocatorAdapter&) = default; |
| 187 | ArenaAllocatorAdapter& operator=(const ArenaAllocatorAdapter&) = default; |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 188 | ~ArenaAllocatorAdapter() = default; |
| 189 | |
| 190 | size_type max_size() const { |
| 191 | return static_cast<size_type>(-1) / sizeof(T); |
| 192 | } |
| 193 | |
| 194 | pointer address(reference x) const { return &x; } |
| 195 | const_pointer address(const_reference x) const { return &x; } |
| 196 | |
Roland Levillain | 4b8f1ec | 2015-08-26 18:34:03 +0100 | [diff] [blame] | 197 | pointer allocate(size_type n, |
| 198 | ArenaAllocatorAdapter<void>::pointer hint ATTRIBUTE_UNUSED = nullptr) { |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 199 | DCHECK_LE(n, max_size()); |
Vladimir Marko | e4fcc5b | 2015-02-13 10:28:29 +0000 | [diff] [blame] | 200 | return arena_allocator_->AllocArray<T>(n, ArenaAllocatorAdapterKind::Kind()); |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 201 | } |
| 202 | void deallocate(pointer p, size_type n) { |
Vladimir Marko | 2a408a3 | 2015-09-18 14:11:00 +0100 | [diff] [blame] | 203 | arena_allocator_->MakeInaccessible(p, sizeof(T) * n); |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 206 | template <typename U, typename... Args> |
| 207 | void construct(U* p, Args&&... args) { |
| 208 | ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...); |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 209 | } |
Vladimir Marko | 1f49764 | 2015-10-05 20:34:42 +0100 | [diff] [blame] | 210 | template <typename U> |
| 211 | void destroy(U* p) { |
| 212 | p->~U(); |
Vladimir Marko | 8081d2b | 2014-07-31 15:33:43 +0100 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | private: |
| 216 | ArenaAllocator* arena_allocator_; |
| 217 | |
| 218 | template <typename U> |
| 219 | friend class ArenaAllocatorAdapter; |
| 220 | |
| 221 | template <typename U> |
| 222 | friend bool operator==(const ArenaAllocatorAdapter<U>& lhs, |
| 223 | const ArenaAllocatorAdapter<U>& rhs); |
| 224 | }; |
| 225 | |
| 226 | template <typename T> |
| 227 | inline bool operator==(const ArenaAllocatorAdapter<T>& lhs, |
| 228 | const ArenaAllocatorAdapter<T>& rhs) { |
| 229 | return lhs.arena_allocator_ == rhs.arena_allocator_; |
| 230 | } |
| 231 | |
| 232 | template <typename T> |
| 233 | inline bool operator!=(const ArenaAllocatorAdapter<T>& lhs, |
| 234 | const ArenaAllocatorAdapter<T>& rhs) { |
| 235 | return !(lhs == rhs); |
| 236 | } |
| 237 | |
| 238 | inline ArenaAllocatorAdapter<void> ArenaAllocator::Adapter(ArenaAllocKind kind) { |
| 239 | return ArenaAllocatorAdapter<void>(this, kind); |
| 240 | } |
| 241 | |
| 242 | } // namespace art |
| 243 | |
Mathieu Chartier | b666f48 | 2015-02-18 14:33:14 -0800 | [diff] [blame] | 244 | #endif // ART_RUNTIME_BASE_ARENA_CONTAINERS_H_ |