1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_COMPILER_UTILS_SCOPED_ARENA_CONTAINERS_H_
#define ART_COMPILER_UTILS_SCOPED_ARENA_CONTAINERS_H_
#include <deque>
#include <queue>
#include <set>
#include <vector>
#include "utils/arena_containers.h" // For ArenaAllocatorAdapterKind.
#include "utils/scoped_arena_allocator.h"
#include "safe_map.h"
namespace art {
// Adapter for use of ScopedArenaAllocator in STL containers.
// Use ScopedArenaAllocator::Adapter() to create an adapter to pass to container constructors.
// For example,
// void foo(ScopedArenaAllocator* allocator) {
// ScopedArenaVector<int> foo_vector(allocator->Adapter(kArenaAllocMisc));
// ScopedArenaSafeMap<int, int> foo_map(std::less<int>(), allocator->Adapter());
// // Use foo_vector and foo_map...
// }
template <typename T>
class ScopedArenaAllocatorAdapter;
template <typename T>
using ScopedArenaDeque = std::deque<T, ScopedArenaAllocatorAdapter<T>>;
template <typename T>
using ScopedArenaQueue = std::queue<T, ScopedArenaDeque<T>>;
template <typename T>
using ScopedArenaVector = std::vector<T, ScopedArenaAllocatorAdapter<T>>;
template <typename T, typename Comparator = std::less<T>>
using ScopedArenaSet = std::set<T, Comparator, ScopedArenaAllocatorAdapter<T>>;
template <typename K, typename V, typename Comparator = std::less<K>>
using ScopedArenaSafeMap =
SafeMap<K, V, Comparator, ScopedArenaAllocatorAdapter<std::pair<const K, V>>>;
// Implementation details below.
template <>
class ScopedArenaAllocatorAdapter<void>
: private DebugStackReference, private DebugStackIndirectTopRef,
private ArenaAllocatorAdapterKind {
public:
typedef void value_type;
typedef void* pointer;
typedef const void* const_pointer;
template <typename U>
struct rebind {
typedef ScopedArenaAllocatorAdapter<U> other;
};
explicit ScopedArenaAllocatorAdapter(ScopedArenaAllocator* arena_allocator,
ArenaAllocKind kind = kArenaAllocSTL)
: DebugStackReference(arena_allocator),
DebugStackIndirectTopRef(arena_allocator),
ArenaAllocatorAdapterKind(kind),
arena_stack_(arena_allocator->arena_stack_) {
}
template <typename U>
ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other)
: DebugStackReference(other),
DebugStackIndirectTopRef(other),
ArenaAllocatorAdapterKind(other),
arena_stack_(other.arena_stack_) {
}
ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter& other) = default;
ScopedArenaAllocatorAdapter& operator=(const ScopedArenaAllocatorAdapter& other) = default;
~ScopedArenaAllocatorAdapter() = default;
private:
ArenaStack* arena_stack_;
template <typename U>
friend class ScopedArenaAllocatorAdapter;
};
template <typename T>
class ScopedArenaAllocatorAdapter
: private DebugStackReference, private DebugStackIndirectTopRef,
private ArenaAllocatorAdapterKind {
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T* const_pointer;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
template <typename U>
struct rebind {
typedef ScopedArenaAllocatorAdapter<U> other;
};
explicit ScopedArenaAllocatorAdapter(ScopedArenaAllocator* arena_allocator,
ArenaAllocKind kind = kArenaAllocSTL)
: DebugStackReference(arena_allocator),
DebugStackIndirectTopRef(arena_allocator),
ArenaAllocatorAdapterKind(kind),
arena_stack_(arena_allocator->arena_stack_) {
}
template <typename U>
ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other)
: DebugStackReference(other),
DebugStackIndirectTopRef(other),
ArenaAllocatorAdapterKind(other),
arena_stack_(other.arena_stack_) {
}
ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter& other) = default;
ScopedArenaAllocatorAdapter& operator=(const ScopedArenaAllocatorAdapter& other) = default;
~ScopedArenaAllocatorAdapter() = default;
size_type max_size() const {
return static_cast<size_type>(-1) / sizeof(T);
}
pointer address(reference x) const { return &x; }
const_pointer address(const_reference x) const { return &x; }
pointer allocate(size_type n, ScopedArenaAllocatorAdapter<void>::pointer hint = nullptr) {
DCHECK_LE(n, max_size());
DebugStackIndirectTopRef::CheckTop();
return reinterpret_cast<T*>(arena_stack_->Alloc(n * sizeof(T),
ArenaAllocatorAdapterKind::Kind()));
}
void deallocate(pointer p, size_type n) {
DebugStackIndirectTopRef::CheckTop();
}
void construct(pointer p, const_reference val) {
// Don't CheckTop(), allow reusing existing capacity of a vector/deque below the top.
new (static_cast<void*>(p)) value_type(val);
}
void destroy(pointer p) {
// Don't CheckTop(), allow reusing existing capacity of a vector/deque below the top.
p->~value_type();
}
private:
ArenaStack* arena_stack_;
template <typename U>
friend class ScopedArenaAllocatorAdapter;
template <typename U>
friend bool operator==(const ScopedArenaAllocatorAdapter<U>& lhs,
const ScopedArenaAllocatorAdapter<U>& rhs);
};
template <typename T>
inline bool operator==(const ScopedArenaAllocatorAdapter<T>& lhs,
const ScopedArenaAllocatorAdapter<T>& rhs) {
return lhs.arena_stack_ == rhs.arena_stack_;
}
template <typename T>
inline bool operator!=(const ScopedArenaAllocatorAdapter<T>& lhs,
const ScopedArenaAllocatorAdapter<T>& rhs) {
return !(lhs == rhs);
}
inline ScopedArenaAllocatorAdapter<void> ScopedArenaAllocator::Adapter(ArenaAllocKind kind) {
return ScopedArenaAllocatorAdapter<void>(this, kind);
}
} // namespace art
#endif // ART_COMPILER_UTILS_SCOPED_ARENA_CONTAINERS_H_
|