diff options
author | davidben <davidben@chromium.org> | 2016-01-21 17:41:41 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-22 01:43:27 +0000 |
commit | 411d3f7a3a18335713a46d9022ce25e018a19e47 (patch) | |
tree | 72cd74ea04f27cc8b01f7153f43e24ae8d48b512 | |
parent | 58be7eea6963f63c4395e97a8a8099916f738a61 (diff) | |
download | chromium_src-411d3f7a3a18335713a46d9022ce25e018a19e47.zip chromium_src-411d3f7a3a18335713a46d9022ce25e018a19e47.tar.gz chromium_src-411d3f7a3a18335713a46d9022ce25e018a19e47.tar.bz2 |
Allow std::unordered_*.
This is a reland of https://codereview.chromium.org/1502373009 with some fixes
for components/metrics/leak_detector allocator type mismatches.
Original issue's description:
> Allow std::unordered_*.
>
> base::hash_* is, as a transition step, implemented in terms of
> std::unordered_*. Later commits will convert existing uses.
>
> Also fix a host of IWYU problems that arose from this CL.
>
> (NOPRESUBMIT because the wstring presubmit check is overzealous
> and complains about the reference to wstring in the comment.)
>
> Committed: https://crrev.com/3f37f7f1459e7b5a452c0e433493e0a6e9649ca7
> Cr-Commit-Position: refs/heads/master@{#370553}
BUG=576864
TBR=derat@chromium.org,danakj@chromium.org,dalecurtis@chromium.org,jbauman@chromium.org,blundell@chromium.org
NOPRESUBMIT=true
CQ_INCLUDE_TRYBOTS=tryserver.blink:linux_blink_rel
Review URL: https://codereview.chromium.org/1615713003
Cr-Commit-Position: refs/heads/master@{#370867}
42 files changed, 228 insertions, 321 deletions
diff --git a/ash/display/display_info.cc b/ash/display/display_info.cc index b89d4d5..a85d175 100644 --- a/ash/display/display_info.cc +++ b/ash/display/display_info.cc @@ -2,11 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "ash/display/display_info.h" + #include <stdio.h> + +#include <algorithm> #include <string> #include <vector> -#include "ash/display/display_info.h" #include "base/logging.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_split.h" diff --git a/base/containers/hash_tables.h b/base/containers/hash_tables.h index c421ddd..8da7b67 100644 --- a/base/containers/hash_tables.h +++ b/base/containers/hash_tables.h @@ -1,281 +1,75 @@ // Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -// - -// -// Deal with the differences between Microsoft and GNU implemenations -// of hash_map. Allows all platforms to use |base::hash_map| and -// |base::hash_set|. -// eg: -// base::hash_map<int> my_map; -// base::hash_set<int> my_set; -// -// NOTE: It is an explicit non-goal of this class to provide a generic hash -// function for pointers. If you want to hash a pointers to a particular class, -// please define the template specialization elsewhere (for example, in its -// header file) and keep it specific to just pointers to that class. This is -// because identity hashes are not desirable for all types that might show up -// in containers as pointers. #ifndef BASE_CONTAINERS_HASH_TABLES_H_ #define BASE_CONTAINERS_HASH_TABLES_H_ -#include <stddef.h> -#include <stdint.h> - -#include <utility> - -#include "base/strings/string16.h" -#include "build/build_config.h" - -#if defined(COMPILER_MSVC) +#include <cstddef> #include <unordered_map> #include <unordered_set> +#include <utility> -#define BASE_HASH_NAMESPACE std +#include "base/hash.h" -#elif defined(COMPILER_GCC) +// This header file is deprecated. Use the corresponding C++11 type +// instead. https://crbug.com/576864 +// Use a custom hasher instead. #define BASE_HASH_NAMESPACE base_hash -// This is a hack to disable the gcc 4.4 warning about hash_map and hash_set -// being deprecated. We can get rid of this when we upgrade to VS2008 and we -// can use <tr1/unordered_map> and <tr1/unordered_set>. -#ifdef __DEPRECATED -#define CHROME_OLD__DEPRECATED __DEPRECATED -#undef __DEPRECATED -#endif - -#include <ext/hash_map> -#include <ext/hash_set> -#define BASE_HASH_IMPL_NAMESPACE __gnu_cxx - -#include <string> - -#ifdef CHROME_OLD__DEPRECATED -#define __DEPRECATED CHROME_OLD__DEPRECATED -#undef CHROME_OLD__DEPRECATED -#endif - namespace BASE_HASH_NAMESPACE { -// The pre-standard hash behaves like C++11's std::hash, except around pointers. -// const char* is specialized to hash the C string and hash functions for -// general T* are missing. Define a BASE_HASH_NAMESPACE::hash which aligns with -// the C++11 behavior. - +// A separate hasher which, by default, forwards to std::hash. This is so legacy +// uses of BASE_HASH_NAMESPACE with base::hash_map do not interfere with +// std::hash mid-transition. template<typename T> struct hash { - std::size_t operator()(const T& value) const { - return BASE_HASH_IMPL_NAMESPACE::hash<T>()(value); - } + std::size_t operator()(const T& value) const { return std::hash<T>()(value); } }; -template<typename T> -struct hash<T*> { - std::size_t operator()(T* value) const { - return BASE_HASH_IMPL_NAMESPACE::hash<uintptr_t>()( - reinterpret_cast<uintptr_t>(value)); +// Use base::IntPairHash from base/hash.h as a custom hasher instead. +template <typename Type1, typename Type2> +struct hash<std::pair<Type1, Type2>> { + std::size_t operator()(std::pair<Type1, Type2> value) const { + return base::HashInts(value.first, value.second); } }; -// The GNU C++ library provides identity hash functions for many integral types, -// but not for |long long|. This hash function will truncate if |size_t| is -// narrower than |long long|. This is probably good enough for what we will -// use it for. - -#define DEFINE_TRIVIAL_HASH(integral_type) \ - template<> \ - struct hash<integral_type> { \ - std::size_t operator()(integral_type value) const { \ - return static_cast<std::size_t>(value); \ - } \ - } - -DEFINE_TRIVIAL_HASH(long long); -DEFINE_TRIVIAL_HASH(unsigned long long); - -#undef DEFINE_TRIVIAL_HASH - -// Implement string hash functions so that strings of various flavors can -// be used as keys in STL maps and sets. The hash algorithm comes from the -// GNU C++ library, in <tr1/functional>. It is duplicated here because GCC -// versions prior to 4.3.2 are unable to compile <tr1/functional> when RTTI -// is disabled, as it is in our build. - -#define DEFINE_STRING_HASH(string_type) \ - template<> \ - struct hash<string_type> { \ - std::size_t operator()(const string_type& s) const { \ - std::size_t result = 0; \ - for (string_type::const_iterator i = s.begin(); i != s.end(); ++i) \ - result = (result * 131) + *i; \ - return result; \ - } \ - } - -DEFINE_STRING_HASH(std::string); -DEFINE_STRING_HASH(base::string16); - -#undef DEFINE_STRING_HASH - } // namespace BASE_HASH_NAMESPACE -#else // COMPILER -#error define BASE_HASH_NAMESPACE for your compiler -#endif // COMPILER - namespace base { -// On MSVC, use the C++11 containers. -#if defined(COMPILER_MSVC) - -template<class Key, class T, - class Hash = std::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<std::pair<const Key, T>>> +// Use std::unordered_map instead. +template <class Key, + class T, + class Hash = BASE_HASH_NAMESPACE::hash<Key>, + class Pred = std::equal_to<Key>, + class Alloc = std::allocator<std::pair<const Key, T>>> using hash_map = std::unordered_map<Key, T, Hash, Pred, Alloc>; -template<class Key, class T, - class Hash = std::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<std::pair<const Key, T>>> +// Use std::unordered_multimap instead. +template <class Key, + class T, + class Hash = BASE_HASH_NAMESPACE::hash<Key>, + class Pred = std::equal_to<Key>, + class Alloc = std::allocator<std::pair<const Key, T>>> using hash_multimap = std::unordered_multimap<Key, T, Hash, Pred, Alloc>; -template<class Key, - class Hash = std::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<Key>> +// Use std::unordered_multiset instead. +template <class Key, + class Hash = BASE_HASH_NAMESPACE::hash<Key>, + class Pred = std::equal_to<Key>, + class Alloc = std::allocator<Key>> using hash_multiset = std::unordered_multiset<Key, Hash, Pred, Alloc>; -template<class Key, - class Hash = std::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<Key>> +// Use std::unordered_set instead. +template <class Key, + class Hash = BASE_HASH_NAMESPACE::hash<Key>, + class Pred = std::equal_to<Key>, + class Alloc = std::allocator<Key>> using hash_set = std::unordered_set<Key, Hash, Pred, Alloc>; -#else // !COMPILER_MSVC - -// Otherwise, use the pre-standard ones, but override the default hash to match -// C++11. -template<class Key, class T, - class Hash = BASE_HASH_NAMESPACE::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<std::pair<const Key, T>>> -using hash_map = BASE_HASH_IMPL_NAMESPACE::hash_map<Key, T, Hash, Pred, Alloc>; - -template<class Key, class T, - class Hash = BASE_HASH_NAMESPACE::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<std::pair<const Key, T>>> -using hash_multimap = - BASE_HASH_IMPL_NAMESPACE::hash_multimap<Key, T, Hash, Pred, Alloc>; - -template<class Key, - class Hash = BASE_HASH_NAMESPACE::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<Key>> -using hash_multiset = - BASE_HASH_IMPL_NAMESPACE::hash_multiset<Key, Hash, Pred, Alloc>; - -template<class Key, - class Hash = BASE_HASH_NAMESPACE::hash<Key>, - class Pred = std::equal_to<Key>, - class Alloc = std::allocator<Key>> -using hash_set = BASE_HASH_IMPL_NAMESPACE::hash_set<Key, Hash, Pred, Alloc>; - -#undef BASE_HASH_IMPL_NAMESPACE - -#endif // COMPILER_MSVC - -// Implement hashing for pairs of at-most 32 bit integer values. -// When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using -// multiply-add hashing. This algorithm, as described in -// Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in -// eingeschränkten Branchingprogrammmodellen" by Woelfel, is: -// -// h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 -// -// Contact danakj@chromium.org for any questions. -inline std::size_t HashInts32(uint32_t value1, uint32_t value2) { - uint64_t value1_64 = value1; - uint64_t hash64 = (value1_64 << 32) | value2; - - if (sizeof(std::size_t) >= sizeof(uint64_t)) - return static_cast<std::size_t>(hash64); - - uint64_t odd_random = 481046412LL << 32 | 1025306955LL; - uint32_t shift_random = 10121U << 16; - - hash64 = hash64 * odd_random + shift_random; - std::size_t high_bits = static_cast<std::size_t>( - hash64 >> (8 * (sizeof(uint64_t) - sizeof(std::size_t)))); - return high_bits; -} - -// Implement hashing for pairs of up-to 64-bit integer values. -// We use the compound integer hash method to produce a 64-bit hash code, by -// breaking the two 64-bit inputs into 4 32-bit values: -// http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000 -// Then we reduce our result to 32 bits if required, similar to above. -inline std::size_t HashInts64(uint64_t value1, uint64_t value2) { - uint32_t short_random1 = 842304669U; - uint32_t short_random2 = 619063811U; - uint32_t short_random3 = 937041849U; - uint32_t short_random4 = 3309708029U; - - uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff); - uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff); - uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff); - uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff); - - uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1; - uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2; - uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3; - uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4; - - uint64_t hash64 = product1 + product2 + product3 + product4; - - if (sizeof(std::size_t) >= sizeof(uint64_t)) - return static_cast<std::size_t>(hash64); - - uint64_t odd_random = 1578233944LL << 32 | 194370989LL; - uint32_t shift_random = 20591U << 16; - - hash64 = hash64 * odd_random + shift_random; - std::size_t high_bits = static_cast<std::size_t>( - hash64 >> (8 * (sizeof(uint64_t) - sizeof(std::size_t)))); - return high_bits; -} - -template<typename T1, typename T2> -inline std::size_t HashPair(T1 value1, T2 value2) { - // This condition is expected to be compile-time evaluated and optimised away - // in release builds. - if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t))) - return HashInts64(value1, value2); - - return HashInts32(value1, value2); -} - } // namespace base -namespace BASE_HASH_NAMESPACE { - -// Implement methods for hashing a pair of integers, so they can be used as -// keys in STL containers. - -template<typename Type1, typename Type2> -struct hash<std::pair<Type1, Type2> > { - std::size_t operator()(std::pair<Type1, Type2> value) const { - return base::HashPair(value.first, value.second); - } -}; - -} // namespace BASE_HASH_NAMESPACE - -#undef DEFINE_PAIR_HASH_FUNCTION_START -#undef DEFINE_PAIR_HASH_FUNCTION_END - #endif // BASE_CONTAINERS_HASH_TABLES_H_ diff --git a/base/containers/scoped_ptr_hash_map.h b/base/containers/scoped_ptr_hash_map.h index 189c314..dd100c6 100644 --- a/base/containers/scoped_ptr_hash_map.h +++ b/base/containers/scoped_ptr_hash_map.h @@ -18,6 +18,8 @@ namespace base { +// Deprecated. Use std::unordered_map instead. https://crbug.com/579229 +// // This type acts like a hash_map<K, scoped_ptr<V, D> >, based on top of // base::hash_map. The ScopedPtrHashMap has ownership of all values in the data // structure. diff --git a/base/hash.h b/base/hash.h index ed8d9fd..97e251c 100644 --- a/base/hash.h +++ b/base/hash.h @@ -10,6 +10,7 @@ #include <limits> #include <string> +#include <utility> #include "base/base_export.h" #include "base/logging.h" @@ -35,6 +36,87 @@ inline uint32_t Hash(const std::string& str) { return Hash(str.data(), str.size()); } +// Implement hashing for pairs of at-most 32 bit integer values. +// When size_t is 32 bits, we turn the 64-bit hash code into 32 bits by using +// multiply-add hashing. This algorithm, as described in +// Theorem 4.3.3 of the thesis "Über die Komplexität der Multiplikation in +// eingeschränkten Branchingprogrammmodellen" by Woelfel, is: +// +// h32(x32, y32) = (h64(x32, y32) * rand_odd64 + rand16 * 2^16) % 2^64 / 2^32 +// +// Contact danakj@chromium.org for any questions. +inline size_t HashInts32(uint32_t value1, uint32_t value2) { + uint64_t value1_64 = value1; + uint64_t hash64 = (value1_64 << 32) | value2; + + if (sizeof(size_t) >= sizeof(uint64_t)) + return static_cast<size_t>(hash64); + + uint64_t odd_random = 481046412LL << 32 | 1025306955LL; + uint32_t shift_random = 10121U << 16; + + hash64 = hash64 * odd_random + shift_random; + size_t high_bits = + static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t)))); + return high_bits; +} + +// Implement hashing for pairs of up-to 64-bit integer values. +// We use the compound integer hash method to produce a 64-bit hash code, by +// breaking the two 64-bit inputs into 4 32-bit values: +// http://opendatastructures.org/versions/edition-0.1d/ods-java/node33.html#SECTION00832000000000000000 +// Then we reduce our result to 32 bits if required, similar to above. +inline size_t HashInts64(uint64_t value1, uint64_t value2) { + uint32_t short_random1 = 842304669U; + uint32_t short_random2 = 619063811U; + uint32_t short_random3 = 937041849U; + uint32_t short_random4 = 3309708029U; + + uint32_t value1a = static_cast<uint32_t>(value1 & 0xffffffff); + uint32_t value1b = static_cast<uint32_t>((value1 >> 32) & 0xffffffff); + uint32_t value2a = static_cast<uint32_t>(value2 & 0xffffffff); + uint32_t value2b = static_cast<uint32_t>((value2 >> 32) & 0xffffffff); + + uint64_t product1 = static_cast<uint64_t>(value1a) * short_random1; + uint64_t product2 = static_cast<uint64_t>(value1b) * short_random2; + uint64_t product3 = static_cast<uint64_t>(value2a) * short_random3; + uint64_t product4 = static_cast<uint64_t>(value2b) * short_random4; + + uint64_t hash64 = product1 + product2 + product3 + product4; + + if (sizeof(size_t) >= sizeof(uint64_t)) + return static_cast<size_t>(hash64); + + uint64_t odd_random = 1578233944LL << 32 | 194370989LL; + uint32_t shift_random = 20591U << 16; + + hash64 = hash64 * odd_random + shift_random; + size_t high_bits = + static_cast<size_t>(hash64 >> (8 * (sizeof(uint64_t) - sizeof(size_t)))); + return high_bits; +} + +template <typename T1, typename T2> +inline size_t HashInts(T1 value1, T2 value2) { + // This condition is expected to be compile-time evaluated and optimised away + // in release builds. + if (sizeof(T1) > sizeof(uint32_t) || (sizeof(T2) > sizeof(uint32_t))) + return HashInts64(value1, value2); + + return HashInts32(value1, value2); +} + +// A templated hasher for pairs of integer types. +template <typename T> +struct IntPairHash; + +template <typename Type1, typename Type2> +struct IntPairHash<std::pair<Type1, Type2>> { + size_t operator()(std::pair<Type1, Type2> value) const { + return HashInts(value.first, value.second); + } +}; + } // namespace base #endif // BASE_HASH_H_ diff --git a/base/location.h b/base/location.h index d3bb23c..21e270c 100644 --- a/base/location.h +++ b/base/location.h @@ -11,7 +11,7 @@ #include <string> #include "base/base_export.h" -#include "base/containers/hash_tables.h" +#include "base/hash.h" namespace tracked_objects { @@ -59,7 +59,7 @@ class BASE_EXPORT Location { // it comes from __FILE__, so no need to check the contents of the string. // See the definition of FROM_HERE in location.h, and how it is used // elsewhere. - return base::HashPair(reinterpret_cast<uintptr_t>(location.file_name()), + return base::HashInts(reinterpret_cast<uintptr_t>(location.file_name()), location.line_number()); } }; diff --git a/base/strings/string16.h b/base/strings/string16.h index e47669c..82dd0fa 100644 --- a/base/strings/string16.h +++ b/base/strings/string16.h @@ -29,6 +29,8 @@ #include <stddef.h> #include <stdint.h> #include <stdio.h> + +#include <functional> #include <string> #include "base/base_export.h" @@ -182,6 +184,21 @@ BASE_EXPORT extern void PrintTo(const string16& str, std::ostream* out); extern template class BASE_EXPORT std::basic_string<base::char16, base::string16_char_traits>; +// Specialize std::hash for base::string16. Although the style guide forbids +// this in general, it is necessary for consistency with WCHAR_T_IS_UTF16 +// platforms, where base::string16 is a type alias for std::wstring. +namespace std { +template <> +struct hash<base::string16> { + std::size_t operator()(const base::string16& s) const { + std::size_t result = 0; + for (base::char16 c : s) + result = (result * 131) + c; + return result; + } +}; +} // namespace std + #endif // WCHAR_T_IS_UTF32 #endif // BASE_STRINGS_STRING16_H_ diff --git a/base/strings/string16_unittest.cc b/base/strings/string16_unittest.cc index 4e58218..0d2ca80 100644 --- a/base/strings/string16_unittest.cc +++ b/base/strings/string16_unittest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include <sstream> +#include <unordered_set> #include "base/strings/string16.h" @@ -11,8 +12,6 @@ namespace base { -#if defined(WCHAR_T_IS_UTF32) - // We define a custom operator<< for string16 so we can use it with logging. // This tests that conversion. TEST(String16Test, OutputStream) { @@ -53,6 +52,15 @@ TEST(String16Test, OutputStream) { } } -#endif +TEST(String16Test, Hash) { + string16 str1 = ASCIIToUTF16("hello"); + string16 str2 = ASCIIToUTF16("world"); + + std::unordered_set<string16> set; + + set.insert(str1); + EXPECT_EQ(1u, set.count(str1)); + EXPECT_EQ(0u, set.count(str2)); +} } // namespace base diff --git a/base/strings/string_piece.h b/base/strings/string_piece.h index 31e7596..92634b9 100644 --- a/base/strings/string_piece.h +++ b/base/strings/string_piece.h @@ -439,9 +439,9 @@ BASE_EXPORT std::ostream& operator<<(std::ostream& o, // We provide appropriate hash functions so StringPiece and StringPiece16 can // be used as keys in hash sets and maps. -// This hash function is copied from base/containers/hash_tables.h. We don't -// use the ones already defined for string and string16 directly because it -// would require the string constructors to be called, which we don't want. +// This hash function is copied from base/strings/string16.h. We don't use the +// ones already defined for string and string16 directly because it would +// require the string constructors to be called, which we don't want. #define HASH_STRING_PIECE(StringPieceType, string_piece) \ std::size_t result = 0; \ for (StringPieceType::const_iterator i = string_piece.begin(); \ diff --git a/cc/debug/picture_debug_util.cc b/cc/debug/picture_debug_util.cc index b1879af..50ae60b 100644 --- a/cc/debug/picture_debug_util.cc +++ b/cc/debug/picture_debug_util.cc @@ -6,6 +6,7 @@ #include <stddef.h> +#include <limits> #include <vector> #include "base/base64.h" diff --git a/cc/layers/delegated_renderer_layer_impl.cc b/cc/layers/delegated_renderer_layer_impl.cc index 5d51116..8158edcf 100644 --- a/cc/layers/delegated_renderer_layer_impl.cc +++ b/cc/layers/delegated_renderer_layer_impl.cc @@ -109,16 +109,7 @@ void DelegatedRendererLayerImpl::SetFrameData( bool invalid_frame = false; ResourceProvider::ResourceIdSet resources_in_frame; size_t reserve_size = frame_data->resource_list.size(); -#if defined(COMPILER_MSVC) resources_in_frame.reserve(reserve_size); -#elif defined(COMPILER_GCC) - // Pre-standard hash-tables only implement resize, which behaves similarly - // to reserve for these keys. Resizing to 0 may also be broken (particularly - // on stlport). - // TODO(jbauman): Replace with reserve when C++11 is supported everywhere. - if (reserve_size) - resources_in_frame.resize(reserve_size); -#endif for (const auto& pass : render_pass_list) { for (const auto& quad : pass->quad_list) { for (ResourceId& resource_id : quad->resources) { diff --git a/cc/quads/render_pass.h b/cc/quads/render_pass.h index 9ba727e..552643d 100644 --- a/cc/quads/render_pass.h +++ b/cc/quads/render_pass.h @@ -12,6 +12,7 @@ #include "base/callback.h" #include "base/containers/hash_tables.h" +#include "base/hash.h" #include "base/macros.h" #include "cc/base/cc_export.h" #include "cc/base/list_container.h" @@ -139,7 +140,7 @@ namespace BASE_HASH_NAMESPACE { template <> struct hash<cc::RenderPassId> { size_t operator()(cc::RenderPassId key) const { - return base::HashPair(key.layer_id, static_cast<int>(key.index)); + return base::HashInts(key.layer_id, static_cast<int>(key.index)); } }; } // namespace BASE_HASH_NAMESPACE diff --git a/cc/quads/render_pass_id.cc b/cc/quads/render_pass_id.cc index 0f53e81..742c191 100644 --- a/cc/quads/render_pass_id.cc +++ b/cc/quads/render_pass_id.cc @@ -2,9 +2,11 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "cc/quads/render_pass_id.h" + #include <stddef.h> -#include "cc/quads/render_pass_id.h" +#include "base/hash.h" namespace cc { @@ -12,7 +14,7 @@ void* RenderPassId::AsTracingId() const { static_assert(sizeof(size_t) <= sizeof(void*), // NOLINT "size of size_t should not be greater than that of a pointer"); return reinterpret_cast<void*>( - base::HashPair(layer_id, static_cast<int>(index))); + base::HashInts(layer_id, static_cast<int>(index))); } } // namespace cc diff --git a/cc/quads/render_pass_id.h b/cc/quads/render_pass_id.h index 11cb777..5dff1e1 100644 --- a/cc/quads/render_pass_id.h +++ b/cc/quads/render_pass_id.h @@ -9,7 +9,6 @@ #include <tuple> -#include "base/containers/hash_tables.h" #include "cc/base/cc_export.h" namespace cc { diff --git a/cc/surfaces/surface_aggregator.cc b/cc/surfaces/surface_aggregator.cc index 9901d00..d60a217 100644 --- a/cc/surfaces/surface_aggregator.cc +++ b/cc/surfaces/surface_aggregator.cc @@ -526,16 +526,7 @@ gfx::Rect SurfaceAggregator::PrewalkTree(SurfaceId surface_id, ResourceProvider::ResourceIdSet referenced_resources; size_t reserve_size = frame_data->resource_list.size(); -#if defined(COMPILER_MSVC) referenced_resources.reserve(reserve_size); -#elif defined(COMPILER_GCC) - // Pre-standard hash-tables only implement resize, which behaves similarly - // to reserve for these keys. Resizing to 0 may also be broken (particularly - // on stlport). - // TODO(jbauman): Replace with reserve when C++11 is supported everywhere. - if (reserve_size) - referenced_resources.resize(reserve_size); -#endif bool invalid_frame = false; ResourceProvider::ResourceIdMap empty_map; diff --git a/cc/surfaces/surface_sequence.h b/cc/surfaces/surface_sequence.h index 21decf6..b4ab8ef 100644 --- a/cc/surfaces/surface_sequence.h +++ b/cc/surfaces/surface_sequence.h @@ -11,6 +11,7 @@ #include <tuple> #include "base/containers/hash_tables.h" +#include "base/hash.h" namespace cc { @@ -46,7 +47,7 @@ namespace BASE_HASH_NAMESPACE { template <> struct hash<cc::SurfaceSequence> { size_t operator()(cc::SurfaceSequence key) const { - return base::HashPair(key.id_namespace, key.sequence); + return base::HashInts(key.id_namespace, key.sequence); } }; } // namespace BASE_HASH_NAMESPACE diff --git a/cc/tiles/image_decode_controller.h b/cc/tiles/image_decode_controller.h index 2c18212..f814d21 100644 --- a/cc/tiles/image_decode_controller.h +++ b/cc/tiles/image_decode_controller.h @@ -8,6 +8,7 @@ #include <stdint.h> #include "base/containers/hash_tables.h" +#include "base/hash.h" #include "base/memory/discardable_memory_allocator.h" #include "base/memory/ref_counted.h" #include "base/numerics/safe_math.h" @@ -79,16 +80,16 @@ struct hash<cc::ImageDecodeControllerKey> { // TODO(vmpstr): This is a mess. Maybe it's faster to just search the vector // always (forwards or backwards to account for LRU). uint64_t src_rect_hash = - base::HashPair(static_cast<uint64_t>(base::HashPair( + base::HashInts(static_cast<uint64_t>(base::HashInts( key.src_rect().x(), key.src_rect().y())), - static_cast<uint64_t>(base::HashPair( + static_cast<uint64_t>(base::HashInts( key.src_rect().width(), key.src_rect().height()))); uint64_t target_size_hash = - base::HashPair(key.target_size().width(), key.target_size().height()); + base::HashInts(key.target_size().width(), key.target_size().height()); - return base::HashPair(base::HashPair(src_rect_hash, target_size_hash), - base::HashPair(key.image_id(), key.filter_quality())); + return base::HashInts(base::HashInts(src_rect_hash, target_size_hash), + base::HashInts(key.image_id(), key.filter_quality())); } }; } // namespace BASE_HASH_NAMESPACE diff --git a/chrome/browser/devtools/devtools_file_watcher.cc b/chrome/browser/devtools/devtools_file_watcher.cc index bfc4906..d966de5 100644 --- a/chrome/browser/devtools/devtools_file_watcher.cc +++ b/chrome/browser/devtools/devtools_file_watcher.cc @@ -4,6 +4,7 @@ #include "chrome/browser/devtools/devtools_file_watcher.h" +#include <algorithm> #include <map> #include <set> diff --git a/chrome/browser/extensions/api/log_private/filter_handler.cc b/chrome/browser/extensions/api/log_private/filter_handler.cc index 6965932..02c1306 100644 --- a/chrome/browser/extensions/api/log_private/filter_handler.cc +++ b/chrome/browser/extensions/api/log_private/filter_handler.cc @@ -4,6 +4,7 @@ #include "chrome/browser/extensions/api/log_private/filter_handler.h" +#include <algorithm> #include <string> #include <vector> diff --git a/chrome/browser/google/google_brand.cc b/chrome/browser/google/google_brand.cc index 5360948..7e07284e 100644 --- a/chrome/browser/google/google_brand.cc +++ b/chrome/browser/google/google_brand.cc @@ -4,6 +4,7 @@ #include "chrome/browser/google/google_brand.h" +#include <algorithm> #include <string> #include "base/macros.h" diff --git a/chrome/browser/spellchecker/spellcheck_host_metrics.h b/chrome/browser/spellchecker/spellcheck_host_metrics.h index fe0df15..05da7ce 100644 --- a/chrome/browser/spellchecker/spellcheck_host_metrics.h +++ b/chrome/browser/spellchecker/spellcheck_host_metrics.h @@ -11,6 +11,7 @@ #include <vector> #include "base/containers/hash_tables.h" +#include "base/strings/string16.h" #include "base/time/time.h" #include "base/timer/timer.h" diff --git a/chrome/browser/sync_file_system/drive_backend/metadata_database_index.h b/chrome/browser/sync_file_system/drive_backend/metadata_database_index.h index a6f4510..4069652 100644 --- a/chrome/browser/sync_file_system/drive_backend/metadata_database_index.h +++ b/chrome/browser/sync_file_system/drive_backend/metadata_database_index.h @@ -15,6 +15,7 @@ #include "base/containers/hash_tables.h" #include "base/containers/scoped_ptr_hash_map.h" +#include "base/hash.h" #include "base/macros.h" #include "base/memory/scoped_vector.h" #include "chrome/browser/sync_file_system/drive_backend/metadata_database_index_interface.h" diff --git a/chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.cc b/chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.cc index 3bd7a34..96800d9 100644 --- a/chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.cc +++ b/chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.cc @@ -4,6 +4,8 @@ #include "chrome/browser/task_management/providers/web_contents/web_contents_tags_manager.h" +#include <algorithm> + #include "base/memory/singleton.h" #include "chrome/browser/task_management/providers/web_contents/web_contents_task_provider.h" diff --git a/components/favicon_base/favicon_util.cc b/components/favicon_base/favicon_util.cc index 8c66920..e17ebdf 100644 --- a/components/favicon_base/favicon_util.cc +++ b/components/favicon_base/favicon_util.cc @@ -6,6 +6,7 @@ #include <stddef.h> +#include <algorithm> #include <cmath> #include "build/build_config.h" diff --git a/components/metrics/leak_detector/call_stack_table.h b/components/metrics/leak_detector/call_stack_table.h index 08503b3..42657c8 100644 --- a/components/metrics/leak_detector/call_stack_table.h +++ b/components/metrics/leak_detector/call_stack_table.h @@ -64,7 +64,7 @@ class CallStackTable { // Hash table containing entries. Uses CustomAllocator to avoid recursive // malloc hook invocation when analyzing allocs and frees. using TableEntryAllocator = - STLAllocator<std::pair<const CallStack*, Entry>, CustomAllocator>; + STLAllocator<std::pair<const CallStack* const, Entry>, CustomAllocator>; base::hash_map<const CallStack*, Entry, StoredHash, diff --git a/components/metrics/leak_detector/leak_detector_impl.h b/components/metrics/leak_detector/leak_detector_impl.h index daa1041..8af99ff0 100644 --- a/components/metrics/leak_detector/leak_detector_impl.h +++ b/components/metrics/leak_detector/leak_detector_impl.h @@ -15,6 +15,7 @@ #include "components/metrics/leak_detector/call_stack_manager.h" #include "components/metrics/leak_detector/custom_allocator.h" #include "components/metrics/leak_detector/leak_analyzer.h" +#include "components/metrics/leak_detector/stl_allocator.h" namespace metrics { namespace leak_detector { @@ -104,7 +105,7 @@ class LeakDetectorImpl { // Allocator class for allocation entry map. Maps allocated addresses to // AllocInfo objects. using AllocationEntryAllocator = - STLAllocator<std::pair<const void*, AllocInfo>, CustomAllocator>; + STLAllocator<std::pair<const uintptr_t, AllocInfo>, CustomAllocator>; // Hash class for addresses. struct AddressHash { diff --git a/components/mus/ws/display_manager.h b/components/mus/ws/display_manager.h index 32f0c63..4cf6e4b 100644 --- a/components/mus/ws/display_manager.h +++ b/components/mus/ws/display_manager.h @@ -12,6 +12,7 @@ #include "base/macros.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" +#include "base/strings/string16.h" #include "base/timer/timer.h" #include "build/build_config.h" #include "components/mus/public/interfaces/window_tree.mojom.h" diff --git a/components/sync_bookmarks/bookmark_model_associator.h b/components/sync_bookmarks/bookmark_model_associator.h index d5c777c..f10e8d4 100644 --- a/components/sync_bookmarks/bookmark_model_associator.h +++ b/components/sync_bookmarks/bookmark_model_associator.h @@ -15,9 +15,11 @@ #include <vector> #include "base/compiler_specific.h" +#include "base/containers/hash_tables.h" #include "base/hash.h" #include "base/macros.h" #include "base/memory/weak_ptr.h" +#include "base/strings/string16.h" #include "base/threading/thread_checker.h" #include "components/sync_driver/data_type_error_handler.h" #include "components/sync_driver/model_associator.h" diff --git a/content/browser/gpu/browser_gpu_memory_buffer_manager.h b/content/browser/gpu/browser_gpu_memory_buffer_manager.h index 0057192..94095f1 100644 --- a/content/browser/gpu/browser_gpu_memory_buffer_manager.h +++ b/content/browser/gpu/browser_gpu_memory_buffer_manager.h @@ -12,6 +12,7 @@ #include "base/callback.h" #include "base/containers/hash_tables.h" +#include "base/hash.h" #include "base/macros.h" #include "base/trace_event/memory_dump_provider.h" #include "content/common/content_export.h" @@ -31,7 +32,7 @@ namespace BASE_HASH_NAMESPACE { template <> struct hash<content::GpuMemoryBufferConfigurationKey> { size_t operator()(const content::GpuMemoryBufferConfigurationKey& key) const { - return base::HashPair(static_cast<int>(key.first), + return base::HashInts(static_cast<int>(key.first), static_cast<int>(key.second)); } }; diff --git a/gpu/command_buffer/service/framebuffer_completeness_cache.h b/gpu/command_buffer/service/framebuffer_completeness_cache.h index 44741a4..932aa62 100644 --- a/gpu/command_buffer/service/framebuffer_completeness_cache.h +++ b/gpu/command_buffer/service/framebuffer_completeness_cache.h @@ -5,6 +5,8 @@ #ifndef GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_COMPLETENESS_CACHE_H_ #define GPU_COMMAND_BUFFER_SERVICE_FRAMEBUFFER_COMPLETENESS_CACHE_H_ +#include <string> + #include "base/containers/hash_tables.h" #include "base/macros.h" #include "base/memory/ref_counted.h" diff --git a/media/blink/multibuffer.h b/media/blink/multibuffer.h index 6478914..cecca4a 100644 --- a/media/blink/multibuffer.h +++ b/media/blink/multibuffer.h @@ -15,6 +15,7 @@ #include "base/callback.h" #include "base/containers/hash_tables.h" +#include "base/hash.h" #include "base/macros.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" @@ -43,15 +44,7 @@ namespace BASE_HASH_NAMESPACE { template <> struct hash<media::MultiBufferGlobalBlockId> { std::size_t operator()(const media::MultiBufferGlobalBlockId& key) const { -// It would be nice if we could use intptr_t instead of int64_t here, but -// on some platforms, int64_t is declared as "long" which doesn't match -// any of the HashPair() functions. This leads to a compile error since -// the compiler can't decide which HashPair() function to call. -#if defined(ARCH_CPU_64_BITS) - return base::HashPair(reinterpret_cast<int64_t>(key.first), key.second); -#else - return base::HashPair(reinterpret_cast<int32_t>(key.first), key.second); -#endif + return base::HashInts(reinterpret_cast<uintptr_t>(key.first), key.second); } }; diff --git a/net/base/network_change_notifier_linux.h b/net/base/network_change_notifier_linux.h index 85238cf9..48bda6b 100644 --- a/net/base/network_change_notifier_linux.h +++ b/net/base/network_change_notifier_linux.h @@ -6,6 +6,7 @@ #define NET_BASE_NETWORK_CHANGE_NOTIFIER_LINUX_H_ #include "base/compiler_specific.h" +#include "base/containers/hash_tables.h" #include "base/macros.h" #include "base/memory/scoped_ptr.h" #include "net/base/net_export.h" diff --git a/net/base/port_util.cc b/net/base/port_util.cc index 1867dc3..7328b5e 100644 --- a/net/base/port_util.cc +++ b/net/base/port_util.cc @@ -4,6 +4,7 @@ #include "net/base/port_util.h" +#include <limits> #include <set> #include "base/lazy_instance.h" diff --git a/net/disk_cache/simple/simple_index_file.cc b/net/disk_cache/simple/simple_index_file.cc index 7e80c02..025ffe9 100644 --- a/net/disk_cache/simple/simple_index_file.cc +++ b/net/disk_cache/simple/simple_index_file.cc @@ -423,10 +423,7 @@ void SimpleIndexFile::Deserialize(const char* data, int data_len, return; } -#if !defined(OS_WIN) - // TODO(gavinp): Consider using std::unordered_map. - entries->resize(index_metadata.GetNumberOfEntries() + kExtraSizeForMerge); -#endif + entries->reserve(index_metadata.GetNumberOfEntries() + kExtraSizeForMerge); while (entries->size() < index_metadata.GetNumberOfEntries()) { uint64_t hash_key; EntryMetadata entry_metadata; diff --git a/net/server/web_socket_encoder.cc b/net/server/web_socket_encoder.cc index 6d59713..d90a156 100644 --- a/net/server/web_socket_encoder.cc +++ b/net/server/web_socket_encoder.cc @@ -4,6 +4,7 @@ #include "net/server/web_socket_encoder.h" +#include <limits> #include <utility> #include <vector> diff --git a/net/spdy/spdy_alt_svc_wire_format.cc b/net/spdy/spdy_alt_svc_wire_format.cc index ba32160..a50590e 100644 --- a/net/spdy/spdy_alt_svc_wire_format.cc +++ b/net/spdy/spdy_alt_svc_wire_format.cc @@ -4,6 +4,7 @@ #include "net/spdy/spdy_alt_svc_wire_format.h" +#include <algorithm> #include <limits> #include <string> diff --git a/net/ssl/client_cert_store_nss.cc b/net/ssl/client_cert_store_nss.cc index 8a6c329..ef29b55 100644 --- a/net/ssl/client_cert_store_nss.cc +++ b/net/ssl/client_cert_store_nss.cc @@ -6,6 +6,8 @@ #include <nss.h> #include <ssl.h> + +#include <algorithm> #include <utility> #include "base/bind.h" diff --git a/net/tools/quic/quic_server_session_base_test.cc b/net/tools/quic/quic_server_session_base_test.cc index 36432aa..efe96ec 100644 --- a/net/tools/quic/quic_server_session_base_test.cc +++ b/net/tools/quic/quic_server_session_base_test.cc @@ -26,7 +26,6 @@ #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" -using __gnu_cxx::vector; using net::test::CryptoTestUtils; using net::test::MockConnection; using net::test::MockConnectionHelper; diff --git a/net/tools/quic/quic_simple_server_session_test.cc b/net/tools/quic/quic_simple_server_session_test.cc index e3ed3c9..fe9482f 100644 --- a/net/tools/quic/quic_simple_server_session_test.cc +++ b/net/tools/quic/quic_simple_server_session_test.cc @@ -30,7 +30,6 @@ #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" -using __gnu_cxx::vector; using net::test::CryptoTestUtils; using net::test::MockConnection; using net::test::MockConnectionHelper; diff --git a/styleguide/c++/c++11.html b/styleguide/c++/c++11.html index 2776be9..8c3cbc0 100644 --- a/styleguide/c++/c++11.html +++ b/styleguide/c++/c++11.html @@ -363,6 +363,17 @@ std::end</a></td> </tr> <tr> +<td>Forwarding references</td> +<td><code>std::forward()</code></td> +<td>Perfectly forwards arguments (including rvalues)</td> +<td><a href="http://en.cppreference.com/w/cpp/utility/forward"><code>std::forward</code></a></td> +<td> + Allowed, though usage should be rare (primarily for forwarding constructor arguments, or in carefully reviewed library code). + <a href="https://groups.google.com/a/chromium.org/d/topic/cxx/-O7euklhSxs/discussion">Discussion thread</a> +</td> +</tr> + +<tr> <td>Lexicographical struct comparison</td> <td><code>tie(a, b, c) <<br> tie(rhs.a, rhs.b, rhs.c)</code></td> <td>Idiom for <code>operator<</code> implementation</td> @@ -395,17 +406,6 @@ std::end</a></td> </tr> <tr> -<td>Forwarding references</td> -<td><code>std::forward()</code></td> -<td>Perfectly forwards arguments (including rvalues)</td> -<td><a href="http://en.cppreference.com/w/cpp/utility/forward"><code>std::forward</code></a></td> -<td> - Allowed, though usage should be rare (primarily for forwarding constructor arguments, or in carefully reviewed library code). - <a href="https://groups.google.com/a/chromium.org/d/topic/cxx/-O7euklhSxs/discussion">Discussion thread</a> -</td> -</tr> - -<tr> <td>Type Traits</td> <td>Class templates within <code><type_traits></code></td> <td>Allows compile-time inspection of the properties of types</td> @@ -422,6 +422,17 @@ Standard library header <type_traits></a></td> <td>Anything in <code><cmath></code> is allowed. <a href="https://groups.google.com/a/chromium.org/forum/#!topic/cxx/P-1bFBXMeUk">Discussion thread</a></td> </tr> +<tr> +<td>Unordered Associative Containers</td> +<td><code>std::unordered_set</code>, <code>std::unordered_map</code>, +<code>std::unordered_multiset</code>, and <code>std::unordered_multimap</code></td> +<td>Allows efficient containers of key/value pairs</td> +<td><a href="http://en.cppreference.com/w/cpp/container/unordered_map">std::unordered_map</a> +and <a href="http://en.cppreference.com/w/cpp/container/unordered_set">std::unordered_set</a> +</td> +<td>Per the <a href="https://google.github.io/styleguide/cppguide.html#std_hash">Google style guide</a>, specify custom hashers instead of specializing <code>std::hash</code> for custom types. <a href="https://groups.google.com/a/chromium.org/forum/#!topic/cxx/nCdjQqnouO4">Discussion thread</a>.</td> +</tr> + </tbody> </table> @@ -1058,17 +1069,6 @@ Ownership and Smart Pointers</a></td> </tr> <tr> -<td>Unordered Associative Containers</td> -<td><code>std::unordered_set</code>, <code>std::unordered_map</code>, -<code>std::unordered_multiset</code>, and <code>std::unordered_multimap</code></td> -<td>Allows efficient containers of key/value pairs</td> -<td><a href="http://en.cppreference.com/w/cpp/container/unordered_map">std::unordered_map</a> -and <a href="http://en.cppreference.com/w/cpp/container/unordered_set">std::unordered_set</a> -</td> -<td></td> -</tr> - -<tr> <td>Variadic Copy Macro</td> <td><code>va_copy(va_list <i>dest</i>, va_list <i>src</i>)</code></td> <td>Makes a copy of the variadic function arguments</td> diff --git a/ui/base/x/selection_utils.cc b/ui/base/x/selection_utils.cc index af84c4b..7b76e5f 100644 --- a/ui/base/x/selection_utils.cc +++ b/ui/base/x/selection_utils.cc @@ -6,6 +6,7 @@ #include <stdint.h> +#include <algorithm> #include <set> #include "base/i18n/icu_string_conversions.h" diff --git a/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc b/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc index aee2282..f85b651 100644 --- a/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc +++ b/ui/events/ozone/layout/xkb/xkb_keyboard_layout_engine.cc @@ -7,6 +7,8 @@ #include <stddef.h> #include <xkbcommon/xkbcommon-names.h> +#include <algorithm> + #include "base/bind.h" #include "base/location.h" #include "base/logging.h" diff --git a/ui/gfx/generic_shared_memory_id.h b/ui/gfx/generic_shared_memory_id.h index 51f553b..b18c869 100644 --- a/ui/gfx/generic_shared_memory_id.h +++ b/ui/gfx/generic_shared_memory_id.h @@ -8,6 +8,8 @@ #include <stddef.h> #include <stdint.h> +#include "base/containers/hash_tables.h" +#include "base/hash.h" #include "base/trace_event/memory_allocator_dump.h" #include "ui/gfx/gfx_export.h" @@ -58,7 +60,7 @@ template <typename Second> struct hash<std::pair<gfx::GenericSharedMemoryId, Second>> { size_t operator()( const std::pair<gfx::GenericSharedMemoryId, Second>& pair) const { - return base::HashPair(pair.first.id, pair.second); + return base::HashInts(pair.first.id, pair.second); } }; |