diff options
author | 2017-06-08 14:11:18 -0700 | |
---|---|---|
committer | 2017-06-08 14:11:18 -0700 | |
commit | 5678db5b3a0275d04bc610236f89fac9f76b5b1e (patch) | |
tree | efc4ffe5d59a0c6c5f4c15a886459962d24de4aa | |
parent | 83b140474aa1759739c8ee4464bf226c4fa0f6d7 (diff) |
ART: Refactor bit_utils and stl_util
Move iterator code from bit_utils.h into bit_utils_iterator.h. Move
Identity into stl_util_identity.h. Remove now unnecessary includes,
and fix up transitive users.
Test: m
Change-Id: Id1ce9cda66827c5d00584f39ed310b6b37629906
31 files changed, 185 insertions, 102 deletions
diff --git a/compiler/image_test.h b/compiler/image_test.h index 394b7f1818..026e9e15bf 100644 --- a/compiler/image_test.h +++ b/compiler/image_test.h @@ -26,6 +26,7 @@ #include "android-base/stringprintf.h" #include "art_method-inl.h" +#include "base/stl_util.h" #include "base/unix_file/fd_file.h" #include "class_linker-inl.h" #include "compiler_callbacks.h" diff --git a/compiler/linker/arm/relative_patcher_arm_base.cc b/compiler/linker/arm/relative_patcher_arm_base.cc index c1ac230d43..18ff1c9bb6 100644 --- a/compiler/linker/arm/relative_patcher_arm_base.cc +++ b/compiler/linker/arm/relative_patcher_arm_base.cc @@ -16,6 +16,7 @@ #include "linker/arm/relative_patcher_arm_base.h" +#include "base/stl_util.h" #include "compiled_method.h" #include "linker/output_stream.h" #include "oat.h" diff --git a/compiler/oat_test.cc b/compiler/oat_test.cc index fdb21e4dba..84ec8b3ee1 100644 --- a/compiler/oat_test.cc +++ b/compiler/oat_test.cc @@ -19,6 +19,7 @@ #include "arch/instruction_set_features.h" #include "art_method-inl.h" #include "base/enums.h" +#include "base/stl_util.h" #include "base/unix_file/fd_file.h" #include "class_linker.h" #include "common_compiler_test.h" diff --git a/compiler/optimizing/code_generator.cc b/compiler/optimizing/code_generator.cc index 136401898c..93234f9630 100644 --- a/compiler/optimizing/code_generator.cc +++ b/compiler/optimizing/code_generator.cc @@ -41,6 +41,8 @@ #include "code_generator_mips64.h" #endif +#include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "bytecode_utils.h" #include "class_linker.h" #include "compiled_method.h" diff --git a/compiler/optimizing/code_generator_arm.cc b/compiler/optimizing/code_generator_arm.cc index 804bc0f1ff..e4efbef394 100644 --- a/compiler/optimizing/code_generator_arm.cc +++ b/compiler/optimizing/code_generator_arm.cc @@ -19,6 +19,8 @@ #include "arch/arm/asm_support_arm.h" #include "arch/arm/instruction_set_features_arm.h" #include "art_method.h" +#include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "code_generator_utils.h" #include "common_arm.h" #include "compiled_method.h" diff --git a/compiler/optimizing/code_generator_arm64.cc b/compiler/optimizing/code_generator_arm64.cc index 9ba38e577f..34397e66bc 100644 --- a/compiler/optimizing/code_generator_arm64.cc +++ b/compiler/optimizing/code_generator_arm64.cc @@ -19,6 +19,8 @@ #include "arch/arm64/asm_support_arm64.h" #include "arch/arm64/instruction_set_features_arm64.h" #include "art_method.h" +#include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "code_generator_utils.h" #include "compiled_method.h" #include "entrypoints/quick/quick_entrypoints.h" diff --git a/compiler/optimizing/code_generator_arm_vixl.cc b/compiler/optimizing/code_generator_arm_vixl.cc index 9cd776139b..c6bd871bc5 100644 --- a/compiler/optimizing/code_generator_arm_vixl.cc +++ b/compiler/optimizing/code_generator_arm_vixl.cc @@ -19,6 +19,8 @@ #include "arch/arm/asm_support_arm.h" #include "arch/arm/instruction_set_features_arm.h" #include "art_method.h" +#include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "code_generator_utils.h" #include "common_arm.h" #include "compiled_method.h" diff --git a/compiler/utils/arm/assembler_arm.h b/compiler/utils/arm/assembler_arm.h index 0f24e81be2..bb23a29064 100644 --- a/compiler/utils/arm/assembler_arm.h +++ b/compiler/utils/arm/assembler_arm.h @@ -25,7 +25,7 @@ #include "base/bit_utils.h" #include "base/enums.h" #include "base/logging.h" -#include "base/stl_util.h" +#include "base/stl_util_identity.h" #include "base/value_object.h" #include "constants_arm.h" #include "utils/arm/assembler_arm_shared.h" diff --git a/compiler/utils/arm/assembler_arm_vixl.cc b/compiler/utils/arm/assembler_arm_vixl.cc index 6afc3ddecb..eb3f870432 100644 --- a/compiler/utils/arm/assembler_arm_vixl.cc +++ b/compiler/utils/arm/assembler_arm_vixl.cc @@ -18,6 +18,8 @@ #include <type_traits> #include "assembler_arm_vixl.h" +#include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "entrypoints/quick/quick_entrypoints.h" #include "thread.h" diff --git a/compiler/utils/mips/assembler_mips.h b/compiler/utils/mips/assembler_mips.h index 463daeb5d7..3b262cb329 100644 --- a/compiler/utils/mips/assembler_mips.h +++ b/compiler/utils/mips/assembler_mips.h @@ -25,6 +25,7 @@ #include "base/arena_containers.h" #include "base/enums.h" #include "base/macros.h" +#include "base/stl_util_identity.h" #include "constants_mips.h" #include "globals.h" #include "managed_register_mips.h" diff --git a/compiler/utils/mips64/assembler_mips64.h b/compiler/utils/mips64/assembler_mips64.h index b212958ddb..773db9b208 100644 --- a/compiler/utils/mips64/assembler_mips64.h +++ b/compiler/utils/mips64/assembler_mips64.h @@ -25,6 +25,7 @@ #include "base/arena_containers.h" #include "base/enums.h" #include "base/macros.h" +#include "base/stl_util_identity.h" #include "constants_mips64.h" #include "globals.h" #include "managed_register_mips64.h" diff --git a/dexlayout/dex_ir.h b/dexlayout/dex_ir.h index 5692eb2b39..95e64bf3e7 100644 --- a/dexlayout/dex_ir.h +++ b/dexlayout/dex_ir.h @@ -23,6 +23,7 @@ #include <vector> #include <stdint.h> +#include "base/stl_util.h" #include "dex_file-inl.h" #include "leb128.h" #include "utf.h" diff --git a/runtime/arch/arm/context_arm.cc b/runtime/arch/arm/context_arm.cc index 817dcf5783..0db14fb8a5 100644 --- a/runtime/arch/arm/context_arm.cc +++ b/runtime/arch/arm/context_arm.cc @@ -17,6 +17,7 @@ #include "context_arm.h" #include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "quick/quick_method_frame_info.h" #include "thread-current-inl.h" diff --git a/runtime/arch/arm64/context_arm64.cc b/runtime/arch/arm64/context_arm64.cc index a8f034eaf4..0465c1e79d 100644 --- a/runtime/arch/arm64/context_arm64.cc +++ b/runtime/arch/arm64/context_arm64.cc @@ -19,6 +19,7 @@ #include "context_arm64.h" #include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "quick/quick_method_frame_info.h" #include "thread-current-inl.h" diff --git a/runtime/arch/mips/context_mips.cc b/runtime/arch/mips/context_mips.cc index 98ed5e60e6..ca1de0ae2a 100644 --- a/runtime/arch/mips/context_mips.cc +++ b/runtime/arch/mips/context_mips.cc @@ -17,6 +17,7 @@ #include "context_mips.h" #include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "quick/quick_method_frame_info.h" namespace art { diff --git a/runtime/arch/mips64/context_mips64.cc b/runtime/arch/mips64/context_mips64.cc index bd1ac3b0a7..b14908fa04 100644 --- a/runtime/arch/mips64/context_mips64.cc +++ b/runtime/arch/mips64/context_mips64.cc @@ -17,6 +17,7 @@ #include "context_mips64.h" #include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "quick/quick_method_frame_info.h" namespace art { diff --git a/runtime/arch/x86/context_x86.cc b/runtime/arch/x86/context_x86.cc index cb3dfec5f5..5c3171299c 100644 --- a/runtime/arch/x86/context_x86.cc +++ b/runtime/arch/x86/context_x86.cc @@ -17,6 +17,7 @@ #include "context_x86.h" #include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "base/memory_tool.h" #include "quick/quick_method_frame_info.h" diff --git a/runtime/arch/x86_64/context_x86_64.cc b/runtime/arch/x86_64/context_x86_64.cc index 7c49e9c2b2..a4db223b1a 100644 --- a/runtime/arch/x86_64/context_x86_64.cc +++ b/runtime/arch/x86_64/context_x86_64.cc @@ -17,6 +17,7 @@ #include "context_x86_64.h" #include "base/bit_utils.h" +#include "base/bit_utils_iterator.h" #include "quick/quick_method_frame_info.h" namespace art { diff --git a/runtime/base/bit_utils.h b/runtime/base/bit_utils.h index f536c72bae..0844678b74 100644 --- a/runtime/base/bit_utils.h +++ b/runtime/base/bit_utils.h @@ -17,13 +17,11 @@ #ifndef ART_RUNTIME_BASE_BIT_UTILS_H_ #define ART_RUNTIME_BASE_BIT_UTILS_H_ -#include <iterator> #include <limits> #include <type_traits> -#include "base/iteration_range.h" #include "base/logging.h" -#include "base/stl_util.h" +#include "base/stl_util_identity.h" namespace art { @@ -312,85 +310,6 @@ constexpr T MinInt(size_t bits) { : static_cast<T>(0); } -// Using the Curiously Recurring Template Pattern to implement everything shared -// by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*(). -template <typename T, typename Iter> -class BitIteratorBase - : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> { - static_assert(std::is_integral<T>::value, "T must be integral"); - static_assert(std::is_unsigned<T>::value, "T must be unsigned"); - - static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size"); - - public: - BitIteratorBase() : bits_(0u) { } - explicit BitIteratorBase(T bits) : bits_(bits) { } - - Iter& operator++() { - DCHECK_NE(bits_, 0u); - uint32_t bit = *static_cast<Iter&>(*this); - bits_ &= ~(static_cast<T>(1u) << bit); - return static_cast<Iter&>(*this); - } - - Iter& operator++(int) { - Iter tmp(static_cast<Iter&>(*this)); - ++*this; - return tmp; - } - - protected: - T bits_; - - template <typename U, typename I> - friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs); -}; - -template <typename T, typename Iter> -bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { - return lhs.bits_ == rhs.bits_; -} - -template <typename T, typename Iter> -bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { - return !(lhs == rhs); -} - -template <typename T> -class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> { - public: - using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase; - - uint32_t operator*() const { - DCHECK_NE(this->bits_, 0u); - return CTZ(this->bits_); - } -}; - -template <typename T> -class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> { - public: - using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase; - - uint32_t operator*() const { - DCHECK_NE(this->bits_, 0u); - static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); - return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_); - } -}; - -template <typename T> -IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) { - return IterationRange<LowToHighBitIterator<T>>( - LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>()); -} - -template <typename T> -IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) { - return IterationRange<HighToLowBitIterator<T>>( - HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>()); -} - // Returns value with bit set in lowest one-bit position or 0 if 0. (java.lang.X.lowestOneBit). template <typename kind> inline static kind LowestOneBitValue(kind opnd) { diff --git a/runtime/base/bit_utils_iterator.h b/runtime/base/bit_utils_iterator.h new file mode 100644 index 0000000000..8514de6b75 --- /dev/null +++ b/runtime/base/bit_utils_iterator.h @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2015 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_RUNTIME_BASE_BIT_UTILS_ITERATOR_H_ +#define ART_RUNTIME_BASE_BIT_UTILS_ITERATOR_H_ + +#include <iterator> +#include <limits> +#include <type_traits> + +#include "base/bit_utils.h" +#include "base/iteration_range.h" +#include "base/logging.h" +#include "base/stl_util.h" + +namespace art { + +// Using the Curiously Recurring Template Pattern to implement everything shared +// by LowToHighBitIterator and HighToLowBitIterator, i.e. everything but operator*(). +template <typename T, typename Iter> +class BitIteratorBase + : public std::iterator<std::forward_iterator_tag, uint32_t, ptrdiff_t, void, void> { + static_assert(std::is_integral<T>::value, "T must be integral"); + static_assert(std::is_unsigned<T>::value, "T must be unsigned"); + + static_assert(sizeof(T) == sizeof(uint32_t) || sizeof(T) == sizeof(uint64_t), "Unsupported size"); + + public: + BitIteratorBase() : bits_(0u) { } + explicit BitIteratorBase(T bits) : bits_(bits) { } + + Iter& operator++() { + DCHECK_NE(bits_, 0u); + uint32_t bit = *static_cast<Iter&>(*this); + bits_ &= ~(static_cast<T>(1u) << bit); + return static_cast<Iter&>(*this); + } + + Iter& operator++(int) { + Iter tmp(static_cast<Iter&>(*this)); + ++*this; + return tmp; + } + + protected: + T bits_; + + template <typename U, typename I> + friend bool operator==(const BitIteratorBase<U, I>& lhs, const BitIteratorBase<U, I>& rhs); +}; + +template <typename T, typename Iter> +bool operator==(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { + return lhs.bits_ == rhs.bits_; +} + +template <typename T, typename Iter> +bool operator!=(const BitIteratorBase<T, Iter>& lhs, const BitIteratorBase<T, Iter>& rhs) { + return !(lhs == rhs); +} + +template <typename T> +class LowToHighBitIterator : public BitIteratorBase<T, LowToHighBitIterator<T>> { + public: + using BitIteratorBase<T, LowToHighBitIterator<T>>::BitIteratorBase; + + uint32_t operator*() const { + DCHECK_NE(this->bits_, 0u); + return CTZ(this->bits_); + } +}; + +template <typename T> +class HighToLowBitIterator : public BitIteratorBase<T, HighToLowBitIterator<T>> { + public: + using BitIteratorBase<T, HighToLowBitIterator<T>>::BitIteratorBase; + + uint32_t operator*() const { + DCHECK_NE(this->bits_, 0u); + static_assert(std::numeric_limits<T>::radix == 2, "Unexpected radix!"); + return std::numeric_limits<T>::digits - 1u - CLZ(this->bits_); + } +}; + +template <typename T> +IterationRange<LowToHighBitIterator<T>> LowToHighBits(T bits) { + return IterationRange<LowToHighBitIterator<T>>( + LowToHighBitIterator<T>(bits), LowToHighBitIterator<T>()); +} + +template <typename T> +IterationRange<HighToLowBitIterator<T>> HighToLowBits(T bits) { + return IterationRange<HighToLowBitIterator<T>>( + HighToLowBitIterator<T>(bits), HighToLowBitIterator<T>()); +} + +} // namespace art + +#endif // ART_RUNTIME_BASE_BIT_UTILS_ITERATOR_H_ diff --git a/runtime/base/bit_utils_test.cc b/runtime/base/bit_utils_test.cc index 77bd0b815e..9f22fb4670 100644 --- a/runtime/base/bit_utils_test.cc +++ b/runtime/base/bit_utils_test.cc @@ -17,6 +17,7 @@ #include <vector> #include "bit_utils.h" +#include "bit_utils_iterator.h" #include "gtest/gtest.h" diff --git a/runtime/base/stl_util.h b/runtime/base/stl_util.h index cfe27f3811..f99465ab66 100644 --- a/runtime/base/stl_util.h +++ b/runtime/base/stl_util.h @@ -171,23 +171,6 @@ struct FNVHash { } }; -// Use to suppress type deduction for a function argument. -// See std::identity<> for more background: -// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1856.html#20.2.2 - move/forward helpers -// -// e.g. "template <typename X> void bar(identity<X>::type foo); -// bar(5); // compilation error -// bar<int>(5); // ok -// or "template <typename T> void foo(T* x, typename Identity<T*>::type y); -// Base b; -// Derived d; -// foo(&b, &d); // Use implicit Derived* -> Base* conversion. -// If T was deduced from both &b and &d, there would be a mismatch, i.e. deduction failure. -template <typename T> -struct Identity { - using type = T; -}; - // Merge `other` entries into `to_update`. template <typename T> static inline void MergeSets(std::set<T>& to_update, const std::set<T>& other) { diff --git a/runtime/base/stl_util_identity.h b/runtime/base/stl_util_identity.h new file mode 100644 index 0000000000..40a93f79c5 --- /dev/null +++ b/runtime/base/stl_util_identity.h @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2011 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_RUNTIME_BASE_STL_UTIL_IDENTITY_H_ +#define ART_RUNTIME_BASE_STL_UTIL_IDENTITY_H_ + +namespace art { + +// Use to suppress type deduction for a function argument. +// See std::identity<> for more background: +// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1856.html#20.2.2 - move/forward helpers +// +// e.g. "template <typename X> void bar(identity<X>::type foo); +// bar(5); // compilation error +// bar<int>(5); // ok +// or "template <typename T> void foo(T* x, typename Identity<T*>::type y); +// Base b; +// Derived d; +// foo(&b, &d); // Use implicit Derived* -> Base* conversion. +// If T was deduced from both &b and &d, there would be a mismatch, i.e. deduction failure. +template <typename T> +struct Identity { + using type = T; +}; + +} // namespace art + +#endif // ART_RUNTIME_BASE_STL_UTIL_IDENTITY_H_ diff --git a/runtime/base/variant_map.h b/runtime/base/variant_map.h index 531cb37355..fdb60c4141 100644 --- a/runtime/base/variant_map.h +++ b/runtime/base/variant_map.h @@ -22,7 +22,7 @@ #include <type_traits> #include <utility> -#include "base/stl_util.h" +#include "base/stl_util_identity.h" namespace art { diff --git a/runtime/class_table.cc b/runtime/class_table.cc index b71610ac7f..c45bbe5334 100644 --- a/runtime/class_table.cc +++ b/runtime/class_table.cc @@ -16,6 +16,7 @@ #include "class_table-inl.h" +#include "base/stl_util.h" #include "mirror/class-inl.h" #include "oat_file.h" diff --git a/runtime/dex2oat_environment_test.h b/runtime/dex2oat_environment_test.h index 6765407949..93daa45519 100644 --- a/runtime/dex2oat_environment_test.h +++ b/runtime/dex2oat_environment_test.h @@ -23,6 +23,7 @@ #include <gtest/gtest.h> +#include "base/stl_util.h" #include "common_runtime_test.h" #include "compiler_callbacks.h" #include "exec_utils.h" diff --git a/runtime/dex_file.cc b/runtime/dex_file.cc index 1301cc2216..b267e5f22a 100644 --- a/runtime/dex_file.cc +++ b/runtime/dex_file.cc @@ -35,6 +35,7 @@ #include "base/enums.h" #include "base/file_magic.h" #include "base/logging.h" +#include "base/stl_util.h" #include "base/systrace.h" #include "base/unix_file/fd_file.h" #include "dex_file-inl.h" diff --git a/runtime/jit/profile_saver.cc b/runtime/jit/profile_saver.cc index 6128d82e24..2a191f2962 100644 --- a/runtime/jit/profile_saver.cc +++ b/runtime/jit/profile_saver.cc @@ -25,6 +25,7 @@ #include "art_method-inl.h" #include "base/enums.h" +#include "base/stl_util.h" #include "base/systrace.h" #include "base/time_utils.h" #include "compiler_filter.h" diff --git a/runtime/mirror/string.cc b/runtime/mirror/string.cc index 80745d271d..d0636932cb 100644 --- a/runtime/mirror/string.cc +++ b/runtime/mirror/string.cc @@ -19,6 +19,7 @@ #include "arch/memcmp16.h" #include "array.h" #include "base/array_ref.h" +#include "base/stl_util.h" #include "class-inl.h" #include "gc/accounting/card_table-inl.h" #include "gc_root-inl.h" diff --git a/runtime/oat_file_assistant.cc b/runtime/oat_file_assistant.cc index f0912cfa65..2e2e8c3ef6 100644 --- a/runtime/oat_file_assistant.cc +++ b/runtime/oat_file_assistant.cc @@ -24,6 +24,7 @@ #include "android-base/strings.h" #include "base/logging.h" +#include "base/stl_util.h" #include "compiler_filter.h" #include "class_linker.h" #include "exec_utils.h" diff --git a/runtime/verifier/method_verifier.h b/runtime/verifier/method_verifier.h index cb208f44a5..46fdc5419d 100644 --- a/runtime/verifier/method_verifier.h +++ b/runtime/verifier/method_verifier.h @@ -24,7 +24,6 @@ #include "base/arena_allocator.h" #include "base/macros.h" #include "base/scoped_arena_containers.h" -#include "base/stl_util.h" #include "base/value_object.h" #include "dex_file.h" #include "dex_file_types.h" |