diff options
author | 2014-07-31 15:33:43 +0100 | |
---|---|---|
committer | 2014-08-05 09:45:24 +0100 | |
commit | 8081d2b8d7a743729557051d0294e040e61c747a (patch) | |
tree | 37c2bd8616831df3a0f9f2df0932ebb517dbcd3d | |
parent | 8f1dc7a20049ba6e2f3c7f800908bff811cdbea1 (diff) |
Create allocator adapter for using Arena in std containers.
Create ArenaAllocatorAdapter, similar to the existing
ScopedArenaAllocatorAdapter, for allocating memory for
standard containers via the ArenaAllocator. Add the ability
to specify allocation kind rather than just kArenaAllocSTL
to both adapters. Move the scoped arena allocator to the
scoped_arena_containers.h header file.
Define template aliases for containers using the new adapter
and change a few MIRGraph and Mir2Lir members to use them.
Change-Id: I9bbc50248e0fed81729497b848cb29bf68444268
-rw-r--r-- | compiler/dex/mir_graph.cc | 4 | ||||
-rw-r--r-- | compiler/dex/mir_graph.h | 11 | ||||
-rw-r--r-- | compiler/dex/quick/codegen_util.cc | 2 | ||||
-rw-r--r-- | compiler/dex/quick/mir_to_lir.h | 5 | ||||
-rw-r--r-- | compiler/utils/arena_allocator.h | 12 | ||||
-rw-r--r-- | compiler/utils/arena_containers.h | 205 | ||||
-rw-r--r-- | compiler/utils/scoped_arena_allocator.h | 123 | ||||
-rw-r--r-- | compiler/utils/scoped_arena_containers.h | 142 |
8 files changed, 375 insertions, 129 deletions
diff --git a/compiler/dex/mir_graph.cc b/compiler/dex/mir_graph.cc index 8d0a5a34bf..6aee56373c 100644 --- a/compiler/dex/mir_graph.cc +++ b/compiler/dex/mir_graph.cc @@ -69,6 +69,7 @@ const char* MIRGraph::extended_mir_op_names_[kMirOpLast - kMirOpFirst] = { MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena) : reg_location_(NULL), + block_id_map_(std::less<unsigned int>(), arena->Adapter()), cu_(cu), ssa_base_vregs_(NULL), ssa_subscripts_(NULL), @@ -101,11 +102,14 @@ MIRGraph::MIRGraph(CompilationUnit* cu, ArenaAllocator* arena) num_blocks_(0), current_code_item_(NULL), dex_pc_to_block_map_(arena, 0, kGrowableArrayMisc), + m_units_(arena->Adapter()), + method_stack_(arena->Adapter()), current_method_(kInvalidEntry), current_offset_(kInvalidEntry), def_count_(0), opcode_count_(NULL), num_ssa_regs_(0), + extended_basic_blocks_(arena->Adapter()), method_sreg_(0), attributes_(METHOD_IS_LEAF), // Start with leaf assumption, change on encountering invoke. checkstats_(NULL), diff --git a/compiler/dex/mir_graph.h b/compiler/dex/mir_graph.h index 768ae21c28..491d72e9e8 100644 --- a/compiler/dex/mir_graph.h +++ b/compiler/dex/mir_graph.h @@ -27,6 +27,7 @@ #include "mir_method_info.h" #include "utils/arena_bit_vector.h" #include "utils/growable_array.h" +#include "utils/arena_containers.h" #include "utils/scoped_arena_containers.h" #include "reg_location.h" #include "reg_storage.h" @@ -1051,8 +1052,8 @@ class MIRGraph { std::set<uint32_t> catches_; // TODO: make these private. - RegLocation* reg_location_; // Map SSA names to location. - SafeMap<unsigned int, unsigned int> block_id_map_; // Block collapse lookup cache. + RegLocation* reg_location_; // Map SSA names to location. + ArenaSafeMap<unsigned int, unsigned int> block_id_map_; // Block collapse lookup cache. static const char* extended_mir_op_names_[kMirOpLast - kMirOpFirst]; static const uint32_t analysis_attributes_[kMirOpLast]; @@ -1171,15 +1172,15 @@ class MIRGraph { unsigned int num_blocks_; const DexFile::CodeItem* current_code_item_; GrowableArray<uint16_t> dex_pc_to_block_map_; // FindBlock lookup cache. - std::vector<DexCompilationUnit*> m_units_; // List of methods included in this graph + ArenaVector<DexCompilationUnit*> m_units_; // List of methods included in this graph typedef std::pair<int, int> MIRLocation; // Insert point, (m_unit_ index, offset) - std::vector<MIRLocation> method_stack_; // Include stack + ArenaVector<MIRLocation> method_stack_; // Include stack int current_method_; DexOffset current_offset_; // Offset in code units int def_count_; // Used to estimate size of ssa name storage. int* opcode_count_; // Dex opcode coverage stats. int num_ssa_regs_; // Number of names following SSA transformation. - std::vector<BasicBlockId> extended_basic_blocks_; // Heads of block "traces". + ArenaVector<BasicBlockId> extended_basic_blocks_; // Heads of block "traces". int method_sreg_; unsigned int attributes_; Checkstats* checkstats_; diff --git a/compiler/dex/quick/codegen_util.cc b/compiler/dex/quick/codegen_util.cc index 463f277e54..2a51b496a3 100644 --- a/compiler/dex/quick/codegen_util.cc +++ b/compiler/dex/quick/codegen_util.cc @@ -983,6 +983,8 @@ Mir2Lir::Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena estimated_native_code_size_(0), reg_pool_(NULL), live_sreg_(0), + core_vmap_table_(mir_graph->GetArena()->Adapter()), + fp_vmap_table_(mir_graph->GetArena()->Adapter()), num_core_spills_(0), num_fp_spills_(0), frame_size_(0), diff --git a/compiler/dex/quick/mir_to_lir.h b/compiler/dex/quick/mir_to_lir.h index 981b1405ac..4ed9929338 100644 --- a/compiler/dex/quick/mir_to_lir.h +++ b/compiler/dex/quick/mir_to_lir.h @@ -32,6 +32,7 @@ #include "safe_map.h" #include "utils/array_ref.h" #include "utils/arena_allocator.h" +#include "utils/arena_containers.h" #include "utils/growable_array.h" #include "utils/stack_checks.h" @@ -1699,8 +1700,8 @@ class Mir2Lir : public Backend { CodeBuffer code_buffer_; // The encoding mapping table data (dex -> pc offset and pc offset -> dex) with a size prefix. std::vector<uint8_t> encoded_mapping_table_; - std::vector<uint32_t> core_vmap_table_; - std::vector<uint32_t> fp_vmap_table_; + ArenaVector<uint32_t> core_vmap_table_; + ArenaVector<uint32_t> fp_vmap_table_; std::vector<uint8_t> native_gc_map_; int num_core_spills_; int num_fp_spills_; diff --git a/compiler/utils/arena_allocator.h b/compiler/utils/arena_allocator.h index f4bcb1d44d..7bfbb6f93b 100644 --- a/compiler/utils/arena_allocator.h +++ b/compiler/utils/arena_allocator.h @@ -24,6 +24,7 @@ #include "base/mutex.h" #include "mem_map.h" #include "utils.h" +#include "utils/debug_stack.h" namespace art { @@ -34,6 +35,9 @@ class ArenaStack; class ScopedArenaAllocator; class MemStats; +template <typename T> +class ArenaAllocatorAdapter; + static constexpr bool kArenaAllocatorCountAllocations = false; // Type of allocation for memory tuning. @@ -147,11 +151,14 @@ class ArenaPool { DISALLOW_COPY_AND_ASSIGN(ArenaPool); }; -class ArenaAllocator : private ArenaAllocatorStats { +class ArenaAllocator : private DebugStackRefCounter, private ArenaAllocatorStats { public: explicit ArenaAllocator(ArenaPool* pool); ~ArenaAllocator(); + // Get adapter for use in STL containers. See arena_containers.h . + ArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL); + // Returns zeroed memory. void* Alloc(size_t bytes, ArenaAllocKind kind) ALWAYS_INLINE { if (UNLIKELY(running_on_valgrind_)) { @@ -190,6 +197,9 @@ class ArenaAllocator : private ArenaAllocatorStats { Arena* arena_head_; bool running_on_valgrind_; + template <typename U> + friend class ArenaAllocatorAdapter; + DISALLOW_COPY_AND_ASSIGN(ArenaAllocator); }; // ArenaAllocator diff --git a/compiler/utils/arena_containers.h b/compiler/utils/arena_containers.h new file mode 100644 index 0000000000..c48b0c81ec --- /dev/null +++ b/compiler/utils/arena_containers.h @@ -0,0 +1,205 @@ +/* + * 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_ARENA_CONTAINERS_H_ +#define ART_COMPILER_UTILS_ARENA_CONTAINERS_H_ + +#include <deque> +#include <queue> +#include <set> +#include <vector> + +#include "utils/arena_allocator.h" +#include "safe_map.h" + +namespace art { + +// Adapter for use of ArenaAllocator in STL containers. +// Use ArenaAllocator::Adapter() to create an adapter to pass to container constructors. +// For example, +// struct Foo { +// explicit Foo(ArenaAllocator* allocator) +// : foo_vector(allocator->Adapter(kArenaAllocMisc)), +// foo_map(std::less<int>(), allocator->Adapter()) { +// } +// ArenaVector<int> foo_vector; +// ArenaSafeMap<int, int> foo_map; +// }; +template <typename T> +class ArenaAllocatorAdapter; + +template <typename T> +using ArenaDeque = std::deque<T, ArenaAllocatorAdapter<T>>; + +template <typename T> +using ArenaQueue = std::queue<T, ArenaDeque<T>>; + +template <typename T> +using ArenaVector = std::vector<T, ArenaAllocatorAdapter<T>>; + +template <typename T, typename Comparator = std::less<T>> +using ArenaSet = std::set<T, Comparator, ArenaAllocatorAdapter<T>>; + +template <typename K, typename V, typename Comparator = std::less<K>> +using ArenaSafeMap = + SafeMap<K, V, Comparator, ArenaAllocatorAdapter<std::pair<const K, V>>>; + +// Implementation details below. + +template <bool kCount> +class ArenaAllocatorAdapterKindImpl; + +template <> +class ArenaAllocatorAdapterKindImpl<false> { + public: + // Not tracking allocations, ignore the supplied kind and arbitrarily provide kArenaAllocSTL. + explicit ArenaAllocatorAdapterKindImpl(ArenaAllocKind kind) { } + ArenaAllocatorAdapterKindImpl& operator=(const ArenaAllocatorAdapterKindImpl& other) = default; + ArenaAllocKind Kind() { return kArenaAllocSTL; } +}; + +template <bool kCount> +class ArenaAllocatorAdapterKindImpl { + public: + explicit ArenaAllocatorAdapterKindImpl(ArenaAllocKind kind) : kind_(kind) { } + ArenaAllocatorAdapterKindImpl& operator=(const ArenaAllocatorAdapterKindImpl& other) = default; + ArenaAllocKind Kind() { return kind_; } + + private: + ArenaAllocKind kind_; +}; + +typedef ArenaAllocatorAdapterKindImpl<kArenaAllocatorCountAllocations> ArenaAllocatorAdapterKind; + +template <> +class ArenaAllocatorAdapter<void> + : private DebugStackReference, private ArenaAllocatorAdapterKind { + public: + typedef void value_type; + typedef void* pointer; + typedef const void* const_pointer; + + template <typename U> + struct rebind { + typedef ArenaAllocatorAdapter<U> other; + }; + + explicit ArenaAllocatorAdapter(ArenaAllocator* arena_allocator, + ArenaAllocKind kind = kArenaAllocSTL) + : DebugStackReference(arena_allocator), + ArenaAllocatorAdapterKind(kind), + arena_allocator_(arena_allocator) { + } + template <typename U> + ArenaAllocatorAdapter(const ArenaAllocatorAdapter<U>& other) + : DebugStackReference(other), + ArenaAllocatorAdapterKind(other), + arena_allocator_(other.arena_allocator_) { + } + ArenaAllocatorAdapter(const ArenaAllocatorAdapter& other) = default; + ArenaAllocatorAdapter& operator=(const ArenaAllocatorAdapter& other) = default; + ~ArenaAllocatorAdapter() = default; + + private: + ArenaAllocator* arena_allocator_; + + template <typename U> + friend class ArenaAllocatorAdapter; +}; + +template <typename T> +class ArenaAllocatorAdapter : private DebugStackReference, 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 ArenaAllocatorAdapter<U> other; + }; + + explicit ArenaAllocatorAdapter(ArenaAllocator* arena_allocator, ArenaAllocKind kind) + : DebugStackReference(arena_allocator), + ArenaAllocatorAdapterKind(kind), + arena_allocator_(arena_allocator) { + } + template <typename U> + ArenaAllocatorAdapter(const ArenaAllocatorAdapter<U>& other) + : DebugStackReference(other), + ArenaAllocatorAdapterKind(other), + arena_allocator_(other.arena_allocator_) { + } + ArenaAllocatorAdapter(const ArenaAllocatorAdapter& other) = default; + ArenaAllocatorAdapter& operator=(const ArenaAllocatorAdapter& other) = default; + ~ArenaAllocatorAdapter() = 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, ArenaAllocatorAdapter<void>::pointer hint = nullptr) { + DCHECK_LE(n, max_size()); + return reinterpret_cast<T*>(arena_allocator_->Alloc(n * sizeof(T), + ArenaAllocatorAdapterKind::Kind())); + } + void deallocate(pointer p, size_type n) { + } + + void construct(pointer p, const_reference val) { + new (static_cast<void*>(p)) value_type(val); + } + void destroy(pointer p) { + p->~value_type(); + } + + private: + ArenaAllocator* arena_allocator_; + + template <typename U> + friend class ArenaAllocatorAdapter; + + template <typename U> + friend bool operator==(const ArenaAllocatorAdapter<U>& lhs, + const ArenaAllocatorAdapter<U>& rhs); +}; + +template <typename T> +inline bool operator==(const ArenaAllocatorAdapter<T>& lhs, + const ArenaAllocatorAdapter<T>& rhs) { + return lhs.arena_allocator_ == rhs.arena_allocator_; +} + +template <typename T> +inline bool operator!=(const ArenaAllocatorAdapter<T>& lhs, + const ArenaAllocatorAdapter<T>& rhs) { + return !(lhs == rhs); +} + +inline ArenaAllocatorAdapter<void> ArenaAllocator::Adapter(ArenaAllocKind kind) { + return ArenaAllocatorAdapter<void>(this, kind); +} + +} // namespace art + +#endif // ART_COMPILER_UTILS_ARENA_CONTAINERS_H_ diff --git a/compiler/utils/scoped_arena_allocator.h b/compiler/utils/scoped_arena_allocator.h index 9f33f2d804..62ea3302a3 100644 --- a/compiler/utils/scoped_arena_allocator.h +++ b/compiler/utils/scoped_arena_allocator.h @@ -120,8 +120,8 @@ class ScopedArenaAllocator return arena_stack_->Alloc(bytes, kind); } - // ScopedArenaAllocatorAdapter is incomplete here, we need to define this later. - ScopedArenaAllocatorAdapter<void> Adapter(); + // Get adapter for use in STL containers. See scoped_arena_containers.h . + ScopedArenaAllocatorAdapter<void> Adapter(ArenaAllocKind kind = kArenaAllocSTL); // Allow a delete-expression to destroy but not deallocate allocators created by Create(). static void operator delete(void* ptr) { UNUSED(ptr); } @@ -138,125 +138,6 @@ class ScopedArenaAllocator DISALLOW_COPY_AND_ASSIGN(ScopedArenaAllocator); }; -template <> -class ScopedArenaAllocatorAdapter<void> - : private DebugStackReference, private DebugStackIndirectTopRef { - 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) - : DebugStackReference(arena_allocator), - DebugStackIndirectTopRef(arena_allocator), - arena_stack_(arena_allocator->arena_stack_) { - } - template <typename U> - ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other) - : DebugStackReference(other), - DebugStackIndirectTopRef(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; -}; - -// Adapter for use of ScopedArenaAllocator in STL containers. -template <typename T> -class ScopedArenaAllocatorAdapter : private DebugStackReference, private DebugStackIndirectTopRef { - 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) - : DebugStackReference(arena_allocator), - DebugStackIndirectTopRef(arena_allocator), - arena_stack_(arena_allocator->arena_stack_) { - } - template <typename U> - ScopedArenaAllocatorAdapter(const ScopedArenaAllocatorAdapter<U>& other) - : DebugStackReference(other), - DebugStackIndirectTopRef(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), kArenaAllocSTL)); - } - 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() { - return ScopedArenaAllocatorAdapter<void>(this); -} - } // namespace art #endif // ART_COMPILER_UTILS_SCOPED_ARENA_ALLOCATOR_H_ diff --git a/compiler/utils/scoped_arena_containers.h b/compiler/utils/scoped_arena_containers.h index 67285658e2..0de7403c07 100644 --- a/compiler/utils/scoped_arena_containers.h +++ b/compiler/utils/scoped_arena_containers.h @@ -22,11 +22,23 @@ #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>>; @@ -43,6 +55,136 @@ 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_ |