blob: 82db60e4e44ce4a3562149889093d28c138f5965 [file] [log] [blame]
Vladimir Marko69f08ba2014-04-11 12:28:11 +01001/*
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 Chartierb666f482015-02-18 14:33:14 -080017#ifndef ART_RUNTIME_BASE_SCOPED_ARENA_CONTAINERS_H_
18#define ART_RUNTIME_BASE_SCOPED_ARENA_CONTAINERS_H_
Vladimir Marko69f08ba2014-04-11 12:28:11 +010019
Vladimir Marko622bdbe2014-06-19 14:59:05 +010020#include <deque>
21#include <queue>
Vladimir Marko69f08ba2014-04-11 12:28:11 +010022#include <set>
Mathieu Chartiere401d142015-04-22 13:56:20 -070023#include <unordered_map>
Vladimir Marko622bdbe2014-06-19 14:59:05 +010024#include <vector>
Vladimir Marko69f08ba2014-04-11 12:28:11 +010025
Mathieu Chartierb666f482015-02-18 14:33:14 -080026#include "arena_containers.h" // For ArenaAllocatorAdapterKind.
27#include "scoped_arena_allocator.h"
Vladimir Marko69f08ba2014-04-11 12:28:11 +010028#include "safe_map.h"
29
30namespace art {
31
Vladimir Marko8081d2b2014-07-31 15:33:43 +010032// Adapter for use of ScopedArenaAllocator in STL containers.
33// Use ScopedArenaAllocator::Adapter() to create an adapter to pass to container constructors.
34// For example,
35// void foo(ScopedArenaAllocator* allocator) {
36// ScopedArenaVector<int> foo_vector(allocator->Adapter(kArenaAllocMisc));
37// ScopedArenaSafeMap<int, int> foo_map(std::less<int>(), allocator->Adapter());
38// // Use foo_vector and foo_map...
39// }
40template <typename T>
41class ScopedArenaAllocatorAdapter;
42
Vladimir Marko69f08ba2014-04-11 12:28:11 +010043template <typename T>
Vladimir Marko622bdbe2014-06-19 14:59:05 +010044using ScopedArenaDeque = std::deque<T, ScopedArenaAllocatorAdapter<T>>;
45
46template <typename T>
47using ScopedArenaQueue = std::queue<T, ScopedArenaDeque<T>>;
48
49template <typename T>
Ian Rogers700a4022014-05-19 16:49:03 -070050using ScopedArenaVector = std::vector<T, ScopedArenaAllocatorAdapter<T>>;
Vladimir Marko69f08ba2014-04-11 12:28:11 +010051
Ian Rogers700a4022014-05-19 16:49:03 -070052template <typename T, typename Comparator = std::less<T>>
53using ScopedArenaSet = std::set<T, Comparator, ScopedArenaAllocatorAdapter<T>>;
Vladimir Marko69f08ba2014-04-11 12:28:11 +010054
Ian Rogers700a4022014-05-19 16:49:03 -070055template <typename K, typename V, typename Comparator = std::less<K>>
Vladimir Marko69f08ba2014-04-11 12:28:11 +010056using ScopedArenaSafeMap =
Ian Rogers700a4022014-05-19 16:49:03 -070057 SafeMap<K, V, Comparator, ScopedArenaAllocatorAdapter<std::pair<const K, V>>>;
Vladimir Marko69f08ba2014-04-11 12:28:11 +010058
Mathieu Chartiere401d142015-04-22 13:56:20 -070059template <typename K, typename V, class Hash = std::hash<K>, class KeyEqual = std::equal_to<K>>
60using ScopedArenaUnorderedMap =
61 std::unordered_map<K, V, Hash, KeyEqual, ScopedArenaAllocatorAdapter<std::pair<const K, V>>>;
62
63
Vladimir Marko8081d2b2014-07-31 15:33:43 +010064// Implementation details below.
65
66template <>
67class ScopedArenaAllocatorAdapter<void>
68 : private DebugStackReference, private DebugStackIndirectTopRef,
69 private ArenaAllocatorAdapterKind {
70 public:
71 typedef void value_type;
72 typedef void* pointer;
73 typedef const void* const_pointer;
74
75 template <typename U>
76 struct rebind {
77 typedef ScopedArenaAllocatorAdapter<U> other;
78 };
79
80 explicit ScopedArenaAllocatorAdapter(ScopedArenaAllocator* arena_allocator,
81 ArenaAllocKind kind = kArenaAllocSTL)
82 : DebugStackReference(arena_allocator),
83 DebugStackIndirectTopRef(arena_allocator),
84 ArenaAllocatorAdapterKind(kind),
85 arena_stack_(arena_allocator->arena_stack_) {
86 }
87 template <typename U>
88 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other)
89 : DebugStackReference(other),
90 DebugStackIndirectTopRef(other),
91 ArenaAllocatorAdapterKind(other),
92 arena_stack_(other.arena_stack_) {
93 }
Andreas Gampec801f0d2015-02-24 20:55:16 -080094 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter&) = default;
95 ScopedArenaAllocatorAdapter& operator=(const ScopedArenaAllocatorAdapter&) = default;
Vladimir Marko8081d2b2014-07-31 15:33:43 +010096 ~ScopedArenaAllocatorAdapter() = default;
97
98 private:
99 ArenaStack* arena_stack_;
100
101 template <typename U>
102 friend class ScopedArenaAllocatorAdapter;
103};
104
105template <typename T>
106class ScopedArenaAllocatorAdapter
107 : private DebugStackReference, private DebugStackIndirectTopRef,
108 private ArenaAllocatorAdapterKind {
109 public:
110 typedef T value_type;
111 typedef T* pointer;
112 typedef T& reference;
113 typedef const T* const_pointer;
114 typedef const T& const_reference;
115 typedef size_t size_type;
116 typedef ptrdiff_t difference_type;
117
118 template <typename U>
119 struct rebind {
120 typedef ScopedArenaAllocatorAdapter<U> other;
121 };
122
123 explicit ScopedArenaAllocatorAdapter(ScopedArenaAllocator* arena_allocator,
124 ArenaAllocKind kind = kArenaAllocSTL)
125 : DebugStackReference(arena_allocator),
126 DebugStackIndirectTopRef(arena_allocator),
127 ArenaAllocatorAdapterKind(kind),
128 arena_stack_(arena_allocator->arena_stack_) {
129 }
130 template <typename U>
131 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other)
132 : DebugStackReference(other),
133 DebugStackIndirectTopRef(other),
134 ArenaAllocatorAdapterKind(other),
135 arena_stack_(other.arena_stack_) {
136 }
Andreas Gampec801f0d2015-02-24 20:55:16 -0800137 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter&) = default;
138 ScopedArenaAllocatorAdapter& operator=(const ScopedArenaAllocatorAdapter&) = default;
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100139 ~ScopedArenaAllocatorAdapter() = default;
140
141 size_type max_size() const {
142 return static_cast<size_type>(-1) / sizeof(T);
143 }
144
145 pointer address(reference x) const { return &x; }
146 const_pointer address(const_reference x) const { return &x; }
147
148 pointer allocate(size_type n, ScopedArenaAllocatorAdapter<void>::pointer hint = nullptr) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700149 UNUSED(hint);
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100150 DCHECK_LE(n, max_size());
151 DebugStackIndirectTopRef::CheckTop();
152 return reinterpret_cast<T*>(arena_stack_->Alloc(n * sizeof(T),
153 ArenaAllocatorAdapterKind::Kind()));
154 }
155 void deallocate(pointer p, size_type n) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700156 UNUSED(p);
157 UNUSED(n);
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100158 DebugStackIndirectTopRef::CheckTop();
159 }
160
161 void construct(pointer p, const_reference val) {
162 // Don't CheckTop(), allow reusing existing capacity of a vector/deque below the top.
163 new (static_cast<void*>(p)) value_type(val);
164 }
165 void destroy(pointer p) {
166 // Don't CheckTop(), allow reusing existing capacity of a vector/deque below the top.
167 p->~value_type();
168 }
169
170 private:
171 ArenaStack* arena_stack_;
172
173 template <typename U>
174 friend class ScopedArenaAllocatorAdapter;
175
176 template <typename U>
177 friend bool operator==(const ScopedArenaAllocatorAdapter<U>& lhs,
178 const ScopedArenaAllocatorAdapter<U>& rhs);
179};
180
181template <typename T>
182inline bool operator==(const ScopedArenaAllocatorAdapter<T>& lhs,
183 const ScopedArenaAllocatorAdapter<T>& rhs) {
184 return lhs.arena_stack_ == rhs.arena_stack_;
185}
186
187template <typename T>
188inline bool operator!=(const ScopedArenaAllocatorAdapter<T>& lhs,
189 const ScopedArenaAllocatorAdapter<T>& rhs) {
190 return !(lhs == rhs);
191}
192
193inline ScopedArenaAllocatorAdapter<void> ScopedArenaAllocator::Adapter(ArenaAllocKind kind) {
194 return ScopedArenaAllocatorAdapter<void>(this, kind);
195}
196
Vladimir Marko69f08ba2014-04-11 12:28:11 +0100197} // namespace art
198
Mathieu Chartierb666f482015-02-18 14:33:14 -0800199#endif // ART_RUNTIME_BASE_SCOPED_ARENA_CONTAINERS_H_