diff options
Diffstat (limited to 'runtime/base')
| -rw-r--r-- | runtime/base/allocator.h | 28 | ||||
| -rw-r--r-- | runtime/base/bit_utils.h | 83 | ||||
| -rw-r--r-- | runtime/base/bit_utils_iterator.h | 112 | ||||
| -rw-r--r-- | runtime/base/bit_utils_test.cc | 1 | ||||
| -rw-r--r-- | runtime/base/callee_save_type.h | 37 | ||||
| -rw-r--r-- | runtime/base/stl_util.h | 52 | ||||
| -rw-r--r-- | runtime/base/stl_util_identity.h | 41 | ||||
| -rw-r--r-- | runtime/base/strlcpy.h | 38 | ||||
| -rw-r--r-- | runtime/base/variant_map.h | 2 |
9 files changed, 231 insertions, 163 deletions
diff --git a/runtime/base/allocator.h b/runtime/base/allocator.h index 8d1c982f3d..fba9308e8e 100644 --- a/runtime/base/allocator.h +++ b/runtime/base/allocator.h @@ -17,12 +17,7 @@ #ifndef ART_RUNTIME_BASE_ALLOCATOR_H_ #define ART_RUNTIME_BASE_ALLOCATOR_H_ -#include <map> -#include <set> -#include <unordered_map> - #include "atomic.h" -#include "base/hash_map.h" #include "base/macros.h" #include "base/mutex.h" #include "base/type_static_if.h" @@ -156,29 +151,6 @@ using TrackingAllocator = typename TypeStaticIf<kEnableTrackingAllocator, TrackingAllocatorImpl<T, kTag>, std::allocator<T>>::type; -template<class Key, class T, AllocatorTag kTag, class Compare = std::less<Key>> -using AllocationTrackingMultiMap = std::multimap< - Key, T, Compare, TrackingAllocator<std::pair<const Key, T>, kTag>>; - -template<class Key, AllocatorTag kTag, class Compare = std::less<Key>> -using AllocationTrackingSet = std::set<Key, Compare, TrackingAllocator<Key, kTag>>; - -template<class Key, - class T, - AllocatorTag kTag, - class Hash = std::hash<Key>, - class Pred = std::equal_to<Key>> -using AllocationTrackingUnorderedMap = std::unordered_map< - Key, T, Hash, Pred, TrackingAllocator<std::pair<const Key, T>, kTag>>; - -template<class Key, - class T, - class EmptyFn, - AllocatorTag kTag, - class Hash = std::hash<Key>, - class Pred = std::equal_to<Key>> -using AllocationTrackingHashMap = HashMap< - Key, T, EmptyFn, Hash, Pred, TrackingAllocator<std::pair<Key, T>, kTag>>; } // namespace art #endif // ART_RUNTIME_BASE_ALLOCATOR_H_ 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/callee_save_type.h b/runtime/base/callee_save_type.h new file mode 100644 index 0000000000..501b296d4f --- /dev/null +++ b/runtime/base/callee_save_type.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2017 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_CALLEE_SAVE_TYPE_H_ +#define ART_RUNTIME_BASE_CALLEE_SAVE_TYPE_H_ + +#include <cstddef> +#include <ostream> + +namespace art { + +// Returns a special method that describes all callee saves being spilled to the stack. +enum class CalleeSaveType : uint32_t { + kSaveAllCalleeSaves, // All callee-save registers. + kSaveRefsOnly, // Only those callee-save registers that can hold references. + kSaveRefsAndArgs, // References (see above) and arguments (usually caller-save registers). + kSaveEverything, // All registers, including both callee-save and caller-save. + kLastCalleeSaveType // Value used for iteration. +}; +std::ostream& operator<<(std::ostream& os, const CalleeSaveType& rhs); + +} // namespace art + +#endif // ART_RUNTIME_BASE_CALLEE_SAVE_TYPE_H_ diff --git a/runtime/base/stl_util.h b/runtime/base/stl_util.h index cfe27f3811..b27297241d 100644 --- a/runtime/base/stl_util.h +++ b/runtime/base/stl_util.h @@ -25,13 +25,6 @@ namespace art { -// Sort and remove duplicates of an STL vector or deque. -template<class T> -void STLSortAndRemoveDuplicates(T* v) { - std::sort(v->begin(), v->end()); - v->erase(std::unique(v->begin(), v->end()), v->end()); -} - // STLDeleteContainerPointers() // For a range within a container of pointers, calls delete // (non-array version) on these pointers. @@ -83,20 +76,6 @@ void STLDeleteValues(T *v) { } } -template <class T> -std::string ToString(const T& v) { - std::ostringstream os; - os << "["; - for (size_t i = 0; i < v.size(); ++i) { - os << v[i]; - if (i < v.size() - 1) { - os << ", "; - } - } - os << "]"; - return os.str(); -} - // Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below. struct FreeDelete { // NOTE: Deleting a const object is valid but free() takes a non-const pointer. @@ -109,13 +88,6 @@ struct FreeDelete { template <typename T> using UniqueCPtr = std::unique_ptr<T, FreeDelete>; -// C++14 from-the-future import (std::make_unique) -// Invoke the constructor of 'T' with the provided args, and wrap the result in a unique ptr. -template <typename T, typename ... Args> -std::unique_ptr<T> MakeUnique(Args&& ... args) { - return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); -} - // Find index of the first element with the specified value known to be in the container. template <typename Container, typename T> size_t IndexOfElement(const Container& container, const T& value) { @@ -150,13 +122,6 @@ bool ContainsElement(const Container& container, const T& value, size_t start_po return it != container.end(); } -// const char* compare function suitable for std::map or std::set. -struct CStringLess { - bool operator()(const char* lhs, const char* rhs) const { - return strcmp(lhs, rhs) < 0; - } -}; - // 32-bit FNV-1a hash function suitable for std::unordered_map. // It can be used with any container which works with range-based for loop. // See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function @@ -171,23 +136,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/strlcpy.h b/runtime/base/strlcpy.h new file mode 100644 index 0000000000..de135ea990 --- /dev/null +++ b/runtime/base/strlcpy.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2017 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_STRLCPY_H_ +#define ART_RUNTIME_BASE_STRLCPY_H_ + +#include <cstdio> +#include <cstring> + +// Expose a simple implementation of strlcpy when we're not compiling against bionic. This is to +// make static analyzers happy not using strcpy. +// +// Bionic exposes this function, but the host glibc does not. Remove this shim when we compile +// against bionic on the host, also. + +#if !defined(__BIONIC__) && !defined(__APPLE__) + +static inline size_t strlcpy(char* dst, const char* src, size_t size) { + // Extra-lazy implementation: this is only a host shim, and we don't have to call this often. + return snprintf(dst, size, "%s", src); +} + +#endif + +#endif // ART_RUNTIME_BASE_STRLCPY_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 { |