blob: df79085626d9258f0d3ddf5cfa140acee1525ff0 [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>
Vladimir Marko622bdbe2014-06-19 14:59:05 +010023#include <vector>
Vladimir Marko69f08ba2014-04-11 12:28:11 +010024
Mathieu Chartierb666f482015-02-18 14:33:14 -080025#include "arena_containers.h" // For ArenaAllocatorAdapterKind.
26#include "scoped_arena_allocator.h"
Vladimir Marko69f08ba2014-04-11 12:28:11 +010027#include "safe_map.h"
28
29namespace art {
30
Vladimir Marko8081d2b2014-07-31 15:33:43 +010031// Adapter for use of ScopedArenaAllocator in STL containers.
32// Use ScopedArenaAllocator::Adapter() to create an adapter to pass to container constructors.
33// For example,
34// void foo(ScopedArenaAllocator* allocator) {
35// ScopedArenaVector<int> foo_vector(allocator->Adapter(kArenaAllocMisc));
36// ScopedArenaSafeMap<int, int> foo_map(std::less<int>(), allocator->Adapter());
37// // Use foo_vector and foo_map...
38// }
39template <typename T>
40class ScopedArenaAllocatorAdapter;
41
Vladimir Marko69f08ba2014-04-11 12:28:11 +010042template <typename T>
Vladimir Marko622bdbe2014-06-19 14:59:05 +010043using ScopedArenaDeque = std::deque<T, ScopedArenaAllocatorAdapter<T>>;
44
45template <typename T>
46using ScopedArenaQueue = std::queue<T, ScopedArenaDeque<T>>;
47
48template <typename T>
Ian Rogers700a4022014-05-19 16:49:03 -070049using ScopedArenaVector = std::vector<T, ScopedArenaAllocatorAdapter<T>>;
Vladimir Marko69f08ba2014-04-11 12:28:11 +010050
Ian Rogers700a4022014-05-19 16:49:03 -070051template <typename T, typename Comparator = std::less<T>>
52using ScopedArenaSet = std::set<T, Comparator, ScopedArenaAllocatorAdapter<T>>;
Vladimir Marko69f08ba2014-04-11 12:28:11 +010053
Ian Rogers700a4022014-05-19 16:49:03 -070054template <typename K, typename V, typename Comparator = std::less<K>>
Vladimir Marko69f08ba2014-04-11 12:28:11 +010055using ScopedArenaSafeMap =
Ian Rogers700a4022014-05-19 16:49:03 -070056 SafeMap<K, V, Comparator, ScopedArenaAllocatorAdapter<std::pair<const K, V>>>;
Vladimir Marko69f08ba2014-04-11 12:28:11 +010057
Vladimir Marko8081d2b2014-07-31 15:33:43 +010058// Implementation details below.
59
60template <>
61class ScopedArenaAllocatorAdapter<void>
62 : private DebugStackReference, private DebugStackIndirectTopRef,
63 private ArenaAllocatorAdapterKind {
64 public:
65 typedef void value_type;
66 typedef void* pointer;
67 typedef const void* const_pointer;
68
69 template <typename U>
70 struct rebind {
71 typedef ScopedArenaAllocatorAdapter<U> other;
72 };
73
74 explicit ScopedArenaAllocatorAdapter(ScopedArenaAllocator* arena_allocator,
75 ArenaAllocKind kind = kArenaAllocSTL)
76 : DebugStackReference(arena_allocator),
77 DebugStackIndirectTopRef(arena_allocator),
78 ArenaAllocatorAdapterKind(kind),
79 arena_stack_(arena_allocator->arena_stack_) {
80 }
81 template <typename U>
82 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other)
83 : DebugStackReference(other),
84 DebugStackIndirectTopRef(other),
85 ArenaAllocatorAdapterKind(other),
86 arena_stack_(other.arena_stack_) {
87 }
Andreas Gampec801f0d2015-02-24 20:55:16 -080088 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter&) = default;
89 ScopedArenaAllocatorAdapter& operator=(const ScopedArenaAllocatorAdapter&) = default;
Vladimir Marko8081d2b2014-07-31 15:33:43 +010090 ~ScopedArenaAllocatorAdapter() = default;
91
92 private:
93 ArenaStack* arena_stack_;
94
95 template <typename U>
96 friend class ScopedArenaAllocatorAdapter;
97};
98
99template <typename T>
100class ScopedArenaAllocatorAdapter
101 : private DebugStackReference, private DebugStackIndirectTopRef,
102 private ArenaAllocatorAdapterKind {
103 public:
104 typedef T value_type;
105 typedef T* pointer;
106 typedef T& reference;
107 typedef const T* const_pointer;
108 typedef const T& const_reference;
109 typedef size_t size_type;
110 typedef ptrdiff_t difference_type;
111
112 template <typename U>
113 struct rebind {
114 typedef ScopedArenaAllocatorAdapter<U> other;
115 };
116
117 explicit ScopedArenaAllocatorAdapter(ScopedArenaAllocator* arena_allocator,
118 ArenaAllocKind kind = kArenaAllocSTL)
119 : DebugStackReference(arena_allocator),
120 DebugStackIndirectTopRef(arena_allocator),
121 ArenaAllocatorAdapterKind(kind),
122 arena_stack_(arena_allocator->arena_stack_) {
123 }
124 template <typename U>
125 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other)
126 : DebugStackReference(other),
127 DebugStackIndirectTopRef(other),
128 ArenaAllocatorAdapterKind(other),
129 arena_stack_(other.arena_stack_) {
130 }
Andreas Gampec801f0d2015-02-24 20:55:16 -0800131 ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter&) = default;
132 ScopedArenaAllocatorAdapter& operator=(const ScopedArenaAllocatorAdapter&) = default;
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100133 ~ScopedArenaAllocatorAdapter() = default;
134
135 size_type max_size() const {
136 return static_cast<size_type>(-1) / sizeof(T);
137 }
138
139 pointer address(reference x) const { return &x; }
140 const_pointer address(const_reference x) const { return &x; }
141
142 pointer allocate(size_type n, ScopedArenaAllocatorAdapter<void>::pointer hint = nullptr) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700143 UNUSED(hint);
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100144 DCHECK_LE(n, max_size());
145 DebugStackIndirectTopRef::CheckTop();
146 return reinterpret_cast<T*>(arena_stack_->Alloc(n * sizeof(T),
147 ArenaAllocatorAdapterKind::Kind()));
148 }
149 void deallocate(pointer p, size_type n) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700150 UNUSED(p);
151 UNUSED(n);
Vladimir Marko8081d2b2014-07-31 15:33:43 +0100152 DebugStackIndirectTopRef::CheckTop();
153 }
154
155 void construct(pointer p, const_reference val) {
156 // Don't CheckTop(), allow reusing existing capacity of a vector/deque below the top.
157 new (static_cast<void*>(p)) value_type(val);
158 }
159 void destroy(pointer p) {
160 // Don't CheckTop(), allow reusing existing capacity of a vector/deque below the top.
161 p->~value_type();
162 }
163
164 private:
165 ArenaStack* arena_stack_;
166
167 template <typename U>
168 friend class ScopedArenaAllocatorAdapter;
169
170 template <typename U>
171 friend bool operator==(const ScopedArenaAllocatorAdapter<U>& lhs,
172 const ScopedArenaAllocatorAdapter<U>& rhs);
173};
174
175template <typename T>
176inline bool operator==(const ScopedArenaAllocatorAdapter<T>& lhs,
177 const ScopedArenaAllocatorAdapter<T>& rhs) {
178 return lhs.arena_stack_ == rhs.arena_stack_;
179}
180
181template <typename T>
182inline bool operator!=(const ScopedArenaAllocatorAdapter<T>& lhs,
183 const ScopedArenaAllocatorAdapter<T>& rhs) {
184 return !(lhs == rhs);
185}
186
187inline ScopedArenaAllocatorAdapter<void> ScopedArenaAllocator::Adapter(ArenaAllocKind kind) {
188 return ScopedArenaAllocatorAdapter<void>(this, kind);
189}
190
Vladimir Marko69f08ba2014-04-11 12:28:11 +0100191} // namespace art
192
Mathieu Chartierb666f482015-02-18 14:33:14 -0800193#endif // ART_RUNTIME_BASE_SCOPED_ARENA_CONTAINERS_H_